Skip to content

Commit 53a0820

Browse files
evanlinjinclaude
authored andcommitted
feat(chain)!: Introduce CanonicalView and migrate API
- Add `CanonicalView` structure with canonical transaction methods - Move methods from `TxGraph` to `CanonicalView` (txs, filter_outpoints, balance, etc.) - Add canonical view methods to `IndexedTxGraph` - Update all tests and examples to use new API - Optimize examples to reuse canonical view instances 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 4031e19 commit 53a0820

File tree

18 files changed

+469
-671
lines changed

18 files changed

+469
-671
lines changed

crates/bitcoind_rpc/examples/filter_iter.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,8 @@ fn main() -> anyhow::Result<()> {
6969
println!("\ntook: {}s", start.elapsed().as_secs());
7070
println!("Local tip: {}", chain.tip().height());
7171
let unspent: Vec<_> = graph
72-
.graph()
73-
.filter_chain_unspents(
74-
&chain,
75-
chain.tip().block_id(),
76-
Default::default(),
77-
graph.index.outpoints().clone(),
78-
)
72+
.canonical_view(&chain, chain.tip().block_id(), Default::default())
73+
.filter_unspent_outpoints(graph.index.outpoints().clone())
7974
.collect();
8075
if !unspent.is_empty() {
8176
println!("\nUnspent");
@@ -85,16 +80,16 @@ fn main() -> anyhow::Result<()> {
8580
}
8681
}
8782

88-
for canon_tx in graph.graph().list_canonical_txs(
89-
&chain,
90-
chain.tip().block_id(),
91-
bdk_chain::CanonicalizationParams::default(),
92-
) {
93-
if !canon_tx.chain_position.is_confirmed() {
94-
eprintln!(
95-
"ERROR: canonical tx should be confirmed {}",
96-
canon_tx.tx_node.txid
97-
);
83+
for canon_tx in graph
84+
.canonical_view(
85+
&chain,
86+
chain.tip().block_id(),
87+
bdk_chain::CanonicalizationParams::default(),
88+
)
89+
.txs()
90+
{
91+
if !canon_tx.pos.is_confirmed() {
92+
eprintln!("ERROR: canonical tx should be confirmed {}", canon_tx.txid);
9893
}
9994
}
10095

crates/bitcoind_rpc/tests/test_emitter.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,9 @@ fn get_balance(
310310
) -> anyhow::Result<Balance> {
311311
let chain_tip = recv_chain.tip().block_id();
312312
let outpoints = recv_graph.index.outpoints().clone();
313-
let balance = recv_graph.graph().balance(
314-
recv_chain,
315-
chain_tip,
316-
CanonicalizationParams::default(),
317-
outpoints,
318-
|_, _| true,
319-
);
313+
let balance = recv_graph
314+
.canonical_view(recv_chain, chain_tip, CanonicalizationParams::default())
315+
.balance(outpoints, |_, _| true);
320316
Ok(balance)
321317
}
322318

@@ -621,7 +617,8 @@ fn test_expect_tx_evicted() -> anyhow::Result<()> {
621617

622618
// Retrieve the expected unconfirmed txids and spks from the graph.
623619
let exp_spk_txids = graph
624-
.list_expected_spk_txids(&chain, chain_tip, ..)
620+
.canonical_view(&chain, chain_tip, Default::default())
621+
.list_expected_spk_txids(&graph.index, ..)
625622
.collect::<Vec<_>>();
626623
assert_eq!(exp_spk_txids, vec![(spk, txid_1)]);
627624

@@ -636,9 +633,9 @@ fn test_expect_tx_evicted() -> anyhow::Result<()> {
636633
let _ = graph.batch_insert_relevant_evicted_at(mempool_event.evicted);
637634

638635
let canonical_txids = graph
639-
.graph()
640-
.list_canonical_txs(&chain, chain_tip, CanonicalizationParams::default())
641-
.map(|tx| tx.tx_node.compute_txid())
636+
.canonical_view(&chain, chain_tip, CanonicalizationParams::default())
637+
.txs()
638+
.map(|tx| tx.txid)
642639
.collect::<Vec<_>>();
643640
// tx1 should no longer be canonical.
644641
assert!(!canonical_txids.contains(&txid_1));

crates/chain/benches/canonicalization.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,31 +95,32 @@ fn setup<F: Fn(&mut KeychainTxGraph, &LocalChain)>(f: F) -> (KeychainTxGraph, Lo
9595
}
9696

9797
fn run_list_canonical_txs(tx_graph: &KeychainTxGraph, chain: &LocalChain, exp_txs: usize) {
98-
let txs = tx_graph.graph().list_canonical_txs(
98+
let view = tx_graph.canonical_view(
9999
chain,
100100
chain.tip().block_id(),
101101
CanonicalizationParams::default(),
102102
);
103+
let txs = view.txs();
103104
assert_eq!(txs.count(), exp_txs);
104105
}
105106

106107
fn run_filter_chain_txouts(tx_graph: &KeychainTxGraph, chain: &LocalChain, exp_txos: usize) {
107-
let utxos = tx_graph.graph().filter_chain_txouts(
108+
let view = tx_graph.canonical_view(
108109
chain,
109110
chain.tip().block_id(),
110111
CanonicalizationParams::default(),
111-
tx_graph.index.outpoints().clone(),
112112
);
113+
let utxos = view.filter_outpoints(tx_graph.index.outpoints().clone());
113114
assert_eq!(utxos.count(), exp_txos);
114115
}
115116

116117
fn run_filter_chain_unspents(tx_graph: &KeychainTxGraph, chain: &LocalChain, exp_utxos: usize) {
117-
let utxos = tx_graph.graph().filter_chain_unspents(
118+
let view = tx_graph.canonical_view(
118119
chain,
119120
chain.tip().block_id(),
120121
CanonicalizationParams::default(),
121-
tx_graph.index.outpoints().clone(),
122122
);
123+
let utxos = view.filter_unspent_outpoints(tx_graph.index.outpoints().clone());
123124
assert_eq!(utxos.count(), exp_utxos);
124125
}
125126

crates/chain/benches/indexer.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,9 @@ fn do_bench(indexed_tx_graph: &KeychainTxGraph, chain: &LocalChain) {
8484
// Check balance
8585
let chain_tip = chain.tip().block_id();
8686
let op = graph.index.outpoints().clone();
87-
let bal = graph.graph().balance(
88-
chain,
89-
chain_tip,
90-
CanonicalizationParams::default(),
91-
op,
92-
|_, _| false,
93-
);
87+
let bal = graph
88+
.canonical_view(chain, chain_tip, CanonicalizationParams::default())
89+
.balance(op, |_, _| false);
9490
assert_eq!(bal.total(), AMOUNT * TX_CT as u64);
9591
}
9692

0 commit comments

Comments
 (0)