[bdk_chain_redesign] Change insert_relevant_txs method

Instead of forcing all transactions inserted to use the same anchors, we
change the API to have unique anchors per transaction.

This allows for more flexibility in general. For example, use `Anchor`
implementations that contain the position in a block of a transaction.
This commit is contained in:
志宇 2023-04-23 00:12:41 +08:00
parent ecc74ce4cd
commit 1b152647c5
No known key found for this signature in database
GPG Key ID: F6345C9837C2BDE8
2 changed files with 7 additions and 8 deletions

View File

@ -117,14 +117,13 @@ where
/// Insert relevant transactions from the given `txs` iterator. /// Insert relevant transactions from the given `txs` iterator.
/// ///
/// Relevancy is determined by the [`Indexer::is_tx_relevant`] implementation of `I`. Irrelevant /// Relevancy is determined by the [`Indexer::is_tx_relevant`] implementation of `I`. Irrelevant
/// transactions in `txs` will be ignored. Also, `txs` does not need to be in topological order. /// transactions in `txs` will be ignored. `txs` do not need to be in topological order.
/// ///
/// `anchors` can be provided to anchor the transactions to blocks. `seen_at` is a unix /// `anchors` can be provided to anchor the transactions to blocks. `seen_at` is a unix
/// timestamp of when the transactions are last seen. /// timestamp of when the transactions are last seen.
pub fn insert_relevant_txs<'t>( pub fn insert_relevant_txs<'t>(
&mut self, &mut self,
txs: impl IntoIterator<Item = &'t Transaction>, txs: impl IntoIterator<Item = (&'t Transaction, impl IntoIterator<Item = A>)>,
anchors: impl IntoIterator<Item = A> + Clone,
seen_at: Option<u64>, seen_at: Option<u64>,
) -> IndexedAdditions<A, I::Additions> { ) -> IndexedAdditions<A, I::Additions> {
// The algorithm below allows for non-topologically ordered transactions by using two loops. // The algorithm below allows for non-topologically ordered transactions by using two loops.
@ -135,15 +134,15 @@ where
// returns true or not. (in a second loop). // returns true or not. (in a second loop).
let mut additions = IndexedAdditions::<A, I::Additions>::default(); let mut additions = IndexedAdditions::<A, I::Additions>::default();
let mut transactions = Vec::new(); let mut transactions = Vec::new();
for tx in txs.into_iter() { for (tx, anchors) in txs.into_iter() {
additions.index_additions.append(self.index.index_tx(tx)); additions.index_additions.append(self.index.index_tx(tx));
transactions.push(tx); transactions.push((tx, anchors));
} }
additions.append( additions.append(
transactions transactions
.into_iter() .into_iter()
.filter_map(|tx| match self.index.is_tx_relevant(tx) { .filter_map(|(tx, anchors)| match self.index.is_tx_relevant(tx) {
true => Some(self.insert_tx(tx, anchors.clone(), seen_at)), true => Some(self.insert_tx(tx, anchors, seen_at)),
false => None, false => None,
}) })
.fold(Default::default(), |mut acc, other| { .fold(Default::default(), |mut acc, other| {

View File

@ -61,7 +61,7 @@ fn insert_relevant_txs() {
let txs = [tx_c, tx_b, tx_a]; let txs = [tx_c, tx_b, tx_a];
assert_eq!( assert_eq!(
graph.insert_relevant_txs(&txs, None, None), graph.insert_relevant_txs(txs.iter().map(|tx| (tx, None)), None),
IndexedAdditions { IndexedAdditions {
graph_additions: Additions { graph_additions: Additions {
tx: txs.into(), tx: txs.into(),