Skip to content

Commit 7307bb1

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.
1 parent d99b3ef commit 7307bb1

File tree

16 files changed

+820
-1014
lines changed

16 files changed

+820
-1014
lines changed

crates/bitcoind_rpc/tests/test_emitter.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use bdk_bitcoind_rpc::Emitter;
44
use bdk_chain::{
55
bitcoin::{Address, Amount, Txid},
66
local_chain::{CheckPoint, LocalChain},
7-
Balance, BlockId, IndexedTxGraph, Merge, SpkTxOutIndex,
7+
tx_graph::TxGraph,
8+
Balance, BlockId, Merge, SpkTxOutIndex,
89
};
910
use bdk_testenv::{anyhow, TestEnv};
1011
use bitcoin::{hashes::Hash, Block, OutPoint, ScriptBuf, WScriptHash};
@@ -149,7 +150,7 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
149150
println!("mined blocks!");
150151

151152
let (mut chain, _) = LocalChain::from_genesis_hash(env.rpc_client().get_block_hash(0)?);
152-
let mut indexed_tx_graph = IndexedTxGraph::<BlockId, _>::new({
153+
let mut tx_graph = TxGraph::<BlockId, _>::new({
153154
let mut index = SpkTxOutIndex::<usize>::default();
154155
index.insert_spk(0, addr_0.script_pubkey());
155156
index.insert_spk(1, addr_1.script_pubkey());
@@ -162,8 +163,8 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
162163
while let Some(emission) = emitter.next_block()? {
163164
let height = emission.block_height();
164165
let _ = chain.apply_update(emission.checkpoint)?;
165-
let indexed_additions = indexed_tx_graph.apply_block_relevant(&emission.block, height);
166-
assert!(indexed_additions.is_empty());
166+
let tx_graph_changeset = tx_graph.apply_block_relevant(&emission.block, height);
167+
assert!(tx_graph_changeset.is_empty());
167168
}
168169

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

192193
let mempool_txs = emitter.mempool()?;
193-
let indexed_additions = indexed_tx_graph.batch_insert_unconfirmed(mempool_txs);
194+
let tx_graph_changeset = tx_graph.batch_insert_unconfirmed(mempool_txs);
194195
assert_eq!(
195-
indexed_additions
196-
.graph
196+
tx_graph_changeset
197197
.txs
198198
.iter()
199199
.map(|tx| tx.compute_txid())
200200
.collect::<BTreeSet<Txid>>(),
201201
exp_txids,
202202
"changeset should have the 3 mempool transactions",
203203
);
204-
assert!(indexed_additions.graph.anchors.is_empty());
204+
assert!(tx_graph_changeset.anchors.is_empty());
205205
}
206206

207207
// mine a block that confirms the 3 txs
@@ -223,10 +223,10 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
223223
let emission = emitter.next_block()?.expect("must get mined block");
224224
let height = emission.block_height();
225225
let _ = chain.apply_update(emission.checkpoint)?;
226-
let indexed_additions = indexed_tx_graph.apply_block_relevant(&emission.block, height);
227-
assert!(indexed_additions.graph.txs.is_empty());
228-
assert!(indexed_additions.graph.txouts.is_empty());
229-
assert_eq!(indexed_additions.graph.anchors, exp_anchors);
226+
let tx_graph_changeset = tx_graph.apply_block_relevant(&emission.block, height);
227+
assert!(tx_graph_changeset.txs.is_empty());
228+
assert!(tx_graph_changeset.txouts.is_empty());
229+
assert_eq!(tx_graph_changeset.anchors, exp_anchors);
230230
}
231231

232232
Ok(())
@@ -277,18 +277,18 @@ fn ensure_block_emitted_after_reorg_is_at_reorg_height() -> anyhow::Result<()> {
277277

278278
fn process_block(
279279
recv_chain: &mut LocalChain,
280-
recv_graph: &mut IndexedTxGraph<BlockId, SpkTxOutIndex<()>>,
280+
recv_graph: &mut TxGraph<BlockId, SpkTxOutIndex<()>>,
281281
block: Block,
282282
block_height: u32,
283283
) -> anyhow::Result<()> {
284284
recv_chain.apply_update(CheckPoint::from_header(&block.header, block_height))?;
285-
let _ = recv_graph.apply_block(block, block_height);
285+
let _ = recv_graph.apply_block(&block, block_height);
286286
Ok(())
287287
}
288288

289289
fn sync_from_emitter<C>(
290290
recv_chain: &mut LocalChain,
291-
recv_graph: &mut IndexedTxGraph<BlockId, SpkTxOutIndex<()>>,
291+
recv_graph: &mut TxGraph<BlockId, SpkTxOutIndex<()>>,
292292
emitter: &mut Emitter<C>,
293293
) -> anyhow::Result<()>
294294
where
@@ -303,13 +303,11 @@ where
303303

304304
fn get_balance(
305305
recv_chain: &LocalChain,
306-
recv_graph: &IndexedTxGraph<BlockId, SpkTxOutIndex<()>>,
306+
recv_graph: &TxGraph<BlockId, SpkTxOutIndex<()>>,
307307
) -> anyhow::Result<Balance> {
308308
let chain_tip = recv_chain.tip().block_id();
309-
let outpoints = recv_graph.index.outpoints().clone();
310-
let balance = recv_graph
311-
.graph()
312-
.balance(recv_chain, chain_tip, outpoints, |_, _| true);
309+
let outpoints = recv_graph.indexer.outpoints().clone();
310+
let balance = recv_graph.balance(recv_chain, chain_tip, outpoints, |_, _| true);
313311
Ok(balance)
314312
}
315313

@@ -341,7 +339,7 @@ fn tx_can_become_unconfirmed_after_reorg() -> anyhow::Result<()> {
341339

342340
// setup receiver
343341
let (mut recv_chain, _) = LocalChain::from_genesis_hash(env.rpc_client().get_block_hash(0)?);
344-
let mut recv_graph = IndexedTxGraph::<BlockId, _>::new({
342+
let mut recv_graph = TxGraph::<BlockId, _>::new({
345343
let mut recv_index = SpkTxOutIndex::default();
346344
recv_index.insert_spk((), spk_to_track.clone());
347345
recv_index

crates/chain/src/changeset.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
pub struct CombinedChangeSet<K, A> {
1616
/// Changes to the [`LocalChain`](crate::local_chain::LocalChain).
1717
pub chain: crate::local_chain::ChangeSet,
18-
/// Changes to [`IndexedTxGraph`](crate::indexed_tx_graph::IndexedTxGraph).
19-
pub indexed_tx_graph:
20-
crate::indexed_tx_graph::ChangeSet<A, crate::indexer::keychain_txout::ChangeSet<K>>,
18+
/// Changes to [`TxGraph`](crate::tx_graph::TxGraph).
19+
pub tx_graph: crate::tx_graph::ChangeSet<A, crate::indexer::keychain_txout::ChangeSet<K>>,
2120
/// Stores the network type of the transaction data.
2221
pub network: Option<bitcoin::Network>,
2322
}
@@ -26,8 +25,8 @@ pub struct CombinedChangeSet<K, A> {
2625
impl<K, A> core::default::Default for CombinedChangeSet<K, A> {
2726
fn default() -> Self {
2827
Self {
29-
chain: core::default::Default::default(),
30-
indexed_tx_graph: core::default::Default::default(),
28+
chain: Default::default(),
29+
tx_graph: Default::default(),
3130
network: None,
3231
}
3332
}
@@ -37,7 +36,7 @@ impl<K, A> core::default::Default for CombinedChangeSet<K, A> {
3736
impl<K: Ord, A: crate::Anchor> crate::Merge for CombinedChangeSet<K, A> {
3837
fn merge(&mut self, other: Self) {
3938
crate::Merge::merge(&mut self.chain, other.chain);
40-
crate::Merge::merge(&mut self.indexed_tx_graph, other.indexed_tx_graph);
39+
crate::Merge::merge(&mut self.tx_graph, other.tx_graph);
4140
if other.network.is_some() {
4241
debug_assert!(
4342
self.network.is_none() || self.network == other.network,
@@ -48,7 +47,7 @@ impl<K: Ord, A: crate::Anchor> crate::Merge for CombinedChangeSet<K, A> {
4847
}
4948

5049
fn is_empty(&self) -> bool {
51-
self.chain.is_empty() && self.indexed_tx_graph.is_empty() && self.network.is_none()
50+
self.chain.is_empty() && self.tx_graph.is_empty() && self.network.is_none()
5251
}
5352
}
5453

@@ -63,17 +62,14 @@ impl<K, A> From<crate::local_chain::ChangeSet> for CombinedChangeSet<K, A> {
6362
}
6463

6564
#[cfg(feature = "miniscript")]
66-
impl<K, A> From<crate::indexed_tx_graph::ChangeSet<A, crate::indexer::keychain_txout::ChangeSet<K>>>
65+
impl<K, A> From<crate::tx_graph::ChangeSet<A, crate::indexer::keychain_txout::ChangeSet<K>>>
6766
for CombinedChangeSet<K, A>
6867
{
6968
fn from(
70-
indexed_tx_graph: crate::indexed_tx_graph::ChangeSet<
71-
A,
72-
crate::indexer::keychain_txout::ChangeSet<K>,
73-
>,
69+
tx_graph: crate::tx_graph::ChangeSet<A, crate::indexer::keychain_txout::ChangeSet<K>>,
7470
) -> Self {
7571
Self {
76-
indexed_tx_graph,
72+
tx_graph,
7773
..Default::default()
7874
}
7975
}
@@ -83,7 +79,7 @@ impl<K, A> From<crate::indexed_tx_graph::ChangeSet<A, crate::indexer::keychain_t
8379
impl<K, A> From<crate::indexer::keychain_txout::ChangeSet<K>> for CombinedChangeSet<K, A> {
8480
fn from(indexer: crate::indexer::keychain_txout::ChangeSet<K>) -> Self {
8581
Self {
86-
indexed_tx_graph: crate::indexed_tx_graph::ChangeSet {
82+
tx_graph: crate::tx_graph::ChangeSet {
8783
indexer,
8884
..Default::default()
8985
},

0 commit comments

Comments
 (0)