Skip to content

Commit

Permalink
feat(chain): make various insert tx methods more generic
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
evanlinjin committed Sep 3, 2024
1 parent 56970a9 commit 67d5fa6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
17 changes: 7 additions & 10 deletions crates/chain/src/indexed_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions crates/chain/src/tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit 67d5fa6

Please sign in to comment.