Skip to content

Commit 5ee920d

Browse files
committed
ref(test): move util fns from benches to testenv
Move shared test/bench helpers into testenv utils. Update benches and the affected tests to use them
1 parent 0f0d671 commit 5ee920d

5 files changed

Lines changed: 79 additions & 101 deletions

File tree

crates/chain/benches/canonicalization.rs

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,20 @@ use bdk_chain::{keychain_txout::KeychainTxOutIndex, local_chain::LocalChain, Ind
22
use bdk_core::{BlockId, CheckPoint};
33
use bdk_core::{ConfirmationBlockTime, TxUpdate};
44
use bdk_testenv::hash;
5-
use bitcoin::{
6-
absolute, constants, hashes::Hash, key::Secp256k1, transaction, Amount, BlockHash, Network,
7-
OutPoint, ScriptBuf, Transaction, TxIn, TxOut,
8-
};
5+
use bdk_testenv::utils::{genesis_block_id, new_standard_tx, spk_at_index, tip_block_id};
6+
use bitcoin::{key::Secp256k1, Amount, OutPoint, Transaction, TxIn, TxOut};
97
use criterion::{criterion_group, criterion_main, Criterion};
108
use miniscript::{Descriptor, DescriptorPublicKey};
119
use std::sync::Arc;
1210

1311
type Keychain = ();
1412
type KeychainTxGraph = IndexedTxGraph<ConfirmationBlockTime, KeychainTxOutIndex<Keychain>>;
1513

16-
/// New tx guaranteed to have at least one output
17-
fn new_tx(lt: u32) -> Transaction {
18-
Transaction {
19-
version: transaction::Version::TWO,
20-
lock_time: absolute::LockTime::from_consensus(lt),
21-
input: vec![],
22-
output: vec![TxOut::NULL],
23-
}
24-
}
25-
26-
fn spk_at_index(txout_index: &KeychainTxOutIndex<Keychain>, index: u32) -> ScriptBuf {
27-
txout_index
28-
.get_descriptor(())
29-
.unwrap()
30-
.at_derivation_index(index)
31-
.unwrap()
32-
.script_pubkey()
33-
}
34-
35-
fn genesis_block_id() -> BlockId {
36-
BlockId {
37-
height: 0,
38-
hash: constants::genesis_block(Network::Regtest).block_hash(),
39-
}
40-
}
41-
42-
fn tip_block_id() -> BlockId {
43-
BlockId {
44-
height: 100,
45-
hash: BlockHash::all_zeros(),
46-
}
47-
}
48-
4914
/// Add ancestor tx confirmed at `block_id` with `locktime` (used for uniqueness).
5015
/// The transaction always pays 1 BTC to SPK 0.
5116
fn add_ancestor_tx(graph: &mut KeychainTxGraph, block_id: BlockId, locktime: u32) -> OutPoint {
52-
let spk_0 = spk_at_index(&graph.index, 0);
17+
let descriptor = graph.index.get_descriptor(()).unwrap();
18+
let spk_0 = spk_at_index(descriptor, 0);
5319
let tx = Transaction {
5420
input: vec![TxIn {
5521
previous_output: OutPoint::new(hash!("bogus"), locktime),
@@ -59,7 +25,7 @@ fn add_ancestor_tx(graph: &mut KeychainTxGraph, block_id: BlockId, locktime: u32
5925
value: Amount::ONE_BTC,
6026
script_pubkey: spk_0,
6127
}],
62-
..new_tx(locktime)
28+
..new_standard_tx(locktime)
6329
};
6430
let txid = tx.compute_txid();
6531
let _ = graph.insert_tx(tx);
@@ -76,7 +42,7 @@ fn add_ancestor_tx(graph: &mut KeychainTxGraph, block_id: BlockId, locktime: u32
7642
fn setup<F: Fn(&mut KeychainTxGraph, &LocalChain)>(f: F) -> (KeychainTxGraph, LocalChain) {
7743
const DESC: &str = "tr([ab28dc00/86h/1h/0h]tpubDCdDtzAMZZrkwKBxwNcGCqe4FRydeD9rfMisoi7qLdraG79YohRfPW4YgdKQhpgASdvh612xXNY5xYzoqnyCgPbkpK4LSVcH5Xv4cK7johH/0/*)";
7844
let cp = CheckPoint::from_blocks(
79-
[genesis_block_id(), tip_block_id()]
45+
[genesis_block_id(), tip_block_id(100)]
8046
.into_iter()
8147
.map(|block_id| (block_id.height, block_id.hash)),
8248
)
@@ -114,9 +80,10 @@ fn run_filter_chain_unspents(tx_graph: &KeychainTxGraph, chain: &LocalChain, exp
11480
pub fn many_conflicting_unconfirmed(c: &mut Criterion) {
11581
const CONFLICTING_TX_COUNT: u32 = 2100;
11682
let (tx_graph, chain) = std::hint::black_box(setup(|tx_graph, _chain| {
117-
let previous_output = add_ancestor_tx(tx_graph, tip_block_id(), 0);
83+
let previous_output = add_ancestor_tx(tx_graph, tip_block_id(100), 0);
84+
let descriptor = tx_graph.index.get_descriptor(()).unwrap();
11885
// Create conflicting txs that spend from `previous_output`.
119-
let spk_1 = spk_at_index(&tx_graph.index, 1);
86+
let spk_1 = spk_at_index(descriptor, 1);
12087
for i in 1..=CONFLICTING_TX_COUNT {
12188
let tx = Transaction {
12289
input: vec![TxIn {
@@ -127,7 +94,7 @@ pub fn many_conflicting_unconfirmed(c: &mut Criterion) {
12794
value: Amount::ONE_BTC - Amount::from_sat(i as u64 * 10),
12895
script_pubkey: spk_1.clone(),
12996
}],
130-
..new_tx(i)
97+
..new_standard_tx(i)
13198
};
13299
let mut update = TxUpdate::default();
133100
update.seen_ats = [(tx.compute_txid(), i as u64)].into();
@@ -152,7 +119,7 @@ pub fn many_conflicting_unconfirmed(c: &mut Criterion) {
152119
pub fn many_chained_unconfirmed(c: &mut Criterion) {
153120
const TX_CHAIN_COUNT: u32 = 2100;
154121
let (tx_graph, chain) = std::hint::black_box(setup(|tx_graph, _chain| {
155-
let mut previous_output = add_ancestor_tx(tx_graph, tip_block_id(), 0);
122+
let mut previous_output = add_ancestor_tx(tx_graph, tip_block_id(100), 0);
156123
// Create a chain of unconfirmed txs where each subsequent tx spends the output of the
157124
// previous one.
158125
for i in 0..TX_CHAIN_COUNT {
@@ -162,7 +129,7 @@ pub fn many_chained_unconfirmed(c: &mut Criterion) {
162129
previous_output,
163130
..Default::default()
164131
}],
165-
..new_tx(i)
132+
..new_standard_tx(i)
166133
};
167134
let txid = tx.compute_txid();
168135
let mut update = TxUpdate::default();
@@ -191,7 +158,7 @@ pub fn nested_conflicts(c: &mut Criterion) {
191158
const CONFLICTS_PER_OUTPUT: usize = 3;
192159
const GRAPH_DEPTH: usize = 7;
193160
let (tx_graph, chain) = std::hint::black_box(setup(|tx_graph, _chain| {
194-
let mut prev_ops = core::iter::once(add_ancestor_tx(tx_graph, tip_block_id(), 0))
161+
let mut prev_ops = core::iter::once(add_ancestor_tx(tx_graph, tip_block_id(100), 0))
195162
.collect::<Vec<OutPoint>>();
196163
for depth in 1..GRAPH_DEPTH {
197164
for previous_output in core::mem::take(&mut prev_ops) {
@@ -212,7 +179,7 @@ pub fn nested_conflicts(c: &mut Criterion) {
212179
value,
213180
script_pubkey,
214181
}],
215-
..new_tx(conflict_i as _)
182+
..new_standard_tx(conflict_i as _)
216183
};
217184
let txid = tx.compute_txid();
218185
prev_ops.push(OutPoint::new(txid, 0));

crates/chain/benches/indexer.rs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ use bdk_chain::{
33
local_chain::LocalChain,
44
IndexedTxGraph,
55
};
6-
use bdk_core::{BlockId, CheckPoint, ConfirmationBlockTime, TxUpdate};
7-
use bitcoin::{
8-
absolute, constants, hashes::Hash, key::Secp256k1, transaction, Amount, BlockHash, Network,
9-
Transaction, TxIn, TxOut,
10-
};
6+
use bdk_core::{CheckPoint, ConfirmationBlockTime, TxUpdate};
7+
use bdk_testenv::utils::{genesis_block_id, new_standard_tx, tip_block_id};
8+
use bitcoin::{key::Secp256k1, Amount, Transaction, TxIn, TxOut};
119
use criterion::{criterion_group, criterion_main, Criterion};
1210
use miniscript::Descriptor;
1311
use std::sync::Arc;
@@ -22,36 +20,13 @@ const TX_CT: u32 = 21;
2220
const USE_SPK_CACHE: bool = true;
2321
const AMOUNT: Amount = Amount::from_sat(1_000);
2422

25-
fn new_tx(lt: u32) -> Transaction {
26-
Transaction {
27-
version: transaction::Version::TWO,
28-
lock_time: absolute::LockTime::from_consensus(lt),
29-
input: vec![],
30-
output: vec![TxOut::NULL],
31-
}
32-
}
33-
34-
fn genesis_block_id() -> BlockId {
35-
BlockId {
36-
height: 0,
37-
hash: constants::genesis_block(Network::Regtest).block_hash(),
38-
}
39-
}
40-
41-
fn tip_block_id() -> BlockId {
42-
BlockId {
43-
height: 100,
44-
hash: BlockHash::all_zeros(),
45-
}
46-
}
47-
4823
fn setup<F: Fn(&mut KeychainTxGraph, &LocalChain)>(f: F) -> (KeychainTxGraph, LocalChain) {
4924
let desc = Descriptor::parse_descriptor(&Secp256k1::new(), DESC)
5025
.unwrap()
5126
.0;
5227

5328
let cp = CheckPoint::from_blocks(
54-
[genesis_block_id(), tip_block_id()]
29+
[genesis_block_id(), tip_block_id(100)]
5530
.into_iter()
5631
.map(|block_id| (block_id.height, block_id.hash)),
5732
)
@@ -100,7 +75,7 @@ pub fn reindex_tx_graph(c: &mut Criterion) {
10075
script_pubkey,
10176
value: AMOUNT,
10277
}],
103-
..new_tx(i)
78+
..new_standard_tx(i)
10479
};
10580
let txid = tx.compute_txid();
10681
let mut update = TxUpdate::default();

crates/chain/tests/test_indexed_tx_graph.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use bdk_chain::{
1212
use bdk_testenv::{
1313
anyhow::{self},
1414
bitcoind::{Input, Output},
15-
block_id, hash,
15+
block_id, hash, spk,
1616
utils::{new_tx, DESCRIPTORS},
1717
TestEnv,
1818
};
@@ -22,16 +22,6 @@ use bitcoin::{
2222
};
2323
use miniscript::Descriptor;
2424

25-
fn gen_spk() -> ScriptBuf {
26-
use bitcoin::secp256k1::{Secp256k1, SecretKey};
27-
28-
let secp = Secp256k1::new();
29-
let (x_only_pk, _) = SecretKey::new(&mut rand::thread_rng())
30-
.public_key(&secp)
31-
.x_only_public_key();
32-
ScriptBuf::new_p2tr(&secp, x_only_pk, None)
33-
}
34-
3525
/// Conflicts of relevant transactions must also be considered relevant.
3626
///
3727
/// This allows the receiving structures to determine the reason why a given transaction is not part
@@ -63,7 +53,7 @@ fn relevant_conflicts() -> anyhow::Result<()> {
6353
.address()?
6454
.require_network(Network::Regtest)?;
6555

66-
let recv_spk = gen_spk();
56+
let recv_spk = spk!();
6757
let recv_addr = Address::from_script(&recv_spk, &bitcoin::params::REGTEST)?;
6858

6959
let mut graph = SpkTxGraph::default();

crates/chain/tests/test_keychain_txout_index.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use bdk_chain::{
77
};
88
use bdk_testenv::{
99
hash,
10-
utils::{new_tx, DESCRIPTORS},
10+
utils::{new_tx, spk_at_index, DESCRIPTORS},
1111
};
12-
use bitcoin::{secp256k1::Secp256k1, Amount, OutPoint, ScriptBuf, Transaction, TxOut};
12+
use bitcoin::{Amount, OutPoint, ScriptBuf, Transaction, TxOut};
1313
use miniscript::{Descriptor, DescriptorPublicKey};
1414

1515
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
@@ -52,13 +52,6 @@ fn init_txout_index(
5252
txout_index
5353
}
5454

55-
fn spk_at_index(descriptor: &Descriptor<DescriptorPublicKey>, index: u32) -> ScriptBuf {
56-
descriptor
57-
.derived_descriptor(&Secp256k1::verification_only(), index)
58-
.expect("must derive")
59-
.script_pubkey()
60-
}
61-
6255
// We create two empty changesets lhs and rhs, we then insert various descriptors with various
6356
// last_revealed, merge rhs to lhs, and check that the result is consistent with these rules:
6457
// - Existing index doesn't update if the new index in `other` is lower than `self`.

crates/testenv/src/utils.rs

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
use bdk_chain::bitcoin;
1+
use bdk_chain::{
2+
bitcoin,
3+
miniscript::{Descriptor, DescriptorPublicKey},
4+
BlockId,
5+
};
6+
use bitcoin::{
7+
absolute::LockTime, constants, hashes::Hash, key::Secp256k1, transaction::Version, BlockHash,
8+
Network, ScriptBuf, Transaction, TxOut,
9+
};
210

311
#[allow(unused_macros)]
412
#[macro_export]
@@ -67,16 +75,61 @@ macro_rules! changeset {
6775
}};
6876
}
6977

78+
/// Generate a dummy script pubkey.
79+
#[allow(unused_macros)]
80+
#[macro_export]
81+
macro_rules! spk {
82+
() => {{
83+
let secp = bitcoin::secp256k1::Secp256k1::new();
84+
let (x_only_pk, _) = bitcoin::secp256k1::SecretKey::new(&mut rand::thread_rng())
85+
.public_key(&secp)
86+
.x_only_public_key();
87+
bitcoin::ScriptBuf::new_p2tr(&secp, x_only_pk, None)
88+
}};
89+
}
90+
7091
#[allow(unused)]
71-
pub fn new_tx(lt: u32) -> bitcoin::Transaction {
72-
bitcoin::Transaction {
92+
pub fn new_tx(lt: u32) -> Transaction {
93+
Transaction {
7394
version: bitcoin::transaction::Version::non_standard(0x00),
7495
lock_time: bitcoin::absolute::LockTime::from_consensus(lt),
7596
input: vec![],
7697
output: vec![],
7798
}
7899
}
79100

101+
/// Initialize a standard transaction with a guaranteed output.
102+
pub fn new_standard_tx(lt: u32) -> Transaction {
103+
Transaction {
104+
version: Version::TWO,
105+
lock_time: LockTime::from_consensus(lt),
106+
input: vec![],
107+
output: vec![TxOut::NULL],
108+
}
109+
}
110+
111+
pub fn genesis_block_id() -> BlockId {
112+
BlockId {
113+
height: 0,
114+
hash: constants::genesis_block(Network::Regtest).block_hash(),
115+
}
116+
}
117+
118+
pub fn tip_block_id(height: u32) -> BlockId {
119+
BlockId {
120+
height,
121+
hash: BlockHash::all_zeros(),
122+
}
123+
}
124+
125+
/// Derives a [`ScriptBuf`] (scriptPubkey) from the provided descriptor at a specific index.
126+
pub fn spk_at_index(descriptor: &Descriptor<DescriptorPublicKey>, index: u32) -> ScriptBuf {
127+
descriptor
128+
.derived_descriptor(&Secp256k1::verification_only(), index)
129+
.expect("must derive")
130+
.script_pubkey()
131+
}
132+
80133
#[allow(unused)]
81134
pub const DESCRIPTORS: [&str; 7] = [
82135
"tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/0/*)",

0 commit comments

Comments
 (0)