@@ -659,32 +659,48 @@ impl NakamotoDownloadStateMachine {
659
659
schedule. into_iter ( ) . map ( |( _count, ch) | ch) . collect ( )
660
660
}
661
661
662
- /// Returns the highest Stacks tip height reported by the given neighbors .
662
+ /// Returns the highest Stacks tip height from available sources .
663
663
///
664
- /// For each neighbor, this checks if there's an active unconfirmed download with a known
665
- /// `tip_height`. If so, it's considered when finding the maximum.
664
+ /// In IBD mode (Confirmed state), this queries confirmed tenure downloaders for their
665
+ /// tenure end block heights. In steady-state mode (Unconfirmed state), this checks
666
+ /// the unconfirmed tenure downloads for the given neighbors to find their tip heights.
666
667
///
667
668
/// # Arguments
668
669
///
669
- /// * `neighbors` - A slice of `NeighborAddress` structs to check.
670
+ /// * `neighbors` - A slice of `NeighborAddress` structs to check in unconfirmed mode.
671
+ /// Ignored in IBD mode.
670
672
///
671
673
/// # Returns
672
674
///
673
- /// * `Some(u64)` if at least one neighbor has a tip height.
674
- /// * `None` if no tip heights are found.
675
+ /// * `Some(u64)` - The maximum height found, or None if no heights are available.
675
676
pub ( crate ) fn get_max_stacks_height_of_neighbors (
676
677
& self ,
677
678
neighbors : & [ NeighborAddress ] ,
678
679
) -> Option < u64 > {
679
- neighbors
680
- . iter ( )
681
- . filter_map ( |naddr| {
682
- self . unconfirmed_tenure_downloads
683
- . get ( naddr)
684
- . and_then ( |downloader| downloader. tenure_tip . as_ref ( ) )
685
- . map ( |tip| tip. tip_height )
686
- } )
687
- . max ( )
680
+ match self . state {
681
+ NakamotoDownloadState :: Confirmed => self
682
+ . tenure_downloads
683
+ . downloaders
684
+ . iter ( )
685
+ . flatten ( )
686
+ . filter_map ( |downloader| {
687
+ downloader
688
+ . tenure_end_block
689
+ . as_ref ( )
690
+ . map ( |end_block| end_block. header . chain_length + 1 )
691
+ } )
692
+ . max ( ) ,
693
+ NakamotoDownloadState :: Unconfirmed => neighbors
694
+ . iter ( )
695
+ . filter_map ( |neighbor_addr| {
696
+ self . unconfirmed_tenure_downloads
697
+ . get ( neighbor_addr) ?
698
+ . tenure_tip
699
+ . as_ref ( )
700
+ . map ( |tip| tip. tip_height )
701
+ } )
702
+ . max ( ) ,
703
+ }
688
704
}
689
705
690
706
/// How many neighbors can we contact still, given the map of tenures to neighbors which can
0 commit comments