]> Untitled Git - bdk/commitdiff
feat(chain): make various insert tx methods more generic
author志宇 <hello@evanlinjin.me>
Mon, 2 Sep 2024 09:32:19 +0000 (17:32 +0800)
committer志宇 <hello@evanlinjin.me>
Tue, 3 Sep 2024 06:03:07 +0000 (14:03 +0800)
Instead of having `Transaction` as input, we have a generic parameter
where the bound is `Into<Arc<Transaction>>` for the following methods:

* `IndexedTxGraph::insert_tx`
* `IndexedTxGraph::batch_insert_unconfirmed`
* `TxGraph::batch_insert_unconfirmed`

crates/chain/src/indexed_tx_graph.rs
crates/chain/src/tx_graph.rs

index ed2a1f0ce722b6744ff05f7af09fa5d5780677b2..b2da7bf75b3db3d4b1b2a279a97aa2f1291be5d7 100644 (file)
@@ -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<A, I::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<T: Into<Arc<Transaction>>>(&mut self, tx: T) -> ChangeSet<A, I::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<T: Into<Arc<Transaction>>>(
         &mut self,
-        txs: impl IntoIterator<Item = (Transaction, u64)>,
+        txs: impl IntoIterator<Item = (T, u64)>,
     ) -> ChangeSet<A, I::ChangeSet> {
         let graph = self.graph.batch_insert_unconfirmed(txs);
         let indexer = self.index_tx_graph_changeset(&graph);
index 127b47cf20e9c10cea3471fc014f9dcd5e933a55..9a32ccdfc2822395f355c33773478dc00ab2714f 100644 (file)
@@ -606,12 +606,13 @@ impl<A: Clone + Ord> TxGraph<A> {
     /// 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<T: Into<Arc<Transaction>>>(
         &mut self,
-        txs: impl IntoIterator<Item = (Transaction, u64)>,
+        txs: impl IntoIterator<Item = (T, u64)>,
     ) -> ChangeSet<A> {
         let mut changeset = ChangeSet::<A>::default();
         for (tx, seen_at) in txs {
+            let tx: Arc<Transaction> = tx.into();
             changeset.merge(self.insert_seen_at(tx.compute_txid(), seen_at));
             changeset.merge(self.insert_tx(tx));
         }