Skip to content

Add functions to test persistence #2012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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)?),
);
}
}
5 changes: 3 additions & 2 deletions crates/testenv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ 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, features = ["miniscript"]}
electrsd = { version = "0.28.0", features = [ "legacy" ], default-features = false }
anyhow = "1.0.98"
tempfile = "3.20.0"

[dev-dependencies]
bdk_testenv = { path = "." }
Expand All @@ -27,6 +29,5 @@ default = ["std", "download"]
download = ["electrsd/bitcoind_25_0", "electrsd/esplora_a33e97e1"]
std = ["bdk_chain/std"]
serde = ["bdk_chain/serde"]

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

pub mod persist_test_utils;
pub mod utils;

use bdk_chain::{
Expand Down
Loading
Loading