From 35bc2b0cd61a4aba41806c0d97f85dab06abfa1d Mon Sep 17 00:00:00 2001 From: Pablo Deymonnaz Date: Fri, 4 Dec 2026 22:22:03 -0300 Subject: [PATCH] fix(blockchain): apply a VALID forkchoiceUpdated that names the payload the finalized block builds on A gloas head with no FULL payload from the finalized block up sends, as its head hash, the payload the finalized block builds on. The verdict's walk (`payload_holder`) goes from the head down to the finalized block and finds no block carrying that hash, so a `VALID` answer for it was dropped. That payload's verdict is the one the finalized block's `execution_optimistic` flag is read from, and the head's too while no payload above it is FULL. Left `NOT_VALIDATED` (the execution client was syncing when finality passed it, or a restart seeded it `SYNCING`), the node kept reporting the finalized block and the head as optimistic, and refused attestation data for that head, until a FULL payload was validated above finality. `apply_gloas_forkchoice_verdict` now compares the head hash with that payload's own when no holder is found and the answer is `VALID`, and moves its verdict to `VALID` on a match. `INVALID` for a payload below finality is still ignored: nothing at or below finality leaves fork choice. Part of #991. --- crates/blockchain/src/beacon_payloads.rs | 154 +++++++++++++++++++++-- docs/rpc.md | 4 +- 2 files changed, 150 insertions(+), 8 deletions(-) diff --git a/crates/blockchain/src/beacon_payloads.rs b/crates/blockchain/src/beacon_payloads.rs index 3046c3db5..dd1281fb5 100644 --- a/crates/blockchain/src/beacon_payloads.rs +++ b/crates/blockchain/src/beacon_payloads.rs @@ -362,12 +362,9 @@ fn finalized_payload_base(store: &Store) -> Option<(H256, u64, ExecutionBlockHas /// `VALID` payload's parent is valid. The hashes are compared rather than /// assumed, so a chain that stopped anywhere else promotes nothing. fn validate_finalized_payload_base(store: &mut Store, lowest: H256) { - let Some((base, slot, hash)) = finalized_payload_base(store) else { + let Some((base, slot, hash)) = unvalidated_finalized_payload_base(store) else { return; }; - if !store.beacon_block_payload_status(base).is_not_validated() { - return; - } let builds_on = store .get_signed_block(&lowest) .ok() @@ -378,6 +375,30 @@ fn validate_finalized_payload_base(store: &mut Store, lowest: H256) { } } +/// Moves the payload the finalized block builds on to `VALID`, when +/// `validated`, a hash the execution client just answered `VALID` for, is +/// that payload's own. +/// +/// [`validate_finalized_payload_base`] gets there from a validated chain above +/// finality. This is for the answer that names the payload directly: a head +/// with no FULL payload from the finalized block up sends this payload as its +/// head hash, and no block [`payload_holder`] can reach carries it. +fn validate_finalized_payload_base_named(store: &mut Store, validated: ExecutionBlockHash) { + let Some((base, slot, hash)) = unvalidated_finalized_payload_base(store) else { + return; + }; + if validated == hash { + store.insert_beacon_block_payload_status(base, slot, PayloadStatusEnum::Valid); + } +} + +/// [`finalized_payload_base`] while its verdict is still `NOT_VALIDATED`, the +/// only state a `VALID` answer moves it from. +fn unvalidated_finalized_payload_base(store: &Store) -> Option<(H256, u64, ExecutionBlockHash)> { + finalized_payload_base(store) + .filter(|(base, ..)| store.beacon_block_payload_status(*base).is_not_validated()) +} + /// Gives a payload restored from disk a verdict: the status map is scratch, so /// without this a verified payload from before a restart has no entry, reads /// as `NOT_VALIDATED`, and is never promoted. With an engine configured they @@ -411,6 +432,12 @@ impl BlockChainServer { /// `VALID` promotes every optimistic payload on the holder's ancestry; /// `INVALID` condemns from the first payload after `latestValidHash`. /// `SYNCING` and `ACCEPTED` change nothing. + /// + /// A head with no FULL payload from the finalized block up names a payload + /// below finality, which has no holder to find. `VALID` for it promotes + /// the payload the finalized block builds on when that is the one named; + /// `INVALID` for it is ignored, since nothing at or below finality leaves + /// fork choice. pub(crate) fn apply_gloas_forkchoice_verdict( &mut self, head_root: H256, @@ -422,8 +449,15 @@ impl BlockChainServer { return; }; let Some(holder) = payload_holder(&self.store, head_root, head_hash) else { - // Expected for a head whose parent payload is below the index (an - // anchor): the specification treats it as known-null. + // The walk stops at the finalized block, one payload above the + // one that block builds on. That payload's verdict is what the + // finalized block's `execution_optimistic` flag is read from, and + // this answer may be the only one that ever names it. + if matches!(verdict, PayloadValidity::Validated) { + validate_finalized_payload_base_named(&mut self.store, head_hash); + } + // Also expected for a head whose parent payload is below the index + // (an anchor): the specification treats it as known-null. debug!( head_root = %ShortRoot(&head_root.0), "No block on the head's chain carries the payload forkchoiceUpdated named" @@ -535,11 +569,12 @@ impl BlockChainServer { #[cfg(test)] mod tests { + use ethlambda_engine::PayloadStatusValue; use ethlambda_types::beacon::containers::{SignedBeaconBlock, gloas}; use ethlambda_types::beacon::fork_choice::FastConfirmationVariables; use super::*; - use crate::tests::{beacon_store, fulu_block}; + use crate::tests::{beacon_server, beacon_store, fulu_block}; /// Makes `head_root` the store's head, which `head_payload_status` needs /// to agree with before it answers. The zero anchor gets a block of its @@ -843,4 +878,109 @@ mod tests { ); assert!(store.is_beacon_block_optimistic(b2)); } + + fn engine_answer(status: PayloadStatusValue) -> ethlambda_engine::PayloadStatusV1 { + ethlambda_engine::PayloadStatusV1 { + status, + latest_valid_hash: None, + validation_error: None, + } + } + + /// The head hash `forkchoiceUpdated` carries for an EMPTY head. + fn empty_head_hash(store: &Store, head: H256) -> ExecutionBlockHash { + store.set_head_payload_status(head, PayloadStatus::Empty); + let plan = forkchoice_plan(store, head).expect("a plan"); + plan.state.head_block_hash + } + + /// A head with no FULL payload from the finalized block up sends the + /// payload that block builds on as its head hash. No block the verdict's + /// walk reaches carries it, so the `VALID` answer was dropped and the + /// finalized block went on reading as optimistic (#991). + #[test] + fn a_valid_answer_naming_the_payload_below_finality_validates_it() { + let (store, [b1, b2, b3], [hash_1, _, _]) = finalized_gloas_chain(0); + let mut server = beacon_server(store); + seed_resumed_payload_statuses(&mut server.store, true); + let head_hash = empty_head_hash(&server.store, b3); + assert_eq!(head_hash, hash_1, "the finalized block's parent payload"); + assert_eq!(payload_holder(&server.store, b3, head_hash), None); + assert!(server.store.is_beacon_block_optimistic(b2)); + // The head reads the same verdict, and an optimistic head is refused + // attestation data. + assert!(server.store.is_beacon_block_optimistic(b3)); + + let valid = engine_answer(PayloadStatusValue::Valid); + server.apply_gloas_forkchoice_verdict(b3, head_hash, &valid); + + assert_eq!( + server.store.beacon_block_payload_status(b1), + PayloadStatusEnum::Valid + ); + assert!(!server.store.is_beacon_block_optimistic(b2)); + assert!(!server.store.is_beacon_block_optimistic(b3)); + assert!(!server.store.has_unvalidated_block_payloads()); + } + + /// The same with the finalized block itself built on an EMPTY parent, so + /// the payload is two blocks below it. + /// + /// ```text + /// anchor(fulu) - b1 - b2 - b3 (finalized) - b4 (head) + /// ``` + /// + /// `b2`, `b3` and `b4` all name `b1`'s payload in their bid. + #[test] + fn a_valid_answer_validates_the_payload_below_a_finalized_block_built_on_empty() { + let (mut store, [b1, _, b3], [hash_1, _, _]) = finalized_gloas_chain(0); + let block_4 = gloas_block(4, b3, hash_1, H256::repeat_byte(0x34)); + let b4 = block_4.message_hash_tree_root(); + store.insert_pending_block(b4, block_4).expect("insert"); + store.insert_live_chain_entry(4, b4, b3); + let finalized = ethlambda_types::checkpoint::Checkpoint { root: b3, slot: 3 }; + let checkpoints = ethlambda_storage::ForkCheckpoints::new(b4, None, Some(finalized)); + store.update_checkpoints(checkpoints).expect("finalize"); + assert_eq!(store.beacon_finalized_payload_base(), Some(b1)); + + let mut server = beacon_server(store); + seed_resumed_payload_statuses(&mut server.store, true); + let head_hash = empty_head_hash(&server.store, b4); + assert_eq!(head_hash, hash_1); + assert_eq!(payload_holder(&server.store, b4, head_hash), None); + assert!(server.store.is_beacon_block_optimistic(b3)); + + let valid = engine_answer(PayloadStatusValue::Valid); + server.apply_gloas_forkchoice_verdict(b4, head_hash, &valid); + + assert_eq!( + server.store.beacon_block_payload_status(b1), + PayloadStatusEnum::Valid + ); + assert!(!server.store.is_beacon_block_optimistic(b3)); + } + + /// Only a `VALID` answer that names that payload promotes it: `INVALID` + /// for it has nothing to act on below finality, and a hash no stored + /// block carries says nothing about it. + #[test] + fn no_other_answer_validates_the_payload_below_finality() { + let (store, [b1, b2, b3], [hash_1, _, _]) = finalized_gloas_chain(0); + let mut server = beacon_server(store); + seed_resumed_payload_statuses(&mut server.store, true); + + for (hash, status) in [ + (hash_1, PayloadStatusValue::Invalid), + (hash_1, PayloadStatusValue::Syncing), + (H256::repeat_byte(0x99), PayloadStatusValue::Valid), + ] { + server.apply_gloas_forkchoice_verdict(b3, hash, &engine_answer(status)); + assert_eq!( + server.store.beacon_block_payload_status(b1), + PayloadStatusEnum::Syncing, + "{status:?} for {hash:?}" + ); + assert!(server.store.is_beacon_block_optimistic(b2)); + } + } } diff --git a/docs/rpc.md b/docs/rpc.md index c1d1f9201..89ed88a5f 100644 --- a/docs/rpc.md +++ b/docs/rpc.md @@ -1008,7 +1008,9 @@ Finality prunes both records, so at and below it the flag is read differently: child on its FULL branch reads). A restart seeds that verdict with the unfinalized window's, and a `VALID` answer for a head that builds on it validates it, since the walk a `VALID` answer starts stops at the finalized - block; + block. That covers a head with no FULL payload from the finalized block up, + whose head hash is this payload itself: no block the walk reaches carries + it, so it is validated by its hash; - a **block the finalized one descends from**, gloas or not, answers with the finalized block's flag. Its own verdict is gone, and nothing below finality is validated apart from the finalized block. Before gloas this differs from