Skip to content

Commit

Permalink
[bdk_chain_redesign] Modify signature of TxIndex
Browse files Browse the repository at this point in the history
This makes the API of `TxIndex` more consistent between scanning in data
and checking whether certain data is relevant.
  • Loading branch information
evanlinjin committed Apr 5, 2023
1 parent da4cef0 commit ddd5e95
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion crates/chain/src/indexed_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl<A: BlockAnchor, I: TxIndex> IndexedTxGraph<A, I> {
{
self.graph
.all_txouts()
.filter(|(_, txo)| self.index.is_spk_owned(&txo.script_pubkey))
.filter(|&(op, txo)| self.index.is_txout_relevant(op, txo))
.filter_map(move |(op, txout)| -> Option<Result<_, C::Error>> {
let graph_tx = self.graph.get_tx(op.txid)?;

Expand Down
8 changes: 4 additions & 4 deletions crates/chain/src/keychain/txout_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ impl<K: Clone + Ord + Debug + 'static> TxIndex for KeychainTxOutIndex<K> {
self.apply_additions(additions)
}

fn is_tx_relevant(&self, tx: &bitcoin::Transaction) -> bool {
self.is_relevant(tx)
fn is_txout_relevant(&self, _outpoint: OutPoint, txout: &TxOut) -> bool {
self.index_of_spk(&txout.script_pubkey).is_some()
}

fn is_spk_owned(&self, spk: &Script) -> bool {
self.index_of_spk(spk).is_some()
fn is_tx_relevant(&self, tx: &bitcoin::Transaction) -> bool {
self.is_relevant(tx)
}
}

Expand Down
8 changes: 4 additions & 4 deletions crates/chain/src/spk_txout_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ impl<I: Clone + Ord + 'static> TxIndex for SpkTxOutIndex<I> {
// This applies nothing.
}

fn is_tx_relevant(&self, tx: &Transaction) -> bool {
self.is_relevant(tx)
fn is_txout_relevant(&self, _outpoint: OutPoint, txout: &TxOut) -> bool {
self.index_of_spk(&txout.script_pubkey).is_some()
}

fn is_spk_owned(&self, spk: &Script) -> bool {
self.index_of_spk(spk).is_some()
fn is_tx_relevant(&self, tx: &Transaction) -> bool {
self.is_relevant(tx)
}
}

Expand Down
11 changes: 5 additions & 6 deletions crates/chain/src/tx_data_traits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bitcoin::{Block, BlockHash, OutPoint, Script, Transaction, TxOut};
use bitcoin::{Block, BlockHash, OutPoint, Transaction, TxOut};

use crate::BlockId;

Expand Down Expand Up @@ -80,10 +80,9 @@ pub trait TxIndex {
/// Apply additions to itself.
fn apply_additions(&mut self, additions: Self::Additions);

/// A transaction is relevant if it contains a txout with a script_pubkey that we own, or if it
/// spends an already-indexed outpoint that we have previously indexed.
fn is_tx_relevant(&self, tx: &Transaction) -> bool;
/// Returns whether the txout is marked as relevant in the index.
fn is_txout_relevant(&self, outpoint: OutPoint, txout: &TxOut) -> bool;

/// Returns whether the script pubkey is owned by us.
fn is_spk_owned(&self, spk: &Script) -> bool;
/// Returns whether the transaction is marked as relevant in the index.
fn is_tx_relevant(&self, tx: &Transaction) -> bool;
}
2 changes: 1 addition & 1 deletion crates/chain/src/tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
//! # use bitcoin::Transaction;
//! # let tx_a = tx_from_hex(RAW_TX_1);
//! # let tx_b = tx_from_hex(RAW_TX_2);
//! let mut graph = TxGraph::<BlockId>::default();
//! let mut graph: TxGraph = TxGraph::default();
//! let update = TxGraph::new(vec![tx_a, tx_b]);
//!
//! // preview additions as the result of the update
Expand Down

0 comments on commit ddd5e95

Please sign in to comment.