feat(tree): make remove_before aware of the finalized hash (#10574)

Co-authored-by: Roman Krasiuk <rokrassyuk@gmail.com>
This commit is contained in:
Dan Cline
2024-08-29 17:24:56 -04:00
committed by GitHub
parent 51524f2534
commit c5a1c0e131
2 changed files with 270 additions and 25 deletions

View File

@ -75,11 +75,55 @@ impl ForkchoiceStateTracker {
self.last_syncing.as_ref().map(|s| s.head_block_hash)
}
/// Returns the latest received `ForkchoiceState`.
///
/// Caution: this can be invalid.
pub const fn latest_state(&self) -> Option<ForkchoiceState> {
self.last_valid
}
/// Returns the last valid `ForkchoiceState`.
pub const fn last_valid_state(&self) -> Option<ForkchoiceState> {
self.last_valid
}
/// Returns the last valid finalized hash.
///
/// This will return [`None`], if either there is no valid finalized forkchoice state, or the
/// finalized hash for the latest valid forkchoice state is zero.
#[inline]
pub fn last_valid_finalized(&self) -> Option<B256> {
self.last_valid.and_then(|state| {
// if the hash is zero then we should act like there is no finalized hash
if state.finalized_block_hash.is_zero() {
None
} else {
Some(state.finalized_block_hash)
}
})
}
/// Returns the last received `ForkchoiceState` to which we need to sync.
pub const fn sync_target_state(&self) -> Option<ForkchoiceState> {
self.last_syncing
}
/// Returns the sync target finalized hash.
///
/// This will return [`None`], if either there is no sync target forkchoice state, or the
/// finalized hash for the sync target forkchoice state is zero.
#[inline]
pub fn sync_target_finalized(&self) -> Option<B256> {
self.last_syncing.and_then(|state| {
// if the hash is zero then we should act like there is no finalized hash
if state.finalized_block_hash.is_zero() {
None
} else {
Some(state.finalized_block_hash)
}
})
}
/// Returns true if no forkchoice state has been received yet.
pub const fn is_empty(&self) -> bool {
self.latest.is_none()