Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 227 additions & 0 deletions crates/chain/tests/test_rusqlite_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
#![cfg(feature = "rusqlite")]
use bdk_chain::{keychain_txout, local_chain, tx_graph, ConfirmationBlockTime};
use bdk_testenv::persist_test_utils::{
persist_anchors, persist_first_seen, persist_indexer_changeset, persist_last_evicted,
persist_last_revealed, persist_last_seen, persist_local_chain_changeset, persist_spk_cache,
persist_txgraph_changeset, persist_txouts, persist_txs,
};

#[test]
fn txgraph_is_persisted() {
persist_txgraph_changeset::<rusqlite::Connection, _, _, _>(
"wallet.sqlite",
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
|db| {
let db_tx = db.transaction()?;
tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?;
let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?;
db_tx.commit()?;
Ok(changeset)
},
|db, changeset| {
let db_tx = db.transaction()?;
changeset.persist_to_sqlite(&db_tx)?;
Ok(db_tx.commit()?)
},
);
}

#[test]
fn indexer_is_persisted() {
persist_indexer_changeset::<rusqlite::Connection, _, _, _>(
"wallet.sqlite",
|path| Ok(rusqlite::Connection::open(path)?),
|db| {
let db_tx = db.transaction()?;
keychain_txout::ChangeSet::init_sqlite_tables(&db_tx)?;
let changeset = keychain_txout::ChangeSet::from_sqlite(&db_tx)?;
db_tx.commit()?;
Ok(changeset)
},
|db, changeset| {
let db_tx = db.transaction()?;
changeset.persist_to_sqlite(&db_tx)?;
Ok(db_tx.commit()?)
},
);
}

#[test]
fn local_chain_is_persisted() {
persist_local_chain_changeset::<rusqlite::Connection, _, _, _>(
"wallet.sqlite",
|path| Ok(rusqlite::Connection::open(path)?),
|db| {
let db_tx = db.transaction()?;
local_chain::ChangeSet::init_sqlite_tables(&db_tx)?;
let changeset = local_chain::ChangeSet::from_sqlite(&db_tx)?;
db_tx.commit()?;
Ok(changeset)
},
|db, changeset| {
let db_tx = db.transaction()?;
changeset.persist_to_sqlite(&db_tx)?;
Ok(db_tx.commit()?)
},
);
}

#[test]
fn txouts_are_persisted() {
persist_txouts::<rusqlite::Connection, _, _, _>(
"wallet.sqlite",
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
|db| {
let db_tx = db.transaction()?;
tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?;
let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?;
db_tx.commit()?;
Ok(changeset)
},
|db, changeset| {
let db_tx = db.transaction()?;
changeset.persist_to_sqlite(&db_tx)?;
Ok(db_tx.commit()?)
},
);
}

#[test]
fn txs_are_persisted() {
persist_txs::<rusqlite::Connection, _, _, _>(
"wallet.sqlite",
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
|db| {
let db_tx = db.transaction()?;
tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?;
let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?;
db_tx.commit()?;
Ok(changeset)
},
|db, changeset| {
let db_tx = db.transaction()?;
changeset.persist_to_sqlite(&db_tx)?;
Ok(db_tx.commit()?)
},
);
}

#[test]
fn anchors_are_persisted() {
persist_anchors::<rusqlite::Connection, _, _, _>(
"wallet.sqlite",
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
|db| {
let db_tx = db.transaction()?;
tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?;
let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?;
db_tx.commit()?;
Ok(changeset)
},
|db, changeset| {
let db_tx = db.transaction()?;
changeset.persist_to_sqlite(&db_tx)?;
Ok(db_tx.commit()?)
},
);
}

#[test]
fn last_seen_is_persisted() {
persist_last_seen::<rusqlite::Connection, _, _, _>(
"wallet.sqlite",
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
|db| {
let db_tx = db.transaction()?;
tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?;
let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?;
db_tx.commit()?;
Ok(changeset)
},
|db, changeset| {
let db_tx = db.transaction()?;
changeset.persist_to_sqlite(&db_tx)?;
Ok(db_tx.commit()?)
},
);
}

#[test]
fn last_evicted_is_persisted() {
persist_last_evicted::<rusqlite::Connection, _, _, _>(
"wallet.sqlite",
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
|db| {
let db_tx = db.transaction()?;
tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?;
let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?;
db_tx.commit()?;
Ok(changeset)
},
|db, changeset| {
let db_tx = db.transaction()?;
changeset.persist_to_sqlite(&db_tx)?;
Ok(db_tx.commit()?)
},
);
}

#[test]
fn first_seen_is_persisted() {
persist_first_seen::<rusqlite::Connection, _, _, _>(
"wallet.sqlite",
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
|db| {
let db_tx = db.transaction()?;
tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?;
let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?;
db_tx.commit()?;
Ok(changeset)
},
|db, changeset| {
let db_tx = db.transaction()?;
changeset.persist_to_sqlite(&db_tx)?;
Ok(db_tx.commit()?)
},
);
}

#[test]
fn last_revealed_is_persisted() {
persist_last_revealed::<rusqlite::Connection, _, _, _>(
"wallet.sqlite",
|path| Ok(rusqlite::Connection::open(path)?),
|db| {
let db_tx = db.transaction()?;
keychain_txout::ChangeSet::init_sqlite_tables(&db_tx)?;
let changeset = keychain_txout::ChangeSet::from_sqlite(&db_tx)?;
db_tx.commit()?;
Ok(changeset)
},
|db, changeset| {
let db_tx = db.transaction()?;
changeset.persist_to_sqlite(&db_tx)?;
Ok(db_tx.commit()?)
},
);
}

#[test]
fn spk_cache_is_persisted() {
persist_spk_cache::<rusqlite::Connection, _, _, _>(
"wallet.sqlite",
|path| Ok(rusqlite::Connection::open(path)?),
|db| {
let db_tx = db.transaction()?;
keychain_txout::ChangeSet::init_sqlite_tables(&db_tx)?;
let changeset = keychain_txout::ChangeSet::from_sqlite(&db_tx)?;
db_tx.commit()?;
Ok(changeset)
},
|db, changeset| {
let db_tx = db.transaction()?;
changeset.persist_to_sqlite(&db_tx)?;
Ok(db_tx.commit()?)
},
);
}
2 changes: 2 additions & 0 deletions crates/file_store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ serde = { version = "1", features = ["derive"] }

[dev-dependencies]
tempfile = "3"
bdk_testenv = {path = "../testenv"}
bdk_chain = { path = "../chain", version = "0.23.1", default-features = false, features = ["serde"]}
117 changes: 117 additions & 0 deletions crates/file_store/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,13 @@ mod test {
const TEST_MAGIC_BYTES: [u8; TEST_MAGIC_BYTES_LEN] =
[98, 100, 107, 102, 115, 49, 49, 49, 49, 49, 49, 49];

use bdk_chain::{keychain_txout, local_chain, tx_graph, ConfirmationBlockTime};
use bdk_testenv::persist_test_utils::{
persist_anchors, persist_first_seen, persist_indexer_changeset, persist_last_evicted,
persist_last_revealed, persist_last_seen, persist_local_chain_changeset, persist_spk_cache,
persist_txgraph_changeset, persist_txouts, persist_txs,
};

type TestChangeSet = BTreeSet<String>;

/// Check behavior of [`Store::create`] and [`Store::load`].
Expand Down Expand Up @@ -599,4 +606,114 @@ mod test {
// current position matches EOF
assert_eq!(current_pointer, expected_pointer);
}

#[test]
fn txgraph_is_persisted() {
persist_txgraph_changeset::<Store<tx_graph::ChangeSet<ConfirmationBlockTime>>, _, _, _>(
"wallet.db",
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
|db, changeset| Ok(db.append(changeset)?),
);
}

#[test]
fn indexer_is_persisted() {
persist_indexer_changeset::<Store<keychain_txout::ChangeSet>, _, _, _>(
"wallet.db",
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
|db, changeset| Ok(db.append(changeset)?),
);
}

#[test]
fn local_chain_is_persisted() {
persist_local_chain_changeset::<Store<local_chain::ChangeSet>, _, _, _>(
"wallet.db",
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
|db, changeset| Ok(db.append(changeset)?),
);
}

#[test]
fn txouts_are_persisted() {
persist_txouts::<Store<tx_graph::ChangeSet<ConfirmationBlockTime>>, _, _, _>(
"wallet.db",
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
|db, changeset| Ok(db.append(changeset)?),
);
}

#[test]
fn txs_are_persisted() {
persist_txs::<Store<tx_graph::ChangeSet<ConfirmationBlockTime>>, _, _, _>(
"wallet.db",
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
|db, changeset| Ok(db.append(changeset)?),
);
}

#[test]
fn anchors_are_persisted() {
persist_anchors::<Store<tx_graph::ChangeSet<ConfirmationBlockTime>>, _, _, _>(
"wallet.db",
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
|db, changeset| Ok(db.append(changeset)?),
);
}

#[test]
fn last_seen_is_persisted() {
persist_last_seen::<Store<tx_graph::ChangeSet<ConfirmationBlockTime>>, _, _, _>(
"wallet.db",
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
|db, changeset| Ok(db.append(changeset)?),
);
}

#[test]
fn last_evicted_is_persisted() {
persist_last_evicted::<Store<tx_graph::ChangeSet<ConfirmationBlockTime>>, _, _, _>(
"wallet.db",
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
|db, changeset| Ok(db.append(changeset)?),
);
}

#[test]
fn first_seen_is_persisted() {
persist_first_seen::<Store<tx_graph::ChangeSet<ConfirmationBlockTime>>, _, _, _>(
"wallet.db",
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
|db, changeset| Ok(db.append(changeset)?),
);
}

#[test]
fn last_revealed_is_persisted() {
persist_last_revealed::<Store<keychain_txout::ChangeSet>, _, _, _>(
"wallet.db",
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
|db, changeset| Ok(db.append(changeset)?),
);
}

#[test]
fn spk_cache_is_persisted() {
persist_spk_cache::<Store<keychain_txout::ChangeSet>, _, _, _>(
"wallet.db",
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
|db, changeset| Ok(db.append(changeset)?),
);
}
}
8 changes: 5 additions & 3 deletions crates/testenv/Cargo.toml
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the correct thing to do is something like this by making the dependency explicit.

[dependencies]
miniscript = { version = "12.3.5", default-features = false, optional = true }

[features]
default = ["std" , "download", "bdk_chain/default"]
miniscript = ["dep:miniscript"]
std = ["bdk_chain/std", "miniscript?/std"]
serde = ["bdk_chain/serde", "miniscript?/serde"]

But a simpler hack is to just have default = ["bdk_chain/default"] (which seems to be missing and should probably be done anyway). In that case the cfg attribute would be changed to #[cfg(feature = "default"]. Sorry to cause more bikeshedding on the matter. I'll be in favor of whatever is clear and easy to understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went with the second approach since the first seemed a bit complicated 😅 (like the test utils are gated by miniscript feature which might not be enabled even though all we need is bdk_chain/default).

Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ readme = "README.md"
workspace = true

[dependencies]
bdk_chain = { path = "../chain", version = "0.23.1", default-features = false }
bdk_chain = { path = "../chain", version = "0.23.1", default-features = false}
electrsd = { version = "0.28.0", features = [ "legacy" ], default-features = false }
anyhow = "1.0.98"
tempfile = "3.20.0"

[dev-dependencies]
bdk_testenv = { path = "." }

[features]
default = ["std", "download"]
default = ["std" , "download", "miniscript"]
miniscript = ["std", "bdk_chain/default"]
download = ["electrsd/bitcoind_25_0", "electrsd/esplora_a33e97e1"]
std = ["bdk_chain/std"]
serde = ["bdk_chain/serde"]

[package.metadata.docs.rs]
no-default-features = true
2 changes: 2 additions & 0 deletions crates/testenv/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

#[cfg(feature = "std")]
pub mod persist_test_utils;
pub mod utils;

use bdk_chain::{
Expand Down
Loading
Loading