From ddd5e951f5ec77070034c7390a635d8d5bd7cb85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=97=E5=AE=87?= Date: Wed, 5 Apr 2023 18:17:08 +0800 Subject: [PATCH] [bdk_chain_redesign] Modify signature of `TxIndex` This makes the API of `TxIndex` more consistent between scanning in data and checking whether certain data is relevant. --- crates/chain/src/indexed_tx_graph.rs | 2 +- crates/chain/src/keychain/txout_index.rs | 8 ++++---- crates/chain/src/spk_txout_index.rs | 8 ++++---- crates/chain/src/tx_data_traits.rs | 11 +++++------ crates/chain/src/tx_graph.rs | 2 +- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/crates/chain/src/indexed_tx_graph.rs b/crates/chain/src/indexed_tx_graph.rs index 450d02b82..28b95adfc 100644 --- a/crates/chain/src/indexed_tx_graph.rs +++ b/crates/chain/src/indexed_tx_graph.rs @@ -261,7 +261,7 @@ impl IndexedTxGraph { { 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> { let graph_tx = self.graph.get_tx(op.txid)?; diff --git a/crates/chain/src/keychain/txout_index.rs b/crates/chain/src/keychain/txout_index.rs index fc4c4e62f..7dd570a63 100644 --- a/crates/chain/src/keychain/txout_index.rs +++ b/crates/chain/src/keychain/txout_index.rs @@ -105,12 +105,12 @@ impl TxIndex for KeychainTxOutIndex { 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) } } diff --git a/crates/chain/src/spk_txout_index.rs b/crates/chain/src/spk_txout_index.rs index 6c9739be8..20be073ae 100644 --- a/crates/chain/src/spk_txout_index.rs +++ b/crates/chain/src/spk_txout_index.rs @@ -69,12 +69,12 @@ impl TxIndex for SpkTxOutIndex { // 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) } } diff --git a/crates/chain/src/tx_data_traits.rs b/crates/chain/src/tx_data_traits.rs index 716b45f18..d8cadd13a 100644 --- a/crates/chain/src/tx_data_traits.rs +++ b/crates/chain/src/tx_data_traits.rs @@ -1,4 +1,4 @@ -use bitcoin::{Block, BlockHash, OutPoint, Script, Transaction, TxOut}; +use bitcoin::{Block, BlockHash, OutPoint, Transaction, TxOut}; use crate::BlockId; @@ -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; } diff --git a/crates/chain/src/tx_graph.rs b/crates/chain/src/tx_graph.rs index 620a2dc3d..c502d038d 100644 --- a/crates/chain/src/tx_graph.rs +++ b/crates/chain/src/tx_graph.rs @@ -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::::default(); +//! let mut graph: TxGraph = TxGraph::default(); //! let update = TxGraph::new(vec![tx_a, tx_b]); //! //! // preview additions as the result of the update