Skip to content

Commit

Permalink
refactor(chain)! remove IndexedTxGraph 🗑
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LLFourn committed Jul 11, 2024
1 parent d99b3ef commit 7307bb1
Show file tree
Hide file tree
Showing 16 changed files with 820 additions and 1,014 deletions.
40 changes: 19 additions & 21 deletions crates/bitcoind_rpc/tests/test_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use bdk_bitcoind_rpc::Emitter;
use bdk_chain::{
bitcoin::{Address, Amount, Txid},
local_chain::{CheckPoint, LocalChain},
Balance, BlockId, IndexedTxGraph, Merge, SpkTxOutIndex,
tx_graph::TxGraph,
Balance, BlockId, Merge, SpkTxOutIndex,
};
use bdk_testenv::{anyhow, TestEnv};
use bitcoin::{hashes::Hash, Block, OutPoint, ScriptBuf, WScriptHash};
Expand Down Expand Up @@ -149,7 +150,7 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
println!("mined blocks!");

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

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

let mempool_txs = emitter.mempool()?;
let indexed_additions = indexed_tx_graph.batch_insert_unconfirmed(mempool_txs);
let tx_graph_changeset = tx_graph.batch_insert_unconfirmed(mempool_txs);
assert_eq!(
indexed_additions
.graph
tx_graph_changeset
.txs
.iter()
.map(|tx| tx.compute_txid())
.collect::<BTreeSet<Txid>>(),
exp_txids,
"changeset should have the 3 mempool transactions",
);
assert!(indexed_additions.graph.anchors.is_empty());
assert!(tx_graph_changeset.anchors.is_empty());
}

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

Ok(())
Expand Down Expand Up @@ -277,18 +277,18 @@ fn ensure_block_emitted_after_reorg_is_at_reorg_height() -> anyhow::Result<()> {

fn process_block(
recv_chain: &mut LocalChain,
recv_graph: &mut IndexedTxGraph<BlockId, SpkTxOutIndex<()>>,
recv_graph: &mut TxGraph<BlockId, SpkTxOutIndex<()>>,
block: Block,
block_height: u32,
) -> anyhow::Result<()> {
recv_chain.apply_update(CheckPoint::from_header(&block.header, block_height))?;
let _ = recv_graph.apply_block(block, block_height);
let _ = recv_graph.apply_block(&block, block_height);
Ok(())
}

fn sync_from_emitter<C>(
recv_chain: &mut LocalChain,
recv_graph: &mut IndexedTxGraph<BlockId, SpkTxOutIndex<()>>,
recv_graph: &mut TxGraph<BlockId, SpkTxOutIndex<()>>,
emitter: &mut Emitter<C>,
) -> anyhow::Result<()>
where
Expand All @@ -303,13 +303,11 @@ where

fn get_balance(
recv_chain: &LocalChain,
recv_graph: &IndexedTxGraph<BlockId, SpkTxOutIndex<()>>,
recv_graph: &TxGraph<BlockId, SpkTxOutIndex<()>>,
) -> anyhow::Result<Balance> {
let chain_tip = recv_chain.tip().block_id();
let outpoints = recv_graph.index.outpoints().clone();
let balance = recv_graph
.graph()
.balance(recv_chain, chain_tip, outpoints, |_, _| true);
let outpoints = recv_graph.indexer.outpoints().clone();
let balance = recv_graph.balance(recv_chain, chain_tip, outpoints, |_, _| true);
Ok(balance)
}

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

// setup receiver
let (mut recv_chain, _) = LocalChain::from_genesis_hash(env.rpc_client().get_block_hash(0)?);
let mut recv_graph = IndexedTxGraph::<BlockId, _>::new({
let mut recv_graph = TxGraph::<BlockId, _>::new({
let mut recv_index = SpkTxOutIndex::default();
recv_index.insert_spk((), spk_to_track.clone());
recv_index
Expand Down
24 changes: 10 additions & 14 deletions crates/chain/src/changeset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
pub struct CombinedChangeSet<K, A> {
/// Changes to the [`LocalChain`](crate::local_chain::LocalChain).
pub chain: crate::local_chain::ChangeSet,
/// Changes to [`IndexedTxGraph`](crate::indexed_tx_graph::IndexedTxGraph).
pub indexed_tx_graph:
crate::indexed_tx_graph::ChangeSet<A, crate::indexer::keychain_txout::ChangeSet<K>>,
/// Changes to [`TxGraph`](crate::tx_graph::TxGraph).
pub tx_graph: crate::tx_graph::ChangeSet<A, crate::indexer::keychain_txout::ChangeSet<K>>,
/// Stores the network type of the transaction data.
pub network: Option<bitcoin::Network>,
}
Expand All @@ -26,8 +25,8 @@ pub struct CombinedChangeSet<K, A> {
impl<K, A> core::default::Default for CombinedChangeSet<K, A> {
fn default() -> Self {
Self {
chain: core::default::Default::default(),
indexed_tx_graph: core::default::Default::default(),
chain: Default::default(),
tx_graph: Default::default(),
network: None,
}
}
Expand All @@ -37,7 +36,7 @@ impl<K, A> core::default::Default for CombinedChangeSet<K, A> {
impl<K: Ord, A: crate::Anchor> crate::Merge for CombinedChangeSet<K, A> {
fn merge(&mut self, other: Self) {
crate::Merge::merge(&mut self.chain, other.chain);
crate::Merge::merge(&mut self.indexed_tx_graph, other.indexed_tx_graph);
crate::Merge::merge(&mut self.tx_graph, other.tx_graph);
if other.network.is_some() {
debug_assert!(
self.network.is_none() || self.network == other.network,
Expand All @@ -48,7 +47,7 @@ impl<K: Ord, A: crate::Anchor> crate::Merge for CombinedChangeSet<K, A> {
}

fn is_empty(&self) -> bool {
self.chain.is_empty() && self.indexed_tx_graph.is_empty() && self.network.is_none()
self.chain.is_empty() && self.tx_graph.is_empty() && self.network.is_none()
}
}

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

#[cfg(feature = "miniscript")]
impl<K, A> From<crate::indexed_tx_graph::ChangeSet<A, crate::indexer::keychain_txout::ChangeSet<K>>>
impl<K, A> From<crate::tx_graph::ChangeSet<A, crate::indexer::keychain_txout::ChangeSet<K>>>
for CombinedChangeSet<K, A>
{
fn from(
indexed_tx_graph: crate::indexed_tx_graph::ChangeSet<
A,
crate::indexer::keychain_txout::ChangeSet<K>,
>,
tx_graph: crate::tx_graph::ChangeSet<A, crate::indexer::keychain_txout::ChangeSet<K>>,
) -> Self {
Self {
indexed_tx_graph,
tx_graph,
..Default::default()
}
}
Expand All @@ -83,7 +79,7 @@ impl<K, A> From<crate::indexed_tx_graph::ChangeSet<A, crate::indexer::keychain_t
impl<K, A> From<crate::indexer::keychain_txout::ChangeSet<K>> for CombinedChangeSet<K, A> {
fn from(indexer: crate::indexer::keychain_txout::ChangeSet<K>) -> Self {
Self {
indexed_tx_graph: crate::indexed_tx_graph::ChangeSet {
tx_graph: crate::tx_graph::ChangeSet {
indexer,
..Default::default()
},
Expand Down
Loading

0 comments on commit 7307bb1

Please sign in to comment.