Skip to content

Commit c6047a0

Browse files
committed
refactor(chain)!: remove IndexedTxGraph 🗑
in favour of adding a type parameter to TxGraph. When the second type parameter `X: Indexer` is set then TxGraph behaves like `IndexedTxGraph` used to. I reworked the internals of `TxGraph` as I thought things were a bit convoluted. - feat: allow changing the indexer on TxGraph Co-authored: LLFourn
1 parent 88330f6 commit c6047a0

File tree

17 files changed

+609
-735
lines changed

17 files changed

+609
-735
lines changed

crates/bitcoind_rpc/examples/filter_iter.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use bdk_chain::bitcoin::{constants::genesis_block, secp256k1::Secp256k1, Network
77
use bdk_chain::indexer::keychain_txout::KeychainTxOutIndex;
88
use bdk_chain::local_chain::LocalChain;
99
use bdk_chain::miniscript::Descriptor;
10-
use bdk_chain::{BlockId, ConfirmationBlockTime, IndexedTxGraph, SpkIterator};
10+
use bdk_chain::{BlockId, ConfirmationBlockTime, SpkIterator, TxGraph};
1111
use bdk_testenv::anyhow;
1212
use bitcoin::Address;
1313

@@ -31,7 +31,7 @@ fn main() -> anyhow::Result<()> {
3131
let (descriptor, _) = Descriptor::parse_descriptor(&secp, EXTERNAL)?;
3232
let (change_descriptor, _) = Descriptor::parse_descriptor(&secp, INTERNAL)?;
3333
let (mut chain, _) = LocalChain::from_genesis_hash(genesis_block(NETWORK).block_hash());
34-
let mut graph = IndexedTxGraph::<ConfirmationBlockTime, KeychainTxOutIndex<&str>>::new({
34+
let mut graph = TxGraph::<ConfirmationBlockTime, KeychainTxOutIndex<&str>>::new({
3535
let mut index = KeychainTxOutIndex::default();
3636
index.insert_descriptor("external", descriptor.clone())?;
3737
index.insert_descriptor("internal", change_descriptor.clone())?;
@@ -88,7 +88,6 @@ fn main() -> anyhow::Result<()> {
8888
println!("\ntook: {}s", start.elapsed().as_secs());
8989
println!("Local tip: {}", chain.tip().height());
9090
let unspent: Vec<_> = graph
91-
.graph()
9291
.filter_chain_unspents(
9392
&chain,
9493
chain.tip().block_id(),

crates/bitcoind_rpc/tests/test_emitter.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bdk_chain::{
55
bitcoin::{Address, Amount, Txid},
66
local_chain::{CheckPoint, LocalChain},
77
spk_txout::SpkTxOutIndex,
8-
Balance, BlockId, IndexedTxGraph, Merge,
8+
Balance, BlockId, Merge, TxGraph,
99
};
1010
use bdk_testenv::{anyhow, TestEnv};
1111
use bitcoin::{hashes::Hash, Block, OutPoint, ScriptBuf, WScriptHash};
@@ -148,7 +148,7 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
148148
env.mine_blocks(101, None)?;
149149

150150
let (mut chain, _) = LocalChain::from_genesis_hash(env.rpc_client().get_block_hash(0)?);
151-
let mut indexed_tx_graph = IndexedTxGraph::<BlockId, _>::new({
151+
let mut tx_graph = TxGraph::<BlockId, _>::new({
152152
let mut index = SpkTxOutIndex::<usize>::default();
153153
index.insert_spk(0, addr_0.script_pubkey());
154154
index.insert_spk(1, addr_1.script_pubkey());
@@ -161,8 +161,8 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
161161
while let Some(emission) = emitter.next_block()? {
162162
let height = emission.block_height();
163163
let _ = chain.apply_update(emission.checkpoint)?;
164-
let indexed_additions = indexed_tx_graph.apply_block_relevant(&emission.block, height);
165-
assert!(indexed_additions.is_empty());
164+
let tx_graph_changeset = tx_graph.apply_block_relevant(&emission.block, height);
165+
assert!(tx_graph_changeset.is_empty());
166166
}
167167

168168
// send 3 txs to a tracked address, these txs will be in the mempool
@@ -189,18 +189,17 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
189189
assert!(emitter.next_block()?.is_none());
190190

191191
let mempool_txs = emitter.mempool()?;
192-
let indexed_additions = indexed_tx_graph.batch_insert_unconfirmed(mempool_txs);
192+
let tx_graph_changeset = tx_graph.batch_insert_unconfirmed(mempool_txs);
193193
assert_eq!(
194-
indexed_additions
195-
.tx_graph
194+
tx_graph_changeset
196195
.txs
197196
.iter()
198197
.map(|tx| tx.compute_txid())
199198
.collect::<BTreeSet<Txid>>(),
200199
exp_txids,
201200
"changeset should have the 3 mempool transactions",
202201
);
203-
assert!(indexed_additions.tx_graph.anchors.is_empty());
202+
assert!(tx_graph_changeset.anchors.is_empty());
204203
}
205204

206205
// mine a block that confirms the 3 txs
@@ -222,10 +221,10 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
222221
let emission = emitter.next_block()?.expect("must get mined block");
223222
let height = emission.block_height();
224223
let _ = chain.apply_update(emission.checkpoint)?;
225-
let indexed_additions = indexed_tx_graph.apply_block_relevant(&emission.block, height);
226-
assert!(indexed_additions.tx_graph.txs.is_empty());
227-
assert!(indexed_additions.tx_graph.txouts.is_empty());
228-
assert_eq!(indexed_additions.tx_graph.anchors, exp_anchors);
224+
let tx_graph_changeset = tx_graph.apply_block_relevant(&emission.block, height);
225+
assert!(tx_graph_changeset.txs.is_empty());
226+
assert!(tx_graph_changeset.txouts.is_empty());
227+
assert_eq!(tx_graph_changeset.anchors, exp_anchors);
229228
}
230229

231230
Ok(())
@@ -276,7 +275,7 @@ fn ensure_block_emitted_after_reorg_is_at_reorg_height() -> anyhow::Result<()> {
276275

277276
fn process_block(
278277
recv_chain: &mut LocalChain,
279-
recv_graph: &mut IndexedTxGraph<BlockId, SpkTxOutIndex<()>>,
278+
recv_graph: &mut TxGraph<BlockId, SpkTxOutIndex<()>>,
280279
block: Block,
281280
block_height: u32,
282281
) -> anyhow::Result<()> {
@@ -287,7 +286,7 @@ fn process_block(
287286

288287
fn sync_from_emitter<C>(
289288
recv_chain: &mut LocalChain,
290-
recv_graph: &mut IndexedTxGraph<BlockId, SpkTxOutIndex<()>>,
289+
recv_graph: &mut TxGraph<BlockId, SpkTxOutIndex<()>>,
291290
emitter: &mut Emitter<C>,
292291
) -> anyhow::Result<()>
293292
where
@@ -302,13 +301,11 @@ where
302301

303302
fn get_balance(
304303
recv_chain: &LocalChain,
305-
recv_graph: &IndexedTxGraph<BlockId, SpkTxOutIndex<()>>,
304+
recv_graph: &TxGraph<BlockId, SpkTxOutIndex<()>>,
306305
) -> anyhow::Result<Balance> {
307306
let chain_tip = recv_chain.tip().block_id();
308307
let outpoints = recv_graph.index.outpoints().clone();
309-
let balance = recv_graph
310-
.graph()
311-
.balance(recv_chain, chain_tip, outpoints, |_, _| true);
308+
let balance = recv_graph.balance(recv_chain, chain_tip, outpoints, |_, _| true);
312309
Ok(balance)
313310
}
314311

@@ -340,7 +337,7 @@ fn tx_can_become_unconfirmed_after_reorg() -> anyhow::Result<()> {
340337

341338
// setup receiver
342339
let (mut recv_chain, _) = LocalChain::from_genesis_hash(env.rpc_client().get_block_hash(0)?);
343-
let mut recv_graph = IndexedTxGraph::<BlockId, _>::new({
340+
let mut recv_graph = TxGraph::<BlockId, _>::new({
344341
let mut recv_index = SpkTxOutIndex::default();
345342
recv_index.insert_spk((), spk_to_track.clone());
346343
recv_index

crates/chain/benches/canonicalization.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bdk_chain::{keychain_txout::KeychainTxOutIndex, local_chain::LocalChain, IndexedTxGraph};
1+
use bdk_chain::{keychain_txout::KeychainTxOutIndex, local_chain::LocalChain, TxGraph};
22
use bdk_core::{BlockId, CheckPoint};
33
use bdk_core::{ConfirmationBlockTime, TxUpdate};
44
use bdk_testenv::hash;
@@ -11,7 +11,7 @@ use miniscript::{Descriptor, DescriptorPublicKey};
1111
use std::sync::Arc;
1212

1313
type Keychain = ();
14-
type KeychainTxGraph = IndexedTxGraph<ConfirmationBlockTime, KeychainTxOutIndex<Keychain>>;
14+
type KeychainTxGraph = TxGraph<ConfirmationBlockTime, KeychainTxOutIndex<Keychain>>;
1515

1616
/// New tx guaranteed to have at least one output
1717
fn new_tx(lt: u32) -> Transaction {
@@ -90,14 +90,12 @@ fn setup<F: Fn(&mut KeychainTxGraph, &LocalChain)>(f: F) -> (KeychainTxGraph, Lo
9090
}
9191

9292
fn run_list_canonical_txs(tx_graph: &KeychainTxGraph, chain: &LocalChain, exp_txs: usize) {
93-
let txs = tx_graph
94-
.graph()
95-
.list_canonical_txs(chain, chain.tip().block_id());
93+
let txs = tx_graph.list_canonical_txs(chain, chain.tip().block_id());
9694
assert_eq!(txs.count(), exp_txs);
9795
}
9896

9997
fn run_filter_chain_txouts(tx_graph: &KeychainTxGraph, chain: &LocalChain, exp_txos: usize) {
100-
let utxos = tx_graph.graph().filter_chain_txouts(
98+
let utxos = tx_graph.filter_chain_txouts(
10199
chain,
102100
chain.tip().block_id(),
103101
tx_graph.index.outpoints().clone(),
@@ -106,7 +104,7 @@ fn run_filter_chain_txouts(tx_graph: &KeychainTxGraph, chain: &LocalChain, exp_t
106104
}
107105

108106
fn run_filter_chain_unspents(tx_graph: &KeychainTxGraph, chain: &LocalChain, exp_utxos: usize) {
109-
let utxos = tx_graph.graph().filter_chain_unspents(
107+
let utxos = tx_graph.filter_chain_unspents(
110108
chain,
111109
chain.tip().block_id(),
112110
tx_graph.index.outpoints().clone(),

crates/chain/src/canonical_iter.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use bdk_core::BlockId;
88
use bitcoin::{Transaction, Txid};
99

1010
/// Iterates over canonical txs.
11-
pub struct CanonicalIter<'g, A, C> {
12-
tx_graph: &'g TxGraph<A>,
11+
pub struct CanonicalIter<'g, A, X, C> {
12+
tx_graph: &'g TxGraph<A, X>,
1313
chain: &'g C,
1414
chain_tip: BlockId,
1515

@@ -24,9 +24,9 @@ pub struct CanonicalIter<'g, A, C> {
2424
queue: VecDeque<Txid>,
2525
}
2626

27-
impl<'g, A: Anchor, C: ChainOracle> CanonicalIter<'g, A, C> {
27+
impl<'g, A: Anchor, X, C: ChainOracle> CanonicalIter<'g, A, X, C> {
2828
/// Constructs [`CanonicalIter`].
29-
pub fn new(tx_graph: &'g TxGraph<A>, chain: &'g C, chain_tip: BlockId) -> Self {
29+
pub fn new(tx_graph: &'g TxGraph<A, X>, chain: &'g C, chain_tip: BlockId) -> Self {
3030
let anchors = tx_graph.all_anchors();
3131
let pending_anchored = Box::new(
3232
tx_graph
@@ -133,7 +133,7 @@ impl<'g, A: Anchor, C: ChainOracle> CanonicalIter<'g, A, C> {
133133
}
134134
}
135135

136-
impl<A: Anchor, C: ChainOracle> Iterator for CanonicalIter<'_, A, C> {
136+
impl<A: Anchor, X, C: ChainOracle> Iterator for CanonicalIter<'_, A, X, C> {
137137
type Item = Result<(Txid, Arc<Transaction>, CanonicalReason<A>), C::Error>;
138138

139139
fn next(&mut self) -> Option<Self::Item> {

0 commit comments

Comments
 (0)