Skip to content

Commit e212848

Browse files
committed
feat: add utility to check indexer persistence
1 parent c93f4bc commit e212848

File tree

1 file changed

+74
-6
lines changed

1 file changed

+74
-6
lines changed

crates/testenv/src/persist_test_utils.rs

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use crate::block_id;
2+
use bdk_chain::bitcoin::{self, hashes::Hash};
23
use bdk_chain::{
3-
bitcoin::{
4-
absolute, transaction, Amount, OutPoint, ScriptBuf, Transaction, TxIn, TxOut,
5-
Txid,
6-
},
7-
tx_graph, ConfirmationBlockTime, Merge,
4+
bitcoin::{absolute, transaction, Amount, OutPoint, ScriptBuf, Transaction, TxIn, TxOut, Txid},
5+
indexer::keychain_txout,
6+
tx_graph, ConfirmationBlockTime, Merge, DescriptorExt
87
};
9-
use bdk_chain::bitcoin::{self, hashes::Hash};
108
use std::path::Path;
119
use std::sync::Arc;
10+
use bdk_chain::miniscript::{Descriptor, DescriptorPublicKey};
1211

1312
pub fn create_one_inp_one_out_tx(txid: Txid, amount: u64) -> Transaction {
1413
Transaction {
@@ -99,4 +98,73 @@ where
9998
assert_eq!(tx_graph_changeset1, changeset);
10099
}
101100

101+
pub fn parse_descriptor(descriptor: &str) -> Descriptor<DescriptorPublicKey> {
102+
let secp = bdk_chain::bitcoin::secp256k1::Secp256k1::signing_only();
103+
Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, descriptor)
104+
.unwrap()
105+
.0
106+
}
107+
108+
pub fn persist_indexer_changeset<Db, CreateDb, Initialize, Persist>(
109+
filename: &str,
110+
create_db: CreateDb,
111+
initialize: Initialize,
112+
persist: Persist,
113+
) where
114+
CreateDb: Fn(&Path) -> anyhow::Result<Db>,
115+
Initialize: Fn(&mut Db) -> anyhow::Result<keychain_txout::ChangeSet>,
116+
Persist: Fn(&mut Db, &keychain_txout::ChangeSet) -> anyhow::Result<()>,
117+
{
118+
let temp_dir = tempfile::tempdir().expect("must create tempdir");
119+
let file_path = temp_dir.path().join(filename);
120+
let mut db = create_db(&file_path).expect("db should get created");
121+
122+
let descriptor_ids = crate::utils::DESCRIPTORS.map(|d| parse_descriptor(d).descriptor_id());
123+
124+
let mut keychain_txout_changeset = keychain_txout::ChangeSet {
125+
last_revealed: [(descriptor_ids[0], 1), (descriptor_ids[1], 100)].into(),
126+
spk_cache: [
127+
(
128+
descriptor_ids[0],
129+
[(0u32, ScriptBuf::from_bytes(vec![1, 2, 3]))].into(),
130+
),
131+
(
132+
descriptor_ids[1],
133+
[
134+
(100u32, ScriptBuf::from_bytes(vec![3])),
135+
(1000u32, ScriptBuf::from_bytes(vec![5, 6, 8])),
136+
]
137+
.into(),
138+
),
139+
]
140+
.into(),
141+
};
142+
143+
let changeset = initialize(&mut db).expect("should load empty changeset");
144+
assert_eq!(changeset, keychain_txout::ChangeSet::default());
145+
146+
147+
persist(&mut db, &keychain_txout_changeset).expect("should persist keychain_txout");
148+
149+
let changeset = initialize(&mut db).expect("should load persisted changeset");
150+
151+
assert_eq!(changeset, keychain_txout_changeset);
152+
153+
let keychain_txout_changeset_new = keychain_txout::ChangeSet {
154+
last_revealed: [(descriptor_ids[0], 2)].into(),
155+
spk_cache: [(
156+
descriptor_ids[0],
157+
[(1u32, ScriptBuf::from_bytes(vec![1, 2, 3]))].into(),
158+
)]
159+
.into(),
160+
};
161+
162+
persist(&mut db, &keychain_txout_changeset_new).expect("should persist second changeset");
163+
164+
let changeset_new = initialize(&mut db).expect("should load merged changesets");
165+
keychain_txout_changeset.merge(keychain_txout_changeset_new);
166+
167+
assert_eq!(changeset_new, keychain_txout_changeset);
168+
}
169+
102170
// perhaps add test for file_store, sqlite, redb here.

0 commit comments

Comments
 (0)