Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions rust/kona/crates/node/engine/src/request.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
//! Requests sent to the node engine actor and their replies.
use crate::{
BuildTaskError, CanonicalizeTaskError, ConsolidateInput, FinalizeBlockId, SealTaskError,
};
use crate::{BuildTaskError, CanonicalizeTaskError, FinalizeBlockId, SealTaskError};
use alloy_rpc_types_engine::PayloadId;
use kona_protocol::OpAttributesWithParent;
use kona_protocol::{L2BlockInfo, OpAttributesWithParent};
use op_alloy_rpc_types_engine::OpExecutionPayloadEnvelope;
use thiserror::Error;
use tokio::sync::mpsc;
Expand All @@ -14,8 +12,24 @@ use tokio::sync::mpsc;
pub enum EngineActorRequest {
/// Request to start building a block.
Build(Box<BuildRequest>),
/// Request to process derived attributes or delegated safe block information.
ProcessSafeL2Signal(ConsolidateInput),
/// Request to consolidate attributes from the derivation pipeline.
ProcessDerivedAttributes {
/// The derived attributes.
attributes: Box<OpAttributesWithParent>,
/// How many engine resets derivation had handled when it derived the attributes. The
/// engine drops attributes derived before its latest reset.
reset_epoch: u64,
},
/// Request to finalize an L2 block that derivation recorded as safe.
ProcessDerivedFinalization {
/// The L2 block number to finalize.
number: u64,
/// How many engine resets derivation had handled when it recorded the block. The engine
/// drops finalizations recorded before its latest reset.
reset_epoch: u64,
},
/// Request to adopt a safe block reported by a derivation delegate.
ProcessDelegatedSafeHead(Box<L2BlockInfo>),
/// Request to process the finalized L2 block.
ProcessFinalizedL2Block(Box<FinalizeBlockId>),
/// Request to process a received unsafe L2 block.
Expand Down
57 changes: 57 additions & 0 deletions rust/kona/crates/node/engine/src/task_queue/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ impl Engine {
self.active = None;
}

/// Drops every queued [`EngineTask::Consolidate`] and [`EngineTask::Finalize`]. Call it when a
/// reset succeeds: derivation resets too and re-derives from the reset head, so attributes it
/// derived and blocks it asked to finalize before the reset are stale, even when they still
/// extend that head. In delegated mode this also drops the delegate's safe and finalized
/// heads, which it sends again on its next poll.
pub fn drop_derived_tasks(&mut self) {
self.tasks.retain(|queued| {
!matches!(queued.task, EngineTask::Consolidate(_) | EngineTask::Finalize(_))
});
}

/// Attempts to drain the queue by executing all [`EngineTask`]s in-order. If any task returns
/// an error along the way, the error is returned and the task is not popped from the queue (in
/// case it must be retried), unless it is a flush or skip error.
Expand Down Expand Up @@ -442,4 +453,50 @@ mod recovery_tests {
l1.assert_finished();
l2.assert_finished();
}

// Attributes derived and finalizations requested before a reset must not run after it. Other
// queued work, such as unsafe payloads received meanwhile, survives.
#[test]
fn drop_derived_tasks_keeps_other_tasks() {
let config = Arc::new(RollupConfig::default());
let (client, _l1, _l2) = test_engine_client(config.clone());
let client = Arc::new(client);
let consolidate = || {
EngineTask::Consolidate(Box::new(ConsolidateTask::new(
client.clone(),
config.clone(),
ConsolidateInput::Attributes(Box::new(
TestAttributesBuilder::new().with_parent(test_block_info(10)).build(),
)),
Arc::new(NoopBlockSink),
)))
};
let insert = EngineTask::Insert(Box::new(InsertTask::new(
client.clone(),
config.clone(),
OpExecutionPayloadEnvelope::V1(ExecutionPayloadV1::from_block_slow(
&alloy_consensus::Block::<op_alloy_consensus::OpTxEnvelope>::default(),
)),
false,
Arc::new(NoopBlockSink),
)));
let (state_tx, _state_rx) = tokio::sync::watch::channel(EngineState::default());
let mut engine = Engine::new(EngineState::default(), state_tx);
engine.enqueue(consolidate());
engine.enqueue(insert);
engine.enqueue(EngineTask::Finalize(Box::new(crate::FinalizeTask::new(
client.clone(),
config.clone(),
crate::FinalizeBlockId::ByNumber(10),
))));
engine.enqueue(consolidate());
assert_eq!(engine.len(), 4);

engine.drop_derived_tasks();
assert_eq!(engine.len(), 1);
assert!(matches!(
engine.tasks.peek().map(|queued| &queued.task),
Some(EngineTask::Insert(_))
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ pub enum ConsolidateTaskError {
/// The consolidation forkchoice update call to the engine api failed.
#[error(transparent)]
ForkchoiceUpdateFailed(#[from] SynchronizeTaskError),
/// Derived attributes do not extend the local-safe head, so derivation and the engine
/// disagree about the safe chain.
#[error(
"Derived attributes with parent {parent:?} do not extend the local-safe head {local_safe:?}"
)]
DoesNotExtendLocalSafe {
/// The attributes' parent.
parent: alloy_eips::BlockNumHash,
/// The engine's local-safe head.
local_safe: alloy_eips::BlockNumHash,
},
}

impl From<BuildAndImportError> for ConsolidateTaskError {
Expand All @@ -46,7 +57,9 @@ impl EngineTaskError for ConsolidateTaskError {
fn severity(&self) -> EngineTaskErrorSeverity {
match self {
Self::BreaksFinality(_) => EngineTaskErrorSeverity::Skip,
Self::MissingUnsafeL2Block(_) => EngineTaskErrorSeverity::Reset,
Self::MissingUnsafeL2Block(_) | Self::DoesNotExtendLocalSafe { .. } => {
EngineTaskErrorSeverity::Reset
}
Self::FailedToFetchDelegatedSafeBlock(_) | Self::FailedToFetchUnsafeL2Block => {
EngineTaskErrorSeverity::Temporary
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,19 @@ impl ExecutableEngineTask for ConsolidateTask {
// injected safe head is ahead of the EngineActor's unsafe head, we reconcile the unsafe chain
// up to the safe head instead of consolidating.
async fn execute(&self, state: &mut EngineState) -> Result<(), ConsolidateTaskError> {
if let ConsolidateInput::Attributes(attributes) = &self.input {
let parent = attributes.parent.block_info;
let local_safe = state.sync_state.local_safe_head().block_info;
if parent.id() != local_safe.id() {
// The engine drops attributes derived before its latest reset, so these are
// current: derivation and the engine disagree about the safe chain. Consolidating
// them would mark blocks safe that were never derived on it, so reset instead.
return Err(ConsolidateTaskError::DoesNotExtendLocalSafe {
parent: parent.id(),
local_safe: local_safe.id(),
});
}
}
if let ConsolidateInput::BlockInfo(safe_l2) = &self.input {
state.sync_state.ensure_respects_finality(safe_l2.block_info.id())?;
// Labels with the unsafe head below the safe head take the paths below, which move the
Expand Down Expand Up @@ -717,6 +730,8 @@ mod tests {
let cfg = Arc::new(RollupConfig::default());
let attributes = derived_attributes(true);
let mut state = engine_state_with_safe_head(attributes.parent);
// Derived attributes extend the local-safe head.
state.sync_state = with_safe_head(&state, attributes.parent);
let state_before = state;
let (client, l1, l2) = test_engine_client(cfg.clone());
l2.expect_params(
Expand Down Expand Up @@ -784,6 +799,8 @@ mod tests {
let block = unsafe_block_matching(&attributes);
let derived_safe_head = block_info_of(&cfg, &block);
let mut state = engine_state_with_safe_head(attributes.parent);
// Derived attributes extend the local-safe head.
state.sync_state = with_safe_head(&state, attributes.parent);
let expected_sync_state = with_safe_head(&state, derived_safe_head);
let (client, l1, l2) = test_engine_client(cfg.clone());
l2.expect_params("eth_getBlockByNumber", serde_json::json!(["0xa", true]), block);
Expand Down Expand Up @@ -1128,4 +1145,47 @@ mod tests {
l1.assert_finished();
l2.assert_finished();
}

// Derived attributes whose parent is not the local-safe head mean derivation and the engine
// disagree about the safe chain. The task asks for a reset, with no engine call, instead of
// marking blocks safe that were never derived or moving the safe head backwards.
#[rstest::rstest]
#[case::parent_above_local_safe(-1, false)]
#[case::parent_below_local_safe(1, false)]
#[case::parent_on_another_chain(0, true)]
#[tokio::test]
async fn attributes_off_the_local_safe_chain_reset(
#[case] local_safe_offset: i64,
#[case] other_hash: bool,
) {
use crate::ExecutableEngineTask;
let attributes = derived(vec![alloy_primitives::Bytes::from(vec![DEPOSIT, 0x01])]);
// The local-safe head differs from the attributes' parent in only one way: its number, or
// its hash at the same number.
let mut head = attributes.parent;
head.block_info.number =
head.block_info.number.checked_add_signed(local_safe_offset).unwrap();
if other_hash {
head.block_info.hash = B256::repeat_byte(0xab);
}
let mut state =
crate::test_utils::TestEngineStateBuilder::new().with_unsafe_head(head).build();
let before = state.sync_state;
let cfg = holocene_cfg();
let (client, l1, l2) = test_engine_client(cfg.clone());
let task = ConsolidateTask::new(
Arc::new(client),
cfg,
ConsolidateInput::Attributes(Box::new(attributes)),
Arc::new(crate::NoopBlockSink),
);

let err = task.execute(&mut state).await.unwrap_err();

assert!(matches!(err, ConsolidateTaskError::DoesNotExtendLocalSafe { .. }), "{err:?}");
assert_eq!(err.severity(), crate::EngineTaskErrorSeverity::Reset);
assert_eq!(state.sync_state, before);
l1.assert_finished();
l2.assert_finished();
}
}
61 changes: 33 additions & 28 deletions rust/kona/crates/node/service/src/actors/derivation/actor/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use kona_derive::{
ActivationSignal, Pipeline, PipelineError, PipelineErrorKind, ResetError, ResetSignal, Signal,
SignalReceiver, StepResult,
};
use kona_engine::FinalizeBlockId;
use kona_protocol::{BlockInfo, L2BlockInfo, OpAttributesWithParent};
use kona_safedb::{SafeDb, SafeDbError};
use std::{sync::Arc, time::Duration};
Expand Down Expand Up @@ -53,6 +52,9 @@ where
pub(super) attributes_awaiting_confirmation: Option<OpAttributesWithParent>,
/// A pipeline reset that has not yet succeeded. Derivation must not resume before it does.
pending_reset: Option<PendingReset>,
/// How many engine resets this actor has handled. Attributes and finalizations carry it so
/// that the engine can drop those derived before its latest reset.
reset_epoch: u64,
}

/// A pipeline reset that has not yet succeeded.
Expand Down Expand Up @@ -122,6 +124,7 @@ where
l1_block_zero,
attributes_awaiting_confirmation: None,
pending_reset: None,
reset_epoch: 0,
}
}

Expand Down Expand Up @@ -333,7 +336,11 @@ where
) -> Result<(), DerivationError> {
match request_type {
DerivationActorRequest::ProcessEngineSignalRequest(signal) => match *signal {
Signal::Reset(reset) => self.reset_pipeline_and_resume(reset).await?,
Signal::Reset(reset) => {
// Count each engine reset once, not each retry of its pipeline reset.
self.reset_epoch += 1;
self.reset_pipeline_and_resume(reset).await?
}
Signal::Activation(_) => {
self.forward_non_reset_signal(*signal).await;
self.derivation_state_machine
Expand Down Expand Up @@ -369,9 +376,24 @@ where
}
DerivationActorRequest::ProcessEngineSafeHeadUpdateRequest(safe_head) => {
info!(target: "derivation", safe_head = ?*safe_head, "Received safe head from engine.");
// Only consolidating the attributes derived here moves the engine's safe head, and
// each consolidation advances it. Adopting any other head would skip blocks that
// were never derived and leave a gap in the safe head database.
let Some(attributes) = self
.attributes_awaiting_confirmation
.take_if(|attributes| attributes_produce(attributes, &safe_head))
else {
warn!(
target: "derivation",
safe_head = safe_head.block_info.number,
awaiting = ?self.attributes_awaiting_confirmation.as_ref().map(|attributes| attributes.block_number()),
"Ignoring a safe head that does not confirm the derived attributes"
);
return Ok(());
};
self.derivation_state_machine
.update(&DerivationStateUpdate::NewAttributesConfirmed(safe_head.clone()))?;
self.settle_derived_attributes(*safe_head)?;
self.record_confirmed_attributes(&attributes, *safe_head)?;

self.attempt_derivation().await?;
}
Expand All @@ -388,40 +410,23 @@ where
Ok(())
}

/// Records and queues for finalization the attributes that `safe_head` confirms.
/// Records and queues for finalization the derived `attributes` that `safe_head` confirms.
///
/// A head that confirms nothing pending was adopted by the engine rather than derived here,
/// so there is nothing to record. Finalization is queued only once the record is written, so
/// the engine's finalized head can never run ahead of the recorded history.
pub(super) fn settle_derived_attributes(
/// Finalization is queued only once the record is written, so the engine's finalized head can
/// never run ahead of the recorded history.
pub(super) fn record_confirmed_attributes(
&mut self,
attributes: &OpAttributesWithParent,
safe_head: L2BlockInfo,
) -> Result<(), DerivationError> {
let Some(attributes) = self.attributes_awaiting_confirmation.take() else {
debug!(
target: "derivation",
safe_head = safe_head.block_info.number,
"Safe head confirms no derived attributes"
);
return Ok(());
};
if !attributes_produce(&attributes, &safe_head) {
warn!(
target: "derivation",
expected = attributes.block_number(),
safe_head = safe_head.block_info.number,
"Safe head does not match the derived attributes awaiting confirmation"
);
return Ok(());
}
if let (Some(database), Some(derived_from)) = (&self.safe_db, attributes.derived_from) {
debug!(target: "derivation", l1_block = derived_from.number, safe_head = safe_head.block_info.number, "Recording safe head");
database.safe_head_updated(safe_head, derived_from.id()).inspect_err(|error| {
error!(target: "derivation", %error, l1_block = derived_from.number, safe_head = safe_head.block_info.number, "Failed to record safe head");
})?;
debug!(target: "derivation", l1_block = derived_from.number, safe_head = safe_head.block_info.number, "Recorded safe head");
}
self.finalizer.enqueue_for_finalization(&attributes);
self.finalizer.enqueue_for_finalization(attributes);
Ok(())
}

Expand Down Expand Up @@ -478,7 +483,7 @@ where
// 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))
.send_derived_finalization(l2_block_number, self.reset_epoch)
.await
.map_err(|e| DerivationError::Sender(Box::new(e)))
}
Expand Down Expand Up @@ -521,7 +526,7 @@ where

// Send payload attributes out for processing.
self.engine_client
.send_safe_l2_signal(payload_attributes.into())
.send_derived_attributes(payload_attributes, self.reset_epoch)
.await
.map_err(|e| DerivationError::Sender(Box::new(e)))?;

Expand Down
Loading
Loading