From 78d078a6f991ed67ee24b3ab6abff33a218d2a1c 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] Refactor: use tx utils from testenv in chain crate - import testenv crate in chain crate - replace all macros and fns in the following tests with import from testenv crate - tests/test_indexed_tx_graph - tests/test_keychain_txout_index - tests/test_local_chain - tests/test_tx_graph - tests/test_tx_graph_conflicts [issue: #1602] Refactor: Replace block_id macro def with tx util - import testenv crate - delete common module and import `block_id` from testenv crate [issue: #1602] Refactor: replace bitcoin with bdk_chain::bitcoin replace external crate `bitcoin` with re-exported `bdk_chain::bitcoin` [Ticket: #1602] Refactor: rename `tx_utils` to `utils` in testenv - Rename `tx_utils.rs` to `utils.rs` in `testenv` to allow all testing utilities to be included. - update all imports [Ticket: X] Refactor: move DESCRIPTORS sample array to testenv move `DESCRIPTORS` sample array to `testenv` for other crates to use [Ticket: X] --- crates/chain/Cargo.toml | 2 + crates/chain/tests/common/mod.rs | 84 ----------------- crates/chain/tests/common/tx_template.rs | 3 +- crates/chain/tests/test_indexed_tx_graph.rs | 37 ++++---- .../chain/tests/test_keychain_txout_index.rs | 13 ++- crates/chain/tests/test_local_chain.rs | 1 + crates/chain/tests/test_tx_graph.rs | 51 +++++------ crates/chain/tests/test_tx_graph_conflicts.rs | 4 +- crates/core/Cargo.toml | 1 + crates/core/tests/common.rs | 9 -- crates/core/tests/test_checkpoint.rs | 4 +- crates/testenv/src/lib.rs | 2 + crates/testenv/src/utils.rs | 90 +++++++++++++++++++ 13 files changed, 153 insertions(+), 148 deletions(-) delete mode 100644 crates/core/tests/common.rs create mode 100644 crates/testenv/src/utils.rs diff --git a/crates/chain/Cargo.toml b/crates/chain/Cargo.toml index 95bcaf9f6..6782a7d35 100644 --- a/crates/chain/Cargo.toml +++ b/crates/chain/Cargo.toml @@ -28,6 +28,8 @@ serde_json = { version = "1", optional = true } [dev-dependencies] rand = "0.8" proptest = "1.2.0" +bdk_testenv = { path = "../testenv", default-features = false } + [features] default = ["std", "miniscript"] diff --git a/crates/chain/tests/common/mod.rs b/crates/chain/tests/common/mod.rs index 3fad37f93..cb3ee66f3 100644 --- a/crates/chain/tests/common/mod.rs +++ b/crates/chain/tests/common/mod.rs @@ -3,87 +3,3 @@ 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/*)", - "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/*)", - "wpkh([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/0/*)", - "tr(tprv8ZgxMBicQKsPd3krDUsBAmtnRsK3rb8u5yi1zhQgMhF1tR8MW7xfE4rnrbbsrbPR52e7rKapu6ztw1jXveJSCGHEriUGZV7mCe88duLp5pj/86'/1'/0'/0/*)", - "tr(tprv8ZgxMBicQKsPd3krDUsBAmtnRsK3rb8u5yi1zhQgMhF1tR8MW7xfE4rnrbbsrbPR52e7rKapu6ztw1jXveJSCGHEriUGZV7mCe88duLp5pj/86'/1'/0'/1/*)", - "wpkh(xprv9s21ZrQH143K4EXURwMHuLS469fFzZyXk7UUpdKfQwhoHcAiYTakpe8pMU2RiEdvrU9McyuE7YDoKcXkoAwEGoK53WBDnKKv2zZbb9BzttX/1/0/*)", - // non-wildcard - "wpkh([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/0)", -]; diff --git a/crates/chain/tests/common/tx_template.rs b/crates/chain/tests/common/tx_template.rs index c7ce392e5..6ece64cbb 100644 --- a/crates/chain/tests/common/tx_template.rs +++ b/crates/chain/tests/common/tx_template.rs @@ -1,5 +1,6 @@ #![cfg(feature = "miniscript")] +use bdk_testenv::utils::DESCRIPTORS; use rand::distributions::{Alphanumeric, DistString}; use std::collections::HashMap; @@ -55,7 +56,7 @@ pub fn init_graph<'a, A: Anchor + Clone + 'a>( tx_templates: impl IntoIterator>, ) -> (TxGraph, SpkTxOutIndex, HashMap<&'a str, Txid>) { let (descriptor, _) = - Descriptor::parse_descriptor(&Secp256k1::signing_only(), super::DESCRIPTORS[2]).unwrap(); + Descriptor::parse_descriptor(&Secp256k1::signing_only(), DESCRIPTORS[2]).unwrap(); let mut graph = TxGraph::::default(); let mut spk_index = SpkTxOutIndex::default(); (0..10).for_each(|index| { diff --git a/crates/chain/tests/test_indexed_tx_graph.rs b/crates/chain/tests/test_indexed_tx_graph.rs index 1fbbcd0df..0409e5900 100644 --- a/crates/chain/tests/test_indexed_tx_graph.rs +++ b/crates/chain/tests/test_indexed_tx_graph.rs @@ -5,13 +5,16 @@ mod common; use std::{collections::BTreeSet, sync::Arc}; -use crate::common::DESCRIPTORS; use bdk_chain::{ indexed_tx_graph::{self, IndexedTxGraph}, indexer::keychain_txout::KeychainTxOutIndex, local_chain::LocalChain, tx_graph, Balance, ChainPosition, ConfirmationBlockTime, DescriptorExt, }; +use bdk_testenv::{ + block_id, h, + utils::{new_tx, DESCRIPTORS}, +}; use bitcoin::{secp256k1::Secp256k1, Amount, OutPoint, ScriptBuf, Transaction, TxIn, TxOut}; use miniscript::Descriptor; @@ -49,7 +52,7 @@ fn insert_relevant_txs() { script_pubkey: spk_1, }, ], - ..common::new_tx(0) + ..new_tx(0) }; let tx_b = Transaction { @@ -57,7 +60,7 @@ fn insert_relevant_txs() { previous_output: OutPoint::new(tx_a.compute_txid(), 0), ..Default::default() }], - ..common::new_tx(1) + ..new_tx(1) }; let tx_c = Transaction { @@ -65,7 +68,7 @@ fn insert_relevant_txs() { previous_output: OutPoint::new(tx_a.compute_txid(), 1), ..Default::default() }], - ..common::new_tx(2) + ..new_tx(2) }; let txs = [tx_c, tx_b, tx_a]; @@ -132,9 +135,9 @@ fn test_list_owned_txouts() { // Initiate IndexedTxGraph let (desc_1, _) = - Descriptor::parse_descriptor(&Secp256k1::signing_only(), common::DESCRIPTORS[2]).unwrap(); + Descriptor::parse_descriptor(&Secp256k1::signing_only(), DESCRIPTORS[2]).unwrap(); let (desc_2, _) = - Descriptor::parse_descriptor(&Secp256k1::signing_only(), common::DESCRIPTORS[3]).unwrap(); + Descriptor::parse_descriptor(&Secp256k1::signing_only(), DESCRIPTORS[3]).unwrap(); let mut graph = IndexedTxGraph::>::new( KeychainTxOutIndex::new(10), @@ -187,7 +190,7 @@ fn test_list_owned_txouts() { value: Amount::from_sat(70000), script_pubkey: trusted_spks[0].to_owned(), }], - ..common::new_tx(0) + ..new_tx(0) }; // tx2 is an incoming transaction received at untrusted keychain at block 1. @@ -196,7 +199,7 @@ fn test_list_owned_txouts() { value: Amount::from_sat(30000), script_pubkey: untrusted_spks[0].to_owned(), }], - ..common::new_tx(0) + ..new_tx(0) }; // tx3 spends tx2 and gives a change back in trusted keychain. Confirmed at Block 2. @@ -209,7 +212,7 @@ fn test_list_owned_txouts() { value: Amount::from_sat(10000), script_pubkey: trusted_spks[1].to_owned(), }], - ..common::new_tx(0) + ..new_tx(0) }; // tx4 is an external transaction receiving at untrusted keychain, unconfirmed. @@ -218,7 +221,7 @@ fn test_list_owned_txouts() { value: Amount::from_sat(20000), script_pubkey: untrusted_spks[1].to_owned(), }], - ..common::new_tx(0) + ..new_tx(0) }; // tx5 is an external transaction receiving at trusted keychain, unconfirmed. @@ -227,11 +230,11 @@ fn test_list_owned_txouts() { value: Amount::from_sat(15000), script_pubkey: trusted_spks[2].to_owned(), }], - ..common::new_tx(0) + ..new_tx(0) }; // tx6 is an unrelated transaction confirmed at 3. - let tx6 = common::new_tx(0); + let tx6 = new_tx(0); // Insert transactions into graph with respective anchors // Insert unconfirmed txs with a last_seen timestamp @@ -597,7 +600,7 @@ fn test_get_chain_position() { value: Amount::ONE_BTC, script_pubkey: spk.clone(), }], - ..common::new_tx(0) + ..new_tx(0) }, anchor: None, last_seen: None, @@ -610,7 +613,7 @@ fn test_get_chain_position() { value: Amount::ONE_BTC, script_pubkey: spk.clone(), }], - ..common::new_tx(1) + ..new_tx(1) }, anchor: None, last_seen: Some(2), @@ -623,7 +626,7 @@ fn test_get_chain_position() { value: Amount::ONE_BTC, script_pubkey: spk.clone(), }], - ..common::new_tx(2) + ..new_tx(2) }, anchor: Some(blocks[1]), last_seen: None, @@ -636,7 +639,7 @@ fn test_get_chain_position() { value: Amount::ONE_BTC, script_pubkey: spk.clone(), }], - ..common::new_tx(3) + ..new_tx(3) }, anchor: Some(block_id!(2, "B'")), last_seen: Some(2), @@ -649,7 +652,7 @@ fn test_get_chain_position() { value: Amount::ONE_BTC, script_pubkey: spk.clone(), }], - ..common::new_tx(4) + ..new_tx(4) }, anchor: Some(block_id!(2, "B'")), last_seen: None, diff --git a/crates/chain/tests/test_keychain_txout_index.rs b/crates/chain/tests/test_keychain_txout_index.rs index 267ee2e99..af374dc39 100644 --- a/crates/chain/tests/test_keychain_txout_index.rs +++ b/crates/chain/tests/test_keychain_txout_index.rs @@ -1,18 +1,17 @@ #![cfg(feature = "miniscript")] -#[macro_use] -mod common; use bdk_chain::{ collections::BTreeMap, indexer::keychain_txout::{ChangeSet, KeychainTxOutIndex}, DescriptorExt, DescriptorId, Indexer, Merge, }; - +use bdk_testenv::{ + h, + utils::{new_tx, DESCRIPTORS}, +}; use bitcoin::{secp256k1::Secp256k1, Amount, OutPoint, ScriptBuf, Transaction, TxOut}; use miniscript::{Descriptor, DescriptorPublicKey}; -use crate::common::DESCRIPTORS; - #[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)] enum TestKeychain { External, @@ -253,7 +252,7 @@ fn test_lookahead() { value: Amount::from_sat(10_000), }, ], - ..common::new_tx(external_index) + ..new_tx(external_index) }; assert_eq!(txout_index.index_tx(&tx), ChangeSet::default()); assert_eq!( @@ -656,7 +655,7 @@ fn reassigning_keychain_to_a_new_descriptor_should_error() { #[test] fn when_querying_over_a_range_of_keychains_the_utxos_should_show_up() { let mut indexer = KeychainTxOutIndex::::new(0); - let mut tx = common::new_tx(0); + let mut tx = new_tx(0); for (i, descriptor) in DESCRIPTORS.iter().enumerate() { let descriptor = parse_descriptor(descriptor); diff --git a/crates/chain/tests/test_local_chain.rs b/crates/chain/tests/test_local_chain.rs index 29686564d..6ffc5d6e2 100644 --- a/crates/chain/tests/test_local_chain.rs +++ b/crates/chain/tests/test_local_chain.rs @@ -9,6 +9,7 @@ use bdk_chain::{ }, BlockId, }; +use bdk_testenv::{chain_update, h, local_chain}; use bitcoin::{block::Header, hashes::Hash, BlockHash}; use proptest::prelude::*; diff --git a/crates/chain/tests/test_tx_graph.rs b/crates/chain/tests/test_tx_graph.rs index a49c9e5f5..bfc1b18c2 100644 --- a/crates/chain/tests/test_tx_graph.rs +++ b/crates/chain/tests/test_tx_graph.rs @@ -9,6 +9,7 @@ use bdk_chain::{ tx_graph::{ChangeSet, TxGraph}, Anchor, ChainOracle, ChainPosition, Merge, }; +use bdk_testenv::{block_id, h, utils::new_tx}; use bitcoin::{ absolute, hashes::Hash, transaction, Amount, BlockHash, OutPoint, ScriptBuf, SignedAmount, Transaction, TxIn, TxOut, Txid, @@ -474,7 +475,7 @@ fn test_walk_ancestors() { ..TxIn::default() }], output: vec![TxOut::NULL, TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; // tx_b0 spends tx_a0 @@ -484,7 +485,7 @@ fn test_walk_ancestors() { ..TxIn::default() }], output: vec![TxOut::NULL, TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; // tx_b1 spends tx_a0 @@ -494,7 +495,7 @@ fn test_walk_ancestors() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; let tx_b2 = Transaction { @@ -503,7 +504,7 @@ fn test_walk_ancestors() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; // tx_c0 spends tx_b0 @@ -513,7 +514,7 @@ fn test_walk_ancestors() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; // tx_c1 spends tx_b0 @@ -523,7 +524,7 @@ fn test_walk_ancestors() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; // tx_c2 spends tx_b1 and tx_b2 @@ -539,7 +540,7 @@ fn test_walk_ancestors() { }, ], output: vec![TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; let tx_c3 = Transaction { @@ -548,7 +549,7 @@ fn test_walk_ancestors() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; // tx_d0 spends tx_c1 @@ -558,7 +559,7 @@ fn test_walk_ancestors() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; // tx_d1 spends tx_c2 and tx_c3 @@ -574,7 +575,7 @@ fn test_walk_ancestors() { }, ], output: vec![TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; // tx_e0 spends tx_d1 @@ -584,7 +585,7 @@ fn test_walk_ancestors() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; let mut graph = TxGraph::::new([ @@ -667,7 +668,7 @@ fn test_conflicting_descendants() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; // tx_a2 spends previous_output and conflicts with tx_a @@ -677,7 +678,7 @@ fn test_conflicting_descendants() { ..TxIn::default() }], output: vec![TxOut::NULL, TxOut::NULL], - ..common::new_tx(1) + ..new_tx(1) }; // tx_b spends tx_a @@ -687,7 +688,7 @@ fn test_conflicting_descendants() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(2) + ..new_tx(2) }; let txid_a = tx_a.compute_txid(); @@ -709,7 +710,7 @@ fn test_conflicting_descendants() { fn test_descendants_no_repeat() { let tx_a = Transaction { output: vec![TxOut::NULL, TxOut::NULL, TxOut::NULL], - ..common::new_tx(0) + ..new_tx(0) }; let txs_b = (0..3) @@ -719,7 +720,7 @@ fn test_descendants_no_repeat() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(1) + ..new_tx(1) }) .collect::>(); @@ -730,7 +731,7 @@ fn test_descendants_no_repeat() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(2) + ..new_tx(2) }) .collect::>(); @@ -746,7 +747,7 @@ fn test_descendants_no_repeat() { }, ], output: vec![TxOut::NULL], - ..common::new_tx(3) + ..new_tx(3) }; let tx_e = Transaction { @@ -755,7 +756,7 @@ fn test_descendants_no_repeat() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(4) + ..new_tx(4) }; let txs_not_connected = (10..20) @@ -765,7 +766,7 @@ fn test_descendants_no_repeat() { ..TxIn::default() }], output: vec![TxOut::NULL], - ..common::new_tx(v) + ..new_tx(v) }) .collect::>(); @@ -819,7 +820,7 @@ fn test_chain_spends() { script_pubkey: ScriptBuf::new(), }, ], - ..common::new_tx(0) + ..new_tx(0) }; // The first confirmed transaction spends vout: 0. And is confirmed at block 98. @@ -838,7 +839,7 @@ fn test_chain_spends() { script_pubkey: ScriptBuf::new(), }, ], - ..common::new_tx(0) + ..new_tx(0) }; // The second transactions spends vout:1, and is unconfirmed. @@ -857,7 +858,7 @@ fn test_chain_spends() { script_pubkey: ScriptBuf::new(), }, ], - ..common::new_tx(0) + ..new_tx(0) }; let mut graph = TxGraph::::default(); @@ -929,7 +930,7 @@ fn test_chain_spends() { previous_output: OutPoint::new(tx_0.compute_txid(), 0), ..Default::default() }], - ..common::new_tx(0) + ..new_tx(0) }; let _ = graph.insert_tx(tx_1_conflict.clone()); @@ -944,7 +945,7 @@ fn test_chain_spends() { previous_output: OutPoint::new(tx_0.compute_txid(), 1), ..Default::default() }], - ..common::new_tx(0) + ..new_tx(0) }; // Insert in graph and mark it as seen. diff --git a/crates/chain/tests/test_tx_graph_conflicts.rs b/crates/chain/tests/test_tx_graph_conflicts.rs index 45370b676..3f4845582 100644 --- a/crates/chain/tests/test_tx_graph_conflicts.rs +++ b/crates/chain/tests/test_tx_graph_conflicts.rs @@ -3,11 +3,11 @@ #[macro_use] mod common; -use std::collections::{BTreeSet, HashSet}; - use bdk_chain::{Balance, BlockId}; +use bdk_testenv::{block_id, h, local_chain}; use bitcoin::{Amount, OutPoint, ScriptBuf}; use common::*; +use std::collections::{BTreeSet, HashSet}; #[allow(dead_code)] struct Scenario<'a> { diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 3749b9157..58f43ef01 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -22,3 +22,4 @@ serde = ["dep:serde", "bitcoin/serde", "hashbrown?/serde"] [dev-dependencies] bdk_chain = { path = "../chain" } +bdk_testenv = { path = "../testenv", default-features = false } diff --git a/crates/core/tests/common.rs b/crates/core/tests/common.rs deleted file mode 100644 index 347a90bf6..000000000 --- a/crates/core/tests/common.rs +++ /dev/null @@ -1,9 +0,0 @@ -#[allow(unused_macros)] -macro_rules! block_id { - ($height:expr, $hash:literal) => {{ - bdk_chain::BlockId { - height: $height, - hash: bitcoin::hashes::Hash::hash($hash.as_bytes()), - } - }}; -} diff --git a/crates/core/tests/test_checkpoint.rs b/crates/core/tests/test_checkpoint.rs index 705f1c474..2ec96c153 100644 --- a/crates/core/tests/test_checkpoint.rs +++ b/crates/core/tests/test_checkpoint.rs @@ -1,7 +1,5 @@ -#[macro_use] -mod common; - use bdk_core::CheckPoint; +use bdk_testenv::block_id; /// Inserting a block that already exists in the checkpoint chain must always succeed. #[test] diff --git a/crates/testenv/src/lib.rs b/crates/testenv/src/lib.rs index 6d169bdce..abf4cac43 100644 --- a/crates/testenv/src/lib.rs +++ b/crates/testenv/src/lib.rs @@ -1,3 +1,5 @@ +pub mod utils; + use bdk_chain::{ bitcoin::{ address::NetworkChecked, block::Header, hash_types::TxMerkleNode, hashes::Hash, diff --git a/crates/testenv/src/utils.rs b/crates/testenv/src/utils.rs new file mode 100644 index 000000000..6bcc67a2b --- /dev/null +++ b/crates/testenv/src/utils.rs @@ -0,0 +1,90 @@ +use bdk_chain::bitcoin; + +#[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![], + } +} + +#[allow(unused)] +pub const DESCRIPTORS: [&str; 7] = [ + "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/0/*)", + "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/*)", + "wpkh([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/0/*)", + "tr(tprv8ZgxMBicQKsPd3krDUsBAmtnRsK3rb8u5yi1zhQgMhF1tR8MW7xfE4rnrbbsrbPR52e7rKapu6ztw1jXveJSCGHEriUGZV7mCe88duLp5pj/86'/1'/0'/0/*)", + "tr(tprv8ZgxMBicQKsPd3krDUsBAmtnRsK3rb8u5yi1zhQgMhF1tR8MW7xfE4rnrbbsrbPR52e7rKapu6ztw1jXveJSCGHEriUGZV7mCe88duLp5pj/86'/1'/0'/1/*)", + "wpkh(xprv9s21ZrQH143K4EXURwMHuLS469fFzZyXk7UUpdKfQwhoHcAiYTakpe8pMU2RiEdvrU9McyuE7YDoKcXkoAwEGoK53WBDnKKv2zZbb9BzttX/1/0/*)", + // non-wildcard + "wpkh([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/0)", +];