From: 志宇 Date: Sat, 22 Apr 2023 16:12:41 +0000 (+0800) Subject: [bdk_chain_redesign] Change `insert_relevant_txs` method X-Git-Tag: v1.0.0-alpha.1~19^2~5 X-Git-Url: http://internal-gitweb-vhost/script/%22https:/database/scripts/struct.CommandStringError.html?a=commitdiff_plain;h=1b152647c557d2ff492cd241999c5320f7c4f6c5;p=bdk [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. --- diff --git a/crates/chain/src/indexed_tx_graph.rs b/crates/chain/src/indexed_tx_graph.rs index 20d9958e..c4ee3209 100644 --- a/crates/chain/src/indexed_tx_graph.rs +++ b/crates/chain/src/indexed_tx_graph.rs @@ -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, - anchors: impl IntoIterator + Clone, + txs: impl IntoIterator)>, seen_at: Option, ) -> IndexedAdditions { // 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::::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| { diff --git a/crates/chain/tests/test_indexed_tx_graph.rs b/crates/chain/tests/test_indexed_tx_graph.rs index 26a30cb8..4ca340d1 100644 --- a/crates/chain/tests/test_indexed_tx_graph.rs +++ b/crates/chain/tests/test_indexed_tx_graph.rs @@ -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(),