diff --git a/rust/kona/crates/node/service/src/actors/derivation/actor.rs b/rust/kona/crates/node/service/src/actors/derivation/actor.rs index 73f2ce4d265..36842b1801f 100644 --- a/rust/kona/crates/node/service/src/actors/derivation/actor.rs +++ b/rust/kona/crates/node/service/src/actors/derivation/actor.rs @@ -325,15 +325,20 @@ where ); return Ok(()); } - if let Some(derived_from) = attributes.derived_from { - self.safe_head_history.record(safe_head, derived_from.id()); - } - 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"); + if let Some(derived_from) = attributes.derived_from.map(|block| block.id()) { + // Both records take the L1 block the history works out: the derivation source, + // raised to the L1 block the parent was recorded at, since a block cannot become safe + // before its parent. Below a floor known only as a bound, the history records nothing + // and the database keeps the derivation source. + let safe_at = + self.safe_head_history.record(safe_head, derived_from).unwrap_or(derived_from); + if let Some(database) = &self.safe_db { + debug!(target: "derivation", l1_block = safe_at.number, safe_head = safe_head.block_info.number, "Recording safe head"); + database.safe_head_updated(safe_head, safe_at).inspect_err(|error| { + error!(target: "derivation", %error, l1_block = safe_at.number, safe_head = safe_head.block_info.number, "Failed to record safe head"); + })?; + debug!(target: "derivation", l1_block = safe_at.number, safe_head = safe_head.block_info.number, "Recorded safe head"); + } } self.finalizer.enqueue_for_finalization(&attributes); Ok(()) 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 6d3a67f5bcb..046d17ed2a3 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 @@ -584,6 +584,51 @@ async fn a_reset_that_keeps_the_confirmed_safe_head_resumes_derivation( } } +/// Attributes for the child of `parent`, derived from L1 block `derived_from`. +fn attributes_after(parent: u64, derived_from: u64) -> OpAttributesWithParent { + OpAttributesWithParent { + attributes: OpPayloadAttributes::default(), + parent: l2_block(parent), + derived_from: Some(l1_block(derived_from)), + is_last_in_span: true, + } +} + +/// After a reset, the pipeline replays from before the safe head's L1 origin and can produce the +/// next block from an L1 block below the one the safe head became safe at. That block became safe +/// only once its parent did, so the database records it at its parent's L1 block: updates stay +/// in ascending L1 order, and no query answers it for an L1 block where the parent was not safe. +#[tokio::test] +async fn replay_after_a_reset_records_no_safe_head_below_its_parents_l1_block() { + let mut node = RecordingActor::new(engine_client_accepting_attributes()); + node.derive_and_confirm_one_block().await; + let parent_l1 = DERIVED_FROM_L1 + 6; + node.actor.pipeline.attributes.push_back(attributes_after(DERIVED_HEAD, parent_l1)); + node.handle(DerivationActorRequest::ProcessL1HeadUpdateRequest(Box::default())).await; + node.confirm_safe_head(DERIVED_HEAD + 1).await; + assert_eq!(node.db.last_entry().unwrap(), record(parent_l1, DERIVED_HEAD + 1)); + + // A reset that keeps that safe head, then a replay that finds the next block's batch lower. + node.actor.pipeline.attributes.push_back(attributes_after(DERIVED_HEAD + 1, parent_l1 - 3)); + node.signal(Signal::Reset(kona_derive::ResetSignal { + l2_safe_head: l2_block(DERIVED_HEAD + 1), + })) + .await; + node.actor + .pipeline + .attributes + .retain(|attributes| attributes.parent == l2_block(DERIVED_HEAD + 1)); + node.confirm_safe_head(DERIVED_HEAD + 2).await; + + assert_eq!(node.db.last_entry().unwrap(), record(parent_l1, DERIVED_HEAD + 2)); + // Below the parent's L1 block, the safe head is still the grandparent. + assert_eq!( + node.db.safe_head_at_l1(parent_l1 - 1).unwrap(), + record(DERIVED_FROM_L1, DERIVED_HEAD) + ); + assert_eq!(node.db.l1_at_safe_head(DERIVED_HEAD + 1).unwrap().l1, l1_block(parent_l1).id()); +} + #[tokio::test] async fn a_confirmed_block_is_recorded_against_its_derivation_source() { let mut node = RecordingActor::new(engine_client_accepting_attributes()); diff --git a/rust/kona/crates/node/service/src/actors/derivation/safe_head_history.rs b/rust/kona/crates/node/service/src/actors/derivation/safe_head_history.rs index 92311d5c749..5fc55c84dd6 100644 --- a/rust/kona/crates/node/service/src/actors/derivation/safe_head_history.rs +++ b/rust/kona/crates/node/service/src/actors/derivation/safe_head_history.rs @@ -15,37 +15,68 @@ const MAX_ENTRIES: usize = 64; /// see [`kona_engine::find_reorg_forkchoice`]. /// /// This is kept in memory whether or not the safe head database is enabled, and is bounded to -/// the depth an L1 reorg can plausibly reach. +/// the depth an L1 reorg can plausibly reach. The database records each safe head at the L1 block +/// [`SafeHeadHistory::record`] returns, so the two never disagree. /// /// A safe head depends on the L1 data its own attributes were derived from, and on everything its /// parent depends on. The pipeline's origin bounds only the former: after a reset the pipeline /// replays from before the safe head's L1 origin, and can produce the next block from an L1 block -/// below the one its parent needed. So the history keeps a floor that is at least the L1 block -/// the current safe head depends on, and records a safe head only if its attributes came from at -/// or above the floor. +/// below the one its parent was recorded at, as when its batch landed first and was buffered. +/// So the history keeps a [`Floor`] that is at least the L1 block the current safe head depends +/// on, and records no safe head below it. #[derive(Debug, Default)] pub(crate) struct SafeHeadHistory { /// L1 block number -> the highest safe head produced with the pipeline at that L1 block. by_l1_block: BTreeMap, - /// The lowest L1 block a new record may claim: at least the L1 block the current safe head - /// depends on. - floor: u64, + /// The lowest L1 block a new record may claim. + floor: Floor, } -impl SafeHeadHistory { - /// Records `safe_head`, confirmed from attributes the pipeline produced at `derived_from`, if - /// that is at or above the floor. - pub(crate) fn record(&mut self, safe_head: L2BlockInfo, derived_from: BlockNumHash) { - if derived_from.number < self.floor { - // The safe head depends on no more than the floor, which stays put. - return; - } +/// The lowest L1 block a new record may claim: at least the L1 block the current safe head +/// depends on. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Floor { + /// The L1 block the current safe head was recorded at. A safe head derived below it cannot + /// have become safe before its parent, so it is recorded there. + Recorded(BlockNumHash), + /// Only a bound on the L1 blocks the current safe head depends on, with no block to record + /// at. A safe head derived below it is not recorded. + Bound(u64), +} - self.floor = derived_from.number; - self.by_l1_block.insert(derived_from.number, DerivedSafeHead { safe_head, derived_from }); +impl Default for Floor { + fn default() -> Self { + Self::Bound(0) + } +} + +impl SafeHeadHistory { + /// Records `safe_head`, confirmed from attributes the pipeline produced at `derived_from`, at + /// `derived_from` raised to the floor, and returns that L1 block. `None` when it is below a + /// floor known only as a bound, and so is not recorded. + /// + /// The returned block bounds the L1 data `safe_head` depends on, and is the earliest L1 block + /// it can have become safe at. Before Holocene that is where it became safe. After Holocene a + /// node that reset can derive it from a batch its first pass dropped as a future batch, and + /// the first pass took it from a later one. + pub(crate) fn record( + &mut self, + safe_head: L2BlockInfo, + derived_from: BlockNumHash, + ) -> Option { + let safe_at = match self.floor { + Floor::Recorded(floor) if derived_from.number < floor.number => floor, + Floor::Bound(bound) if derived_from.number < bound => return None, + _ => derived_from, + }; + + self.floor = Floor::Recorded(safe_at); + self.by_l1_block + .insert(safe_at.number, DerivedSafeHead { safe_head, derived_from: safe_at }); while self.by_l1_block.len() > MAX_ENTRIES { self.by_l1_block.pop_first(); } + Some(safe_at) } /// The recorded safe heads, newest first. @@ -55,20 +86,22 @@ impl SafeHeadHistory { /// Derivation restarts from `safe_head`: forgets the safe heads above it. /// - /// If `safe_head` is recorded, the floor is the L1 block it was recorded with. Otherwise - /// nothing kept is known to lead to it, so the history is cleared and the floor is the L1 - /// block by which derivation must have decided `safe_head`: one past the end of its epoch's - /// sequencing window. + /// If `safe_head` is recorded, the floor is the L1 block it was recorded at. Otherwise + /// nothing kept is known to lead to it, so the history is cleared and the floor is bounded by + /// the L1 block by which derivation must have decided `safe_head`: one past the end of its + /// epoch's sequencing window. pub(crate) fn reset_to(&mut self, safe_head: L2BlockInfo, seq_window_size: u64) { self.by_l1_block.retain(|_, derived| { derived.safe_head.block_info.number <= safe_head.block_info.number }); match self.by_l1_block.last_key_value() { - Some((&number, derived)) if derived.safe_head == safe_head => self.floor = number, + Some((_, derived)) if derived.safe_head == safe_head => { + self.floor = Floor::Recorded(derived.derived_from); + } _ => { self.by_l1_block.clear(); - self.floor = safe_head.l1_origin.number + seq_window_size + 1; + self.floor = Floor::Bound(safe_head.l1_origin.number + seq_window_size + 1); } } } @@ -98,8 +131,8 @@ mod tests { BlockNumHash { number, hash: B256::repeat_byte(number as u8) } } - fn record(history: &mut SafeHeadHistory, number: u64, derived_from: u64) { - history.record(l2_block(number), l1_block(derived_from)); + fn record(history: &mut SafeHeadHistory, number: u64, derived_from: u64) -> Option { + history.record(l2_block(number), l1_block(derived_from)).map(|safe_at| safe_at.number) } /// `(safe head number, L1 block number)` of each record, newest first. @@ -141,21 +174,21 @@ mod tests { /// After a reset to a recorded safe head, the pipeline replays from before its L1 origin and /// can produce the next block from an L1 block below the one that safe head was recorded - /// with. That block depends on its parent, so it must not be recorded as derived from the - /// lower L1 block, and must not overwrite the record already at that L1 block. + /// with. That block depends on its parent, so it became safe at its parent's L1 block, where + /// it is recorded instead. #[test] - fn replay_after_reset_does_not_record_below_the_kept_safe_head() { + fn replay_after_reset_records_at_the_kept_safe_heads_l1_block() { let mut history = SafeHeadHistory::default(); record(&mut history, 11, 100); record(&mut history, 12, 106); record(&mut history, 13, 107); history.reset_to(l2_block(12), SEQ_WINDOW_SIZE); - record(&mut history, 13, 100); - assert_eq!(recorded(&history), vec![(12, 106), (11, 100)]); + assert_eq!(record(&mut history, 13, 100), Some(106)); + assert_eq!(recorded(&history), vec![(13, 106), (11, 100)]); - record(&mut history, 14, 108); - assert_eq!(recorded(&history), vec![(14, 108), (12, 106), (11, 100)]); + assert_eq!(record(&mut history, 14, 108), Some(108)); + assert_eq!(recorded(&history), vec![(14, 108), (13, 106), (11, 100)]); } /// A reset to a safe head the history did not record, such as one from a full reset, leaves @@ -173,11 +206,11 @@ mod tests { assert!(history.newest_first().is_empty()); // Derived below the end of the sequencing window: not recorded. - record(&mut history, 12, 105); + assert_eq!(record(&mut history, 12, 105), None); assert!(history.newest_first().is_empty()); // From the floor on, recording resumes. - record(&mut history, 13, 106); + assert_eq!(record(&mut history, 13, 106), Some(106)); assert_eq!(recorded(&history), vec![(13, 106)]); } }