Skip to content

Commit 6b6448b

Browse files
committed
feat: add utility to check local_chain persistence
1 parent e212848 commit 6b6448b

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

crates/testenv/src/persist_test_utils.rs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use crate::block_id;
22
use bdk_chain::bitcoin::{self, hashes::Hash};
3+
use bdk_chain::miniscript::{Descriptor, DescriptorPublicKey};
34
use bdk_chain::{
45
bitcoin::{absolute, transaction, Amount, OutPoint, ScriptBuf, Transaction, TxIn, TxOut, Txid},
56
indexer::keychain_txout,
6-
tx_graph, ConfirmationBlockTime, Merge, DescriptorExt
7+
local_chain, tx_graph, ConfirmationBlockTime, DescriptorExt, Merge
78
};
9+
use crate::hash;
810
use std::path::Path;
911
use std::sync::Arc;
10-
use bdk_chain::miniscript::{Descriptor, DescriptorPublicKey};
1112

1213
pub fn create_one_inp_one_out_tx(txid: Txid, amount: u64) -> Transaction {
1314
Transaction {
@@ -143,7 +144,6 @@ pub fn persist_indexer_changeset<Db, CreateDb, Initialize, Persist>(
143144
let changeset = initialize(&mut db).expect("should load empty changeset");
144145
assert_eq!(changeset, keychain_txout::ChangeSet::default());
145146

146-
147147
persist(&mut db, &keychain_txout_changeset).expect("should persist keychain_txout");
148148

149149
let changeset = initialize(&mut db).expect("should load persisted changeset");
@@ -167,4 +167,50 @@ pub fn persist_indexer_changeset<Db, CreateDb, Initialize, Persist>(
167167
assert_eq!(changeset_new, keychain_txout_changeset);
168168
}
169169

170+
pub fn persist_local_chain_changeset<Db, CreateDb, Initialize, Persist>(
171+
filename: &str,
172+
create_db: CreateDb,
173+
initialize: Initialize,
174+
persist: Persist,
175+
) where
176+
CreateDb: Fn(&Path) -> anyhow::Result<Db>,
177+
Initialize: Fn(&mut Db) -> anyhow::Result<local_chain::ChangeSet>,
178+
Persist: Fn(&mut Db, &local_chain::ChangeSet) -> anyhow::Result<()>,
179+
{
180+
let temp_dir = tempfile::tempdir().expect("must create tempdir");
181+
let file_path = temp_dir.path().join(filename);
182+
let mut db = create_db(&file_path).expect("db should get created");
183+
184+
// create a local_chain_changeset, persist that and read it
185+
let local_chain_changeset = local_chain::ChangeSet {
186+
blocks: [
187+
(0, Some(hash!("B"))),
188+
(1, Some(hash!("D"))),
189+
]
190+
.into(),
191+
};
192+
let changeset = initialize(&mut db).expect("should load empty changeset");
193+
assert_eq!(changeset, local_chain::ChangeSet::default());
194+
195+
persist(&mut db, &local_chain_changeset).expect("should persist changeset");
196+
197+
let changeset = initialize(&mut db).expect("should load persisted changeset");
198+
assert_eq!(local_chain_changeset, changeset);
199+
200+
// create another local_chain_changeset, persist that and read it
201+
let local_chain_changeset = local_chain::ChangeSet {
202+
blocks: [(2, Some(hash!("K")))].into(),
203+
};
204+
205+
persist(&mut db, &local_chain_changeset).expect("should persist changeset");
206+
207+
let changeset = initialize(&mut db).unwrap();
208+
209+
let local_chain_changeset = local_chain::ChangeSet {
210+
blocks: [(0, Some(hash!("B"))), (1, Some(hash!("D"))), (2, Some(hash!("K")))].into(),
211+
};
212+
213+
assert_eq!(local_chain_changeset, changeset);
214+
}
215+
170216
// perhaps add test for file_store, sqlite, redb here.

0 commit comments

Comments
 (0)