Repository navigation
kona-engine,kona-node-service: keep a stale delegated safe head from moving safe below finality #24004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
geoknee
merged 1 commit into
einar/23912-delegated-parity-verification
from
einar/23910-delegated-stale-safe
Nov 19, 2026
+925
−187
Merged
kona-engine,kona-node-service: keep a stale delegated safe head from moving safe below finality #24004
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| //! Checks that keep a label from moving below finality. | ||
|
|
||
| use crate::EngineSyncState; | ||
| use alloy_eips::BlockNumHash; | ||
| use thiserror::Error; | ||
|
|
||
| /// A block that a label cannot move to, because it would move the label below finality. | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq, Error)] | ||
| pub enum FinalityViolation { | ||
| /// The block is below the finalized head. | ||
| #[error("Block {target} is behind the finalized head {finalized:?}")] | ||
| BehindFinalizedHead { | ||
| /// The finalized head. | ||
| finalized: BlockNumHash, | ||
| /// The number of the block. | ||
| target: u64, | ||
| }, | ||
| /// The block is a different block at the finalized head's height. | ||
| #[error("Block {target:?} conflicts with the finalized head {finalized:?}")] | ||
| ConflictsWithFinalizedHead { | ||
| /// The finalized head. | ||
| finalized: BlockNumHash, | ||
| /// The block. | ||
| target: BlockNumHash, | ||
| }, | ||
| } | ||
|
|
||
| impl EngineSyncState { | ||
| /// Rejects a block number below the established finalized head. | ||
| pub fn ensure_not_behind_finality(&self, target: u64) -> Result<(), FinalityViolation> { | ||
| match self.established_finalized_head() { | ||
| Some(finalized) if target < finalized.number => { | ||
| Err(FinalityViolation::BehindFinalizedHead { finalized, target }) | ||
| } | ||
| _ => Ok(()), | ||
| } | ||
| } | ||
|
|
||
| /// Rejects a different block at the established finalized head's height. Before finality is | ||
| /// established no block pins it, so any block, including the configured genesis, passes. | ||
| pub fn ensure_no_conflict_with_finality( | ||
| &self, | ||
| target: BlockNumHash, | ||
| ) -> Result<(), FinalityViolation> { | ||
| match self.established_finalized_head() { | ||
| Some(finalized) | ||
| if target.number == finalized.number && target.hash != finalized.hash => | ||
| { | ||
| Err(FinalityViolation::ConflictsWithFinalizedHead { finalized, target }) | ||
| } | ||
| _ => Ok(()), | ||
| } | ||
| } | ||
|
|
||
| /// Rejects a block below the established finalized head or conflicting with it. | ||
| pub fn ensure_respects_finality(&self, target: BlockNumHash) -> Result<(), FinalityViolation> { | ||
| self.ensure_not_behind_finality(target.number)?; | ||
| self.ensure_no_conflict_with_finality(target) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::EngineSyncStateUpdate; | ||
| use alloy_primitives::B256; | ||
| use kona_protocol::{BlockInfo, L2BlockInfo}; | ||
|
|
||
| const FINALIZED_NUMBER: u64 = 5; | ||
|
|
||
| fn block(number: u64, hash: u8) -> L2BlockInfo { | ||
| L2BlockInfo { | ||
| block_info: BlockInfo { hash: B256::repeat_byte(hash), number, ..Default::default() }, | ||
| ..Default::default() | ||
| } | ||
| } | ||
|
|
||
| fn sync_state_finalized_at(finalized_head: L2BlockInfo) -> EngineSyncState { | ||
| EngineSyncState::default().apply_update(EngineSyncStateUpdate { | ||
| finalized_head: Some(finalized_head), | ||
| ..Default::default() | ||
| }) | ||
| } | ||
|
|
||
| /// The finalized head at 5 once finality is established, and the default head before. | ||
| fn finalized_head(is_finality_established: bool) -> L2BlockInfo { | ||
| if is_finality_established { block(FINALIZED_NUMBER, 1) } else { L2BlockInfo::default() } | ||
| } | ||
|
|
||
| /// The violation expected for `target` checked against the finalized head at 5. | ||
| fn behind(target: L2BlockInfo) -> Option<FinalityViolation> { | ||
| Some(FinalityViolation::BehindFinalizedHead { | ||
| finalized: block(FINALIZED_NUMBER, 1).block_info.id(), | ||
| target: target.block_info.number, | ||
| }) | ||
| } | ||
|
|
||
| /// The violation expected for `target` conflicting with the finalized head at 5. | ||
| fn conflicting(target: L2BlockInfo) -> Option<FinalityViolation> { | ||
| Some(FinalityViolation::ConflictsWithFinalizedHead { | ||
| finalized: block(FINALIZED_NUMBER, 1).block_info.id(), | ||
| target: target.block_info.id(), | ||
| }) | ||
| } | ||
|
|
||
| /// A block below the established finalized head, or a different block at its height, breaks | ||
| /// finality. Before finality is established no block pins it, so a different block at the | ||
| /// default head's height, genesis, passes. | ||
| #[rstest::rstest] | ||
| #[case::above(true, block(FINALIZED_NUMBER + 1, 2), None)] | ||
| #[case::at(true, block(FINALIZED_NUMBER, 1), None)] | ||
| #[case::below(true, block(FINALIZED_NUMBER - 1, 2), behind(block(FINALIZED_NUMBER - 1, 2)))] | ||
| #[case::conflicting(true, block(FINALIZED_NUMBER, 2), conflicting(block(FINALIZED_NUMBER, 2)))] | ||
| #[case::not_established(false, block(0, 2), None)] | ||
| fn block_is_checked_against_established_finality( | ||
| #[case] is_finality_established: bool, | ||
| #[case] target: L2BlockInfo, | ||
| #[case] expected_violation: Option<FinalityViolation>, | ||
| ) { | ||
| let sync_state = sync_state_finalized_at(finalized_head(is_finality_established)); | ||
|
|
||
| let result = sync_state.ensure_respects_finality(target.block_info.id()); | ||
|
|
||
| assert_eq!(result.err(), expected_violation); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.