[bdk_chain_redesign] Introduce ChainOracle and TxIndex traits

The chain oracle keeps track of the best chain, while the transaction
index indexes transaction data in relation to script pubkeys.

This commit also includes initial work on `IndexedTxGraph`.
This commit is contained in:
志宇
2023-03-24 15:47:39 +08:00
parent 5ae5fe30eb
commit 61a8606fbc
7 changed files with 315 additions and 5 deletions

View File

@@ -2,7 +2,7 @@ use core::ops::RangeBounds;
use crate::{
collections::{hash_map::Entry, BTreeMap, BTreeSet, HashMap},
ForEachTxOut,
ForEachTxOut, TxIndex,
};
use bitcoin::{self, OutPoint, Script, Transaction, TxOut, Txid};
@@ -52,6 +52,25 @@ impl<I> Default for SpkTxOutIndex<I> {
}
}
impl<I: Clone + Ord> TxIndex for SpkTxOutIndex<I> {
type Additions = BTreeSet<I>;
fn index_txout(&mut self, outpoint: OutPoint, txout: &TxOut) -> Self::Additions {
self.scan_txout(outpoint, txout)
.cloned()
.into_iter()
.collect()
}
fn index_tx(&mut self, tx: &Transaction) -> Self::Additions {
self.scan(tx)
}
fn is_tx_relevant(&self, tx: &Transaction) -> bool {
self.is_relevant(tx)
}
}
/// This macro is used instead of a member function of `SpkTxOutIndex`, which would result in a
/// compiler error[E0521]: "borrowed data escapes out of closure" when we attempt to take a
/// reference out of the `ForEachTxOut` closure during scanning.