Skip to content

Commit

Permalink
feat(chain)!: change IndexedTxGraph methods that take in `&Transact…
Browse files Browse the repository at this point in the history
…ion`

to take in generic with bound `Into<Arc<Transaction>>` instead of
`&'t Transaction`.
  • Loading branch information
evanlinjin committed Sep 3, 2024
1 parent 4c3e0cb commit b178493
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
31 changes: 17 additions & 14 deletions crates/chain/src/indexed_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,38 +157,38 @@ where
///
/// Relevancy is determined by the [`Indexer::is_tx_relevant`] implementation of `I`. Irrelevant
/// transactions in `txs` will be ignored. `txs` do not need to be in topological order.
pub fn batch_insert_relevant<'t>(
pub fn batch_insert_relevant<T: Into<Arc<Transaction>>>(
&mut self,
txs: impl IntoIterator<Item = (&'t Transaction, impl IntoIterator<Item = A>)>,
txs: impl IntoIterator<Item = (T, impl IntoIterator<Item = A>)>,
) -> ChangeSet<A, I::ChangeSet> {
// The algorithm below allows for non-topologically ordered transactions by using two loops.
// This is achieved by:
// 1. insert all txs into the index. If they are irrelevant then that's fine it will just
// not store anything about them.
// 2. decide whether to insert them into the graph depending on whether `is_tx_relevant`
// returns true or not. (in a second loop).
let txs = txs.into_iter().collect::<Vec<_>>();
let txs = txs
.into_iter()
.map(|(tx, anchors)| (<T as Into<Arc<Transaction>>>::into(tx), anchors))
.collect::<Vec<_>>();

let mut indexer = I::ChangeSet::default();
for (tx, _) in &txs {
indexer.merge(self.index.index_tx(tx));
}

let mut graph = tx_graph::ChangeSet::default();
let mut tx_graph = tx_graph::ChangeSet::default();
for (tx, anchors) in txs {
if self.index.is_tx_relevant(tx) {
if self.index.is_tx_relevant(&tx) {
let txid = tx.compute_txid();
graph.merge(self.graph.insert_tx(tx.clone()));
tx_graph.merge(self.graph.insert_tx(tx.clone()));
for anchor in anchors {
graph.merge(self.graph.insert_anchor(txid, anchor));
tx_graph.merge(self.graph.insert_anchor(txid, anchor));
}
}
}

ChangeSet {
tx_graph: graph,
indexer,
}
ChangeSet { tx_graph, indexer }
}

/// Batch insert unconfirmed transactions, filtering out those that are irrelevant.
Expand All @@ -199,17 +199,20 @@ where
/// Items of `txs` are tuples containing the transaction and a *last seen* timestamp. The
/// *last seen* communicates when the transaction is last seen in the mempool which is used for
/// conflict-resolution in [`TxGraph`] (refer to [`TxGraph::insert_seen_at`] for details).
pub fn batch_insert_relevant_unconfirmed<'t>(
pub fn batch_insert_relevant_unconfirmed<T: Into<Arc<Transaction>>>(
&mut self,
unconfirmed_txs: impl IntoIterator<Item = (&'t Transaction, u64)>,
unconfirmed_txs: impl IntoIterator<Item = (T, u64)>,
) -> ChangeSet<A, I::ChangeSet> {
// The algorithm below allows for non-topologically ordered transactions by using two loops.
// This is achieved by:
// 1. insert all txs into the index. If they are irrelevant then that's fine it will just
// not store anything about them.
// 2. decide whether to insert them into the graph depending on whether `is_tx_relevant`
// returns true or not. (in a second loop).
let txs = unconfirmed_txs.into_iter().collect::<Vec<_>>();
let txs = unconfirmed_txs
.into_iter()
.map(|(tx, last_seen)| (<T as Into<Arc<Transaction>>>::into(tx), last_seen))
.collect::<Vec<_>>();

let mut indexer = I::ChangeSet::default();
for (tx, _) in &txs {
Expand Down
9 changes: 5 additions & 4 deletions crates/chain/tests/test_indexed_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn insert_relevant_txs() {
};

assert_eq!(
graph.batch_insert_relevant(txs.iter().map(|tx| (tx, None))),
graph.batch_insert_relevant(txs.iter().cloned().map(|tx| (tx, None))),
changeset,
);

Expand Down Expand Up @@ -237,10 +237,10 @@ fn test_list_owned_txouts() {
// Insert unconfirmed txs with a last_seen timestamp

let _ =
graph.batch_insert_relevant([&tx1, &tx2, &tx3, &tx6].iter().enumerate().map(|(i, tx)| {
graph.batch_insert_relevant([&tx1, &tx2, &tx3, &tx6].iter().enumerate().map(|(i, &tx)| {
let height = i as u32;
(
*tx,
tx.clone(),
local_chain
.get(height)
.map(|cp| cp.block_id())
Expand All @@ -251,7 +251,8 @@ fn test_list_owned_txouts() {
)
}));

let _ = graph.batch_insert_relevant_unconfirmed([&tx4, &tx5].iter().map(|tx| (*tx, 100)));
let _ =
graph.batch_insert_relevant_unconfirmed([&tx4, &tx5].iter().map(|&tx| (tx.clone(), 100)));

// A helper lambda to extract and filter data from the graph.
let fetch =
Expand Down

0 comments on commit b178493

Please sign in to comment.