Skip to content

Commit

Permalink
text(chain,wallet): Test assume_canonical mod
Browse files Browse the repository at this point in the history
Also change `TxTemplate` API to allow for testing with
`assume_canonical`.
  • Loading branch information
evanlinjin committed Jan 23, 2025
1 parent 9624b00 commit 0f00cdb
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 39 deletions.
8 changes: 5 additions & 3 deletions crates/chain/benches/canonicalization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ fn setup<F: Fn(&mut KeychainTxGraph, &LocalChain)>(f: F) -> (KeychainTxGraph, Lo
}

fn run_list_canonical_txs(tx_graph: &KeychainTxGraph, chain: &LocalChain, exp_txs: usize) {
let txs = tx_graph
.graph()
.list_canonical_txs(chain, chain.tip().block_id(), CanonicalizationMods::NONE);
let txs = tx_graph.graph().list_canonical_txs(
chain,
chain.tip().block_id(),
CanonicalizationMods::NONE,
);
assert_eq!(txs.count(), exp_txs);
}

Expand Down
47 changes: 33 additions & 14 deletions crates/chain/tests/common/tx_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bdk_testenv::utils::DESCRIPTORS;
use rand::distributions::{Alphanumeric, DistString};
use std::collections::HashMap;

use bdk_chain::{spk_txout::SpkTxOutIndex, tx_graph::TxGraph, Anchor};
use bdk_chain::{spk_txout::SpkTxOutIndex, tx_graph::TxGraph, Anchor, CanonicalizationMods};
use bitcoin::{
locktime::absolute::LockTime, secp256k1::Secp256k1, transaction, Amount, OutPoint, ScriptBuf,
Sequence, Transaction, TxIn, TxOut, Txid, Witness,
Expand All @@ -24,6 +24,7 @@ pub struct TxTemplate<'a, A> {
pub outputs: &'a [TxOutTemplate],
pub anchors: &'a [A],
pub last_seen: Option<u64>,
pub assume_canonical: bool,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -51,25 +52,34 @@ impl TxOutTemplate {
}
}

#[allow(dead_code)]
pub struct TxTemplateEnv<'a, A> {
pub tx_graph: TxGraph<A>,
pub indexer: SpkTxOutIndex<u32>,
pub txid_to_name: HashMap<&'a str, Txid>,
pub canonicalization_mods: CanonicalizationMods,
}

#[allow(dead_code)]
pub fn init_graph<'a, A: Anchor + Clone + 'a>(
tx_templates: impl IntoIterator<Item = &'a TxTemplate<'a, A>>,
) -> (TxGraph<A>, SpkTxOutIndex<u32>, HashMap<&'a str, Txid>) {
) -> TxTemplateEnv<'a, A> {
let (descriptor, _) =
Descriptor::parse_descriptor(&Secp256k1::signing_only(), DESCRIPTORS[2]).unwrap();
let mut graph = TxGraph::<A>::default();
let mut spk_index = SpkTxOutIndex::default();
let mut tx_graph = TxGraph::<A>::default();
let mut indexer = SpkTxOutIndex::default();
(0..10).for_each(|index| {
spk_index.insert_spk(
indexer.insert_spk(
index,
descriptor
.at_derivation_index(index)
.unwrap()
.script_pubkey(),
);
});
let mut tx_ids = HashMap::<&'a str, Txid>::new();
let mut txid_to_name = HashMap::<&'a str, Txid>::new();

let mut canonicalization_mods = CanonicalizationMods::default();
for (bogus_txin_vout, tx_tmp) in tx_templates.into_iter().enumerate() {
let tx = Transaction {
version: transaction::Version::non_standard(0),
Expand Down Expand Up @@ -98,7 +108,7 @@ pub fn init_graph<'a, A: Anchor + Clone + 'a>(
witness: Witness::new(),
},
TxInTemplate::PrevTx(prev_name, prev_vout) => {
let prev_txid = tx_ids.get(prev_name).expect(
let prev_txid = txid_to_name.get(prev_name).expect(
"txin template must spend from tx of template that comes before",
);
TxIn {
Expand All @@ -120,21 +130,30 @@ pub fn init_graph<'a, A: Anchor + Clone + 'a>(
},
Some(index) => TxOut {
value: Amount::from_sat(output.value),
script_pubkey: spk_index.spk_at_index(index).unwrap(),
script_pubkey: indexer.spk_at_index(index).unwrap(),
},
})
.collect(),
};

tx_ids.insert(tx_tmp.tx_name, tx.compute_txid());
spk_index.scan(&tx);
let _ = graph.insert_tx(tx.clone());
let txid = tx.compute_txid();
if tx_tmp.assume_canonical {
canonicalization_mods.assume_canonical.push(txid);
}
txid_to_name.insert(tx_tmp.tx_name, txid);
indexer.scan(&tx);
let _ = tx_graph.insert_tx(tx.clone());
for anchor in tx_tmp.anchors.iter() {
let _ = graph.insert_anchor(tx.compute_txid(), anchor.clone());
let _ = tx_graph.insert_anchor(txid, anchor.clone());
}
if let Some(last_seen) = tx_tmp.last_seen {
let _ = graph.insert_seen_at(tx.compute_txid(), last_seen);
let _ = tx_graph.insert_seen_at(txid, last_seen);
}
}
(graph, spk_index, tx_ids)
TxTemplateEnv {
tx_graph,
indexer,
txid_to_name,
canonicalization_mods,
}
}
3 changes: 2 additions & 1 deletion crates/chain/tests/test_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,7 @@ fn call_map_anchors_with_non_deterministic_anchor() {
outputs: &[TxOutTemplate::new(10000, Some(1))],
anchors: &[block_id!(1, "A")],
last_seen: None,
..Default::default()
},
TxTemplate {
tx_name: "tx2",
Expand All @@ -1227,7 +1228,7 @@ fn call_map_anchors_with_non_deterministic_anchor() {
..Default::default()
},
];
let (graph, _, _) = init_graph(&template);
let graph = init_graph(&template).tx_graph;
let new_graph = graph.clone().map_anchors(|a| NonDeterministicAnchor {
anchor_block: a,
// A non-deterministic value
Expand Down
Loading

0 comments on commit 0f00cdb

Please sign in to comment.