]> Untitled Git - bdk/commitdiff
[bdk_chain_redesign] Add `apply_additions` to `IndexedTxGraph`
author志宇 <hello@evanlinjin.me>
Mon, 27 Mar 2023 07:36:37 +0000 (15:36 +0800)
committer志宇 <hello@evanlinjin.me>
Mon, 27 Mar 2023 08:02:21 +0000 (16:02 +0800)
* Get mutable index from `IndexedChainGraph`.
* Also add `apply_additions` method to `TxIndex` trait.

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

index b0d547c7834fd8df2e901e5021c62c8a6891b5a9..5071fb2c76afbec582b9fe66e985068522250858 100644 (file)
@@ -71,6 +71,21 @@ impl<A: BlockAnchor, I: TxIndex> IndexedTxGraph<A, I> {
         &self.index
     }
 
+    /// Get a mutable reference to the internal transaction index.
+    pub fn mut_index(&mut self) -> &mut I {
+        &mut self.index
+    }
+
+    /// Applies the [`IndexedAdditions`] to the [`IndexedTxGraph`].
+    pub fn apply_additions(&mut self, additions: IndexedAdditions<A, I::Additions>) {
+        let IndexedAdditions {
+            graph_additions,
+            index_delta,
+        } = additions;
+        self.graph.apply_additions(graph_additions);
+        self.index.apply_additions(index_delta);
+    }
+
     /// Insert a `txout` that exists in `outpoint` with the given `observation`.
     pub fn insert_txout(
         &mut self,
index 176254b4ac97f2a79f86e2a0fb2578b76a3e7fb7..d19aada7a1fe93b564514471a29e60b6f5f18a00 100644 (file)
@@ -101,6 +101,10 @@ impl<K: Clone + Ord + Debug + 'static> TxIndex for KeychainTxOutIndex<K> {
         self.scan(tx)
     }
 
+    fn apply_additions(&mut self, additions: Self::Additions) {
+        self.apply_additions(additions)
+    }
+
     fn is_tx_relevant(&self, tx: &bitcoin::Transaction) -> bool {
         self.is_relevant(tx)
     }
index 3d2f783e357785757b4539a07ab0da58549bd713..3d1af94854c07f5e7a0ea4e6af02a5ea730b8d18 100644 (file)
@@ -68,6 +68,10 @@ impl<I: Clone + Ord + 'static> TxIndex for SpkTxOutIndex<I> {
         self.scan(tx)
     }
 
+    fn apply_additions(&mut self, _additions: Self::Additions) {
+        // This applies nothing.
+    }
+
     fn is_tx_relevant(&self, tx: &Transaction) -> bool {
         self.is_relevant(tx)
     }
index f412f452938436c8b07b15503193dab0438947ca..2ffb9a608ecd920209b4d771e54d5c4fef74fb30 100644 (file)
@@ -119,6 +119,9 @@ pub trait TxIndex {
             .unwrap_or_default()
     }
 
+    /// 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;