Skip to content

Commit 58fa94a

Browse files
committed
fix
1 parent 48912ee commit 58fa94a

4 files changed

Lines changed: 66 additions & 182 deletions

File tree

Lines changed: 50 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,87 @@
11
#![cfg(feature = "rusqlite")]
22
use bdk_chain::{keychain_txout, local_chain, tx_graph, ConfirmationBlockTime};
33
use bdk_testenv::persist_test_utils::{
4-
persist_indexer_changeset, persist_local_chain_changeset, persist_txgraph_changeset,
4+
assert_persist_changesets, keychain_txout_changesets, local_chain_changesets,
5+
tx_graph_changesets,
56
};
67

7-
fn tx_graph_changeset_init(
8-
db: &mut rusqlite::Connection,
9-
) -> rusqlite::Result<tx_graph::ChangeSet<ConfirmationBlockTime>> {
10-
let db_tx = db.transaction()?;
11-
tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?;
12-
let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?;
13-
db_tx.commit()?;
14-
Ok(changeset)
15-
}
16-
17-
fn tx_graph_changeset_persist(
18-
db: &mut rusqlite::Connection,
19-
changeset: &tx_graph::ChangeSet<ConfirmationBlockTime>,
20-
) -> rusqlite::Result<()> {
21-
let db_tx = db.transaction()?;
22-
changeset.persist_to_sqlite(&db_tx)?;
23-
db_tx.commit()
24-
}
25-
26-
fn keychain_txout_changeset_init(
27-
db: &mut rusqlite::Connection,
28-
) -> rusqlite::Result<keychain_txout::ChangeSet> {
29-
let db_tx = db.transaction()?;
30-
keychain_txout::ChangeSet::init_sqlite_tables(&db_tx)?;
31-
let changeset = keychain_txout::ChangeSet::from_sqlite(&db_tx)?;
32-
db_tx.commit()?;
33-
Ok(changeset)
34-
}
35-
36-
fn keychain_txout_changeset_persist(
37-
db: &mut rusqlite::Connection,
38-
changeset: &keychain_txout::ChangeSet,
39-
) -> rusqlite::Result<()> {
40-
let db_tx = db.transaction()?;
41-
changeset.persist_to_sqlite(&db_tx)?;
42-
db_tx.commit()
43-
}
44-
45-
fn local_chain_changeset_init(
46-
db: &mut rusqlite::Connection,
47-
) -> rusqlite::Result<local_chain::ChangeSet> {
48-
let db_tx = db.transaction()?;
49-
local_chain::ChangeSet::init_sqlite_tables(&db_tx)?;
50-
let changeset = local_chain::ChangeSet::from_sqlite(&db_tx)?;
51-
db_tx.commit()?;
52-
Ok(changeset)
53-
}
54-
55-
fn local_chain_changeset_persist(
56-
db: &mut rusqlite::Connection,
57-
changeset: &local_chain::ChangeSet,
58-
) -> rusqlite::Result<()> {
59-
let db_tx = db.transaction()?;
60-
changeset.persist_to_sqlite(&db_tx)?;
61-
db_tx.commit()
62-
}
63-
648
#[test]
659
fn txgraph_is_persisted() -> anyhow::Result<()> {
6610
let temp_dir = tempfile::tempdir().unwrap();
67-
Ok(persist_txgraph_changeset::<rusqlite::Connection, _, _, _>(
11+
let changesets = tx_graph_changesets();
12+
Ok(assert_persist_changesets(
6813
|| {
6914
Ok(rusqlite::Connection::open(
7015
temp_dir.path().join("wallet.sqlite"),
7116
)?)
7217
},
73-
|db| Ok(tx_graph_changeset_init(db)?),
74-
|db, changeset| Ok(tx_graph_changeset_persist(db, changeset)?),
18+
|db| {
19+
let db_tx = db.transaction()?;
20+
tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?;
21+
let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?;
22+
db_tx.commit()?;
23+
Ok(changeset)
24+
},
25+
|db, changeset| {
26+
let db_tx = db.transaction()?;
27+
changeset.persist_to_sqlite(&db_tx)?;
28+
db_tx.commit()?;
29+
Ok(())
30+
},
31+
&changesets,
7532
)?)
7633
}
7734

7835
#[test]
7936
fn indexer_is_persisted() -> anyhow::Result<()> {
8037
let temp_dir = tempfile::tempdir().unwrap();
81-
Ok(persist_indexer_changeset::<rusqlite::Connection, _, _, _>(
38+
let changesets = keychain_txout_changesets();
39+
Ok(assert_persist_changesets(
8240
|| {
8341
Ok(rusqlite::Connection::open(
8442
temp_dir.path().join("wallet.sqlite"),
8543
)?)
8644
},
87-
|db| Ok(keychain_txout_changeset_init(db)?),
88-
|db, changeset| Ok(keychain_txout_changeset_persist(db, changeset)?),
45+
|db| {
46+
let db_tx = db.transaction()?;
47+
keychain_txout::ChangeSet::init_sqlite_tables(&db_tx)?;
48+
let changeset = keychain_txout::ChangeSet::from_sqlite(&db_tx)?;
49+
db_tx.commit()?;
50+
Ok(changeset)
51+
},
52+
|db, changeset| {
53+
let db_tx = db.transaction()?;
54+
changeset.persist_to_sqlite(&db_tx)?;
55+
db_tx.commit()?;
56+
Ok(())
57+
},
58+
&changesets,
8959
)?)
9060
}
9161

9262
#[test]
9363
fn local_chain_is_persisted() -> anyhow::Result<()> {
9464
let temp_dir = tempfile::tempdir().unwrap();
95-
Ok(persist_local_chain_changeset::<
96-
rusqlite::Connection,
97-
_,
98-
_,
99-
_,
100-
>(
65+
let changesets = local_chain_changesets();
66+
Ok(assert_persist_changesets(
10167
|| {
10268
Ok(rusqlite::Connection::open(
10369
temp_dir.path().join("wallet.sqlite"),
10470
)?)
10571
},
106-
|db| Ok(local_chain_changeset_init(db)?),
107-
|db, changeset| Ok(local_chain_changeset_persist(db, changeset)?),
72+
|db| {
73+
let db_tx = db.transaction()?;
74+
local_chain::ChangeSet::init_sqlite_tables(&db_tx)?;
75+
let changeset = local_chain::ChangeSet::from_sqlite(&db_tx)?;
76+
db_tx.commit()?;
77+
Ok(changeset)
78+
},
79+
|db, changeset| {
80+
let db_tx = db.transaction()?;
81+
changeset.persist_to_sqlite(&db_tx)?;
82+
db_tx.commit()?;
83+
Ok(())
84+
},
85+
&changesets,
10886
)?)
10987
}

crates/file_store/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ serde = { version = "1", features = ["derive"] }
2121

2222
[dev-dependencies]
2323
tempfile = "3"
24-
anyhow = { version = "1.0.102", default-features = false}
24+
anyhow = { version = "1", default-features = false}
2525
bdk_testenv = {path = "../testenv"}
2626
bdk_chain = { path = "../chain", version = "0.23.1", default-features = false, features = ["serde"]}

crates/file_store/src/store.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,9 @@ mod test {
295295
const TEST_MAGIC_BYTES: [u8; TEST_MAGIC_BYTES_LEN] =
296296
[98, 100, 107, 102, 115, 49, 49, 49, 49, 49, 49, 49];
297297

298-
use bdk_chain::{keychain_txout, local_chain, tx_graph, ConfirmationBlockTime};
299298
use bdk_testenv::persist_test_utils::{
300-
persist_indexer_changeset, persist_local_chain_changeset, persist_txgraph_changeset,
299+
assert_persist_changesets, keychain_txout_changesets, local_chain_changesets,
300+
tx_graph_changesets,
301301
};
302302

303303
type TestChangeSet = BTreeSet<String>;
@@ -608,12 +608,8 @@ mod test {
608608
#[test]
609609
fn txgraph_is_persisted() -> anyhow::Result<()> {
610610
let temp_dir = tempfile::tempdir().unwrap();
611-
Ok(persist_txgraph_changeset::<
612-
Store<tx_graph::ChangeSet<ConfirmationBlockTime>>,
613-
_,
614-
_,
615-
_,
616-
>(
611+
let changesets = tx_graph_changesets();
612+
Ok(assert_persist_changesets(
617613
|| {
618614
Ok(Store::create(
619615
&TEST_MAGIC_BYTES,
@@ -622,18 +618,15 @@ mod test {
622618
},
623619
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
624620
|db, changeset| Ok(db.append(changeset)?),
621+
&changesets,
625622
)?)
626623
}
627624

628625
#[test]
629626
fn indexer_is_persisted() -> anyhow::Result<()> {
630627
let temp_dir = tempfile::tempdir().unwrap();
631-
Ok(persist_indexer_changeset::<
632-
Store<keychain_txout::ChangeSet>,
633-
_,
634-
_,
635-
_,
636-
>(
628+
let changesets = keychain_txout_changesets();
629+
Ok(assert_persist_changesets(
637630
|| {
638631
Ok(Store::create(
639632
&TEST_MAGIC_BYTES,
@@ -642,18 +635,15 @@ mod test {
642635
},
643636
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
644637
|db, changeset| Ok(db.append(changeset)?),
638+
&changesets,
645639
)?)
646640
}
647641

648642
#[test]
649643
fn local_chain_is_persisted() -> anyhow::Result<()> {
650644
let temp_dir = tempfile::tempdir().unwrap();
651-
Ok(persist_local_chain_changeset::<
652-
Store<local_chain::ChangeSet>,
653-
_,
654-
_,
655-
_,
656-
>(
645+
let changesets = local_chain_changesets();
646+
Ok(assert_persist_changesets(
657647
|| {
658648
Ok(Store::create(
659649
&TEST_MAGIC_BYTES,
@@ -662,6 +652,7 @@ mod test {
662652
},
663653
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
664654
|db, changeset| Ok(db.append(changeset)?),
655+
&changesets,
665656
)?)
666657
}
667658
}

crates/testenv/src/persist_test_utils.rs

Lines changed: 4 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl<C: Debug> Err for PersistErr<C> {}
6565
/// We create a dummy `ChangeSet`, persist it and check if loaded `ChangeSet` matches
6666
/// the persisted one. We then create another such dummy `ChangeSet`, persist it and load it to
6767
/// check if merged `ChangeSet` is returned.
68-
pub fn persist_changeset<CS, Store, CreateStore, Initialize, Persist>(
68+
pub fn assert_persist_changesets<CS, Store, CreateStore, Initialize, Persist>(
6969
create_store: CreateStore,
7070
initialize: Initialize,
7171
persist: Persist,
@@ -107,93 +107,8 @@ where
107107
Ok(())
108108
}
109109

110-
/// Tests if [`TxGraph`](tx_graph::TxGraph) is being persisted correctly.
111-
///
112-
/// We create a dummy [`tx_graph::ChangeSet`], persist it and check if loaded
113-
/// `ChangeSet` matches the persisted one. We then create another such dummy `ChangeSet`, persist it
114-
/// and load it to check if merged `ChangeSet` is returned.
115-
pub fn persist_txgraph_changeset<Store, CreateStore, Initialize, Persist>(
116-
create_store: CreateStore,
117-
initialize: Initialize,
118-
persist: Persist,
119-
) -> Result<(), PersistErr<tx_graph::ChangeSet<ConfirmationBlockTime>>>
120-
where
121-
CreateStore: Fn() -> Result<Store, Box<dyn Err + 'static + Send + Sync>>,
122-
Initialize: Fn(
123-
&mut Store,
124-
) -> Result<
125-
tx_graph::ChangeSet<ConfirmationBlockTime>,
126-
Box<dyn Err + 'static + Send + Sync>,
127-
>,
128-
Persist: Fn(
129-
&mut Store,
130-
&tx_graph::ChangeSet<ConfirmationBlockTime>,
131-
) -> Result<(), Box<dyn Err + 'static + Send + Sync>>,
132-
{
133-
let changesets = tx_graph_changesets();
134-
persist_changeset::<
135-
tx_graph::ChangeSet<ConfirmationBlockTime>,
136-
Store,
137-
CreateStore,
138-
Initialize,
139-
Persist,
140-
>(create_store, initialize, persist, &changesets)
141-
}
142-
143-
/// Tests if [`KeychainTxOutIndex`](keychain_txout::KeychainTxOutIndex) is being persisted
144-
/// correctly.
145-
///
146-
/// See [`persist_txgraph_changeset`].
147-
#[cfg(feature = "miniscript")]
148-
pub fn persist_indexer_changeset<Store, CreateStore, Initialize, Persist>(
149-
create_store: CreateStore,
150-
initialize: Initialize,
151-
persist: Persist,
152-
) -> Result<(), PersistErr<keychain_txout::ChangeSet>>
153-
where
154-
CreateStore: Fn() -> Result<Store, Box<dyn Err + 'static + Send + Sync>>,
155-
Initialize:
156-
Fn(&mut Store) -> Result<keychain_txout::ChangeSet, Box<dyn Err + 'static + Send + Sync>>,
157-
Persist: Fn(
158-
&mut Store,
159-
&keychain_txout::ChangeSet,
160-
) -> Result<(), Box<dyn Err + 'static + Send + Sync>>,
161-
{
162-
let changesets = keychain_txout_changesets();
163-
persist_changeset::<keychain_txout::ChangeSet, Store, CreateStore, Initialize, Persist>(
164-
create_store,
165-
initialize,
166-
persist,
167-
&changesets,
168-
)
169-
}
170-
171-
/// Tests if [`LocalChain`](local_chain::LocalChain) is being persisted correctly.
172-
///
173-
/// See [`persist_txgraph_changeset`].
174-
pub fn persist_local_chain_changeset<Store, CreateStore, Initialize, Persist>(
175-
create_store: CreateStore,
176-
initialize: Initialize,
177-
persist: Persist,
178-
) -> Result<(), PersistErr<local_chain::ChangeSet>>
179-
where
180-
CreateStore: Fn() -> Result<Store, Box<dyn Err + 'static + Send + Sync>>,
181-
Initialize:
182-
Fn(&mut Store) -> Result<local_chain::ChangeSet, Box<dyn Err + 'static + Send + Sync>>,
183-
Persist:
184-
Fn(&mut Store, &local_chain::ChangeSet) -> Result<(), Box<dyn Err + 'static + Send + Sync>>,
185-
{
186-
let changesets = local_chain_changesets();
187-
persist_changeset::<local_chain::ChangeSet, Store, CreateStore, Initialize, Persist>(
188-
create_store,
189-
initialize,
190-
persist,
191-
&changesets,
192-
)
193-
}
194-
195110
/// Get two [`tx_graph::ChangeSet`](tx_graph::ChangeSet)s.
196-
fn tx_graph_changesets() -> [tx_graph::ChangeSet<ConfirmationBlockTime>; 2] {
111+
pub fn tx_graph_changesets() -> [tx_graph::ChangeSet<ConfirmationBlockTime>; 2] {
197112
use tx_graph::ChangeSet;
198113

199114
let tx1 = Arc::new(create_test_tx(
@@ -258,7 +173,7 @@ fn tx_graph_changesets() -> [tx_graph::ChangeSet<ConfirmationBlockTime>; 2] {
258173

259174
/// Get two [`keychain_txout::ChangeSet`](keychain_txout::ChangeSet)s.
260175
#[cfg(feature = "miniscript")]
261-
fn keychain_txout_changesets() -> [keychain_txout::ChangeSet; 2] {
176+
pub fn keychain_txout_changesets() -> [keychain_txout::ChangeSet; 2] {
262177
use crate::utils::DESCRIPTORS;
263178
use keychain_txout::ChangeSet;
264179

@@ -293,7 +208,7 @@ fn keychain_txout_changesets() -> [keychain_txout::ChangeSet; 2] {
293208
}
294209

295210
/// Get two [`local_chain::ChangeSet`](local_chain::ChangeSet)s.
296-
fn local_chain_changesets() -> [local_chain::ChangeSet; 2] {
211+
pub fn local_chain_changesets() -> [local_chain::ChangeSet; 2] {
297212
use local_chain::ChangeSet;
298213

299214
let changeset = ChangeSet {

0 commit comments

Comments
 (0)