]> Untitled Git - bdk/commitdiff
feat(chain): `TxGraph::insert_tx` reuses `Arc`
author志宇 <hello@evanlinjin.me>
Tue, 30 Apr 2024 06:49:03 +0000 (14:49 +0800)
committer志宇 <hello@evanlinjin.me>
Fri, 10 May 2024 06:11:19 +0000 (14:11 +0800)
When we insert a transaction that is already wrapped in `Arc`, we should
reuse the `Arc`.

crates/chain/src/tx_graph.rs

index cf514855475ad93d0740ee216fac316a407af931..d097b8fa57767dd7b1346f3d9c9dbc3920d4d24c 100644 (file)
@@ -516,12 +516,12 @@ impl<A: Clone + Ord> TxGraph<A> {
     /// Inserts the given transaction into [`TxGraph`].
     ///
     /// The [`ChangeSet`] returned will be empty if `tx` already exists.
-    pub fn insert_tx(&mut self, tx: Transaction) -> ChangeSet<A> {
+    pub fn insert_tx<T: Into<Arc<Transaction>>>(&mut self, tx: T) -> ChangeSet<A> {
+        let tx = tx.into();
         let mut update = Self::default();
-        update.txs.insert(
-            tx.txid(),
-            (TxNodeInternal::Whole(tx.into()), BTreeSet::new(), 0),
-        );
+        update
+            .txs
+            .insert(tx.txid(), (TxNodeInternal::Whole(tx), BTreeSet::new(), 0));
         self.apply_update(update)
     }