@@ -343,7 +343,8 @@ impl<A: Clone> CanonicalReason<A> {
343
343
}
344
344
}
345
345
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.
347
348
pub ( crate ) struct TopologicalIterator < ' a , A > {
348
349
/// Map of txid to its canonical transaction
349
350
canonical_txs : HashMap < Txid , CanonicalTx < ' a , Arc < Transaction > , A > > ,
@@ -363,7 +364,9 @@ pub(crate) struct TopologicalIterator<'a, A> {
363
364
}
364
365
365
366
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 {
367
370
// Build a map from txid to canonical tx for quick lookup
368
371
let mut tx_map: HashMap < Txid , CanonicalTx < ' a , Arc < Transaction > , A > > = HashMap :: new ( ) ;
369
372
let mut canonical_set: HashSet < Txid > = HashSet :: new ( ) ;
@@ -442,46 +445,7 @@ impl<'a, A: Clone + Anchor> TopologicalIterator<'a, A> {
442
445
let a_tx = canonical_txs. get ( & a_txid) . expect ( "txid must exist" ) ;
443
446
let b_tx = canonical_txs. get ( & b_txid) . expect ( "txid must exist" ) ;
444
447
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)
485
449
} ) ;
486
450
}
487
451
0 commit comments