Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 14 additions & 47 deletions crates/chain/benches/canonicalization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,20 @@ use bdk_chain::{keychain_txout::KeychainTxOutIndex, local_chain::LocalChain, Ind
use bdk_core::{BlockId, CheckPoint};
use bdk_core::{ConfirmationBlockTime, TxUpdate};
use bdk_testenv::hash;
use bitcoin::{
absolute, constants, hashes::Hash, key::Secp256k1, transaction, Amount, BlockHash, Network,
OutPoint, ScriptBuf, Transaction, TxIn, TxOut,
};
use bdk_testenv::utils::{genesis_block_id, new_standard_tx, spk_at_index, tip_block_id};
use bitcoin::{key::Secp256k1, Amount, OutPoint, Transaction, TxIn, TxOut};
use criterion::{criterion_group, criterion_main, Criterion};
use miniscript::{Descriptor, DescriptorPublicKey};
use std::sync::Arc;

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

/// New tx guaranteed to have at least one output
fn new_tx(lt: u32) -> Transaction {
Transaction {
version: transaction::Version::TWO,
lock_time: absolute::LockTime::from_consensus(lt),
input: vec![],
output: vec![TxOut::NULL],
}
}

fn spk_at_index(txout_index: &KeychainTxOutIndex<Keychain>, index: u32) -> ScriptBuf {
txout_index
.get_descriptor(())
.unwrap()
.at_derivation_index(index)
.unwrap()
.script_pubkey()
}

fn genesis_block_id() -> BlockId {
BlockId {
height: 0,
hash: constants::genesis_block(Network::Regtest).block_hash(),
}
}

fn tip_block_id() -> BlockId {
BlockId {
height: 100,
hash: BlockHash::all_zeros(),
}
}

/// Add ancestor tx confirmed at `block_id` with `locktime` (used for uniqueness).
/// The transaction always pays 1 BTC to SPK 0.
fn add_ancestor_tx(graph: &mut KeychainTxGraph, block_id: BlockId, locktime: u32) -> OutPoint {
let spk_0 = spk_at_index(&graph.index, 0);
let descriptor = graph.index.get_descriptor(()).unwrap();
let spk_0 = spk_at_index(descriptor, 0);
let tx = Transaction {
input: vec![TxIn {
previous_output: OutPoint::new(hash!("bogus"), locktime),
Expand All @@ -60,7 +26,7 @@ fn add_ancestor_tx(graph: &mut KeychainTxGraph, block_id: BlockId, locktime: u32
value: Amount::ONE_BTC,
script_pubkey: spk_0,
}],
..new_tx(locktime)
..new_standard_tx(locktime)
};
let txid = tx.compute_txid();
let _ = graph.insert_tx(tx);
Expand All @@ -77,7 +43,7 @@ fn add_ancestor_tx(graph: &mut KeychainTxGraph, block_id: BlockId, locktime: u32
fn setup<F: Fn(&mut KeychainTxGraph, &LocalChain)>(f: F) -> (KeychainTxGraph, LocalChain) {
const DESC: &str = "tr([ab28dc00/86h/1h/0h]tpubDCdDtzAMZZrkwKBxwNcGCqe4FRydeD9rfMisoi7qLdraG79YohRfPW4YgdKQhpgASdvh612xXNY5xYzoqnyCgPbkpK4LSVcH5Xv4cK7johH/0/*)";
let cp = CheckPoint::from_blocks(
[genesis_block_id(), tip_block_id()]
[genesis_block_id(), tip_block_id(100)]
.into_iter()
.map(|block_id| (block_id.height, block_id.hash)),
)
Expand Down Expand Up @@ -127,9 +93,10 @@ fn run_filter_chain_unspents(tx_graph: &KeychainTxGraph, chain: &LocalChain, exp
pub fn many_conflicting_unconfirmed(c: &mut Criterion) {
const CONFLICTING_TX_COUNT: u32 = 2100;
let (tx_graph, chain) = std::hint::black_box(setup(|tx_graph, _chain| {
let previous_output = add_ancestor_tx(tx_graph, tip_block_id(), 0);
let previous_output = add_ancestor_tx(tx_graph, tip_block_id(100), 0);
let descriptor = tx_graph.index.get_descriptor(()).unwrap();
// Create conflicting txs that spend from `previous_output`.
let spk_1 = spk_at_index(&tx_graph.index, 1);
let spk_1 = spk_at_index(descriptor, 1);
for i in 1..=CONFLICTING_TX_COUNT {
let tx = Transaction {
input: vec![TxIn {
Expand All @@ -140,7 +107,7 @@ pub fn many_conflicting_unconfirmed(c: &mut Criterion) {
value: Amount::ONE_BTC - Amount::from_sat(i as u64 * 10),
script_pubkey: spk_1.clone(),
}],
..new_tx(i)
..new_standard_tx(i)
};
let mut update = TxUpdate::default();
update.seen_ats = [(tx.compute_txid(), i as u64)].into();
Expand All @@ -165,7 +132,7 @@ pub fn many_conflicting_unconfirmed(c: &mut Criterion) {
pub fn many_chained_unconfirmed(c: &mut Criterion) {
const TX_CHAIN_COUNT: u32 = 2100;
let (tx_graph, chain) = std::hint::black_box(setup(|tx_graph, _chain| {
let mut previous_output = add_ancestor_tx(tx_graph, tip_block_id(), 0);
let mut previous_output = add_ancestor_tx(tx_graph, tip_block_id(100), 0);
// Create a chain of unconfirmed txs where each subsequent tx spends the output of the
// previous one.
for i in 0..TX_CHAIN_COUNT {
Expand All @@ -175,7 +142,7 @@ pub fn many_chained_unconfirmed(c: &mut Criterion) {
previous_output,
..Default::default()
}],
..new_tx(i)
..new_standard_tx(i)
};
let txid = tx.compute_txid();
let mut update = TxUpdate::default();
Expand Down Expand Up @@ -204,7 +171,7 @@ pub fn nested_conflicts(c: &mut Criterion) {
const CONFLICTS_PER_OUTPUT: usize = 3;
const GRAPH_DEPTH: usize = 7;
let (tx_graph, chain) = std::hint::black_box(setup(|tx_graph, _chain| {
let mut prev_ops = core::iter::once(add_ancestor_tx(tx_graph, tip_block_id(), 0))
let mut prev_ops = core::iter::once(add_ancestor_tx(tx_graph, tip_block_id(100), 0))
.collect::<Vec<OutPoint>>();
for depth in 1..GRAPH_DEPTH {
for previous_output in core::mem::take(&mut prev_ops) {
Expand All @@ -225,7 +192,7 @@ pub fn nested_conflicts(c: &mut Criterion) {
value,
script_pubkey,
}],
..new_tx(conflict_i as _)
..new_standard_tx(conflict_i as _)
};
let txid = tx.compute_txid();
prev_ops.push(OutPoint::new(txid, 0));
Expand Down
35 changes: 5 additions & 30 deletions crates/chain/benches/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ use bdk_chain::{
local_chain::LocalChain,
CanonicalizationParams, IndexedTxGraph,
};
use bdk_core::{BlockId, CheckPoint, ConfirmationBlockTime, TxUpdate};
use bitcoin::{
absolute, constants, hashes::Hash, key::Secp256k1, transaction, Amount, BlockHash, Network,
Transaction, TxIn, TxOut,
};
use bdk_core::{CheckPoint, ConfirmationBlockTime, TxUpdate};
use bdk_testenv::utils::{genesis_block_id, new_standard_tx, tip_block_id};
use bitcoin::{key::Secp256k1, Amount, Transaction, TxIn, TxOut};
use criterion::{criterion_group, criterion_main, Criterion};
use miniscript::Descriptor;
use std::sync::Arc;
Expand All @@ -22,36 +20,13 @@ const TX_CT: u32 = 21;
const USE_SPK_CACHE: bool = true;
const AMOUNT: Amount = Amount::from_sat(1_000);

fn new_tx(lt: u32) -> Transaction {
Transaction {
version: transaction::Version::TWO,
lock_time: absolute::LockTime::from_consensus(lt),
input: vec![],
output: vec![TxOut::NULL],
}
}

fn genesis_block_id() -> BlockId {
BlockId {
height: 0,
hash: constants::genesis_block(Network::Regtest).block_hash(),
}
}

fn tip_block_id() -> BlockId {
BlockId {
height: 100,
hash: BlockHash::all_zeros(),
}
}

fn setup<F: Fn(&mut KeychainTxGraph, &LocalChain)>(f: F) -> (KeychainTxGraph, LocalChain) {
let desc = Descriptor::parse_descriptor(&Secp256k1::new(), DESC)
.unwrap()
.0;

let cp = CheckPoint::from_blocks(
[genesis_block_id(), tip_block_id()]
[genesis_block_id(), tip_block_id(100)]
.into_iter()
.map(|block_id| (block_id.height, block_id.hash)),
)
Expand Down Expand Up @@ -101,7 +76,7 @@ pub fn reindex_tx_graph(c: &mut Criterion) {
script_pubkey,
value: AMOUNT,
}],
..new_tx(i)
..new_standard_tx(i)
};
let txid = tx.compute_txid();
let mut update = TxUpdate::default();
Expand Down
5 changes: 0 additions & 5 deletions crates/chain/tests/common/mod.rs

This file was deleted.

159 changes: 0 additions & 159 deletions crates/chain/tests/common/tx_template.rs

This file was deleted.

Loading
Loading