]> Untitled Git - bdk/commitdiff
[bdk_chain_redesign] Modify signature of `TxIndex`
author志宇 <hello@evanlinjin.me>
Wed, 5 Apr 2023 10:17:08 +0000 (18:17 +0800)
committer志宇 <hello@evanlinjin.me>
Wed, 5 Apr 2023 10:17:08 +0000 (18:17 +0800)
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
crates/chain/src/keychain/txout_index.rs
crates/chain/src/spk_txout_index.rs
crates/chain/src/tx_data_traits.rs
crates/chain/src/tx_graph.rs

index 450d02b82752878fc012b9883ac84fa1330ccc53..28b95adfceacfbb9c9fd3bd16d5fc7ba5a75715e 100644 (file)
@@ -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)?;
 
index fc4c4e62f2a78210fd06a9f64b65dee950560e8c..7dd570a6360171f80750ba55ccb052768b47de57 100644 (file)
@@ -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)
     }
 }
 
index 6c9739be89a26f0335ea49b8d604a07bd07edda7..20be073ae5ffc456667e61b0823e281da835092d 100644 (file)
@@ -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)
     }
 }
 
index 716b45f18010fa81f15eb85e3ca62100a927a056..d8cadd13a14522ba616f0dccbaf184c893cb32a1 100644 (file)
@@ -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;
 }
index 620a2dc3d0ed9f9e1b5672c28d41b05864596a96..c502d038d9ceec015e027b3e85cc16ded511a6ed 100644 (file)
@@ -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