From 016692ee27df752ff6e457a3368e07805ea86b32 Mon Sep 17 00:00:00 2001 From: Vihiga Tyonum Date: Sat, 14 Sep 2024 21:52:50 +0100 Subject: [PATCH] Refactor(test): Move tx test utils to testenv - add `tx_utils` to house all transaction utilities moved from `crates/chain/test/common` - deleted all moved macros and functions in `crates/chain/test/common/mod` - add `bitcoin` crate to `testenv` crate - add `tx_utils` mod to lib in testenv [Issue: #1602] --- crates/chain/tests/common/mod.rs | 72 ------------------------------ crates/testenv/Cargo.toml | 1 + crates/testenv/src/lib.rs | 2 + crates/testenv/src/tx_utils.rs | 76 ++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 72 deletions(-) create mode 100644 crates/testenv/src/tx_utils.rs diff --git a/crates/chain/tests/common/mod.rs b/crates/chain/tests/common/mod.rs index 3fad37f93..67b12fb98 100644 --- a/crates/chain/tests/common/mod.rs +++ b/crates/chain/tests/common/mod.rs @@ -4,78 +4,6 @@ mod tx_template; #[allow(unused_imports)] pub use tx_template::*; -#[allow(unused_macros)] -macro_rules! block_id { - ($height:expr, $hash:literal) => {{ - bdk_chain::BlockId { - height: $height, - hash: bitcoin::hashes::Hash::hash($hash.as_bytes()), - } - }}; -} - -#[allow(unused_macros)] -macro_rules! h { - ($index:literal) => {{ - bitcoin::hashes::Hash::hash($index.as_bytes()) - }}; -} - -#[allow(unused_macros)] -macro_rules! local_chain { - [ $(($height:expr, $block_hash:expr)), * ] => {{ - #[allow(unused_mut)] - bdk_chain::local_chain::LocalChain::from_blocks([$(($height, $block_hash).into()),*].into_iter().collect()) - .expect("chain must have genesis block") - }}; -} - -#[allow(unused_macros)] -macro_rules! chain_update { - [ $(($height:expr, $hash:expr)), * ] => {{ - #[allow(unused_mut)] - bdk_chain::local_chain::LocalChain::from_blocks([$(($height, $hash).into()),*].into_iter().collect()) - .expect("chain must have genesis block") - .tip() - }}; -} - -#[allow(unused_macros)] -macro_rules! changeset { - (checkpoints: $($tail:tt)*) => { changeset!(index: TxHeight, checkpoints: $($tail)*) }; - ( - index: $ind:ty, - checkpoints: [ $(( $height:expr, $cp_to:expr )),* ] - $(,txids: [ $(( $txid:expr, $tx_to:expr )),* ])? - ) => {{ - use bdk_chain::collections::BTreeMap; - - #[allow(unused_mut)] - bdk_chain::sparse_chain::ChangeSet::<$ind> { - checkpoints: { - let mut changes = BTreeMap::default(); - $(changes.insert($height, $cp_to);)* - changes - }, - txids: { - let mut changes = BTreeMap::default(); - $($(changes.insert($txid, $tx_to.map(|h: TxHeight| h.into()));)*)? - changes - } - } - }}; -} - -#[allow(unused)] -pub fn new_tx(lt: u32) -> bitcoin::Transaction { - bitcoin::Transaction { - version: bitcoin::transaction::Version::non_standard(0x00), - lock_time: bitcoin::absolute::LockTime::from_consensus(lt), - input: vec![], - output: vec![], - } -} - #[allow(unused)] pub const DESCRIPTORS: [&str; 7] = [ "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/0/*)", diff --git a/crates/testenv/Cargo.toml b/crates/testenv/Cargo.toml index 98afa8d00..c4c941ef8 100644 --- a/crates/testenv/Cargo.toml +++ b/crates/testenv/Cargo.toml @@ -18,6 +18,7 @@ workspace = true [dependencies] bdk_chain = { path = "../chain", version = "0.19.0", default-features = false } electrsd = { version = "0.28.0", features = [ "bitcoind_25_0", "esplora_a33e97e1", "legacy" ] } +bitcoin = { version = "0.32.0", default-features = false } [features] default = ["std"] diff --git a/crates/testenv/src/lib.rs b/crates/testenv/src/lib.rs index 6d169bdce..040948d6d 100644 --- a/crates/testenv/src/lib.rs +++ b/crates/testenv/src/lib.rs @@ -1,3 +1,5 @@ +pub mod tx_utils; + use bdk_chain::{ bitcoin::{ address::NetworkChecked, block::Header, hash_types::TxMerkleNode, hashes::Hash, diff --git a/crates/testenv/src/tx_utils.rs b/crates/testenv/src/tx_utils.rs new file mode 100644 index 000000000..656f46c40 --- /dev/null +++ b/crates/testenv/src/tx_utils.rs @@ -0,0 +1,76 @@ +#[allow(unused_macros)] +#[macro_export] +macro_rules! block_id { + ($height:expr, $hash:literal) => {{ + bdk_chain::BlockId { + height: $height, + hash: bitcoin::hashes::Hash::hash($hash.as_bytes()), + } + }}; +} + +#[allow(unused_macros)] +#[macro_export] +macro_rules! h { + ($index:literal) => {{ + bitcoin::hashes::Hash::hash($index.as_bytes()) + }}; +} + +#[allow(unused_macros)] +#[macro_export] +macro_rules! local_chain { + [ $(($height:expr, $block_hash:expr)), * ] => {{ + #[allow(unused_mut)] + bdk_chain::local_chain::LocalChain::from_blocks([$(($height, $block_hash).into()),*].into_iter().collect()) + .expect("chain must have genesis block") + }}; +} + +#[allow(unused_macros)] +#[macro_export] +macro_rules! chain_update { + [ $(($height:expr, $hash:expr)), * ] => {{ + #[allow(unused_mut)] + bdk_chain::local_chain::LocalChain::from_blocks([$(($height, $hash).into()),*].into_iter().collect()) + .expect("chain must have genesis block") + .tip() + }}; +} + +#[allow(unused_macros)] +#[macro_export] +macro_rules! changeset { + (checkpoints: $($tail:tt)*) => { changeset!(index: TxHeight, checkpoints: $($tail)*) }; + ( + index: $ind:ty, + checkpoints: [ $(( $height:expr, $cp_to:expr )),* ] + $(,txids: [ $(( $txid:expr, $tx_to:expr )),* ])? + ) => {{ + use bdk_chain::collections::BTreeMap; + + #[allow(unused_mut)] + bdk_chain::sparse_chain::ChangeSet::<$ind> { + checkpoints: { + let mut changes = BTreeMap::default(); + $(changes.insert($height, $cp_to);)* + changes + }, + txids: { + let mut changes = BTreeMap::default(); + $($(changes.insert($txid, $tx_to.map(|h: TxHeight| h.into()));)*)? + changes + } + } + }}; +} + +#[allow(unused)] +pub fn new_tx(lt: u32) -> bitcoin::Transaction { + bitcoin::Transaction { + version: bitcoin::transaction::Version::non_standard(0x00), + lock_time: bitcoin::absolute::LockTime::from_consensus(lt), + input: vec![], + output: vec![], + } +}