From 67d5fa695f3f748d176c182d25be5ef70f5127ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=97=E5=AE=87?= Date: Mon, 2 Sep 2024 17:32:19 +0800 Subject: [PATCH] feat(chain): make various insert tx methods more generic Instead of having `Transaction` as input, we have a generic parameter where the bound is `Into>` for the following methods: * `IndexedTxGraph::insert_tx` * `IndexedTxGraph::batch_insert_unconfirmed` * `TxGraph::batch_insert_unconfirmed` --- crates/chain/src/indexed_tx_graph.rs | 17 +++++++---------- crates/chain/src/tx_graph.rs | 5 +++-- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/crates/chain/src/indexed_tx_graph.rs b/crates/chain/src/indexed_tx_graph.rs index ed2a1f0ce..b2da7bf75 100644 --- a/crates/chain/src/indexed_tx_graph.rs +++ b/crates/chain/src/indexed_tx_graph.rs @@ -2,7 +2,7 @@ //! [`IndexedTxGraph`] documentation for more. use core::fmt::Debug; -use alloc::vec::Vec; +use alloc::{sync::Arc, vec::Vec}; use bitcoin::{Block, OutPoint, Transaction, TxOut, Txid}; use crate::{ @@ -133,13 +133,10 @@ where } /// Insert and index a transaction into the graph. - pub fn insert_tx(&mut self, tx: Transaction) -> ChangeSet { - let graph = self.graph.insert_tx(tx); - let indexer = self.index_tx_graph_changeset(&graph); - ChangeSet { - tx_graph: graph, - indexer, - } + pub fn insert_tx>>(&mut self, tx: T) -> ChangeSet { + let tx_graph = self.graph.insert_tx(tx); + let indexer = self.index_tx_graph_changeset(&tx_graph); + ChangeSet { tx_graph, indexer } } /// Insert an `anchor` for a given transaction. @@ -239,9 +236,9 @@ where /// To filter out irrelevant transactions, use [`batch_insert_relevant_unconfirmed`] instead. /// /// [`batch_insert_relevant_unconfirmed`]: IndexedTxGraph::batch_insert_relevant_unconfirmed - pub fn batch_insert_unconfirmed( + pub fn batch_insert_unconfirmed>>( &mut self, - txs: impl IntoIterator, + txs: impl IntoIterator, ) -> ChangeSet { let graph = self.graph.batch_insert_unconfirmed(txs); let indexer = self.index_tx_graph_changeset(&graph); diff --git a/crates/chain/src/tx_graph.rs b/crates/chain/src/tx_graph.rs index 127b47cf2..9a32ccdfc 100644 --- a/crates/chain/src/tx_graph.rs +++ b/crates/chain/src/tx_graph.rs @@ -606,12 +606,13 @@ impl TxGraph { /// Items of `txs` are tuples containing the transaction and a *last seen* timestamp. The /// *last seen* communicates when the transaction is last seen in mempool which is used for /// conflict-resolution (refer to [`TxGraph::insert_seen_at`] for details). - pub fn batch_insert_unconfirmed( + pub fn batch_insert_unconfirmed>>( &mut self, - txs: impl IntoIterator, + txs: impl IntoIterator, ) -> ChangeSet { let mut changeset = ChangeSet::::default(); for (tx, seen_at) in txs { + let tx: Arc = tx.into(); changeset.merge(self.insert_seen_at(tx.compute_txid(), seen_at)); changeset.merge(self.insert_tx(tx)); }