Skip to content

Commit 00efcde

Browse files
committed
Merge #2029: CanonicalView
1311a2e refactor(chain)!: Change trust_predicate to accept FullTxOut (志宇) 3f9eec5 refactor(example): Reuse `CanonicalView` in filter iter example (志宇) 40790d0 docs(chain): Tighten `CanonicalView` documentation (志宇) f444a8d refactor(chain)!: Rename `CanonicalViewTx` to `CanonicalTx` (志宇) 8ad138d docs(chain): Improve CanonicalView documentation (志宇) beb16a1 feat(chain)!: Remove `CanonicalTx` (志宇) 54409bc test(chain): Add comprehensive tests for min_confirmations parameter (志宇) 4524945 feat(chain): Add min_confirmations parameter to CanonicalView::balance (志宇) 0a55710 feat(chain)!: Introduce `CanonicalView` and migrate API (志宇) Pull request description: ### Description `CanonicalView` allows us to canonicalize upfront, reducing our API surface and improving performance by reducing canonicalizations we need to do. This is also the first step to achieving many of our goals. * Event notifications. * Intent tracker. * Getting rid of `CanonicalUnspents` structure in `bdk_tx`. ### Changelog notice ```md Added - Introduce `CanonicalView` which allows us to canonicalize once upfront. - Added `TxGraph::canonical_view` which constructs a `CanonicalView`. Changed - `TxGraph` methods which require canonicalization now have `CanonicalView` equivalents. Removed - `TxGraph` methods which take in a fallible `ChainOracle` implementations are now removed. ``` ### Checklists #### All Submissions: * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) #### New Features: * [x] I've added tests for the new feature * [x] I've added docs for the new feature ACKs for top commit: evanlinjin: self-ACK 1311a2e ValuedMammal: ACK 1311a2e oleonardolima: ACK 1311a2e Tree-SHA512: 520879ac5aba37ae53d6fa0465ee5920d65108208f82e9092fc6d391f9ce5741198c474fe49462e3bf6ed9f65f94f6019be0c0e02a0b19bcdb8398eb1c7ea3e0
2 parents 4031e19 + 1311a2e commit 00efcde

File tree

19 files changed

+950
-703
lines changed

19 files changed

+950
-703
lines changed

crates/bitcoind_rpc/examples/filter_iter.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,11 @@ fn main() -> anyhow::Result<()> {
6868

6969
println!("\ntook: {}s", start.elapsed().as_secs());
7070
println!("Local tip: {}", chain.tip().height());
71-
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-
)
71+
72+
let canonical_view = graph.canonical_view(&chain, chain.tip().block_id(), Default::default());
73+
74+
let unspent: Vec<_> = canonical_view
75+
.filter_unspent_outpoints(graph.index.outpoints().clone())
7976
.collect();
8077
if !unspent.is_empty() {
8178
println!("\nUnspent");
@@ -85,16 +82,9 @@ fn main() -> anyhow::Result<()> {
8582
}
8683
}
8784

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-
);
85+
for canon_tx in canonical_view.txs() {
86+
if !canon_tx.pos.is_confirmed() {
87+
eprintln!("ERROR: canonical tx should be confirmed {}", canon_tx.txid);
9888
}
9989
}
10090

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, 1);
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, 1);
9490
assert_eq!(bal.total(), AMOUNT * TX_CT as u64);
9591
}
9692

0 commit comments

Comments
 (0)