diff --git a/crates/chain/Cargo.toml b/crates/chain/Cargo.toml index 949f177c8..ac449b19c 100644 --- a/crates/chain/Cargo.toml +++ b/crates/chain/Cargo.toml @@ -29,6 +29,8 @@ rand = "0.8" proptest = "1.2.0" bdk_testenv = { path = "../testenv" } criterion = { version = "0.7" } +tempfile = "3" +anyhow = "1.0.102" [features] default = ["std", "miniscript"] diff --git a/crates/chain/tests/test_rusqlite_impl.rs b/crates/chain/tests/test_rusqlite_impl.rs new file mode 100644 index 000000000..a9f377b1f --- /dev/null +++ b/crates/chain/tests/test_rusqlite_impl.rs @@ -0,0 +1,109 @@ +#![cfg(feature = "rusqlite")] +use bdk_chain::{keychain_txout, local_chain, tx_graph, ConfirmationBlockTime}; +use bdk_testenv::persist_test_utils::{ + persist_indexer_changeset, persist_local_chain_changeset, persist_txgraph_changeset, +}; + +fn tx_graph_changeset_init( + db: &mut rusqlite::Connection, +) -> rusqlite::Result> { + let db_tx = db.transaction()?; + tx_graph::ChangeSet::::init_sqlite_tables(&db_tx)?; + let changeset = tx_graph::ChangeSet::::from_sqlite(&db_tx)?; + db_tx.commit()?; + Ok(changeset) +} + +fn tx_graph_changeset_persist( + db: &mut rusqlite::Connection, + changeset: &tx_graph::ChangeSet, +) -> rusqlite::Result<()> { + let db_tx = db.transaction()?; + changeset.persist_to_sqlite(&db_tx)?; + db_tx.commit() +} + +fn keychain_txout_changeset_init( + db: &mut rusqlite::Connection, +) -> rusqlite::Result { + 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) +} + +fn keychain_txout_changeset_persist( + db: &mut rusqlite::Connection, + changeset: &keychain_txout::ChangeSet, +) -> rusqlite::Result<()> { + let db_tx = db.transaction()?; + changeset.persist_to_sqlite(&db_tx)?; + db_tx.commit() +} + +fn local_chain_changeset_init( + db: &mut rusqlite::Connection, +) -> rusqlite::Result { + 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) +} + +fn local_chain_changeset_persist( + db: &mut rusqlite::Connection, + changeset: &local_chain::ChangeSet, +) -> rusqlite::Result<()> { + let db_tx = db.transaction()?; + changeset.persist_to_sqlite(&db_tx)?; + db_tx.commit() +} + +#[test] +fn txgraph_is_persisted() -> anyhow::Result<()> { + let temp_dir = tempfile::tempdir().unwrap(); + Ok(persist_txgraph_changeset::( + || { + Ok(rusqlite::Connection::open( + temp_dir.path().join("wallet.sqlite"), + )?) + }, + |db| Ok(tx_graph_changeset_init(db)?), + |db, changeset| Ok(tx_graph_changeset_persist(db, changeset)?), + )?) +} + +#[test] +fn indexer_is_persisted() -> anyhow::Result<()> { + let temp_dir = tempfile::tempdir().unwrap(); + Ok(persist_indexer_changeset::( + || { + Ok(rusqlite::Connection::open( + temp_dir.path().join("wallet.sqlite"), + )?) + }, + |db| Ok(keychain_txout_changeset_init(db)?), + |db, changeset| Ok(keychain_txout_changeset_persist(db, changeset)?), + )?) +} + +#[test] +fn local_chain_is_persisted() -> anyhow::Result<()> { + let temp_dir = tempfile::tempdir().unwrap(); + Ok(persist_local_chain_changeset::< + rusqlite::Connection, + _, + _, + _, + >( + || { + Ok(rusqlite::Connection::open( + temp_dir.path().join("wallet.sqlite"), + )?) + }, + |db| Ok(local_chain_changeset_init(db)?), + |db, changeset| Ok(local_chain_changeset_persist(db, changeset)?), + )?) +} diff --git a/crates/file_store/Cargo.toml b/crates/file_store/Cargo.toml index 8fbdc358d..c7b5e336f 100644 --- a/crates/file_store/Cargo.toml +++ b/crates/file_store/Cargo.toml @@ -21,3 +21,6 @@ serde = { version = "1", features = ["derive"] } [dev-dependencies] tempfile = "3" +anyhow = { version = "1.0.102", default-features = false} +bdk_testenv = {path = "../testenv"} +bdk_chain = { path = "../chain", version = "0.23.1", default-features = false, features = ["serde"]} \ No newline at end of file diff --git a/crates/file_store/src/store.rs b/crates/file_store/src/store.rs index 858b9d2cd..949fd3c3a 100644 --- a/crates/file_store/src/store.rs +++ b/crates/file_store/src/store.rs @@ -295,6 +295,11 @@ 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_indexer_changeset, persist_local_chain_changeset, persist_txgraph_changeset, + }; + type TestChangeSet = BTreeSet; /// Check behavior of [`Store::create`] and [`Store::load`]. @@ -599,4 +604,64 @@ mod test { // current position matches EOF assert_eq!(current_pointer, expected_pointer); } + + #[test] + fn txgraph_is_persisted() -> anyhow::Result<()> { + let temp_dir = tempfile::tempdir().unwrap(); + Ok(persist_txgraph_changeset::< + Store>, + _, + _, + _, + >( + || { + Ok(Store::create( + &TEST_MAGIC_BYTES, + temp_dir.path().join("store.db"), + )?) + }, + |db| Ok(db.dump().map(Option::unwrap_or_default)?), + |db, changeset| Ok(db.append(changeset)?), + )?) + } + + #[test] + fn indexer_is_persisted() -> anyhow::Result<()> { + let temp_dir = tempfile::tempdir().unwrap(); + Ok(persist_indexer_changeset::< + Store, + _, + _, + _, + >( + || { + Ok(Store::create( + &TEST_MAGIC_BYTES, + temp_dir.path().join("store.db"), + )?) + }, + |db| Ok(db.dump().map(Option::unwrap_or_default)?), + |db, changeset| Ok(db.append(changeset)?), + )?) + } + + #[test] + fn local_chain_is_persisted() -> anyhow::Result<()> { + let temp_dir = tempfile::tempdir().unwrap(); + Ok(persist_local_chain_changeset::< + Store, + _, + _, + _, + >( + || { + Ok(Store::create( + &TEST_MAGIC_BYTES, + temp_dir.path().join("store.db"), + )?) + }, + |db| Ok(db.dump().map(Option::unwrap_or_default)?), + |db, changeset| Ok(db.append(changeset)?), + )?) + } } diff --git a/crates/testenv/Cargo.toml b/crates/testenv/Cargo.toml index 6cedcf4d3..4f497e06a 100644 --- a/crates/testenv/Cargo.toml +++ b/crates/testenv/Cargo.toml @@ -24,10 +24,10 @@ bitcoin = { version = "0.32.0", default-features = false } bdk_testenv = { path = "." } [features] -default = ["std", "download"] +default = ["std", "download", "miniscript"] download = ["electrsd/bitcoind_download", "electrsd/bitcoind_28_2", "electrsd/esplora_a33e97e1"] std = ["bdk_chain/std", "bitcoin/rand-std"] serde = ["bdk_chain/serde"] - +miniscript = ["bdk_chain/miniscript"] [package.metadata.docs.rs] no-default-features = true diff --git a/crates/testenv/src/lib.rs b/crates/testenv/src/lib.rs index 3c3f8a6f4..2a13a3766 100644 --- a/crates/testenv/src/lib.rs +++ b/crates/testenv/src/lib.rs @@ -1,5 +1,6 @@ #![cfg_attr(coverage_nightly, feature(coverage_attribute))] +pub mod persist_test_utils; pub mod utils; use anyhow::Context; @@ -14,6 +15,8 @@ use core::time::Duration; use electrsd::bitcoind::mtype::GetBlockTemplate; use electrsd::bitcoind::{TemplateRequest, TemplateRules}; +extern crate alloc; + pub use electrsd; pub use electrsd::bitcoind; pub use electrsd::bitcoind::anyhow; diff --git a/crates/testenv/src/persist_test_utils.rs b/crates/testenv/src/persist_test_utils.rs new file mode 100644 index 000000000..9f6c6c0e0 --- /dev/null +++ b/crates/testenv/src/persist_test_utils.rs @@ -0,0 +1,308 @@ +//! This module provides utility functions for testing custom persistence backends. +use crate::{block_id, hash}; +use alloc::sync::Arc; +use bdk_chain::{ + bitcoin::{self, OutPoint}, + local_chain, tx_graph, ConfirmationBlockTime, Merge, +}; + +#[cfg(feature = "miniscript")] +use bdk_chain::{indexer::keychain_txout, DescriptorExt, SpkIterator}; +use core::{ + cmp::PartialEq, + fmt::{Debug, Display}, +}; + +use core::error::Error as Err; + +use crate::utils::{create_test_tx, create_txout}; + +#[cfg(feature = "miniscript")] +use crate::utils::{parse_descriptor, spk_at_index}; + +const ADDRS: [&str; 2] = [ + "bcrt1q3qtze4ys45tgdvguj66zrk4fu6hq3a3v9pfly5", + "bcrt1q8an5jfmpq8w2hr648nn34ecf9zdtxk0qyqtrfl", +]; + +/// Errors caused by a failed persister test. +#[derive(Debug)] +pub enum PersistErr { + /// ChangeSet Mismatch + ChangeSetMismatch { + /// the resulting changeset + got: Box, + /// the expected changeset + expected: Box, + }, + /// Errors thrown by underlying persistence backend. + Persister(Box), +} + +impl From> for PersistErr { + fn from(value: Box) -> Self { + PersistErr::Persister(value) + } +} + +impl Display for PersistErr { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + match self { + PersistErr::ChangeSetMismatch { got, expected } => write!( + f, + "ChangeSet mismatch! Got: {:?}, Expected: {:?}", + got, expected + ), + PersistErr::Persister(err) => write!(f, "{err}"), + } + } +} + +impl Err for PersistErr {} + +/// Tests if `ChangeSet` is being persisted correctly. +/// +/// We create a dummy `ChangeSet`, persist it and check if loaded `ChangeSet` matches +/// the persisted one. We then create another such dummy `ChangeSet`, persist it and load it to +/// check if merged `ChangeSet` is returned. +pub fn persist_changeset( + create_store: CreateStore, + initialize: Initialize, + persist: Persist, + changesets: &[CS], +) -> Result<(), PersistErr> +where + CS: Debug + PartialEq + Default + Merge + Clone, + CreateStore: Fn() -> Result>, + Initialize: Fn(&mut Store) -> Result>, + Persist: Fn(&mut Store, &CS) -> Result<(), Box>, +{ + let mut store = create_store()?; + + let init_changeset = initialize(&mut store)?; + + if init_changeset != CS::default() { + return Err(PersistErr::ChangeSetMismatch { + expected: Box::new(CS::default()), + got: Box::new(init_changeset), + }); + } + + let mut merged_changeset = CS::default(); + + for changeset in changesets { + persist(&mut store, changeset)?; + merged_changeset.merge(changeset.clone()); + } + + let persisted_changeset = initialize(&mut store)?; + + if persisted_changeset != merged_changeset { + return Err(PersistErr::ChangeSetMismatch { + expected: Box::new(merged_changeset), + got: Box::new(persisted_changeset), + }); + } + + Ok(()) +} + +/// Tests if [`TxGraph`](tx_graph::TxGraph) is being persisted correctly. +/// +/// We create a dummy [`tx_graph::ChangeSet`], persist it and check if loaded +/// `ChangeSet` matches the persisted one. We then create another such dummy `ChangeSet`, persist it +/// and load it to check if merged `ChangeSet` is returned. +pub fn persist_txgraph_changeset( + create_store: CreateStore, + initialize: Initialize, + persist: Persist, +) -> Result<(), PersistErr>> +where + CreateStore: Fn() -> Result>, + Initialize: Fn( + &mut Store, + ) -> Result< + tx_graph::ChangeSet, + Box, + >, + Persist: Fn( + &mut Store, + &tx_graph::ChangeSet, + ) -> Result<(), Box>, +{ + let changesets = tx_graph_changesets(); + persist_changeset::< + tx_graph::ChangeSet, + Store, + CreateStore, + Initialize, + Persist, + >(create_store, initialize, persist, &changesets) +} + +/// Tests if [`KeychainTxOutIndex`](keychain_txout::KeychainTxOutIndex) is being persisted +/// correctly. +/// +/// See [`persist_txgraph_changeset`]. +#[cfg(feature = "miniscript")] +pub fn persist_indexer_changeset( + create_store: CreateStore, + initialize: Initialize, + persist: Persist, +) -> Result<(), PersistErr> +where + CreateStore: Fn() -> Result>, + Initialize: + Fn(&mut Store) -> Result>, + Persist: Fn( + &mut Store, + &keychain_txout::ChangeSet, + ) -> Result<(), Box>, +{ + let changesets = keychain_txout_changesets(); + persist_changeset::( + create_store, + initialize, + persist, + &changesets, + ) +} + +/// Tests if [`LocalChain`](local_chain::LocalChain) is being persisted correctly. +/// +/// See [`persist_txgraph_changeset`]. +pub fn persist_local_chain_changeset( + create_store: CreateStore, + initialize: Initialize, + persist: Persist, +) -> Result<(), PersistErr> +where + CreateStore: Fn() -> Result>, + Initialize: + Fn(&mut Store) -> Result>, + Persist: + Fn(&mut Store, &local_chain::ChangeSet) -> Result<(), Box>, +{ + let changesets = local_chain_changesets(); + persist_changeset::( + create_store, + initialize, + persist, + &changesets, + ) +} + +/// Get two [`tx_graph::ChangeSet`](tx_graph::ChangeSet)s. +fn tx_graph_changesets() -> [tx_graph::ChangeSet; 2] { + use tx_graph::ChangeSet; + + let tx1 = Arc::new(create_test_tx( + [hash!("BTC")], + [0], + [30_000], + [ADDRS[0]], + 1, + 0, + )); + + let conf_anchor: ConfirmationBlockTime = ConfirmationBlockTime { + block_id: block_id!(910425, "Rust"), + confirmation_time: 1755416660, + }; + + let tx_graph_changeset1 = ChangeSet:: { + txs: [tx1.clone()].into(), + txouts: [ + (OutPoint::new(hash!("BDK"), 0), create_txout(1300, ADDRS[1])), + ( + OutPoint::new(hash!("Bitcoin_fixes_things"), 0), + create_txout(1400, ADDRS[1]), + ), + ] + .into(), + anchors: [(conf_anchor, tx1.compute_txid())].into(), + last_seen: [(tx1.compute_txid(), 1755416650)].into(), + first_seen: [(tx1.compute_txid(), 1755416655)].into(), + last_evicted: [(tx1.compute_txid(), 1755416660)].into(), + }; + + let tx2 = Arc::new(create_test_tx( + [tx1.compute_txid()], + [0], + [20_000], + [ADDRS[0]], + 1, + 0, + )); + + let conf_anchor: ConfirmationBlockTime = ConfirmationBlockTime { + block_id: block_id!(910426, "BOSS"), + confirmation_time: 1755416700, + }; + + let tx_graph_changeset2 = ChangeSet:: { + txs: [tx2.clone()].into(), + txouts: [( + OutPoint::new(hash!("Magical_Bitcoin"), 0), + create_txout(10000, ADDRS[1]), + )] + .into(), + anchors: [(conf_anchor, tx2.compute_txid())].into(), + last_seen: [(tx2.compute_txid(), 1755416700)].into(), + first_seen: [(tx2.compute_txid(), 1755416670)].into(), + last_evicted: [(tx2.compute_txid(), 1755416760)].into(), + }; + + [tx_graph_changeset1, tx_graph_changeset2] +} + +/// Get two [`keychain_txout::ChangeSet`](keychain_txout::ChangeSet)s. +#[cfg(feature = "miniscript")] +fn keychain_txout_changesets() -> [keychain_txout::ChangeSet; 2] { + use crate::utils::DESCRIPTORS; + use keychain_txout::ChangeSet; + + let descriptor_ids = DESCRIPTORS.map(|d| parse_descriptor(d).0.descriptor_id()); + let descs = DESCRIPTORS.map(|desc| parse_descriptor(desc).0); + + let changeset = ChangeSet { + last_revealed: [(descriptor_ids[0], 1), (descriptor_ids[1], 100)].into(), + spk_cache: [ + ( + descriptor_ids[0], + SpkIterator::new_with_range(&descs[0], 0..=26).collect(), + ), + ( + descriptor_ids[1], + SpkIterator::new_with_range(&descs[1], 0..=125).collect(), + ), + ] + .into(), + }; + + let changeset_new = ChangeSet { + last_revealed: [(descriptor_ids[0], 2)].into(), + spk_cache: [( + descriptor_ids[0], + [(27, spk_at_index(&descs[0], 27))].into(), + )] + .into(), + }; + + [changeset, changeset_new] +} + +/// Get two [`local_chain::ChangeSet`](local_chain::ChangeSet)s. +fn local_chain_changesets() -> [local_chain::ChangeSet; 2] { + use local_chain::ChangeSet; + + let changeset = ChangeSet { + blocks: [(910425, Some(hash!("B"))), (910426, Some(hash!("D")))].into(), + }; + + let changeset_new = ChangeSet { + blocks: [(910427, Some(hash!("K")))].into(), + }; + + [changeset, changeset_new] +} diff --git a/crates/testenv/src/utils.rs b/crates/testenv/src/utils.rs index 93ca1f217..ee4df4176 100644 --- a/crates/testenv/src/utils.rs +++ b/crates/testenv/src/utils.rs @@ -1,4 +1,10 @@ -use bdk_chain::bitcoin; +use bdk_chain::bitcoin::{ + self, absolute, transaction, Address, Amount, OutPoint, Transaction, TxIn, TxOut, Txid, +}; +use core::str::FromStr; + +#[cfg(feature = "miniscript")] +use bdk_chain::miniscript::{descriptor::KeyMap, Descriptor, DescriptorPublicKey}; #[allow(unused_macros)] #[macro_export] @@ -77,6 +83,71 @@ pub fn new_tx(lt: u32) -> bitcoin::Transaction { } } +/// Utility function to create a [`TxOut`] given amount (in satoshis) and address. +pub fn create_txout(sats: u64, addr: &str) -> TxOut { + TxOut { + value: Amount::from_sat(sats), + script_pubkey: Address::from_str(addr) + .unwrap() + .assume_checked() + .script_pubkey(), + } +} + +/// Utility function to create a transaction given txids, vouts of inputs and amounts (in satoshis), +/// addresses of outputs. +/// +/// The locktime should be in the form given to `OP_CHEKCLOCKTIMEVERIFY`. +pub fn create_test_tx( + txids: impl IntoIterator, + vouts: impl IntoIterator, + amounts: impl IntoIterator, + addrs: impl IntoIterator, + version: u32, + locktime: u32, +) -> Transaction { + let input_vec = core::iter::zip(txids, vouts) + .map(|(txid, vout)| TxIn { + previous_output: OutPoint::new(txid, vout), + ..TxIn::default() + }) + .collect(); + let output_vec = core::iter::zip(amounts, addrs) + .map(|(amount, addr)| create_txout(amount, addr)) + .collect(); + let version = transaction::Version::non_standard(version as i32); + assert!(version.is_standard()); + let lock_time = absolute::LockTime::from_consensus(locktime); + assert_eq!(lock_time.to_consensus_u32(), locktime); + Transaction { + version, + lock_time, + input: input_vec, + output: output_vec, + } +} + +/// Generates `script_pubkey` corresponding to `index` on keychain of `descriptor`. +#[cfg(feature = "miniscript")] +pub fn spk_at_index( + descriptor: &Descriptor, + index: u32, +) -> bdk_chain::bitcoin::ScriptBuf { + use bdk_chain::bitcoin::key::Secp256k1; + descriptor + .derived_descriptor(&Secp256k1::verification_only(), index) + .expect("must derive") + .script_pubkey() +} + +/// Parses a descriptor string. +#[cfg(feature = "miniscript")] +pub fn parse_descriptor(descriptor: &str) -> (Descriptor, KeyMap) { + use bdk_chain::bitcoin::key::Secp256k1; + let secp = Secp256k1::signing_only(); + Descriptor::::parse_descriptor(&secp, descriptor).unwrap() +} + #[allow(unused)] pub const DESCRIPTORS: [&str; 7] = [ "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/0/*)",