Skip to content

Commit 6cb5976

Browse files
committed
retrieve Confirmed tenures max height
1 parent 2493ed1 commit 6cb5976

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

stackslib/src/net/download/nakamoto/download_state_machine.rs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -659,32 +659,48 @@ impl NakamotoDownloadStateMachine {
659659
schedule.into_iter().map(|(_count, ch)| ch).collect()
660660
}
661661

662-
/// Returns the highest Stacks tip height reported by the given neighbors.
662+
/// Returns the highest Stacks tip height from available sources.
663663
///
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.
666667
///
667668
/// # Arguments
668669
///
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.
670672
///
671673
/// # Returns
672674
///
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.
675676
pub(crate) fn get_max_stacks_height_of_neighbors(
676677
&self,
677678
neighbors: &[NeighborAddress],
678679
) -> 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+
}
688704
}
689705

690706
/// How many neighbors can we contact still, given the map of tenures to neighbors which can

0 commit comments

Comments
 (0)