From d0a2aa83befce5dda26f8b3ae05449f6967df25a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=97=E5=AE=87?= Date: Mon, 27 Mar 2023 15:36:37 +0800 Subject: [PATCH] [bdk_chain_redesign] Add `apply_additions` to `IndexedTxGraph` * Get mutable index from `IndexedChainGraph`. * Also add `apply_additions` method to `TxIndex` trait. --- crates/chain/src/indexed_tx_graph.rs | 15 +++++++++++++++ crates/chain/src/keychain/txout_index.rs | 4 ++++ crates/chain/src/spk_txout_index.rs | 4 ++++ crates/chain/src/tx_data_traits.rs | 3 +++ 4 files changed, 26 insertions(+) diff --git a/crates/chain/src/indexed_tx_graph.rs b/crates/chain/src/indexed_tx_graph.rs index b0d547c7..5071fb2c 100644 --- a/crates/chain/src/indexed_tx_graph.rs +++ b/crates/chain/src/indexed_tx_graph.rs @@ -71,6 +71,21 @@ impl IndexedTxGraph { &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) { + 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, diff --git a/crates/chain/src/keychain/txout_index.rs b/crates/chain/src/keychain/txout_index.rs index 176254b4..d19aada7 100644 --- a/crates/chain/src/keychain/txout_index.rs +++ b/crates/chain/src/keychain/txout_index.rs @@ -101,6 +101,10 @@ impl TxIndex for KeychainTxOutIndex { 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) } diff --git a/crates/chain/src/spk_txout_index.rs b/crates/chain/src/spk_txout_index.rs index 3d2f783e..3d1af948 100644 --- a/crates/chain/src/spk_txout_index.rs +++ b/crates/chain/src/spk_txout_index.rs @@ -68,6 +68,10 @@ impl TxIndex for SpkTxOutIndex { 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) } diff --git a/crates/chain/src/tx_data_traits.rs b/crates/chain/src/tx_data_traits.rs index f412f452..2ffb9a60 100644 --- a/crates/chain/src/tx_data_traits.rs +++ b/crates/chain/src/tx_data_traits.rs @@ -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;