Skip to content
Merged
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
180 changes: 178 additions & 2 deletions crates/blockchain/src/beacon_payloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,14 @@ pub(crate) fn resolve_invalid_payload(
/// chain skipped past was not part of what the execution client validated.
///
/// Skipped entirely when nothing is optimistic, and bounded at finality
/// otherwise.
/// otherwise, plus the one payload below it that
/// [`validate_finalized_payload_base`] names.
pub(crate) fn mark_payloads_validated(store: &mut Store, root: H256) {
if !store.has_unvalidated_block_payloads() && !store.has_beacon_optimistic_roots() {
return;
}
for block in el_chain(store, root) {
let chain = el_chain(store, root);
for &block in &chain {
store.remove_beacon_optimistic_root(block);
if store.has_verified_payload(&block)
&& store.beacon_block_payload_status(block).is_not_validated()
Expand All @@ -330,6 +332,50 @@ pub(crate) fn mark_payloads_validated(store: &mut Store, root: H256) {
store.insert_beacon_block_payload_status(block, slot, PayloadStatusEnum::Valid);
}
}
let lowest = *chain.last().expect("the chain holds its start");
validate_finalized_payload_base(store, lowest);
}

/// The gloas payload the finalized block builds on: its block's root and
/// slot, and its execution hash. `None` when the finalized block's flag is not
/// read from a gloas verdict (see [`Store::beacon_finalized_payload_base`]).
fn finalized_payload_base(store: &Store) -> Option<(H256, u64, ExecutionBlockHash)> {
let base = store.beacon_finalized_payload_base()?;
let block = store.get_signed_block(&base).ok().flatten()?;
let hash = bid_of(&block)?.block_hash;
Some((base, block.slot(), hash))
}

/// Moves the payload the finalized block builds on to `VALID`, when `lowest`,
/// the last block of a validated EL chain, builds on it.
///
/// The `execution_optimistic` flag of the finalized block, and of every block
/// below it, is read from that payload's verdict, which is why finality's
/// prune keeps it. It is one step below where [`el_chain`] stops, so the walk
/// in [`mark_payloads_validated`] never promotes it: left alone, a verdict
/// kept while `NOT_VALIDATED` (the execution client was syncing when finality
/// moved, or a restart seeded it) would stay so until finality moves again.
///
/// `lowest` is either the finalized block, whose payload is then on the
/// chain, or the first block above it whose payload is, when the finalized
/// block's is not. Both name the same parent payload in their bid, and a
/// `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 {
return;
};
if !store.beacon_block_payload_status(base).is_not_validated() {
return;
}
let builds_on = store
.get_signed_block(&lowest)
.ok()
.flatten()
.and_then(|block| bid_of(&block).map(|bid| bid.parent_block_hash));
if builds_on == Some(hash) {
store.insert_beacon_block_payload_status(base, slot, PayloadStatusEnum::Valid);
}
}

/// Gives a payload restored from disk a verdict: the status map is scratch, so
Expand All @@ -349,6 +395,13 @@ pub(crate) fn seed_resumed_payload_statuses(store: &mut Store, engine_configured
store.insert_beacon_block_payload_status(root, slot, status);
}
}
// The payload the finalized block builds on is below the window the
// verified set is rebuilt for, and the finalized block could not have been
// imported without it. Its verdict is what the finalized block's
// `execution_optimistic` flag is read from.
if let Some((base, slot, _hash)) = finalized_payload_base(store) {
store.insert_beacon_block_payload_status(base, slot, status);
}
}

impl BlockChainServer {
Expand Down Expand Up @@ -667,4 +720,127 @@ mod tests {
.expect("move head");
assert_eq!(get_safe_execution_block_hash(&store), hash_a);
}
/// Three gloas blocks above the zero anchor, the second finalized under a
/// checkpoint at its own slot, the third the head. The first two are FULL
/// on their parent; `head_parent_hash` says which payload the head builds
/// on. Returns the store, the roots and the three payload hashes.
///
/// ```text
/// anchor(fulu) - b1 - b2 (finalized) - b3 (head)
/// ```
///
/// `b1`'s payload is the one `b2`'s `execution_optimistic` flag is read
/// from. No verdict is recorded for any of them.
fn finalized_gloas_chain(head_builds_on: usize) -> (Store, [H256; 3], [H256; 3]) {
let mut store = beacon_store(0, 0);
let hashes = [
H256::repeat_byte(0x31),
H256::repeat_byte(0x32),
H256::repeat_byte(0x33),
];
let parent_hashes = [H256::repeat_byte(0x30), hashes[0], hashes[head_builds_on]];
let mut roots = [H256::ZERO; 3];
let mut parent = H256::ZERO;
for i in 0..3 {
let slot = i as u64 + 1;
let block = gloas_block(slot, parent, parent_hashes[i], hashes[i]);
let root = block.message_hash_tree_root();
store.insert_pending_block(root, block).expect("insert");
store.insert_live_chain_entry(slot, root, parent);
store.insert_beacon_el_block_hash(root, slot, hashes[i]);
roots[i] = root;
parent = root;
}
make_head(&mut store, roots[2], PayloadStatus::Full);
let finalized = ethlambda_types::checkpoint::Checkpoint {
root: roots[1],
slot: 2,
};
let checkpoints = ethlambda_storage::ForkCheckpoints::new(roots[2], None, Some(finalized));
store.update_checkpoints(checkpoints).expect("finalize");
(store, roots, hashes)
}

/// A restart reloads payloads at or above the finalized block only, and
/// the one the finalized block builds on is below it. Without a verdict
/// of its own the finalized block would read as optimistic for the life
/// of the process (#981).
#[test]
fn a_restart_seeds_the_payload_the_finalized_block_builds_on() {
let (mut store, [b1, b2, _], _) = finalized_gloas_chain(1);
assert_eq!(store.beacon_finalized_payload_base(), Some(b1));

// With an execution client it waits for a `VALID` answer.
seed_resumed_payload_statuses(&mut store, true);
assert_eq!(
store.beacon_block_payload_status(b1),
PayloadStatusEnum::Syncing
);
assert!(store.is_beacon_block_optimistic(b2));

// Without one the follower trusts it, as it does every envelope.
seed_resumed_payload_statuses(&mut store, false);
assert_eq!(
store.beacon_block_payload_status(b1),
PayloadStatusEnum::Valid
);
assert!(!store.is_beacon_block_optimistic(b2));
}

/// The walk a `VALID` answer starts stops at the finalized block, one
/// payload above the one that block's flag is read from.
#[test]
fn a_valid_head_validates_the_payload_the_finalized_block_builds_on() {
let (mut store, [b1, b2, b3], _) = finalized_gloas_chain(1);
seed_resumed_payload_statuses(&mut store, true);
assert!(store.is_beacon_block_optimistic(b2));

mark_payloads_validated(&mut store, b3);

assert_eq!(
store.beacon_block_payload_status(b1),
PayloadStatusEnum::Valid
);
assert!(!store.is_beacon_block_optimistic(b2));
assert!(!store.has_unvalidated_block_payloads());
}

/// When the head's chain skipped the finalized block's own payload, the
/// walk ends above that block, on a block that names the same parent
/// payload in its bid.
#[test]
fn a_valid_head_that_skipped_the_finalized_payload_validates_the_one_below_it() {
let (mut store, [b1, b2, b3], _) = finalized_gloas_chain(0);
seed_resumed_payload_statuses(&mut store, true);

mark_payloads_validated(&mut store, b3);

assert_eq!(
store.beacon_block_payload_status(b1),
PayloadStatusEnum::Valid
);
assert!(!store.is_beacon_block_optimistic(b2));
}

/// A validated chain that does not build on that payload says nothing
/// about it.
#[test]
fn a_valid_block_off_the_finalized_payload_validates_nothing_below_finality() {
let (mut store, [b1, b2, _], _) = finalized_gloas_chain(1);
seed_resumed_payload_statuses(&mut store, true);
let stray = gloas_block(4, b2, H256::repeat_byte(0x77), H256::repeat_byte(0x78));
let stray_root = stray.message_hash_tree_root();
store
.insert_pending_block(stray_root, stray)
.expect("insert");
store.insert_live_chain_entry(4, stray_root, b2);

mark_payloads_validated(&mut store, stray_root);

assert_eq!(
store.beacon_block_payload_status(b1),
PayloadStatusEnum::Syncing
);
assert!(store.is_beacon_block_optimistic(b2));
}
}
5 changes: 4 additions & 1 deletion crates/blockchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3959,8 +3959,11 @@ impl BlockChainServer {
// that follows the import cascade runs.
self.store
.prune_beacon_optimistic_roots(finalized_slot, finalized.root);
// The gloas verdicts share the horizon and keep two entries: the
// finalized block's own, and the one of the payload it builds on,
// which every finalized block's flag is read from.
self.store
.prune_beacon_block_payload_statuses(finalized_slot);
.prune_beacon_block_payload_statuses(finalized_slot, finalized.root);

// Same horizon again: the states of finalized snapshot intervals. Beacon
// only, since lean prunes them inside `Store::prune_old_data`, and doing
Expand Down
Loading
Loading