[bdk_chain_redesign] Add apply_additions to IndexedTxGraph

* Get mutable index from `IndexedChainGraph`.
* Also add `apply_additions` method to `TxIndex` trait.
This commit is contained in:
志宇 2023-03-27 15:36:37 +08:00
parent 6cbb18d409
commit d0a2aa83be
No known key found for this signature in database
GPG Key ID: F6345C9837C2BDE8
4 changed files with 26 additions and 0 deletions

View File

@ -71,6 +71,21 @@ impl<A: BlockAnchor, I: TxIndex> IndexedTxGraph<A, I> {
&self.index &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`. /// Insert a `txout` that exists in `outpoint` with the given `observation`.
pub fn insert_txout( pub fn insert_txout(
&mut self, &mut self,

View File

@ -101,6 +101,10 @@ impl<K: Clone + Ord + Debug + 'static> TxIndex for KeychainTxOutIndex<K> {
self.scan(tx) self.scan(tx)
} }
fn apply_additions(&mut self, additions: Self::Additions) {
self.apply_additions(additions)
}
fn is_tx_relevant(&self, tx: &bitcoin::Transaction) -> bool { fn is_tx_relevant(&self, tx: &bitcoin::Transaction) -> bool {
self.is_relevant(tx) self.is_relevant(tx)
} }

View File

@ -68,6 +68,10 @@ impl<I: Clone + Ord + 'static> TxIndex for SpkTxOutIndex<I> {
self.scan(tx) self.scan(tx)
} }
fn apply_additions(&mut self, _additions: Self::Additions) {
// This applies nothing.
}
fn is_tx_relevant(&self, tx: &Transaction) -> bool { fn is_tx_relevant(&self, tx: &Transaction) -> bool {
self.is_relevant(tx) self.is_relevant(tx)
} }

View File

@ -119,6 +119,9 @@ pub trait TxIndex {
.unwrap_or_default() .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 /// 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. /// spends an already-indexed outpoint that we have previously indexed.
fn is_tx_relevant(&self, tx: &Transaction) -> bool; fn is_tx_relevant(&self, tx: &Transaction) -> bool;