Skip to content

Commit aab6df8

Browse files
committed
Refactor benchmark tests to use TxGraph
Refactor all test cases to use TxGraph and TxTemplate [Ticket: X]
1 parent d121446 commit aab6df8

File tree

1 file changed

+65
-61
lines changed

1 file changed

+65
-61
lines changed

crates/chain/benches/canonicalization.rs

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use bdk_chain::spk_txout::SpkTxOutIndex;
2+
use bdk_chain::TxGraph;
13
use bdk_chain::{keychain_txout::KeychainTxOutIndex, local_chain::LocalChain, IndexedTxGraph};
24
use bdk_core::{BlockId, CheckPoint};
35
use bdk_core::{ConfirmationBlockTime, TxUpdate};
46
use bdk_testenv::tx_template::{init_graph, TxInTemplate, TxOutTemplate, TxTemplate};
5-
use bdk_testenv::utils::DESCRIPTORS;
67
use bdk_testenv::{block_id, hash, local_chain};
78
use bitcoin::{
89
absolute, constants, hashes::Hash, key::Secp256k1, transaction, Amount, BlockHash, Network,
@@ -244,19 +245,40 @@ pub fn nested_conflicts(c: &mut Criterion) {
244245
}
245246

246247
// start of using TxTemplate
247-
fn initialize_graph_and_keychain() -> (KeychainTxGraph, LocalChain) {
248-
let chain = local_chain![(0, hash!("genesis")), (100, hash!("abcd"))];
248+
fn filter_chain_unspents(
249+
tx_graph: &TxGraph<BlockId>,
250+
spk_index: &SpkTxOutIndex<u32>,
251+
chain: &LocalChain,
252+
exp_txos: usize,
253+
) {
254+
let utxos = tx_graph.filter_chain_unspents(
255+
chain,
256+
chain.tip().block_id(),
257+
spk_index.outpoints().clone(),
258+
);
259+
assert_eq!(utxos.count(), exp_txos);
260+
}
249261

250-
let mut keychain_index = KeychainTxOutIndex::new(10);
251-
let (desc, _) = Descriptor::parse_descriptor(&Secp256k1::new(), DESCRIPTORS[2]).unwrap();
252-
keychain_index.insert_descriptor((), desc).unwrap();
262+
fn filter_chain_txouts(
263+
tx_graph: &TxGraph<BlockId>,
264+
spk_index: &SpkTxOutIndex<u32>,
265+
chain: &LocalChain,
266+
exp_txos: usize,
267+
) {
268+
let utxos =
269+
tx_graph.filter_chain_txouts(chain, chain.tip().block_id(), spk_index.outpoints().clone());
270+
assert_eq!(utxos.count(), exp_txos);
271+
}
253272

254-
let new_graph = KeychainTxGraph::new(keychain_index);
255-
(new_graph, chain)
273+
fn list_canonical_txs(tx_graph: &TxGraph<BlockId>, chain: &LocalChain, exp_txs: usize) {
274+
let txs = tx_graph.list_canonical_txs(chain, chain.tip().block_id());
275+
assert_eq!(txs.count(), exp_txs);
256276
}
257277

258-
fn setup_many_conflicting_unconfirmed(tx_count: u32) -> (KeychainTxGraph, LocalChain) {
259-
let (mut tx_graph, chain) = initialize_graph_and_keychain();
278+
fn setup_many_conflicting_unconfirmed(
279+
tx_count: u32,
280+
) -> (TxGraph<BlockId>, SpkTxOutIndex<u32>, LocalChain) {
281+
let chain = local_chain![(0, hash!("genesis")), (100, hash!("abcd"))];
260282
let mut templates = Vec::new();
261283

262284
templates.push(TxTemplate {
@@ -280,21 +302,16 @@ fn setup_many_conflicting_unconfirmed(tx_count: u32) -> (KeychainTxGraph, LocalC
280302
});
281303
}
282304

283-
let (graph, _, _) = init_graph(templates);
284-
let _ = tx_graph.batch_insert_unconfirmed(graph.full_txs().map(|tx_node| {
285-
(
286-
tx_node.tx,
287-
tx_node.last_seen_unconfirmed.unwrap_or_default(),
288-
)
289-
}));
290-
291-
(tx_graph, chain)
305+
let (tx_graph, spk_index, _) = init_graph(templates);
306+
(tx_graph, spk_index, chain)
292307
}
293308

294-
/// Utility function to create a benchmark-ready graph with a chain of unconfirmed transactions
295-
fn setup_many_chained_unconfirmed(tx_chain_count: u32) -> (KeychainTxGraph, LocalChain) {
309+
/// chain of unconfirmed transactions
310+
fn setup_many_chained_unconfirmed(
311+
tx_chain_count: u32,
312+
) -> (TxGraph<BlockId>, SpkTxOutIndex<u32>, LocalChain) {
313+
let chain = local_chain![(0, hash!("genesis"))];
296314
let mut templates = Vec::new();
297-
let (mut tx_graph, chain) = initialize_graph_and_keychain();
298315

299316
templates.push(TxTemplate {
300317
tx_name: "ancestor_tx".into(),
@@ -321,24 +338,17 @@ fn setup_many_chained_unconfirmed(tx_chain_count: u32) -> (KeychainTxGraph, Loca
321338
});
322339
}
323340

324-
let (graph, _, _) = init_graph(templates);
325-
let _ = tx_graph.batch_insert_unconfirmed(graph.full_txs().map(|tx_node| {
326-
(
327-
tx_node.tx,
328-
tx_node.last_seen_unconfirmed.unwrap_or_default(),
329-
)
330-
}));
331-
332-
(tx_graph, chain)
341+
let (tx_graph, spk_index, _) = init_graph(templates);
342+
(tx_graph, spk_index, chain)
333343
}
334344

335345
/// Utility function to create a benchmark-ready graph with nested conflicting transactions
336346
fn setup_nested_conflicts(
337347
graph_depth: usize,
338348
conflicts_per_output: usize,
339-
) -> (KeychainTxGraph, LocalChain) {
349+
) -> (TxGraph<BlockId>, SpkTxOutIndex<u32>, LocalChain) {
350+
let chain = local_chain![(0, hash!("genesis"))];
340351
let mut templates = Vec::new();
341-
let (mut tx_graph, chain) = initialize_graph_and_keychain();
342352

343353
templates.push(TxTemplate {
344354
tx_name: "ancestor_tx".into(),
@@ -350,11 +360,13 @@ fn setup_nested_conflicts(
350360

351361
let mut previous_outputs = vec!["ancestor_tx".to_string()];
352362

353-
for depth in 1..graph_depth {
363+
for depth in 1..=graph_depth {
354364
let mut next_outputs = Vec::new();
365+
355366
for previous_output_name in previous_outputs.drain(..) {
356367
for conflict_i in 1..=conflicts_per_output {
357368
let tx_name = format!("depth_{}_conflict_{}", depth, conflict_i);
369+
358370
let mut last_seen = depth * conflict_i;
359371
if last_seen % 2 == 0 {
360372
last_seen /= 2;
@@ -365,7 +377,7 @@ fn setup_nested_conflicts(
365377
inputs: vec![TxInTemplate::PrevTx(previous_output_name.clone().into(), 0)],
366378
outputs: vec![TxOutTemplate::new(
367379
Amount::ONE_BTC.to_sat() - (depth as u64 * 200 - conflict_i as u64),
368-
Some((depth + conflict_i) as u32),
380+
Some(0),
369381
)],
370382
anchors: vec![],
371383
last_seen: Some(last_seen as u64),
@@ -374,48 +386,42 @@ fn setup_nested_conflicts(
374386
next_outputs.push(tx_name);
375387
}
376388
}
389+
377390
previous_outputs = next_outputs;
378391
}
379392

380-
let (graph, _, _) = init_graph(templates);
381-
let _ = tx_graph.batch_insert_unconfirmed(graph.full_txs().map(|tx_node| {
382-
(
383-
tx_node.tx,
384-
tx_node.last_seen_unconfirmed.unwrap_or_default(),
385-
)
386-
}));
387-
388-
(tx_graph, chain)
393+
let (tx_graph, spk_index, _) = init_graph(templates);
394+
(tx_graph, spk_index, chain)
389395
}
390396

391397
/// Benchmark scenario for many conflicting unconfirmed transactions
392398
fn bench_many_conflicting_unconfirmed(c: &mut Criterion) {
393399
const CONFLICTING_TX_COUNT: u32 = 2100;
394400

395-
let (tx_graph, chain) = setup_many_conflicting_unconfirmed(CONFLICTING_TX_COUNT);
401+
let (tx_graph, spk_index, chain) = setup_many_conflicting_unconfirmed(CONFLICTING_TX_COUNT);
396402

397403
c.bench_function("many_conflicting_unconfirmed::list_canonical_txs", {
398404
let tx_graph = tx_graph.clone();
399405
let chain = chain.clone();
400406
move |b| {
401-
b.iter(|| run_list_canonical_txs(&tx_graph, &chain, 2));
407+
b.iter(|| list_canonical_txs(&tx_graph, &chain, 2));
402408
}
403409
});
404410

405411
c.bench_function("many_conflicting_unconfirmed::filter_chain_txouts", {
406412
let tx_graph = tx_graph.clone();
413+
let spk_index = spk_index.clone();
407414
let chain = chain.clone();
408415
move |b| {
409-
b.iter(|| run_filter_chain_txouts(&tx_graph, &chain, 2));
416+
b.iter(|| filter_chain_txouts(&tx_graph, &spk_index, &chain, 2));
410417
}
411418
});
412419

413420
c.bench_function(
414421
"many_conflicting_unconfirmed::filter_chain_unspents",
415422
move |b| {
416-
let tx_graph = tx_graph.clone();
417423
b.iter(|| {
418-
run_filter_chain_unspents(&tx_graph, &chain, 1);
424+
filter_chain_unspents(&tx_graph, &spk_index, &chain, 1);
419425
});
420426
},
421427
);
@@ -425,34 +431,33 @@ fn bench_many_conflicting_unconfirmed(c: &mut Criterion) {
425431
pub fn bench_many_chained_unconfirmed(c: &mut Criterion) {
426432
const TX_CHAIN_COUNT: u32 = 2100;
427433

428-
let (tx_graph, chain) = setup_many_chained_unconfirmed(TX_CHAIN_COUNT);
434+
let (tx_graph, spk_index, chain) = setup_many_chained_unconfirmed(TX_CHAIN_COUNT);
429435

430436
c.bench_function("many_chained_unconfirmed::list_canonical_txs", {
431437
let tx_graph = tx_graph.clone();
432438
let chain = chain.clone();
433439
move |b| {
434440
b.iter(|| {
435-
run_list_canonical_txs(&tx_graph, &chain, (TX_CHAIN_COUNT + 1).try_into().unwrap());
441+
list_canonical_txs(&tx_graph, &chain, (TX_CHAIN_COUNT + 1).try_into().unwrap());
436442
});
437443
}
438444
});
439445

440446
c.bench_function("many_chained_unconfirmed::filter_chain_txouts", {
441447
let tx_graph = tx_graph.clone();
442448
let chain = chain.clone();
449+
let spk_index = spk_index.clone();
443450
move |b| {
444451
b.iter(|| {
445-
run_filter_chain_txouts(&tx_graph, &chain, 1);
452+
filter_chain_txouts(&tx_graph, &spk_index, &chain, 1);
446453
});
447454
}
448455
});
449456

450457
c.bench_function("many_chained_unconfirmed::filter_chain_unspents", {
451-
let tx_graph = tx_graph.clone();
452-
let chain = chain.clone();
453458
move |b| {
454459
b.iter(|| {
455-
run_filter_chain_unspents(&tx_graph, &chain, 0);
460+
filter_chain_unspents(&tx_graph, &spk_index, &chain, 0);
456461
});
457462
}
458463
});
@@ -463,34 +468,33 @@ pub fn bench_nested_conflicts(c: &mut Criterion) {
463468
const CONFLICTS_PER_OUTPUT: usize = 3;
464469
const GRAPH_DEPTH: usize = 7;
465470

466-
let (tx_graph, chain) = setup_nested_conflicts(GRAPH_DEPTH, CONFLICTS_PER_OUTPUT);
471+
let (tx_graph, spk_index, chain) = setup_nested_conflicts(GRAPH_DEPTH, CONFLICTS_PER_OUTPUT);
467472

468473
c.bench_function("nested_conflicts_unconfirmed::list_canonical_txs", {
469474
let tx_graph = tx_graph.clone();
470475
let chain = chain.clone();
471476
move |b| {
472477
b.iter(|| {
473-
run_list_canonical_txs(&tx_graph, &chain, GRAPH_DEPTH);
478+
list_canonical_txs(&tx_graph, &chain, GRAPH_DEPTH);
474479
});
475480
}
476481
});
477482

478483
c.bench_function("nested_conflicts_unconfirmed::filter_chain_txouts", {
479484
let tx_graph = tx_graph.clone();
480485
let chain = chain.clone();
486+
let spk_index = spk_index.clone();
481487
move |b| {
482488
b.iter(|| {
483-
run_filter_chain_txouts(&tx_graph, &chain, GRAPH_DEPTH);
489+
filter_chain_txouts(&tx_graph, &spk_index, &chain, GRAPH_DEPTH);
484490
});
485491
}
486492
});
487493

488494
c.bench_function("nested_conflicts_unconfirmed::filter_chain_unspents", {
489-
let tx_graph = tx_graph.clone();
490-
let chain = chain.clone();
491495
move |b| {
492496
b.iter(|| {
493-
run_filter_chain_unspents(&tx_graph, &chain, 1);
497+
filter_chain_unspents(&tx_graph, &spk_index, &chain, 1);
494498
});
495499
}
496500
});

0 commit comments

Comments
 (0)