Skip to content

Commit f62470c

Browse files
committed
refactor(test):mv util fns from benches to testenv
1 parent 776e383 commit f62470c

6 files changed

Lines changed: 80 additions & 108 deletions

File tree

crates/chain/benches/canonicalization.rs

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

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

17-
/// New tx guaranteed to have at least one output
18-
fn new_tx(lt: u32) -> Transaction {
19-
Transaction {
20-
version: transaction::Version::TWO,
21-
lock_time: absolute::LockTime::from_consensus(lt),
22-
input: vec![],
23-
output: vec![TxOut::NULL],
24-
}
25-
}
26-
27-
fn spk_at_index(txout_index: &KeychainTxOutIndex<Keychain>, index: u32) -> ScriptBuf {
28-
txout_index
29-
.get_descriptor(())
30-
.unwrap()
31-
.at_derivation_index(index)
32-
.unwrap()
33-
.script_pubkey()
34-
}
35-
36-
fn genesis_block_id() -> BlockId {
37-
BlockId {
38-
height: 0,
39-
hash: constants::genesis_block(Network::Regtest).block_hash(),
40-
}
41-
}
42-
43-
fn tip_block_id() -> BlockId {
44-
BlockId {
45-
height: 100,
46-
hash: BlockHash::all_zeros(),
47-
}
48-
}
49-
5015
/// Add ancestor tx confirmed at `block_id` with `locktime` (used for uniqueness).
5116
/// The transaction always pays 1 BTC to SPK 0.
5217
fn add_ancestor_tx(graph: &mut KeychainTxGraph, block_id: BlockId, locktime: u32) -> OutPoint {
53-
let spk_0 = spk_at_index(&graph.index, 0);
18+
let descriptor = graph.index.get_descriptor(()).unwrap();
19+
let spk_0 = spk_at_index(descriptor, 0);
5420
let tx = Transaction {
5521
input: vec![TxIn {
5622
previous_output: OutPoint::new(hash!("bogus"), locktime),
@@ -60,7 +26,7 @@ fn add_ancestor_tx(graph: &mut KeychainTxGraph, block_id: BlockId, locktime: u32
6026
value: Amount::ONE_BTC,
6127
script_pubkey: spk_0,
6228
}],
63-
..new_tx(locktime)
29+
..new_standard_tx(locktime)
6430
};
6531
let txid = tx.compute_txid();
6632
let _ = graph.insert_tx(tx);
@@ -77,7 +43,7 @@ fn add_ancestor_tx(graph: &mut KeychainTxGraph, block_id: BlockId, locktime: u32
7743
fn setup<F: Fn(&mut KeychainTxGraph, &LocalChain)>(f: F) -> (KeychainTxGraph, LocalChain) {
7844
const DESC: &str = "tr([ab28dc00/86h/1h/0h]tpubDCdDtzAMZZrkwKBxwNcGCqe4FRydeD9rfMisoi7qLdraG79YohRfPW4YgdKQhpgASdvh612xXNY5xYzoqnyCgPbkpK4LSVcH5Xv4cK7johH/0/*)";
7945
let cp = CheckPoint::from_blocks(
80-
[genesis_block_id(), tip_block_id()]
46+
[genesis_block_id(), tip_block_id(100)]
8147
.into_iter()
8248
.map(|block_id| (block_id.height, block_id.hash)),
8349
)
@@ -127,9 +93,10 @@ fn run_filter_chain_unspents(tx_graph: &KeychainTxGraph, chain: &LocalChain, exp
12793
pub fn many_conflicting_unconfirmed(c: &mut Criterion) {
12894
const CONFLICTING_TX_COUNT: u32 = 2100;
12995
let (tx_graph, chain) = std::hint::black_box(setup(|tx_graph, _chain| {
130-
let previous_output = add_ancestor_tx(tx_graph, tip_block_id(), 0);
96+
let previous_output = add_ancestor_tx(tx_graph, tip_block_id(100), 0);
97+
let descriptor = tx_graph.index.get_descriptor(()).unwrap();
13198
// Create conflicting txs that spend from `previous_output`.
132-
let spk_1 = spk_at_index(&tx_graph.index, 1);
99+
let spk_1 = spk_at_index(descriptor, 1);
133100
for i in 1..=CONFLICTING_TX_COUNT {
134101
let tx = Transaction {
135102
input: vec![TxIn {
@@ -140,7 +107,7 @@ pub fn many_conflicting_unconfirmed(c: &mut Criterion) {
140107
value: Amount::ONE_BTC - Amount::from_sat(i as u64 * 10),
141108
script_pubkey: spk_1.clone(),
142109
}],
143-
..new_tx(i)
110+
..new_standard_tx(i)
144111
};
145112
let mut update = TxUpdate::default();
146113
update.seen_ats = [(tx.compute_txid(), i as u64)].into();
@@ -165,7 +132,7 @@ pub fn many_conflicting_unconfirmed(c: &mut Criterion) {
165132
pub fn many_chained_unconfirmed(c: &mut Criterion) {
166133
const TX_CHAIN_COUNT: u32 = 2100;
167134
let (tx_graph, chain) = std::hint::black_box(setup(|tx_graph, _chain| {
168-
let mut previous_output = add_ancestor_tx(tx_graph, tip_block_id(), 0);
135+
let mut previous_output = add_ancestor_tx(tx_graph, tip_block_id(100), 0);
169136
// Create a chain of unconfirmed txs where each subsequent tx spends the output of the
170137
// previous one.
171138
for i in 0..TX_CHAIN_COUNT {
@@ -175,7 +142,7 @@ pub fn many_chained_unconfirmed(c: &mut Criterion) {
175142
previous_output,
176143
..Default::default()
177144
}],
178-
..new_tx(i)
145+
..new_standard_tx(i)
179146
};
180147
let txid = tx.compute_txid();
181148
let mut update = TxUpdate::default();
@@ -204,7 +171,7 @@ pub fn nested_conflicts(c: &mut Criterion) {
204171
const CONFLICTS_PER_OUTPUT: usize = 3;
205172
const GRAPH_DEPTH: usize = 7;
206173
let (tx_graph, chain) = std::hint::black_box(setup(|tx_graph, _chain| {
207-
let mut prev_ops = core::iter::once(add_ancestor_tx(tx_graph, tip_block_id(), 0))
174+
let mut prev_ops = core::iter::once(add_ancestor_tx(tx_graph, tip_block_id(100), 0))
208175
.collect::<Vec<OutPoint>>();
209176
for depth in 1..GRAPH_DEPTH {
210177
for previous_output in core::mem::take(&mut prev_ops) {
@@ -225,7 +192,7 @@ pub fn nested_conflicts(c: &mut Criterion) {
225192
value,
226193
script_pubkey,
227194
}],
228-
..new_tx(conflict_i as _)
195+
..new_standard_tx(conflict_i as _)
229196
};
230197
let txid = tx.compute_txid();
231198
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
CanonicalizationParams, 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
)
@@ -101,7 +76,7 @@ pub fn reindex_tx_graph(c: &mut Criterion) {
10176
script_pubkey,
10277
value: AMOUNT,
10378
}],
104-
..new_tx(i)
79+
..new_standard_tx(i)
10580
};
10681
let txid = tx.compute_txid();
10782
let mut update = TxUpdate::default();

crates/chain/tests/test_indexed_tx_graph.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,14 @@ use bdk_chain::{
1111
SpkIterator,
1212
};
1313
use bdk_testenv::{
14-
anyhow::{self},
15-
bitcoind::{Input, Output},
16-
block_id, hash,
17-
utils::{new_tx, DESCRIPTORS},
18-
TestEnv,
14+
TestEnv, anyhow::{self}, bitcoind::{Input, Output}, block_id, hash, spk, utils::{DESCRIPTORS, new_tx}
1915
};
2016
use bitcoin::{
2117
secp256k1::Secp256k1, Address, Amount, BlockHash, Network, OutPoint, ScriptBuf, Transaction,
2218
TxIn, TxOut, Txid,
2319
};
2420
use miniscript::Descriptor;
2521

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

67-
let recv_spk = gen_spk();
53+
let recv_spk = spk!();
6854
let recv_addr = Address::from_script(&recv_spk, &bitcoin::params::REGTEST)?;
6955

7056
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/chain/tests/test_tx_graph.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,9 +1095,7 @@ fn test_chain_spends() {
10951095

10961096
// Because this tx conflicts with an already confirmed transaction, chain position should
10971097
// return none.
1098-
assert!(canonical_positions
1099-
.get(&tx_1_conflict.compute_txid())
1100-
.is_none());
1098+
assert!(!canonical_positions.contains_key(&tx_1_conflict.compute_txid()));
11011099
}
11021100

11031101
// Another conflicting tx that conflicts with tx_2.

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)