Skip to content

Commit 1311a2e

Browse files
evanlinjinclaude
authored andcommitted
refactor(chain)!: Change trust_predicate to accept FullTxOut
BREAKING CHANGE: The trust_predicate parameter in CanonicalView::balance() now takes &FullTxOut<A> instead of ScriptBuf as its second argument. This provides more context to the predicate function. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 3f9eec5 commit 1311a2e

File tree

4 files changed

+69
-66
lines changed

4 files changed

+69
-66
lines changed

crates/chain/src/canonical_view.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ pub struct CanonicalTx<A> {
6060
/// provides methods to query transaction data, unspent outputs, and balances.
6161
///
6262
/// The view maintains:
63-
/// - An ordered list of canonical transactions (WIP)
63+
/// - An ordered list of canonical transactions in topological-spending order
6464
/// - A mapping of outpoints to the transactions that spend them
6565
/// - The chain tip used for canonicalization
6666
#[derive(Debug)]
6767
pub struct CanonicalView<A> {
68-
/// Ordered list of transaction IDs in canonical order.
68+
/// Ordered list of transaction IDs in in topological-spending order.
6969
order: Vec<Txid>,
7070
/// Map of transaction IDs to their transaction data and chain position.
7171
txs: HashMap<Txid, (Arc<Transaction>, ChainPosition<A>)>,
@@ -357,7 +357,7 @@ impl<A: Anchor> CanonicalView<A> {
357357
pub fn balance<'v, O: Clone + 'v>(
358358
&'v self,
359359
outpoints: impl IntoIterator<Item = (O, OutPoint)> + 'v,
360-
mut trust_predicate: impl FnMut(&O, ScriptBuf) -> bool,
360+
mut trust_predicate: impl FnMut(&O, &FullTxOut<A>) -> bool,
361361
min_confirmations: u32,
362362
) -> Balance {
363363
let mut immature = Amount::ZERO;
@@ -378,7 +378,7 @@ impl<A: Anchor> CanonicalView<A> {
378378

379379
if confirmations < min_confirmations {
380380
// Not enough confirmations, treat as trusted/untrusted pending
381-
if trust_predicate(&spk_i, txout.txout.script_pubkey) {
381+
if trust_predicate(&spk_i, &txout) {
382382
trusted_pending += txout.txout.value;
383383
} else {
384384
untrusted_pending += txout.txout.value;
@@ -390,7 +390,7 @@ impl<A: Anchor> CanonicalView<A> {
390390
}
391391
}
392392
ChainPosition::Unconfirmed { .. } => {
393-
if trust_predicate(&spk_i, txout.txout.script_pubkey) {
393+
if trust_predicate(&spk_i, &txout) {
394394
trusted_pending += txout.txout.value;
395395
} else {
396396
untrusted_pending += txout.txout.value;

crates/chain/tests/test_canonical_view.rs

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
#![cfg(feature = "miniscript")]
22

3+
use std::collections::BTreeMap;
4+
35
use bdk_chain::{local_chain::LocalChain, CanonicalizationParams, ConfirmationBlockTime, TxGraph};
46
use bdk_testenv::{hash, utils::new_tx};
5-
use bitcoin::{Amount, OutPoint, ScriptBuf, Transaction, TxIn, TxOut};
7+
use bitcoin::{Amount, BlockHash, OutPoint, ScriptBuf, Transaction, TxIn, TxOut};
68

79
#[test]
810
fn test_min_confirmations_parameter() {
911
// Create a local chain with several blocks
10-
let chain = LocalChain::from_blocks(
11-
[
12-
(0, hash!("block0")),
13-
(1, hash!("block1")),
14-
(2, hash!("block2")),
15-
(3, hash!("block3")),
16-
(4, hash!("block4")),
17-
(5, hash!("block5")),
18-
(6, hash!("block6")),
19-
(7, hash!("block7")),
20-
(8, hash!("block8")),
21-
(9, hash!("block9")),
22-
(10, hash!("block10")),
23-
]
24-
.into(),
25-
)
26-
.unwrap();
12+
let blocks: BTreeMap<u32, BlockHash> = [
13+
(0, hash!("block0")),
14+
(1, hash!("block1")),
15+
(2, hash!("block2")),
16+
(3, hash!("block3")),
17+
(4, hash!("block4")),
18+
(5, hash!("block5")),
19+
(6, hash!("block6")),
20+
(7, hash!("block7")),
21+
(8, hash!("block8")),
22+
(9, hash!("block9")),
23+
(10, hash!("block10")),
24+
]
25+
.into_iter()
26+
.collect();
27+
let chain = LocalChain::from_blocks(blocks).unwrap();
2728

2829
let mut tx_graph = TxGraph::default();
2930

@@ -98,23 +99,22 @@ fn test_min_confirmations_parameter() {
9899
#[test]
99100
fn test_min_confirmations_with_untrusted_tx() {
100101
// Create a local chain
101-
let chain = LocalChain::from_blocks(
102-
[
103-
(0, hash!("genesis")),
104-
(1, hash!("b1")),
105-
(2, hash!("b2")),
106-
(3, hash!("b3")),
107-
(4, hash!("b4")),
108-
(5, hash!("b5")),
109-
(6, hash!("b6")),
110-
(7, hash!("b7")),
111-
(8, hash!("b8")),
112-
(9, hash!("b9")),
113-
(10, hash!("tip")),
114-
]
115-
.into(),
116-
)
117-
.unwrap();
102+
let blocks: BTreeMap<u32, BlockHash> = [
103+
(0, hash!("genesis")),
104+
(1, hash!("b1")),
105+
(2, hash!("b2")),
106+
(3, hash!("b3")),
107+
(4, hash!("b4")),
108+
(5, hash!("b5")),
109+
(6, hash!("b6")),
110+
(7, hash!("b7")),
111+
(8, hash!("b8")),
112+
(9, hash!("b9")),
113+
(10, hash!("tip")),
114+
]
115+
.into_iter()
116+
.collect();
117+
let chain = LocalChain::from_blocks(blocks).unwrap();
118118

119119
let mut tx_graph = TxGraph::default();
120120

@@ -164,28 +164,27 @@ fn test_min_confirmations_with_untrusted_tx() {
164164
#[test]
165165
fn test_min_confirmations_multiple_transactions() {
166166
// Create a local chain
167-
let chain = LocalChain::from_blocks(
168-
[
169-
(0, hash!("genesis")),
170-
(1, hash!("b1")),
171-
(2, hash!("b2")),
172-
(3, hash!("b3")),
173-
(4, hash!("b4")),
174-
(5, hash!("b5")),
175-
(6, hash!("b6")),
176-
(7, hash!("b7")),
177-
(8, hash!("b8")),
178-
(9, hash!("b9")),
179-
(10, hash!("b10")),
180-
(11, hash!("b11")),
181-
(12, hash!("b12")),
182-
(13, hash!("b13")),
183-
(14, hash!("b14")),
184-
(15, hash!("tip")),
185-
]
186-
.into(),
187-
)
188-
.unwrap();
167+
let blocks: BTreeMap<u32, BlockHash> = [
168+
(0, hash!("genesis")),
169+
(1, hash!("b1")),
170+
(2, hash!("b2")),
171+
(3, hash!("b3")),
172+
(4, hash!("b4")),
173+
(5, hash!("b5")),
174+
(6, hash!("b6")),
175+
(7, hash!("b7")),
176+
(8, hash!("b8")),
177+
(9, hash!("b9")),
178+
(10, hash!("b10")),
179+
(11, hash!("b11")),
180+
(12, hash!("b12")),
181+
(13, hash!("b13")),
182+
(14, hash!("b14")),
183+
(15, hash!("tip")),
184+
]
185+
.into_iter()
186+
.collect();
187+
let chain = LocalChain::from_blocks(blocks).unwrap();
189188

190189
let mut tx_graph = TxGraph::default();
191190

crates/chain/tests/test_indexed_tx_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ fn test_list_owned_txouts() {
473473
.canonical_view(&local_chain, chain_tip, CanonicalizationParams::default())
474474
.balance(
475475
graph.index.outpoints().iter().cloned(),
476-
|_, spk: ScriptBuf| trusted_spks.contains(&spk),
476+
|_, txout| trusted_spks.contains(&txout.txout.script_pubkey),
477477
1,
478478
);
479479

crates/chain/tests/test_tx_graph_conflicts.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod common;
55

66
use bdk_chain::{local_chain::LocalChain, Balance, BlockId};
77
use bdk_testenv::{block_id, hash, local_chain};
8-
use bitcoin::{Amount, BlockHash, OutPoint, ScriptBuf};
8+
use bitcoin::{Amount, BlockHash, OutPoint};
99
use common::*;
1010
use std::collections::{BTreeSet, HashSet};
1111

@@ -1032,8 +1032,12 @@ fn test_tx_conflict_handling() {
10321032
.canonical_view(&local_chain, chain_tip, env.canonicalization_params.clone())
10331033
.balance(
10341034
env.indexer.outpoints().iter().cloned(),
1035-
|_, spk: ScriptBuf| env.indexer.index_of_spk(spk).is_some(),
1036-
1,
1035+
|_, txout| {
1036+
env.indexer
1037+
.index_of_spk(txout.txout.script_pubkey.clone())
1038+
.is_some()
1039+
},
1040+
0,
10371041
);
10381042
assert_eq!(
10391043
balance, scenario.exp_balance,

0 commit comments

Comments
 (0)