]> Untitled Git - bdk/commitdiff
[bdk_chain_redesign] Change `insert_relevant_txs` method
author志宇 <hello@evanlinjin.me>
Sat, 22 Apr 2023 16:12:41 +0000 (00:12 +0800)
committer志宇 <hello@evanlinjin.me>
Sat, 22 Apr 2023 16:21:31 +0000 (00:21 +0800)
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.

crates/chain/src/indexed_tx_graph.rs
crates/chain/tests/test_indexed_tx_graph.rs

index 20d9958ea0f7205ddefdd817288c198cca8e5d26..c4ee3209184525bc62184296638e817332e9aca5 100644 (file)
@@ -117,14 +117,13 @@ where
     /// Insert relevant transactions from the given `txs` iterator.
     ///
     /// 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
     /// timestamp of when the transactions are last seen.
     pub fn insert_relevant_txs<'t>(
         &mut self,
-        txs: impl IntoIterator<Item = &'t Transaction>,
-        anchors: impl IntoIterator<Item = A> + Clone,
+        txs: impl IntoIterator<Item = (&'t Transaction, impl IntoIterator<Item = A>)>,
         seen_at: Option<u64>,
     ) -> IndexedAdditions<A, I::Additions> {
         // 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).
         let mut additions = IndexedAdditions::<A, I::Additions>::default();
         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));
-            transactions.push(tx);
+            transactions.push((tx, anchors));
         }
         additions.append(
             transactions
                 .into_iter()
-                .filter_map(|tx| match self.index.is_tx_relevant(tx) {
-                    true => Some(self.insert_tx(tx, anchors.clone(), seen_at)),
+                .filter_map(|(tx, anchors)| match self.index.is_tx_relevant(tx) {
+                    true => Some(self.insert_tx(tx, anchors, seen_at)),
                     false => None,
                 })
                 .fold(Default::default(), |mut acc, other| {
index 26a30cb87fcbd9f868b6d99ff5410dec740a918d..4ca340d1ea1eae64195f26e2fc5e33bb570e1a3a 100644 (file)
@@ -61,7 +61,7 @@ fn insert_relevant_txs() {
     let txs = [tx_c, tx_b, tx_a];
 
     assert_eq!(
-        graph.insert_relevant_txs(&txs, None, None),
+        graph.insert_relevant_txs(txs.iter().map(|tx| (tx, None)), None),
         IndexedAdditions {
             graph_additions: Additions {
                 tx: txs.into(),