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
154 changes: 147 additions & 7 deletions crates/blockchain/src/beacon_payloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
}
}
4 changes: 3 additions & 1 deletion docs/rpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading