@@ -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.
347348pub ( 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
365366impl < ' 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
0 commit comments