Skip to content

Commit 573915f

Browse files
committed
refactor(chain)!: update TopologicalIter to take an iterator
- Update the `TopologicalIter` to take an Iterator<Item = CanonicalTx<'a, Arc<Transaction>, A> of canonical txs. - Update it's documentation to mention the Kahn's Algorithm. - Update it to use `a_tx.cmp(&b_tx)` instead.
1 parent 876bba9 commit 573915f

File tree

2 files changed

+8
-49
lines changed

2 files changed

+8
-49
lines changed

crates/chain/src/canonical_iter.rs

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,8 @@ impl<A: Clone> CanonicalReason<A> {
343343
}
344344
}
345345

346-
/// Iterator that yields transactions in topological order with proper sorting within levels.
346+
/// Iterator based on the Kahn's Algorithm, that yields transactions in topological order with
347+
/// proper sorting within levels.
347348
pub(crate) struct TopologicalIterator<'a, A> {
348349
/// Map of txid to its canonical transaction
349350
canonical_txs: HashMap<Txid, CanonicalTx<'a, Arc<Transaction>, A>>,
@@ -363,7 +364,9 @@ pub(crate) struct TopologicalIterator<'a, A> {
363364
}
364365

365366
impl<'a, A: Clone + Anchor> TopologicalIterator<'a, A> {
366-
pub(crate) fn new(canonical_txs: Vec<CanonicalTx<'a, Arc<Transaction>, A>>) -> Self {
367+
pub(crate) fn new(
368+
canonical_txs: impl Iterator<Item = CanonicalTx<'a, Arc<Transaction>, A>>,
369+
) -> Self {
367370
// Build a map from txid to canonical tx for quick lookup
368371
let mut tx_map: HashMap<Txid, CanonicalTx<'a, Arc<Transaction>, A>> = HashMap::new();
369372
let mut canonical_set: HashSet<Txid> = HashSet::new();
@@ -442,46 +445,7 @@ impl<'a, A: Clone + Anchor> TopologicalIterator<'a, A> {
442445
let a_tx = canonical_txs.get(&a_txid).expect("txid must exist");
443446
let b_tx = canonical_txs.get(&b_txid).expect("txid must exist");
444447

445-
use crate::ChainPosition;
446-
use core::cmp::Ordering;
447-
448-
match (&a_tx.chain_position, &b_tx.chain_position) {
449-
// Both confirmed: sort by confirmation height
450-
(
451-
ChainPosition::Confirmed {
452-
anchor: a_anchor, ..
453-
},
454-
ChainPosition::Confirmed {
455-
anchor: b_anchor, ..
456-
},
457-
) => {
458-
let a_height = a_anchor.confirmation_height_upper_bound();
459-
let b_height = b_anchor.confirmation_height_upper_bound();
460-
a_height.cmp(&b_height)
461-
}
462-
// Confirmed comes before unconfirmed
463-
(ChainPosition::Confirmed { .. }, ChainPosition::Unconfirmed { .. }) => {
464-
Ordering::Less
465-
}
466-
// Unconfirmed comes after confirmed
467-
(ChainPosition::Unconfirmed { .. }, ChainPosition::Confirmed { .. }) => {
468-
Ordering::Greater
469-
}
470-
// Both unconfirmed: sort by first_seen (earlier timestamp first)
471-
(
472-
ChainPosition::Unconfirmed {
473-
first_seen: a_first_seen,
474-
..
475-
},
476-
ChainPosition::Unconfirmed {
477-
first_seen: b_first_seen,
478-
..
479-
},
480-
) => {
481-
// Earlier timestamps come first
482-
a_first_seen.cmp(b_first_seen)
483-
}
484-
}
448+
a_tx.cmp(&b_tx)
485449
});
486450
}
487451

crates/chain/src/tx_graph.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,15 +1118,10 @@ impl<A: Anchor> TxGraph<A> {
11181118
params: CanonicalizationParams,
11191119
) -> impl Iterator<Item = CanonicalTx<'a, Arc<Transaction>, A>> {
11201120
use crate::canonical_iter::TopologicalIterator;
1121-
1122-
// First, get all canonical transactions
1123-
#[allow(deprecated)]
1124-
let canonical_txs: Vec<CanonicalTx<'a, Arc<Transaction>, A>> =
1125-
self.list_canonical_txs(chain, chain_tip, params).collect();
1126-
11271121
// Use the topological iterator to get the correct ordering
11281122
// The iterator handles all the graph building internally
1129-
TopologicalIterator::new(canonical_txs)
1123+
#[allow(deprecated)]
1124+
TopologicalIterator::new(self.list_canonical_txs(chain, chain_tip, params))
11301125
}
11311126

11321127
/// Get a filtered list of outputs from the given `outpoints` that are in `chain` with

0 commit comments

Comments
 (0)