diff --git a/rust/kona/crates/node/engine/src/task_queue/core.rs b/rust/kona/crates/node/engine/src/task_queue/core.rs index b0840513e45..7f2a7ec767b 100644 --- a/rust/kona/crates/node/engine/src/task_queue/core.rs +++ b/rust/kona/crates/node/engine/src/task_queue/core.rs @@ -204,7 +204,8 @@ mod recovery_tests { ConsolidateInput, ConsolidateTask, FinalizeBlockId, FinalizeTask, InsertTask, NoopBlockSink, test_utils::{ - TestAttributesBuilder, TestEngineStateBuilder, test_block_info, test_engine_client, + TestAttributesBuilder, TestEngineStateBuilder, expect_sync_start, test_block_info, + test_engine_client, }, }; use alloy_rpc_types_engine::{ @@ -368,14 +369,7 @@ mod recovery_tests { let state = TestEngineStateBuilder::new().with_unsafe_head(genesis).build(); let forkchoice = state.sync_state.create_forkchoice_state(); let (client, l1, l2) = test_engine_client(config.clone()); - for tag in ["finalized", "safe", "latest"] { - l2.expect_params("eth_getBlockByNumber", serde_json::json!([tag, true]), &block); - } - l1.expect( - "eth_getBlockByHash", - alloy_rpc_types_eth::Block::::default(), - ); - l2.expect_forkchoice_updated(forkchoice, PayloadStatusEnum::Valid); + expect_sync_start(&l1, &l2, &block, forkchoice); let (state_tx, _state_rx) = tokio::sync::watch::channel(state); let mut engine = Engine::new(state, state_tx); diff --git a/rust/kona/crates/node/engine/src/task_queue/tasks/finalize/error.rs b/rust/kona/crates/node/engine/src/task_queue/tasks/finalize/error.rs index 51a7f2c1dda..92eb39d2e96 100644 --- a/rust/kona/crates/node/engine/src/task_queue/tasks/finalize/error.rs +++ b/rust/kona/crates/node/engine/src/task_queue/tasks/finalize/error.rs @@ -22,9 +22,16 @@ pub enum FinalizeTaskError { /// The block to finalize was not found. #[error("The block to finalize was not found: Number {0}")] BlockNotFound(u64), - /// The block is not safe, and therefore cannot be finalized. - #[error("Attempted to finalize a block that is not yet safe")] - BlockNotSafe, + /// The block is not safe yet, and therefore cannot be finalized. A reset can lower the safe + /// head below a target that was safe when it was requested, and a delegate can run ahead of + /// the local safe head. + #[error("Finalization target {target} is above the safe head {safe}")] + BlockNotSafe { + /// The number of the safe head. + safe: u64, + /// The number of the finalization target. + target: u64, + }, /// The engine returned a block at a different height than the one requested. #[error("Requested block {requested} to finalize, but the engine returned block {returned}")] BlockNumberMismatch { @@ -57,6 +64,12 @@ pub enum FinalizeTaskError { /// The finalization target. target: BlockNumHash, }, + /// The engine has no block at the pinned target's height yet, for example while it catches up. + #[error("The engine does not have finalization target {target:?} yet")] + NotYetAvailable { + /// The finalization target. + target: BlockNumHash, + }, /// A temporary RPC failure. #[error(transparent)] TransportError(#[from] RpcError), @@ -66,12 +79,13 @@ impl EngineTaskError for FinalizeTaskError { fn severity(&self) -> EngineTaskErrorSeverity { match self { Self::BehindFinalizedHead { .. } | + Self::BlockNotSafe { .. } | Self::ConflictsWithFinalizedHead { .. } | - Self::NotCanonical { .. } => EngineTaskErrorSeverity::Skip, - Self::BlockNotFound(_) | - Self::BlockNotSafe | - Self::BlockNumberMismatch { .. } | - Self::FromBlock(_) => EngineTaskErrorSeverity::Critical, + Self::NotCanonical { .. } | + Self::NotYetAvailable { .. } => EngineTaskErrorSeverity::Skip, + Self::BlockNotFound(_) | Self::BlockNumberMismatch { .. } | Self::FromBlock(_) => { + EngineTaskErrorSeverity::Critical + } Self::ForkchoiceUpdateFailed(inner) => inner.severity(), Self::TransportError(_) => EngineTaskErrorSeverity::Temporary, } diff --git a/rust/kona/crates/node/engine/src/task_queue/tasks/finalize/id.rs b/rust/kona/crates/node/engine/src/task_queue/tasks/finalize/id.rs index c7c12fd6e73..13d18b4d825 100644 --- a/rust/kona/crates/node/engine/src/task_queue/tasks/finalize/id.rs +++ b/rust/kona/crates/node/engine/src/task_queue/tasks/finalize/id.rs @@ -11,8 +11,8 @@ use alloy_eips::BlockNumHash; /// `L2Finalizer`). The number is sufficient because there is only one chain to consult. /// - [`FinalizeBlockId::ByHash`] is used when an upstream supplies `(number, hash)` and the engine /// must finalize the specific block identified by hash. If the engine's canonical block at that -/// height has a different hash, the task skips the target rather than finalize the engine's block -/// in its place. +/// height has a different hash, or the engine has no block at that height yet, the task skips the +/// target rather than finalize the engine's block in its place. /// /// EL finalization is irreversible, so finalizing the wrong block is unrecoverable. Forcing every /// caller to pick a variant prevents accidental hash-loss at the boundary. @@ -21,7 +21,8 @@ pub enum FinalizeBlockId { /// Finalize whatever block the engine has at the given L2 block number. ByNumber(u64), /// Finalize the block matching the given `(number, hash)` pair, only once it is the engine's - /// canonical block at that height. + /// canonical block at that height. A skipped target leaves the state unchanged and may be + /// requested again. ByHash(BlockNumHash), } diff --git a/rust/kona/crates/node/engine/src/task_queue/tasks/finalize/task.rs b/rust/kona/crates/node/engine/src/task_queue/tasks/finalize/task.rs index dac3404750a..30a08a102bd 100644 --- a/rust/kona/crates/node/engine/src/task_queue/tasks/finalize/task.rs +++ b/rust/kona/crates/node/engine/src/task_queue/tasks/finalize/task.rs @@ -15,8 +15,9 @@ use std::sync::Arc; /// [`EngineState`], and dispatches a forkchoice update to finalize the block. /// /// Finality only moves forward. A target behind the finalized head, a different block at the -/// finalized height, or a pinned hash that is not the engine's canonical block fails with an error -/// that skips the task and leaves the engine state unchanged. +/// finalized height, a pinned hash that is not the engine's canonical block, a target above the +/// safe head, or a pinned target the engine has no block for yet fails with an error that skips +/// the task and leaves the engine state unchanged. #[derive(Debug, Clone, Constructor)] pub struct FinalizeTask { /// The engine client. @@ -28,6 +29,17 @@ pub struct FinalizeTask { } impl FinalizeTask { + /// A pinned target the execution layer answers with null is not available yet, for example + /// while it catches up: the task is skipped rather than held at the head of the queue ahead + /// of the imports that let it catch up. A missing by-number target is below the safe head, so + /// the engine must already have it. + const fn block_missing(&self) -> FinalizeTaskError { + match self.block_id { + FinalizeBlockId::ByHash(target) => FinalizeTaskError::NotYetAvailable { target }, + FinalizeBlockId::ByNumber(number) => FinalizeTaskError::BlockNotFound(number), + } + } + /// Whether `target` is the finalized head itself; a different block at its height is an error. fn is_already_finalized( finalized_head: &L2BlockInfo, @@ -61,7 +73,7 @@ impl FinalizeTask { .full() .await .map_err(FinalizeTaskError::TransportError)? - .ok_or(FinalizeTaskError::BlockNotFound(requested))?; + .ok_or_else(|| self.block_missing())?; let canonical = BlockNumHash { hash: block.header.hash, number: block.header.number }; debug!(target: "engine", ?canonical, "Fetched the block to finalize"); @@ -106,9 +118,14 @@ impl ExecutableEngineTask for FinalizeTask { return Ok(()); } - // Sanity check that the block that is being finalized is at least safe. - if state.sync_state.safe_head().block_info.number < target_number { - return Err(FinalizeTaskError::BlockNotSafe); + // Sanity check that the block that is being finalized is at least safe. A later request + // finalizes a target skipped here, or a newer block. + let safe_number = state.sync_state.safe_head().block_info.number; + if safe_number < target_number { + return Err(FinalizeTaskError::BlockNotSafe { + safe: safe_number, + target: target_number, + }); } let block_info = self.fetch_canonical_block_info().await?; diff --git a/rust/kona/crates/node/engine/src/task_queue/tasks/finalize/task_test.rs b/rust/kona/crates/node/engine/src/task_queue/tasks/finalize/task_test.rs index fb273f99831..45420631095 100644 --- a/rust/kona/crates/node/engine/src/task_queue/tasks/finalize/task_test.rs +++ b/rust/kona/crates/node/engine/src/task_queue/tasks/finalize/task_test.rs @@ -1,5 +1,6 @@ use crate::{ - EngineState, EngineSyncState, EngineSyncStateUpdate, EngineTaskError, EngineTaskErrorSeverity, + EngineState, EngineSyncState, EngineSyncStateUpdate, EngineTaskError, + EngineTaskErrorSeverity::{self, Critical, Skip, Temporary}, ExecutableEngineTask, FinalizeBlockId, FinalizeTask, FinalizeTaskError, test_utils::{ RpcMock, TestEngineStateBuilder, TestRpcBlock as RpcBlock, test_block_info, @@ -117,16 +118,59 @@ const fn is_number_mismatch(err: &FinalizeTaskError) -> bool { matches!(err, FinalizeTaskError::BlockNumberMismatch { requested: ADVANCED, .. }) } +const fn is_not_safe(err: &FinalizeTaskError) -> bool { + matches!(err, FinalizeTaskError::BlockNotSafe { .. }) +} + +fn is_not_yet_available(err: &FinalizeTaskError) -> bool { + let pinned_id = l2_block_info(&canonical(ADVANCED)).block_info.id(); + matches!(err, FinalizeTaskError::NotYetAvailable { target } if *target == pinned_id) +} + const fn is_block_not_found(err: &FinalizeTaskError) -> bool { matches!(err, FinalizeTaskError::BlockNotFound(ADVANCED)) } -/// Targets behind the finalized head, or a pinned hash that conflicts with it, are skipped before -/// asking the execution layer for the block: the test engine expects no request at all. +const fn is_transport_error(err: &FinalizeTaskError) -> bool { + matches!(err, FinalizeTaskError::TransportError(_)) +} + +/// A target above the finalized head, pinned to the engine's canonical block at its height. +fn pinned_target() -> FinalizeBlockId { + by_hash(&canonical(ADVANCED)) +} + +/// A target above the finalized head, identified by number only. +fn numbered_target() -> FinalizeBlockId { + by_number(&canonical(ADVANCED)) +} + +/// What the engine answers for a block it cannot serve. +#[derive(Clone, Copy, Debug)] +enum Unavailable { + ErrorResponse, + Null, + TransportFailure, +} + +fn expect_unavailable_block(l2: &RpcMock, number: u64, answer: Unavailable) { + match answer { + Unavailable::ErrorResponse => l2.expect_error_code("eth_getBlockByNumber", -32000), + Unavailable::Null => l2.expect_block_by_number(number, None), + Unavailable::TransportFailure => l2.expect_transport_error("eth_getBlockByNumber"), + } +} + +/// Targets behind the finalized head, a pinned hash that conflicts with it, and targets above the +/// safe head are skipped before asking the execution layer for the block: the test engine expects +/// no request at all. A reset can lower the safe head below a target that was safe when it was +/// requested, and a later request finalizes it. #[rstest] #[case::stale_by_hash(by_hash(&canonical(STALE)), is_behind)] #[case::stale_by_number(by_number(&canonical(STALE)), is_behind)] #[case::conflicting_by_hash(by_hash(&conflicting(FINALIZED)), is_conflict)] +#[case::above_safe_by_hash(by_hash(&canonical(UNSAFE)), is_not_safe)] +#[case::above_safe_by_number(by_number(&canonical(UNSAFE)), is_not_safe)] #[tokio::test] async fn finalize_task_skips_target_without_lookup( #[case] target: FinalizeBlockId, @@ -138,7 +182,7 @@ async fn finalize_task_skips_target_without_lookup( let err = finalize(target, &mut state, |_| {}).await.unwrap_err(); assert!(is_expected_error(&err), "{err:?}"); - assert_eq!(err.severity(), EngineTaskErrorSeverity::Skip); + assert_eq!(err.severity(), Skip); assert_eq!(state, original_state); } @@ -151,8 +195,11 @@ async fn finalize_task_refuses_target_above_safe_head_without_lookup() { let err = finalize(by_hash(&canonical(SAFE + 5)), &mut state, |_| {}).await.unwrap_err(); - assert!(matches!(err, FinalizeTaskError::BlockNotSafe), "{err:?}"); - assert_eq!(err.severity(), EngineTaskErrorSeverity::Critical); + assert!( + matches!(err, FinalizeTaskError::BlockNotSafe { safe: SAFE, target } if target == SAFE + 5), + "{err:?}" + ); + assert_eq!(err.severity(), EngineTaskErrorSeverity::Skip); assert_eq!(state, original_state); } @@ -178,7 +225,7 @@ async fn finalize_task_skips_target_off_the_canonical_chain( .unwrap_err(); assert!(is_expected_error(&err), "{err:?}"); - assert_eq!(err.severity(), EngineTaskErrorSeverity::Skip); + assert_eq!(err.severity(), Skip); assert_eq!(state, original_state); } @@ -226,38 +273,100 @@ async fn finalize_task_skips_lookup_for_finalized_head() { assert_eq!(state, original_state); } -/// The engine serving no block, or a block at a height other than the requested one, stops the -/// node instead of finalizing anything. A misplaced block could otherwise bypass the guards against -/// moving finality backward. +/// The engine serving a block at a height other than the requested one stops the node instead of +/// finalizing anything. A misplaced block could otherwise bypass the guards against moving +/// finality backward. +#[rstest] +#[case::stale_block_by_number(by_number(&canonical(ADVANCED)), canonical(STALE))] +#[case::higher_block_by_number(by_number(&canonical(ADVANCED)), canonical(SAFE))] +#[case::stale_block_by_hash(by_hash(&canonical(ADVANCED)), canonical(STALE))] +#[tokio::test] +async fn finalize_task_stops_on_block_at_unrequested_height( + #[case] target: FinalizeBlockId, + #[case] served_block: RpcBlock, +) { + let mut state = engine_state(established()); + let original_state = state; + + let err = finalize(target, &mut state, |l2| { + l2.expect_block_by_number(target.number(), Some(served_block)) + }) + .await + .unwrap_err(); + + assert!(is_number_mismatch(&err), "{err:?}"); + assert_eq!(err.severity(), Critical); + assert_eq!(state, original_state); +} + +/// While the execution layer catches up, it answers null for a pinned target's height, and the +/// target is skipped until its upstream resubmits it. An error response or a transport failure is a +/// failed lookup and is retried for either kind of target. A by-number target is below the safe +/// head, so the engine must have it: its absence stops the node. #[rstest] -#[case::missing_by_number(FinalizeBlockId::ByNumber(ADVANCED), None, is_block_not_found)] -#[case::missing_by_hash(by_hash(&canonical(ADVANCED)), None, is_block_not_found)] -#[case::stale_block_by_number( - /* target */ FinalizeBlockId::ByNumber(ADVANCED), - /* served_block */ Some(canonical(STALE)), - /* is_expected_error */ is_number_mismatch +#[case::pinned_null(pinned_target(), Unavailable::Null, is_not_yet_available, Skip)] +#[case::pinned_error_response( + pinned_target(), + Unavailable::ErrorResponse, + is_transport_error, + Temporary +)] +#[case::pinned_transport_failure( + pinned_target(), + Unavailable::TransportFailure, + is_transport_error, + Temporary )] -#[case::higher_block_by_number( - /* target */ FinalizeBlockId::ByNumber(ADVANCED), - /* served_block */ Some(canonical(SAFE)), - /* is_expected_error */ is_number_mismatch +#[case::by_number_null(numbered_target(), Unavailable::Null, is_block_not_found, Critical)] +#[case::by_number_error_response( + numbered_target(), + Unavailable::ErrorResponse, + is_transport_error, + Temporary )] -#[case::stale_block_by_hash(by_hash(&canonical(ADVANCED)), Some(canonical(STALE)), is_number_mismatch)] #[tokio::test] -async fn finalize_task_stops_on_unexpected_lookup_result( +async fn finalize_task_handles_block_the_engine_cannot_serve( #[case] target: FinalizeBlockId, - #[case] served_block: Option, + #[case] answer: Unavailable, #[case] is_expected_error: fn(&FinalizeTaskError) -> bool, + #[case] severity: EngineTaskErrorSeverity, ) { let mut state = engine_state(established()); let original_state = state; let err = - finalize(target, &mut state, |l2| l2.expect_block_by_number(target.number(), served_block)) + finalize(target, &mut state, |l2| expect_unavailable_block(l2, target.number(), answer)) .await .unwrap_err(); assert!(is_expected_error(&err), "{err:?}"); - assert_eq!(err.severity(), EngineTaskErrorSeverity::Critical); + assert_eq!(err.severity(), severity, "{err:?}"); assert_eq!(state, original_state); } + +/// A pinned target skipped while the engine catches up finalizes once its upstream resubmits it +/// and the engine has the block. +#[tokio::test] +async fn resubmitted_pinned_target_finalizes_once_engine_has_it() { + let mut state = engine_state(established()); + let served_block = canonical(ADVANCED); + let target = by_hash(&served_block); + let expected_sync_state = with_finalized_head(&state, l2_block_info(&served_block)); + + let err = finalize(target, &mut state, |l2| l2.expect_block_by_number(ADVANCED, None)) + .await + .unwrap_err(); + assert!(matches!(err, FinalizeTaskError::NotYetAvailable { .. }), "{err:?}"); + + finalize(target, &mut state, |l2| { + l2.expect_block_by_number(ADVANCED, Some(served_block)); + l2.expect_forkchoice_updated( + expected_sync_state.create_forkchoice_state(), + PayloadStatusEnum::Valid, + ); + }) + .await + .unwrap(); + + assert_eq!(state.sync_state, expected_sync_state); +} diff --git a/rust/kona/crates/node/engine/src/test_utils/mod.rs b/rust/kona/crates/node/engine/src/test_utils/mod.rs index 0a60403a0d6..89d57ac5892 100644 --- a/rust/kona/crates/node/engine/src/test_utils/mod.rs +++ b/rust/kona/crates/node/engine/src/test_utils/mod.rs @@ -8,7 +8,7 @@ mod deposits; pub use deposits::l1_info_deposit; mod rpc; -pub use rpc::{RpcMock, test_engine_client}; +pub use rpc::{RpcMock, expect_sync_start, test_engine_client}; mod engine_state; pub use engine_state::TestEngineStateBuilder; diff --git a/rust/kona/crates/node/engine/src/test_utils/rpc.rs b/rust/kona/crates/node/engine/src/test_utils/rpc.rs index 39f23f14de2..0bd0bfef0c6 100644 --- a/rust/kona/crates/node/engine/src/test_utils/rpc.rs +++ b/rust/kona/crates/node/engine/src/test_utils/rpc.rs @@ -8,6 +8,7 @@ use alloy_rpc_client::RpcClient; use alloy_rpc_types_engine::{ ForkchoiceState, ForkchoiceUpdated, PayloadStatus, PayloadStatusEnum, }; +use alloy_rpc_types_eth::{Block as EthBlock, Transaction as EthTransaction}; use alloy_transport::{ TransportError, TransportErrorKind, TransportFut, mock::{Asserter, MockResponse, MockTransport}, @@ -204,3 +205,18 @@ pub fn test_engine_client(cfg: Arc) -> (EngineClient, RpcMock, Rpc let client = EngineClient::new(l1.provider(), l2.provider(), cfg); (client, l1, l2) } + +/// Expects the sync start a reset performs: the execution layer answering `block` for its +/// finalized, safe and latest blocks, the L1 lookup of its origin, and `forkchoice` being sent. +pub fn expect_sync_start( + l1: &RpcMock, + l2: &RpcMock, + block: &TestRpcBlock, + forkchoice: ForkchoiceState, +) { + for label in ["finalized", "safe", "latest"] { + l2.expect_params("eth_getBlockByNumber", serde_json::json!([label, true]), block); + } + l1.expect("eth_getBlockByHash", EthBlock::::default()); + l2.expect_forkchoice_updated(forkchoice, PayloadStatusEnum::Valid); +} diff --git a/rust/kona/crates/node/service/src/actors/derivation/actor/core.rs b/rust/kona/crates/node/service/src/actors/derivation/actor/core.rs index e3a5ea1ba4d..2b602e11ff9 100644 --- a/rust/kona/crates/node/service/src/actors/derivation/actor/core.rs +++ b/rust/kona/crates/node/service/src/actors/derivation/actor/core.rs @@ -346,12 +346,7 @@ where // Attempt to finalize the block. If successful, notify engine. if let Some(l2_block_number) = self.finalizer.try_finalize_next(*finalized_l1_block) { - // Local L1-finality: the engine's own canonical chain is the authoritative - // source at this height, so finalize by number. - self.engine_client - .send_finalized_l2_block(FinalizeBlockId::ByNumber(l2_block_number)) - .await - .map_err(|e| DerivationError::Sender(Box::new(e)))?; + self.finalize_l2_block_by_number(l2_block_number).await?; } } DerivationActorRequest::ProcessL1HeadUpdateRequest(l1_head) => { @@ -359,6 +354,17 @@ where self.derivation_state_machine.update(&DerivationStateUpdate::L1DataReceived)?; + // A block that became safe after L1 finality last moved is finalized on the next + // L1 head update instead of waiting for L1 finality to move again. This runs on + // every L1 head while derivation is idle, unlike op-node, which retries at most + // once per 64 L1 blocks of progress. Outside `Deriving` the queue can still hold + // blocks from before an engine reset that this actor has not been signaled yet. + if self.derivation_state_machine.current_state() == DerivationState::Deriving && + let Some(l2_block_number) = self.finalizer.try_finalize_against_known_l1() + { + self.finalize_l2_block_by_number(l2_block_number).await?; + } + self.attempt_derivation().await?; } DerivationActorRequest::ProcessEngineSafeHeadUpdateRequest(safe_head) => { @@ -462,6 +468,21 @@ where Ok(()) } + /// Asks the engine to finalize the L2 block at `l2_block_number`, the highest derived block + /// whose L1 inputs are finalized. + async fn finalize_l2_block_by_number( + &self, + l2_block_number: u64, + ) -> Result<(), DerivationError> { + debug!(target: "derivation", l2_block_number, "Sending L2 block to finalize to the engine"); + // Local L1-finality: the engine's own canonical chain is the authoritative source at this + // height, so finalize by number. + self.engine_client + .send_finalized_l2_block(FinalizeBlockId::ByNumber(l2_block_number)) + .await + .map_err(|e| DerivationError::Sender(Box::new(e))) + } + /// Attempts to process the next payload attributes. async fn attempt_derivation(&mut self) -> Result<(), DerivationError> { if self.derivation_state_machine.current_state() != DerivationState::Deriving { diff --git a/rust/kona/crates/node/service/src/actors/derivation/actor/tests.rs b/rust/kona/crates/node/service/src/actors/derivation/actor/tests.rs index 5bd4f9cfc00..818294a343b 100644 --- a/rust/kona/crates/node/service/src/actors/derivation/actor/tests.rs +++ b/rust/kona/crates/node/service/src/actors/derivation/actor/tests.rs @@ -665,6 +665,84 @@ async fn a_block_is_queued_for_finalization_only_once_it_is_recorded() { node.finalize_l1(DERIVED_FROM_L1).await; } +/// A block that becomes safe after its L1 inputs were finalized, as when it is derived again after +/// a reset, is finalized on the next L1 head update without waiting for L1 finality to move. A +/// later update with nothing newly safe sends nothing. +#[tokio::test] +async fn a_block_safe_after_its_l1_finalized_is_finalized_on_the_next_l1_head() { + let mut engine_client = engine_client_accepting_attributes(); + engine_client + .expect_send_finalized_l2_block() + .withf(|block| matches!(block, FinalizeBlockId::ByNumber(DERIVED_HEAD))) + .times(1) + .returning(|_| Ok(())); + let mut node = RecordingActor::new(engine_client); + node.complete_el_sync_at(RESUME_HEAD).await; + node.finalize_l1(DERIVED_FROM_L1).await; + node.confirm_safe_head(DERIVED_HEAD).await; + + for l1_head in [DERIVED_FROM_L1 + 1, DERIVED_FROM_L1 + 2] { + node.handle(DerivationActorRequest::ProcessL1HeadUpdateRequest(Box::new(l1_block( + l1_head, + )))) + .await; + } +} + +/// A reset clears the finalization queue but keeps the finalized L1 block. A block sent for +/// finalization before the reset, which the engine may skip once the reset lowers its safe head, is +/// sent again on the next L1 head update after it is derived again. +#[tokio::test] +async fn a_block_derived_again_after_a_reset_is_finalized_on_the_next_l1_head() { + let mut engine_client = engine_client_accepting_attributes(); + engine_client + .expect_send_finalized_l2_block() + .withf(|block| matches!(block, FinalizeBlockId::ByNumber(DERIVED_HEAD))) + .times(2) + .returning(|_| Ok(())); + let mut node = RecordingActor::new(engine_client); + node.complete_el_sync_at(RESUME_HEAD).await; + node.confirm_safe_head(DERIVED_HEAD).await; + node.finalize_l1(DERIVED_FROM_L1).await; + node.reset_to_resume_head().await; + node.confirm_safe_head(DERIVED_HEAD).await; + + node.handle(DerivationActorRequest::ProcessL1HeadUpdateRequest(Box::new(l1_block( + DERIVED_FROM_L1 + 1, + )))) + .await; +} + +/// An L1 head update finalizes against the known L1 block only once derivation is idle: while +/// attributes await the engine's confirmation, the queued block is left for a later update. +#[tokio::test] +async fn an_l1_head_update_does_not_finalize_while_attributes_await_confirmation() { + let mut engine_client = engine_client_accepting_attributes(); + engine_client.expect_send_finalized_l2_block().times(0); + let mut node = RecordingActor::new(engine_client); + node.actor.pipeline.attributes.push_back(OpAttributesWithParent { + parent: l2_block(DERIVED_HEAD), + ..derived_attributes() + }); + node.complete_el_sync_at(RESUME_HEAD).await; + node.finalize_l1(DERIVED_FROM_L1).await; + node.confirm_safe_head(DERIVED_HEAD).await; + assert_eq!( + node.actor.derivation_state_machine.current_state(), + DerivationState::AwaitingSafeHeadConfirmation + ); + + node.handle(DerivationActorRequest::ProcessL1HeadUpdateRequest(Box::new(l1_block( + DERIVED_FROM_L1 + 1, + )))) + .await; + + assert_eq!( + node.actor.derivation_state_machine.current_state(), + DerivationState::AwaitingSafeHeadConfirmation + ); +} + mod database; mod op_node_parity; diff --git a/rust/kona/crates/node/service/src/actors/derivation/delegated/actor.rs b/rust/kona/crates/node/service/src/actors/derivation/delegated/actor.rs index a353a001600..480019925a0 100644 --- a/rust/kona/crates/node/service/src/actors/derivation/delegated/actor.rs +++ b/rust/kona/crates/node/service/src/actors/derivation/delegated/actor.rs @@ -16,6 +16,9 @@ use tokio::{ time, }; +/// How often the derivation delegate is polled for its sync status. +pub(super) const DERIVATION_DELEGATE_POLL_INTERVAL: time::Duration = time::Duration::from_secs(4); + /// Supplies the sync status of the external OP Stack CL node that derivation is delegated to. #[async_trait] pub trait SyncStatusSource: Send + Sync { @@ -92,8 +95,7 @@ where derivation_origin_tx: watch::Sender>, engine_state_rx: watch::Receiver, ) -> Self { - let mut delegated_derivation_ticker = - time::interval(Self::DERIVATION_DELEGATE_POLL_INTERVAL); + let mut delegated_derivation_ticker = time::interval(DERIVATION_DELEGATE_POLL_INTERVAL); delegated_derivation_ticker.set_missed_tick_behavior(time::MissedTickBehavior::Skip); Self { inbound_request_rx, @@ -148,10 +150,6 @@ where Delegate: SyncStatusSource + 'static, L1Provider: ChainProvider + 'static, { - /// Hardcoded poll interval for Derivation Delegation - const DERIVATION_DELEGATE_POLL_INTERVAL: std::time::Duration = - std::time::Duration::from_secs(4); - /// Validates a single L1 block height and hash against the canonical L1 chain. async fn validate_l1_block( &mut self, diff --git a/rust/kona/crates/node/service/src/actors/derivation/delegated/tests.rs b/rust/kona/crates/node/service/src/actors/derivation/delegated/tests.rs index 5e7038eb6c0..fea52b068d1 100644 --- a/rust/kona/crates/node/service/src/actors/derivation/delegated/tests.rs +++ b/rust/kona/crates/node/service/src/actors/derivation/delegated/tests.rs @@ -1,4 +1,4 @@ -use super::{DelegateDerivationActor, SyncStatusSource}; +use super::{DelegateDerivationActor, SyncStatusSource, actor::DERIVATION_DELEGATE_POLL_INTERVAL}; use crate::{DerivationActorRequest, EngineActorRequest, NodeActor, QueuedDerivationEngineClient}; use alloy_eips::BlockNumHash; use alloy_primitives::B256; @@ -9,14 +9,17 @@ use kona_engine::{ }; use kona_protocol::{BlockInfo, L2BlockInfo, SyncStatus}; use rstest::rstest; -use std::{collections::VecDeque, convert::Infallible, sync::Mutex}; -use tokio::sync::{mpsc, watch}; +use std::{collections::VecDeque, sync::Mutex}; +use tokio::{ + sync::{mpsc, watch}, + time::{self, Instant}, +}; const SAFE_ORIGIN: u64 = 1; const FINALIZED_ORIGIN: u64 = 2; const CURRENT_L1: u64 = 3; -type SyncStatusReply = Result; +type SyncStatusReply = Result; /// Answers each sync status poll with the next scripted reply. #[derive(Debug)] @@ -24,7 +27,7 @@ struct ScriptedDelegate(Mutex>); #[async_trait] impl SyncStatusSource for ScriptedDelegate { - type Error = Infallible; + type Error = &'static str; async fn sync_status(&self) -> SyncStatusReply { self.0.lock().unwrap().pop_front().expect("unexpected sync status poll") @@ -253,6 +256,87 @@ async fn poll_accepts_status_after_engine_skipped_forwarded_finality( assert_eq!(harness.forwarded(), forwarded_for(&canonical_status)); } +#[derive(Clone, Copy, Debug)] +enum DelegateReply { + CurrentL1NotCanonical, + FetchFailed, + FinalizedOriginNotCanonical, + SafeOriginNotCanonical, + Valid, +} + +impl DelegateReply { + fn sync_status(self) -> SyncStatusReply { + let mut status = delegate_status(); + match self { + Self::CurrentL1NotCanonical => { + status.current_l1.hash = non_canonical(status.current_l1.id()).hash + } + Self::FetchFailed => { + return Err("unreachable"); + } + Self::FinalizedOriginNotCanonical => { + status.finalized_l2.l1_origin = non_canonical(status.finalized_l2.l1_origin) + } + Self::SafeOriginNotCanonical => { + status.safe_l2.l1_origin = non_canonical(status.safe_l2.l1_origin) + } + Self::Valid => {} + } + Ok(status) + } +} + +/// Polling starts only once the engine has finished syncing. +#[tokio::test(start_paused = true)] +async fn polls_wait_for_engine_sync_completion() { + let mut harness = Harness::new([]); + + assert!( + time::timeout(DERIVATION_DELEGATE_POLL_INTERVAL * 3, harness.actor.step()).await.is_err() + ); + + assert_eq!(harness.forwarded(), Vec::new()); + assert_eq!(*harness.current_l1.borrow(), None); +} + +/// A valid status forwards the safe head and then the hash-pinned finalized block, and publishes +/// the delegate's `current_l1`. A failed fetch, or any L1 reference that is not canonical, skips +/// the poll without forwarding or publishing anything and without stopping the actor. +#[rstest] +#[case::valid(DelegateReply::Valid, true)] +#[case::fetch_failed(DelegateReply::FetchFailed, false)] +#[case::safe_origin_not_canonical(DelegateReply::SafeOriginNotCanonical, false)] +#[case::finalized_origin_not_canonical(DelegateReply::FinalizedOriginNotCanonical, false)] +#[case::current_l1_not_canonical(DelegateReply::CurrentL1NotCanonical, false)] +#[tokio::test(start_paused = true)] +async fn poll_forwards_only_canonical_status(#[case] reply: DelegateReply, #[case] accepted: bool) { + let mut harness = Harness::new([reply.sync_status()]); + harness.complete_engine_sync().await; + + harness.actor.step().await.unwrap(); + + assert_eq!(harness.forwarded(), forwarded_if(accepted, &delegate_status())); + assert_eq!(*harness.current_l1.borrow(), accepted.then(|| delegate_status().current_l1)); +} + +/// An unchanged status is forwarded again on every poll, one poll interval apart, so a target +/// that could not be applied on one poll is offered again on the next. +#[tokio::test(start_paused = true)] +async fn unchanged_status_is_resent_on_every_poll() { + const POLLS: u32 = 3; + let mut harness = Harness::new((0..POLLS).map(|_| DelegateReply::Valid.sync_status())); + harness.complete_engine_sync().await; + let first_poll = Instant::now(); + + for _ in 0..POLLS { + harness.actor.step().await.unwrap(); + assert_eq!(harness.forwarded(), forwarded_for(&delegate_status())); + } + + assert_eq!(Instant::now() - first_poll, DERIVATION_DELEGATE_POLL_INTERVAL * (POLLS - 1)); +} + /// Before the engine establishes finality, any finalized block from the delegate may initialize /// it, including one at genesis height. #[tokio::test(start_paused = true)] diff --git a/rust/kona/crates/node/service/src/actors/derivation/finalizer.rs b/rust/kona/crates/node/service/src/actors/derivation/finalizer.rs index 5fbbb46d8d7..8af4e2645cc 100644 --- a/rust/kona/crates/node/service/src/actors/derivation/finalizer.rs +++ b/rust/kona/crates/node/service/src/actors/derivation/finalizer.rs @@ -12,8 +12,9 @@ type L2BlockNumber = u64; /// The [`L2Finalizer`] is responsible for tracking L2 blocks derived from L1 blocks and /// determining which L2 blocks can be finalized when L1 blocks are finalized. /// -/// It maintains a queue of derived L2 blocks that are awaiting finalization, and returns -/// the L2 block numbers that can be finalized as new finalized L1 blocks are received. +/// It maintains a queue of derived L2 blocks that are awaiting finalization, and returns the +/// L2 block numbers that can be finalized when a new finalized L1 block is received, or when it is +/// asked again against the highest finalized L1 block it already knows. #[derive(Debug, Default)] pub(crate) struct L2Finalizer { /// A map of `L1 block number -> highest derived L2 block number` within the L1 epoch, used to @@ -21,6 +22,8 @@ pub(crate) struct L2Finalizer { /// block is received, the highest L2 block whose inputs are contained within the finalized /// L1 chain is finalized. awaiting_finalization: BTreeMap, + /// The number of the highest finalized L1 block received. + finalized_l1_number: Option, } impl L2Finalizer { @@ -38,12 +41,14 @@ impl L2Finalizer { .or_insert_with(|| attributes.block_number()); } - /// Clears the finalization queue. + /// Clears the finalization queue. The finalized L1 number is kept, since an L2 reset does not + /// change L1 finality. pub(crate) fn clear(&mut self) { self.awaiting_finalization.clear(); } - /// Attempts to find L2 blocks that can be finalized based on the new finalized L1 block. + /// Records `new_finalized_l1_block` as the highest finalized L1 block, unless a higher one is + /// already known, and attempts to finalize against the highest one. /// /// Returns `Some(l2_block_number)` if there is an L2 block that can be finalized, /// or `None` if no blocks are ready for finalization. @@ -51,19 +56,110 @@ impl L2Finalizer { &mut self, new_finalized_l1_block: BlockInfo, ) -> Option { + match self.finalized_l1_number { + Some(known) if new_finalized_l1_block.number < known => warn!( + target: "derivation", + known, + received = new_finalized_l1_block.number, + "Finalized L1 block moved backwards, ignoring it. Is the L1 provider corrupted?" + ), + _ => self.finalized_l1_number = Some(new_finalized_l1_block.number), + } + self.try_finalize_against_known_l1() + } + + /// Attempts to finalize L2 blocks that became safe after the highest finalized L1 block was + /// received, such as blocks derived again after a reset, without waiting for L1 finality to + /// move. + /// + /// The entries up to the finalized L1 block are drained with the returned block: a block whose + /// finalization does not go through has to be queued again to be returned again. + pub(crate) fn try_finalize_against_known_l1(&mut self) -> Option { + let finalized_l1_number = self.finalized_l1_number?; + // Find the highest safe L2 block that is contained within the finalized chain, // that the finalizer is aware of. - let highest_safe = - self.awaiting_finalization.range(..=new_finalized_l1_block.number).next_back(); - - // If the highest safe block is found, return it and drain the - // queue of all L1 blocks not contained in the finalized L1 chain. - if let Some((_, highest_safe_number)) = highest_safe { - let result = *highest_safe_number; - self.awaiting_finalization.retain(|&number, _| number > new_finalized_l1_block.number); - Some(result) - } else { - None + let (_, &highest_safe_number) = + self.awaiting_finalization.range(..=finalized_l1_number).next_back()?; + + // Drain the queue of all L1 blocks contained in the finalized L1 chain. + self.awaiting_finalization.retain(|&number, _| number > finalized_l1_number); + Some(highest_safe_number) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use kona_protocol::L2BlockInfo; + use op_alloy_rpc_types_engine::OpPayloadAttributes; + use rstest::rstest; + + const DERIVING_L1: u64 = 5; + const DERIVED_L2: u64 = 100; + + fn l1_block(number: u64) -> BlockInfo { + BlockInfo { number, ..Default::default() } + } + + /// Attributes for L2 block [`DERIVED_L2`], derived from L1 block [`DERIVING_L1`]. + fn derived_attributes() -> OpAttributesWithParent { + let mut parent = L2BlockInfo::default(); + parent.block_info.number = DERIVED_L2 - 1; + OpAttributesWithParent { + attributes: OpPayloadAttributes::default(), + derived_from: Some(l1_block(DERIVING_L1)), + is_last_in_span: true, + parent, } } + + /// A queued block is finalized once L1 finality reaches the block it was derived from. + #[rstest] + #[case::before_its_l1_block(DERIVING_L1 - 1, None)] + #[case::at_its_l1_block(DERIVING_L1, Some(DERIVED_L2))] + #[case::past_its_l1_block(DERIVING_L1 + 1, Some(DERIVED_L2))] + fn a_queued_block_finalizes_with_the_l1_block_it_was_derived_from( + #[case] finalized_l1: u64, + #[case] finalized_l2: Option, + ) { + let mut finalizer = L2Finalizer::default(); + finalizer.enqueue_for_finalization(&derived_attributes()); + + assert_eq!(finalizer.try_finalize_next(l1_block(finalized_l1)), finalized_l2); + } + + /// A finalized L1 block below the highest one received does not lower it. + #[test] + fn a_lower_finalized_l1_block_keeps_the_highest_one() { + let mut finalizer = L2Finalizer::default(); + assert_eq!(finalizer.try_finalize_next(l1_block(DERIVING_L1)), None); + finalizer.enqueue_for_finalization(&derived_attributes()); + + assert_eq!(finalizer.try_finalize_next(l1_block(DERIVING_L1 - 1)), Some(DERIVED_L2)); + } + + /// Clearing the queue keeps the finalized L1 block, so a block queued again afterwards is + /// finalized without new L1 finality. + #[test] + fn a_block_queued_after_a_clear_finalizes_against_the_kept_l1_block() { + let mut finalizer = L2Finalizer::default(); + finalizer.enqueue_for_finalization(&derived_attributes()); + assert_eq!(finalizer.try_finalize_next(l1_block(DERIVING_L1)), Some(DERIVED_L2)); + finalizer.clear(); + finalizer.enqueue_for_finalization(&derived_attributes()); + + assert_eq!(finalizer.try_finalize_against_known_l1(), Some(DERIVED_L2)); + } + + /// Nothing is finalized before any L1 finality is known, or once the queue is drained. + #[test] + fn nothing_finalizes_without_l1_finality_or_a_queued_block() { + let mut finalizer = L2Finalizer::default(); + finalizer.enqueue_for_finalization(&derived_attributes()); + assert_eq!(finalizer.try_finalize_against_known_l1(), None); + assert_eq!(finalizer.try_finalize_next(l1_block(DERIVING_L1)), Some(DERIVED_L2)); + + assert_eq!(finalizer.try_finalize_against_known_l1(), None); + } } diff --git a/rust/kona/crates/node/service/src/actors/engine/queue_tests.rs b/rust/kona/crates/node/service/src/actors/engine/queue_tests.rs index 1061c17fd31..cd3c695f383 100644 --- a/rust/kona/crates/node/service/src/actors/engine/queue_tests.rs +++ b/rust/kona/crates/node/service/src/actors/engine/queue_tests.rs @@ -1,13 +1,18 @@ use super::*; use crate::QueuedEngineDerivationClient; +use alloy_primitives::B256; use alloy_rpc_types_engine::{ - ExecutionPayloadV1, ForkchoiceUpdated, PayloadStatus, PayloadStatusEnum, + ExecutionPayloadV1, ForkchoiceState, PayloadStatus, PayloadStatusEnum, }; use kona_engine::{ - ConsolidateInput, EngineSyncStateUpdate, FinalizeBlockId, NoopBlockSink, - test_utils::{TestEngineStateBuilder, l1_info_deposit, test_block_info, test_engine_client}, + ConsolidateInput, EngineState, EngineSyncStateUpdate, FinalizeBlockId, NoopBlockSink, + ResetRequest, + test_utils::{ + RpcMock, TestEngineStateBuilder, TestRpcBlock, expect_sync_start, test_block_info, + test_engine_client, test_l2_block, test_rpc_block, + }, }; -use op_alloy_consensus::OpBlock; +use op_alloy_consensus::{OpBlock, OpTxEnvelope}; use op_alloy_rpc_types_engine::OpExecutionPayloadEnvelope; /// Exercise the real actor request/drain loop with forty consecutive unsafe blocks, without @@ -33,11 +38,7 @@ async fn healthy_unsafe_imports_keep_engine_queue_bounded() { ); let mut expected_heads = vec![initial_head]; for number in 1..=40 { - let mut block = OpBlock::default(); - block.header.number = number; - block.header.timestamp = number * 2; - block.header.parent_hash = expected_heads.last().unwrap().hash(); - block.body.transactions.push(l1_info_deposit()); + let block = test_l2_block(number, expected_heads.last().unwrap().hash(), 0); let payload = ExecutionPayloadV1::from_block_slow(&block); let imported: OpBlock = OpExecutionPayloadEnvelope::V1(payload.clone()).try_into_block().unwrap(); @@ -50,17 +51,9 @@ async fn healthy_unsafe_imports_keep_engine_queue_bounded() { serde_json::json!([payload]), PayloadStatus::from_status(PayloadStatusEnum::Valid), ); - l2.expect_params( - "engine_forkchoiceUpdatedV3", - serde_json::json!([ - alloy_rpc_types_engine::ForkchoiceState { - head_block_hash: expected_heads.last().unwrap().hash(), - safe_block_hash: initial_head.hash(), - finalized_block_hash: initial_head.hash(), - }, - null - ]), - ForkchoiceUpdated::new(PayloadStatus::from_status(PayloadStatusEnum::Valid)), + l2.expect_forkchoice_updated( + forkchoice(*expected_heads.last().unwrap(), initial_head, initial_head), + PayloadStatusEnum::Valid, ); requests .send(EngineActorRequest::ProcessUnsafeL2Block(Box::new( @@ -141,6 +134,218 @@ async fn delegated_safe_head_is_sent_on_every_poll() { l2.assert_finished(); } +/// A delegate poll while the execution layer syncs toward the unsafe head: the delegate's safe +/// head is below the unsafe head but not in the execution layer yet, and neither is its finalized +/// block. The consolidate moves the safe labels and the finalize is skipped, so the actor goes on. +#[tokio::test(start_paused = true)] +async fn delegated_poll_during_el_sync_moves_safe_labels_and_skips_finalization() { + let config = Arc::new(RollupConfig::default()); + let (client, l1, l2) = test_engine_client(config.clone()); + let state = TestEngineStateBuilder::new() + .with_unsafe_head(test_block_info(20)) + .with_safe_head(test_block_info(5)) + .with_finalized_head(test_block_info(3)) + .build(); + let delegated_safe_head = test_block_info(10); + let delegated_finalized_head = test_block_info(8); + let expected_sync_state = state.sync_state.apply_update(EngineSyncStateUpdate { + local_safe_head: Some(delegated_safe_head), + safe_head: Some(delegated_safe_head), + ..Default::default() + }); + l2.expect_block_by_number(delegated_safe_head.block_info.number, None); + l2.expect_forkchoice_updated( + expected_sync_state.create_forkchoice_state(), + PayloadStatusEnum::Syncing, + ); + l2.expect_block_by_number(delegated_finalized_head.block_info.number, None); + let (state_tx, state_rx) = watch::channel(state); + let (requests, request_rx) = mpsc::channel(8); + let (derivation_tx, _derivation_rx) = mpsc::channel(8); + let mut actor = EngineActor::new( + Arc::new(client), + config, + QueuedEngineDerivationClient { derivation_actor_request_tx: derivation_tx }, + Engine::new(state, state_tx), + None, + request_rx, + Arc::new(NoopBlockSink), + ); + + // One poll: the safe head, then the finalized block pinned by hash. Each step drains the + // previous request's task and then accepts the next request. + requests + .send(EngineActorRequest::ProcessSafeL2Signal(ConsolidateInput::BlockInfo( + delegated_safe_head, + ))) + .await + .unwrap(); + requests + .send(EngineActorRequest::ProcessFinalizedL2Block(Box::new(FinalizeBlockId::ByHash( + delegated_finalized_head.block_info.id(), + )))) + .await + .unwrap(); + for _ in 0..2 { + time::timeout(Duration::from_secs(1), actor.step()).await.unwrap().unwrap(); + } + drop(requests); + assert!(matches!(actor.step().await, Err(EngineError::ChannelClosed))); + + assert!(actor.engine.is_empty()); + assert!(!actor.reset_pending); + assert_eq!(state_rx.borrow().sync_state, expected_sync_state); + l1.assert_finished(); + l2.assert_finished(); +} + +fn forkchoice(head: L2BlockInfo, safe: L2BlockInfo, finalized: L2BlockInfo) -> ForkchoiceState { + ForkchoiceState { + finalized_block_hash: finalized.hash(), + head_block_hash: head.hash(), + safe_block_hash: safe.hash(), + } +} + +/// The reset finds the EL's finalized, safe and unsafe heads all at genesis, then synchronizes +/// the EL to them. +fn expect_reset_to_genesis( + l1: &RpcMock, + l2: &RpcMock, + genesis_block: &TestRpcBlock, + genesis: L2BlockInfo, +) { + expect_sync_start(l1, l2, genesis_block, forkchoice(genesis, genesis, genesis)); +} + +fn reset_request() -> (EngineActorRequest, mpsc::Receiver>) { + let (result_tx, result_rx) = mpsc::channel(1); + (EngineActorRequest::Reset(Box::new(ResetRequest { result_tx })), result_rx) +} + +/// How the finalization target is identified: delegated derivation pins the hash, and +/// finalization from L1 finality uses the block number. +#[derive(Clone, Copy, Debug)] +enum TargetKind { + ByHash, + ByNumber, +} + +impl TargetKind { + fn finalize_request(self, target: L2BlockInfo) -> EngineActorRequest { + let block_id = match self { + Self::ByHash => FinalizeBlockId::ByHash(target.block_info.id()), + Self::ByNumber => FinalizeBlockId::ByNumber(target.block_info.number), + }; + EngineActorRequest::ProcessFinalizedL2Block(Box::new(block_id)) + } +} + +/// A reset can restore a safe head below a finalization target that was safe when it was +/// requested. The target must wait across repeated resets without stopping the node, holding the +/// queue, or triggering another reset, and must finalize once the safe head reaches it. +#[rstest::rstest] +#[case::by_hash(TargetKind::ByHash)] +#[case::by_number(TargetKind::ByNumber)] +#[tokio::test(start_paused = true)] +async fn finalization_target_survives_resets_below_it(#[case] target_kind: TargetKind) { + let genesis_block = TestRpcBlock::default(); + let mut config = RollupConfig::default(); + config.genesis.l2.hash = genesis_block.header.inner.hash_slow(); + let config = Arc::new(config); + let genesis = L2BlockInfo::from_block_and_genesis( + &genesis_block.clone().into_consensus(), + &config.genesis, + ) + .unwrap(); + let target_block = test_l2_block(15, B256::with_last_byte(14), 0); + let target = L2BlockInfo::from_block_and_genesis(&target_block, &config.genesis).unwrap(); + let restored_safe = test_block_info(20); + let unsafe_payload = OpExecutionPayloadEnvelope::V1(ExecutionPayloadV1::from_block_slow( + &test_l2_block(1, genesis.hash(), 0), + )); + let unsafe_head = L2BlockInfo::from_block_and_genesis( + &unsafe_payload.clone().try_into_block::().unwrap(), + &config.genesis, + ) + .unwrap(); + + let (client, l1, l2) = test_engine_client(config.clone()); + let (requests, request_rx) = mpsc::channel(8); + let (derivation_tx, _derivation_rx) = mpsc::channel(32); + let (state_tx, state_rx) = watch::channel(EngineState::default()); + let mut actor = EngineActor::new( + Arc::new(client), + config.clone(), + QueuedEngineDerivationClient { derivation_actor_request_tx: derivation_tx }, + Engine::new(EngineState::default(), state_tx), + None, + request_rx, + Arc::new(NoopBlockSink), + ); + + // Startup reset, with the finalization target arriving right behind it. + let (reset, mut first_reset_result) = reset_request(); + requests.send(reset).await.unwrap(); + actor.step().await.unwrap(); + expect_reset_to_genesis(&l1, &l2, &genesis_block, genesis); + requests.send(target_kind.finalize_request(target)).await.unwrap(); + actor.step().await.unwrap(); + assert!(first_reset_result.try_recv().unwrap().is_ok()); + assert_eq!(state_rx.borrow().sync_state.safe_head(), genesis); + + // The target is above the reset safe head: skipped without a lookup or forkchoice update, + // and dropped from the queue rather than retried, so the unsafe import received in the same + // step is the only task left. + requests + .send(EngineActorRequest::ProcessUnsafeL2Block(Box::new(unsafe_payload))) + .await + .unwrap(); + actor.step().await.unwrap(); + assert_eq!(actor.engine.len(), 1, "only the unsafe import may remain queued"); + assert!(!actor.reset_pending); + l2.expect("engine_newPayloadV2", PayloadStatus::from_status(PayloadStatusEnum::Valid)); + l2.expect_forkchoice_updated( + forkchoice(unsafe_head, genesis, genesis), + PayloadStatusEnum::Valid, + ); + let (reset, mut second_reset_result) = reset_request(); + requests.send(reset).await.unwrap(); + actor.step().await.unwrap(); + assert_eq!(state_rx.borrow().sync_state.unsafe_head(), unsafe_head); + + // A second reset lowers the heads again, and the same target is requested again. + expect_reset_to_genesis(&l1, &l2, &genesis_block, genesis); + requests.send(target_kind.finalize_request(target)).await.unwrap(); + actor.step().await.unwrap(); + assert!(second_reset_result.try_recv().unwrap().is_ok()); + requests.send(EngineActorRequest::ProcessSafeL2Signal(restored_safe.into())).await.unwrap(); + actor.step().await.unwrap(); + assert_eq!(actor.engine.len(), 1, "only the safe head update may remain queued"); + assert!(!actor.reset_pending); + assert_eq!(state_rx.borrow().sync_state.finalized_head(), genesis); + + // Once the safe head reaches the target, the repeated request finalizes it. + l2.expect_forkchoice_updated( + forkchoice(restored_safe, restored_safe, genesis), + PayloadStatusEnum::Valid, + ); + requests.send(target_kind.finalize_request(target)).await.unwrap(); + actor.step().await.unwrap(); + assert_eq!(state_rx.borrow().sync_state.safe_head(), restored_safe); + l2.expect_block_by_number(15, Some(test_rpc_block(&target_block))); + l2.expect_forkchoice_updated( + forkchoice(restored_safe, restored_safe, target), + PayloadStatusEnum::Valid, + ); + // With nothing left to do, the actor waits for requests instead of retrying. + assert!(time::timeout(Duration::from_secs(60), actor.step()).await.is_err()); + assert_eq!(state_rx.borrow().sync_state.finalized_head(), target); + assert!(actor.engine.is_empty()); + l1.assert_finished(); + l2.assert_finished(); +} + /// A finalization the engine skips leaves the actor running, an identical repeat keeps the same /// skip in memory, and a reset forgets it. #[tokio::test(start_paused = true)]