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 @@ -2,54 +2,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 @@ -59,7 +25,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 @@ -76,7 +42,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 @@ -114,9 +80,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 @@ -127,7 +94,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 @@ -152,7 +119,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 @@ -162,7 +129,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 @@ -191,7 +158,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 @@ -212,7 +179,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,
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 @@ -100,7 +75,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.

45 changes: 15 additions & 30 deletions crates/chain/tests/test_canonical_view_task.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#![cfg(feature = "miniscript")]

mod common;

use bdk_chain::{CanonicalReason, ChainPosition};
use bdk_testenv::{block_id, hash, local_chain};
use bdk_testenv::{
block_id, hash, init_graph, local_chain, TxInTemplate, TxOutTemplate, TxTemplate,
};
use bitcoin::Txid;
use common::*;
use std::collections::HashSet;

#[test]
Expand All @@ -28,37 +27,23 @@ fn test_assumed_canonical_scenarios() {
];
let chain_tip = local_chain.tip().block_id();

// create arrays before scenario to avoid lifetime issues
let tx_templates = [
TxTemplate {
tx_name: "txA",
inputs: &[TxInTemplate::Bogus],
outputs: &[TxOutTemplate::new(100000, Some(0))],
anchors: &[],
last_seen: None,
assume_canonical: false,
},
TxTemplate {
tx_name: "txB",
inputs: &[TxInTemplate::PrevTx("txA", 0)],
outputs: &[TxOutTemplate::new(50000, Some(0))],
anchors: &[block_id!(5, "block5")],
last_seen: None,
assume_canonical: false,
},
TxTemplate {
tx_name: "txC",
inputs: &[TxInTemplate::PrevTx("txB", 0)],
outputs: &[TxOutTemplate::new(25000, Some(0))],
anchors: &[],
last_seen: None,
assume_canonical: true,
},
TxTemplate::new("txA")
.with_inputs(vec![TxInTemplate::Bogus])
.with_outputs(vec![TxOutTemplate::new(100000, Some(0))]),
TxTemplate::new("txB")
.with_inputs(vec![TxInTemplate::PrevTx("txA", 0)])
.with_outputs(vec![TxOutTemplate::new(50000, Some(0))])
.with_anchors(vec![block_id!(5, "block5")]),
TxTemplate::new("txC")
.with_inputs(vec![TxInTemplate::PrevTx("txB", 0)])
.with_outputs(vec![TxOutTemplate::new(25000, Some(0))])
.with_assume_canonical(true),
];

let exp_canonical_txs = HashSet::from(["txA", "txB", "txC"]);

let env = init_graph(&tx_templates);
let env = init_graph(tx_templates);

// get the actual txid from given tx_name.
let txid_c = *env.txid_to_name.get("txC").unwrap();
Expand Down
17 changes: 2 additions & 15 deletions crates/chain/tests/test_indexed_tx_graph.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#![cfg(feature = "miniscript")]

#[macro_use]
mod common;

use std::{collections::BTreeSet, str::FromStr, sync::Arc};

use bdk_chain::{
Expand All @@ -15,7 +12,7 @@ use bdk_chain::{
use bdk_testenv::{
anyhow::{self},
bitcoind::{Input, Output},
block_id, hash,
block_id, hash, spk,
utils::{new_tx, DESCRIPTORS},
TestEnv,
};
Expand All @@ -25,16 +22,6 @@ use bitcoin::{
};
use miniscript::Descriptor;

fn gen_spk() -> ScriptBuf {
use bitcoin::secp256k1::{Secp256k1, SecretKey};

let secp = Secp256k1::new();
let (x_only_pk, _) = SecretKey::new(&mut rand::thread_rng())
.public_key(&secp)
.x_only_public_key();
ScriptBuf::new_p2tr(&secp, x_only_pk, None)
}

/// Conflicts of relevant transactions must also be considered relevant.
///
/// This allows the receiving structures to determine the reason why a given transaction is not part
Expand Down Expand Up @@ -66,7 +53,7 @@ fn relevant_conflicts() -> anyhow::Result<()> {
.address()?
.require_network(Network::Regtest)?;

let recv_spk = gen_spk();
let recv_spk = spk!();
let recv_addr = Address::from_script(&recv_spk, &bitcoin::params::REGTEST)?;

let mut graph = SpkTxGraph::default();
Expand Down
11 changes: 2 additions & 9 deletions crates/chain/tests/test_keychain_txout_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use bdk_chain::{
};
use bdk_testenv::{
hash,
utils::{new_tx, DESCRIPTORS},
utils::{new_tx, spk_at_index, DESCRIPTORS},
};
use bitcoin::{secp256k1::Secp256k1, Amount, OutPoint, ScriptBuf, Transaction, TxOut};
use bitcoin::{Amount, OutPoint, ScriptBuf, Transaction, TxOut};
use miniscript::{Descriptor, DescriptorPublicKey};

#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
Expand Down Expand Up @@ -52,13 +52,6 @@ fn init_txout_index(
txout_index
}

fn spk_at_index(descriptor: &Descriptor<DescriptorPublicKey>, index: u32) -> ScriptBuf {
descriptor
.derived_descriptor(&Secp256k1::verification_only(), index)
.expect("must derive")
.script_pubkey()
}

// We create two empty changesets lhs and rhs, we then insert various descriptors with various
// last_revealed, merge rhs to lhs, and check that the result is consistent with these rules:
// - Existing index doesn't update if the new index in `other` is lower than `self`.
Expand Down
Loading