diff --git a/Cargo.lock b/Cargo.lock index 7da4053ce..7e6148e21 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2116,6 +2116,7 @@ dependencies = [ "ethrex-common", "ethrex-p2p", "ethrex-rlp", + "ethrex-trie", "futures", "hex", "libp2p", diff --git a/crates/blockchain/state_transition/src/beacon/gossip/mod.rs b/crates/blockchain/state_transition/src/beacon/gossip/mod.rs index cf7da15da..b0df5a77a 100644 --- a/crates/blockchain/state_transition/src/beacon/gossip/mod.rs +++ b/crates/blockchain/state_transition/src/beacon/gossip/mod.rs @@ -196,6 +196,13 @@ pub enum IgnoreReason { AlreadyExiting, /// The head state's sync committees cannot answer for the message's period. SyncCommitteeUnavailable, + /// A signed block's execution payload `block_hash` is not the hash of the + /// execution block header rebuilt from the payload's own fields. A rule + /// the specification does not have, run in p2p once the proposer's + /// signature has verified: see `docs/spec_deviations.md`. IGNORE rather + /// than REJECT because other clients forward such a block, and a REJECT + /// would score every honest peer that relayed it. + PayloadBlockHashMismatch, } impl IgnoreReason { @@ -238,6 +245,7 @@ impl IgnoreReason { Self::BeforeFork => "before_fork", Self::AlreadyExiting => "already_exiting", Self::SyncCommitteeUnavailable => "sync_committee_unavailable", + Self::PayloadBlockHashMismatch => "payload_block_hash_mismatch", } } } diff --git a/crates/common/types/src/beacon/containers/bellatrix.rs b/crates/common/types/src/beacon/containers/bellatrix.rs index daafe8ca6..72785bde9 100644 --- a/crates/common/types/src/beacon/containers/bellatrix.rs +++ b/crates/common/types/src/beacon/containers/bellatrix.rs @@ -117,6 +117,34 @@ pub struct ExecutionPayload { pub transactions: Transactions, } +/// The specification's `ExecutionPayload()`: every field zero or empty, which +/// is what a bellatrix block proposed before the merge carries. +/// +/// Written out rather than derived because `logs_bloom` is an [`SszVector`], +/// which libssz gives no `Default` (a vector can never validly be empty), so +/// its all-zero value is built at its exact length. +impl Default for ExecutionPayload { + fn default() -> Self { + Self { + parent_hash: ExecutionBlockHash::ZERO, + fee_recipient: ExecutionAddress::ZERO, + state_root: Bytes32::ZERO, + receipts_root: Bytes32::ZERO, + logs_bloom: LogsBloom::try_from(vec![0u8; preset::BYTES_PER_LOGS_BLOOM]) + .expect("BYTES_PER_LOGS_BLOOM zeros fit LogsBloom's exact length"), + prev_randao: Bytes32::ZERO, + block_number: 0, + gas_limit: 0, + gas_used: 0, + timestamp: 0, + extra_data: Default::default(), + base_fee_per_gas: Uint256::ZERO, + block_hash: ExecutionBlockHash::ZERO, + transactions: Default::default(), + } + } +} + /// An [`ExecutionPayload`] with its transaction list replaced by a merkle /// root, so [`BeaconState::latest_execution_payload_header`] can commit to /// the previous payload without the state growing with every transaction @@ -197,17 +225,11 @@ pub struct BeaconBlockBody { } impl BeaconBlockBody { - /// An empty body: no operations of any kind, and an all-zero execution - /// payload. - /// - /// Not `#[derive(Default)]`, unlike phase0's and altair's bodies (which - /// have no execution payload to build): `execution_payload.logs_bloom` is - /// an [`SszVector`], and unlike a list, a vector can never validly be - /// empty, so libssz gives it no `Default` impl. Its all-zero value is - /// built explicitly at its exact length instead, the same construction - /// `state_transition::beacon::upgrade`'s - /// `empty_bellatrix_execution_payload_header` uses for the header shape - /// of the same payload. + /// An empty body: no operations of any kind, and the all-zero execution + /// payload ([`ExecutionPayload`]'s `Default`, what a pre-merge block + /// carries; `state_transition::beacon::upgrade`'s + /// `empty_bellatrix_execution_payload_header` builds the header shape of + /// the same value). pub fn empty() -> Self { Self { randao_reveal: Default::default(), @@ -219,23 +241,7 @@ impl BeaconBlockBody { deposits: Default::default(), voluntary_exits: Default::default(), sync_aggregate: Default::default(), - execution_payload: ExecutionPayload { - parent_hash: ExecutionBlockHash::ZERO, - fee_recipient: ExecutionAddress::ZERO, - state_root: Bytes32::ZERO, - receipts_root: Bytes32::ZERO, - logs_bloom: LogsBloom::try_from(vec![0u8; preset::BYTES_PER_LOGS_BLOOM]) - .expect("BYTES_PER_LOGS_BLOOM zeros fit LogsBloom's exact length"), - prev_randao: Bytes32::ZERO, - block_number: 0, - gas_limit: 0, - gas_used: 0, - timestamp: 0, - extra_data: Default::default(), - base_fee_per_gas: Uint256::ZERO, - block_hash: ExecutionBlockHash::ZERO, - transactions: Default::default(), - }, + execution_payload: ExecutionPayload::default(), } } } diff --git a/crates/net/p2p/Cargo.toml b/crates/net/p2p/Cargo.toml index c81f4082d..6bbe4e407 100644 --- a/crates/net/p2p/Cargo.toml +++ b/crates/net/p2p/Cargo.toml @@ -70,6 +70,9 @@ rand = "0.8" # against with nothing in the manifest saying so. ethrex-p2p = { git = "https://github.com/lambdaclass/ethrex", tag = "v25.0.0" } ethrex-rlp = { git = "https://github.com/lambdaclass/ethrex", tag = "v25.0.0" } +# The ordered trie behind a payload's transactions root, which `ethrex-common` +# only builds from decoded transactions, not the raw bytes a payload carries. +ethrex-trie = { git = "https://github.com/lambdaclass/ethrex", tag = "v25.0.0" } ethrex-common = { git = "https://github.com/lambdaclass/ethrex", tag = "v25.0.0" } # `rand` seeds the throwaway signing keys the ENR tests build records with. diff --git a/crates/net/p2p/src/beacon/envelope_checks.rs b/crates/net/p2p/src/beacon/envelope_checks.rs index 5dbbd43de..a2cab4154 100644 --- a/crates/net/p2p/src/beacon/envelope_checks.rs +++ b/crates/net/p2p/src/beacon/envelope_checks.rs @@ -33,6 +33,7 @@ use ethlambda_types::beacon::containers::gloas::SignedExecutionPayloadEnvelope; use tracing::{debug, error, warn}; use crate::P2PServer; +use crate::beacon::payload_hash::{self, EnvelopeSource}; /// Whether a fetched envelope goes to the chain actor. pub(crate) fn forwards(outcome: &Outcome) -> bool { @@ -48,10 +49,13 @@ fn judge(store: &Store, envelope: &SignedExecutionPayloadEnvelope) -> Outcome { // A fresh, empty cache: a fetched envelope is not a gossip duplicate of // anything, so the cache's one rule never fires. let seen = SeenEnvelopes::new(NonZeroUsize::MIN); - match cheap_checks(&seen, store, envelope) { - Ok(()) => stateful_checks(store, envelope), - Err(outcome) => outcome, + if let Err(outcome) = cheap_checks(&seen, store, envelope) { + return outcome; } + // The payload block hash rule the gossip topic runs too, after the + // builder's signature verified. Counted, never refused, for gloas. + let outcome = stateful_checks(store, envelope); + payload_hash::envelope_verdict(outcome, envelope, EnvelopeSource::Fetch) } /// Run the gossip rules on `envelopes` and send the chain actor the ones that diff --git a/crates/net/p2p/src/beacon/mod.rs b/crates/net/p2p/src/beacon/mod.rs index 33944b4f7..dce6adf2d 100644 --- a/crates/net/p2p/src/beacon/mod.rs +++ b/crates/net/p2p/src/beacon/mod.rs @@ -21,6 +21,7 @@ pub mod envelope_checks; pub mod fork_schedule; pub mod handler; pub mod messages; +pub mod payload_hash; pub mod protocols; pub mod scoring; pub mod subnets; diff --git a/crates/net/p2p/src/beacon/payload_hash.rs b/crates/net/p2p/src/beacon/payload_hash.rs new file mode 100644 index 000000000..c98a8a4ba --- /dev/null +++ b/crates/net/p2p/src/beacon/payload_hash.rs @@ -0,0 +1,444 @@ +//! The execution block hash an execution payload claims, checked against the +//! header rebuilt from the payload's own fields. +//! +//! A payload carries its `block_hash` next to the fields that hash is taken +//! over, so a node can recompute it without an execution client: the header is +//! the payload's fields plus the transactions, withdrawals and requests tries, +//! RLP-encoded and keccak-hashed. +//! +//! The consensus specification has no such gossip rule, and the Engine API +//! already requires the execution client to validate `blockHash` on every +//! `engine_newPayload`, even while syncing (execution-apis `paris.md`). While +//! an import waits for that answer, this finds nothing the execution client +//! would not. What it buys is a gossip block refused before it is imported: +//! once a pre-gloas payload is applied to fork choice before the execution +//! client answers (lambdaclass/ethlambda#773), a block caught here is one that +//! never has to be reverted. See `docs/spec_deviations.md`. +//! +//! Where it runs, and what it answers: +//! +//! - A pre-gloas `beacon_block` from gossip, once its stateful checks answered +//! `Accept` ([`block_verdict`]). That is the one outcome that says the +//! proposer's signature verified, so no unsigned message makes this node +//! build a transaction trie. A mismatch is `IGNORE`, not `REJECT`: other +//! clients have no such rule and forward the block, and a `REJECT` would +//! score every honest peer that relayed it. +//! - A gloas envelope, from gossip or fetched, once its checks answered +//! `Accept` ([`envelope_verdict`]): counted and logged, never refused, until +//! the Amsterdam header is final. +//! - Nothing else. A block fetched by range or root, and a gossip block +//! forwarded under `Queue` or `Ignore(Overloaded)`, reach the chain actor +//! unhashed; the execution client still checks them. +//! +//! The header's shape per fork: +//! +//! | fork | adds to the header | +//! |-----------|-----------------------------------------------------------| +//! | bellatrix | the London fields, with `base_fee_per_gas` | +//! | capella | `withdrawals_root` | +//! | deneb | `blob_gas_used`, `excess_blob_gas`, `parent_beacon_block_root` | +//! | electra | `requests_hash` (fulu is the same) | +//! | gloas | `block_access_list_hash`, `slot_number` | +//! +//! Fields the execution layer fixes after the merge are constants here: no +//! ommers, difficulty zero, a zero nonce, `prev_randao` in the mix hash slot. +//! The header is encoded here rather than through ethrex's `BlockHeader`, +//! which holds `base_fee_per_gas` as a `u64` where the payload has 256 bits. + +use ethlambda_state_transition::beacon::gossip::{IgnoreReason, Outcome}; +use ethlambda_state_transition::beacon::stf::{electra, gloas as gloas_stf}; +use ethlambda_types::ShortRoot; +use ethlambda_types::beacon::containers::{SignedBeaconBlock, bellatrix, capella, deneb, gloas}; +use ethlambda_types::beacon::primitives::Root; +use ethrex_common::types::requests::{EncodedRequests, compute_requests_hash}; +use ethrex_common::types::{Withdrawal, compute_withdrawals_root}; +use ethrex_common::{Address, Bloom, Bytes, H256, NativeCrypto, U256}; +use ethrex_rlp::encode::RLPEncode; +use ethrex_rlp::structs::Encoder; +use ethrex_trie::Trie; +use tracing::{debug, warn}; + +use crate::metrics; + +/// `keccak256(rlp([]))`: the ommers hash of every post-merge block. +const EMPTY_OMMERS_HASH: [u8; 32] = [ + 0x1d, 0xcc, 0x4d, 0xe8, 0xde, 0xc7, 0x5d, 0x7a, 0xab, 0x85, 0xb5, 0x67, 0xb6, 0xcc, 0xd4, 0x1a, + 0xd3, 0x12, 0x45, 0x1b, 0x94, 0x8a, 0x74, 0x13, 0xf0, 0xa1, 0x42, 0xfd, 0x40, 0xd4, 0x93, 0x47, +]; + +/// The hash the payload claims against the one its fields give. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct HashMismatch { + pub(crate) claimed: [u8; 32], + pub(crate) computed: [u8; 32], +} + +/// What a payload and its surroundings supply to the header, whatever the fork. +struct Fields<'a> { + parent_hash: [u8; 32], + fee_recipient: [u8; 20], + state_root: [u8; 32], + receipts_root: [u8; 32], + /// Always 256 bytes: every fork's `logs_bloom` is a fixed-length + /// `ByteVector[256]`, which is what lets `Bloom::from_slice` (it panics on + /// any other length) take it. + logs_bloom: &'a [u8], + prev_randao: [u8; 32], + block_number: u64, + gas_limit: u64, + gas_used: u64, + timestamp: u64, + extra_data: &'a [u8], + /// 32 little-endian bytes, as SSZ carries it. + base_fee_per_gas: [u8; 32], + claimed_hash: [u8; 32], + transactions: Vec<&'a [u8]>, + /// `None` before capella. + withdrawals: Option<&'a [capella::Withdrawal]>, + /// `None` before deneb. + blob_gas: Option<(u64, u64)>, + /// `None` before deneb. + parent_beacon_block_root: Option<[u8; 32]>, + /// The Engine API's request list; `None` before electra. + requests: Option>>, + /// The payload's block access list bytes and slot number; `None` before gloas. + amsterdam: Option<(&'a [u8], u64)>, +} + +impl<'a> Fields<'a> { + /// The header fields of deneb's payload container, which electra and fulu + /// reuse with the requests list as their only addition. + fn deneb( + payload: &'a deneb::ExecutionPayload, + parent_root: Root, + requests: Option>>, + ) -> Self { + Fields { + parent_hash: payload.parent_hash.0, + fee_recipient: payload.fee_recipient.0, + state_root: payload.state_root.0, + receipts_root: payload.receipts_root.0, + logs_bloom: payload.logs_bloom.as_ref(), + prev_randao: payload.prev_randao.0, + block_number: payload.block_number, + gas_limit: payload.gas_limit, + gas_used: payload.gas_used, + timestamp: payload.timestamp, + extra_data: payload.extra_data.as_ref(), + base_fee_per_gas: payload.base_fee_per_gas.0, + claimed_hash: payload.block_hash.0, + transactions: payload.transactions.iter().map(|tx| &tx[..]).collect(), + withdrawals: Some(payload.withdrawals.as_ref()), + blob_gas: Some((payload.blob_gas_used, payload.excess_blob_gas)), + parent_beacon_block_root: Some(parent_root.0), + requests, + amsterdam: None, + } + } + + /// `keccak256(rlp(header))`. + fn computed_hash(&self) -> [u8; 32] { + // The trie roots are over opaque bytes: a transaction is already + // `type || rlp(tx)` on the wire, so it is never decoded. It is + // copied, since ethrex's trie takes owned values. + let transactions = self + .transactions + .iter() + .enumerate() + .map(|(index, tx)| (index.encode_to_vec(), tx.to_vec())); + let transactions_root = Trie::compute_hash_from_unsorted_iter(transactions, &NativeCrypto); + let withdrawals_root = self.withdrawals.map(|withdrawals| { + let withdrawals: Vec = withdrawals + .iter() + .map(|withdrawal| Withdrawal { + index: withdrawal.index, + validator_index: withdrawal.validator_index, + address: Address::from(withdrawal.address.0), + amount: withdrawal.amount, + }) + .collect(); + compute_withdrawals_root(&withdrawals, &NativeCrypto) + }); + let requests_hash = self.requests.as_ref().map(|requests| { + let requests: Vec = requests + .iter() + .map(|request| EncodedRequests(Bytes::copy_from_slice(request))) + .collect(); + compute_requests_hash(&requests) + }); + // EIP-7928: the header commits to the keccak of the RLP bytes the + // payload carries, whatever they decode to. + let block_access_list_hash = self.amsterdam.map(|(bal, _)| H256(keccak256(bal))); + let slot_number = self.amsterdam.map(|(_, slot)| slot); + let base_fee_per_gas = U256::from_little_endian(&self.base_fee_per_gas); + // An absent optional field writes nothing, so a later fork's field + // after an absent earlier one would land in the earlier one's place. + // Forks only ever add to the header's tail. + let tail = [ + withdrawals_root.is_some(), + self.blob_gas.is_some(), + self.parent_beacon_block_root.is_some(), + requests_hash.is_some(), + self.amsterdam.is_some(), + ]; + debug_assert!( + tail.windows(2).all(|pair| pair[0] || !pair[1]), + "a header field is present after an absent earlier one: {tail:?}" + ); + + let mut buf = Vec::with_capacity(700); + Encoder::new(&mut buf) + .encode_field(&H256(self.parent_hash)) + .encode_field(&H256(EMPTY_OMMERS_HASH)) + .encode_field(&Address::from(self.fee_recipient)) + .encode_field(&H256(self.state_root)) + .encode_field(&transactions_root) + .encode_field(&H256(self.receipts_root)) + .encode_field(&Bloom::from_slice(self.logs_bloom)) + .encode_field(&U256::zero()) + .encode_field(&self.block_number) + .encode_field(&self.gas_limit) + .encode_field(&self.gas_used) + .encode_field(&self.timestamp) + .encode_field(&Bytes::copy_from_slice(self.extra_data)) + .encode_field(&H256(self.prev_randao)) + .encode_field(&[0u8; 8]) + .encode_field(&base_fee_per_gas) + .encode_optional_field(&withdrawals_root) + .encode_optional_field(&self.blob_gas.map(|(used, _)| used)) + .encode_optional_field(&self.blob_gas.map(|(_, excess)| excess)) + .encode_optional_field(&self.parent_beacon_block_root.map(H256)) + .encode_optional_field(&requests_hash) + // Gloas (Amsterdam): the order of these two, and the fields + // themselves, are pinned only by a real Platåberget envelope + // (`plataberget_gloas_envelope_slot_593101.ssz`). Re-check both + // against the final EIP-7928 and EIP-7843 header before a + // mismatch is refused (lambdaclass/ethlambda#818). + .encode_optional_field(&block_access_list_hash) + .encode_optional_field(&slot_number) + .finish(); + keccak256(&buf) + } + + fn verify(&self) -> Result<(), HashMismatch> { + let computed = self.computed_hash(); + if computed == self.claimed_hash { + Ok(()) + } else { + Err(HashMismatch { + claimed: self.claimed_hash, + computed, + }) + } + } +} + +fn keccak256(data: &[u8]) -> [u8; 32] { + ethrex_common::utils::keccak(data).0 +} + +/// The block's payload hash against its header, for every fork that carries +/// the payload in the block body. +/// +/// A block before bellatrix, a bellatrix block from before the merge (its +/// payload is the empty default), and a gloas block (its payload arrives in a +/// separate envelope) have nothing to check. +pub(crate) fn verify_block(block: &SignedBeaconBlock) -> Result<(), HashMismatch> { + match block { + SignedBeaconBlock::Bellatrix(block) => { + let payload = &block.message.body.execution_payload; + // The specification's `is_execution_enabled` asks the same of a + // pre-merge block: its payload is the whole default container. + if *payload == bellatrix::ExecutionPayload::default() { + return Ok(()); + } + Fields { + parent_hash: payload.parent_hash.0, + fee_recipient: payload.fee_recipient.0, + state_root: payload.state_root.0, + receipts_root: payload.receipts_root.0, + logs_bloom: payload.logs_bloom.as_ref(), + prev_randao: payload.prev_randao.0, + block_number: payload.block_number, + gas_limit: payload.gas_limit, + gas_used: payload.gas_used, + timestamp: payload.timestamp, + extra_data: payload.extra_data.as_ref(), + base_fee_per_gas: payload.base_fee_per_gas.0, + claimed_hash: payload.block_hash.0, + transactions: payload.transactions.iter().map(|tx| &tx[..]).collect(), + withdrawals: None, + blob_gas: None, + parent_beacon_block_root: None, + requests: None, + amsterdam: None, + } + .verify() + } + SignedBeaconBlock::Capella(block) => { + let payload = &block.message.body.execution_payload; + Fields { + parent_hash: payload.parent_hash.0, + fee_recipient: payload.fee_recipient.0, + state_root: payload.state_root.0, + receipts_root: payload.receipts_root.0, + logs_bloom: payload.logs_bloom.as_ref(), + prev_randao: payload.prev_randao.0, + block_number: payload.block_number, + gas_limit: payload.gas_limit, + gas_used: payload.gas_used, + timestamp: payload.timestamp, + extra_data: payload.extra_data.as_ref(), + base_fee_per_gas: payload.base_fee_per_gas.0, + claimed_hash: payload.block_hash.0, + transactions: payload.transactions.iter().map(|tx| &tx[..]).collect(), + withdrawals: Some(payload.withdrawals.as_ref()), + blob_gas: None, + parent_beacon_block_root: None, + requests: None, + amsterdam: None, + } + .verify() + } + SignedBeaconBlock::Deneb(block) => { + let message = &block.message; + Fields::deneb(&message.body.execution_payload, message.parent_root, None).verify() + } + SignedBeaconBlock::Electra(block) | SignedBeaconBlock::Fulu(block) => { + let message = &block.message; + let requests = electra::get_execution_requests_list(&message.body.execution_requests); + let payload = &message.body.execution_payload; + Fields::deneb(payload, message.parent_root, Some(requests)).verify() + } + SignedBeaconBlock::Phase0(_) + | SignedBeaconBlock::Altair(_) + | SignedBeaconBlock::Gloas(_) + | SignedBeaconBlock::Lean(_) => Ok(()), + } +} + +/// A gloas envelope's payload hash against its header. +pub(crate) fn verify_envelope( + envelope: &gloas::SignedExecutionPayloadEnvelope, +) -> Result<(), HashMismatch> { + let message = &envelope.message; + let payload = &message.payload; + Fields { + parent_hash: payload.parent_hash.0, + fee_recipient: payload.fee_recipient.0, + state_root: payload.state_root.0, + receipts_root: payload.receipts_root.0, + logs_bloom: payload.logs_bloom.as_ref(), + prev_randao: payload.prev_randao.0, + block_number: payload.block_number, + gas_limit: payload.gas_limit, + gas_used: payload.gas_used, + timestamp: payload.timestamp, + extra_data: payload.extra_data.as_ref(), + base_fee_per_gas: payload.base_fee_per_gas.0, + claimed_hash: payload.block_hash.0, + transactions: payload.transactions.iter().map(|tx| &tx[..]).collect(), + withdrawals: Some(payload.withdrawals.as_ref()), + blob_gas: Some((payload.blob_gas_used, payload.excess_blob_gas)), + parent_beacon_block_root: Some(message.parent_beacon_block_root.0), + requests: Some(gloas_stf::get_execution_requests_list( + &message.execution_requests, + )), + amsterdam: Some((payload.block_access_list.as_ref(), payload.slot_number)), + } + .verify() +} + +/// A gossip block's verdict, given the `outcome` its stateful checks answered. +/// +/// Only an `Accept` is hashed: it is the one outcome that says the proposer's +/// signature verified, so only a proposer can make this node build the trie +/// or reach the `warn!`. A mismatch turns it into +/// `Ignore(PayloadBlockHashMismatch)`, which keeps the block from being +/// imported or relayed without scoring the peers that relayed it. Any other +/// outcome passes through unhashed. +pub(crate) fn block_verdict( + outcome: Outcome, + block: &SignedBeaconBlock, + block_root: Root, +) -> Outcome { + if outcome != Outcome::Accept { + return outcome; + } + let Err(mismatch) = verify_block(block) else { + return outcome; + }; + metrics::inc_beacon_payload_block_hash_mismatch("block"); + let slot = block.slot(); + let proposer = block.proposer_index(); + warn!( + slot, + proposer, + block_root = %ShortRoot(&block_root.0), + claimed = %ShortRoot(&mismatch.claimed), + computed = %ShortRoot(&mismatch.computed), + "Ignoring a gossip block whose payload block hash is not its header's" + ); + Outcome::Ignore(IgnoreReason::PayloadBlockHashMismatch) +} + +/// A gloas envelope's verdict, given the `outcome` its checks answered: always +/// `outcome`, with an `Accept` whose payload hash is not its header's counted +/// and logged. +/// +/// Not refused, unlike a pre-gloas block. The Amsterdam header (EIP-7928's +/// block access list hash, EIP-7843's slot number) is still moving between +/// devnets, and an envelope is judged here on gossip and on fetch alike, so a +/// header this build encodes wrongly would refuse every payload and leave the +/// node unable to follow the chain; a pre-gloas block refused on gossip still +/// comes back by root. The counter says whether the encoding agrees with the +/// chain until the header is final and this can refuse. +/// +/// A mismatch is logged at `warn` for a gossip envelope and at `debug` for a +/// fetched one: if the header did move, range sync would otherwise warn once +/// per envelope of every batch, and the counter already counts them all. +pub(crate) fn envelope_verdict( + outcome: Outcome, + envelope: &gloas::SignedExecutionPayloadEnvelope, + source: EnvelopeSource, +) -> Outcome { + if outcome != Outcome::Accept { + return outcome; + } + let _ = verify_envelope(envelope).inspect_err(|mismatch| { + metrics::inc_beacon_payload_block_hash_mismatch("envelope"); + let slot = envelope.message.payload.slot_number; + let block_root = ShortRoot(&envelope.message.beacon_block_root.0); + let claimed = ShortRoot(&mismatch.claimed); + let computed = ShortRoot(&mismatch.computed); + match source { + EnvelopeSource::Gossip => warn!( + slot, + %block_root, + %claimed, + %computed, + "An envelope's payload block hash is not its header's; not refused before the Amsterdam header is final" + ), + EnvelopeSource::Fetch => debug!( + slot, + %block_root, + %claimed, + %computed, + "A fetched envelope's payload block hash is not its header's; not refused before the Amsterdam header is final" + ), + } + }); + outcome +} + +/// Where an envelope judged by [`envelope_verdict`] came from. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum EnvelopeSource { + /// The `execution_payload` gossip topic. + Gossip, + /// An `execution_payload_envelopes_by_{range,root}` answer. + Fetch, +} + +#[cfg(test)] +mod tests; diff --git a/crates/net/p2p/src/beacon/payload_hash/tests.rs b/crates/net/p2p/src/beacon/payload_hash/tests.rs new file mode 100644 index 000000000..aec6209df --- /dev/null +++ b/crates/net/p2p/src/beacon/payload_hash/tests.rs @@ -0,0 +1,487 @@ +//! Every fork the node follows, against a payload taken from a real chain. +//! The vectors are SSZ as the Beacon API served it +//! (`beacon/testdata/payload_hash/`): +//! +//! | fork | chain | object | carries | +//! |-----------|-------------|-----------------------|----------------------------------------| +//! | bellatrix | Sepolia | block, slot 500000 | | +//! | capella | Sepolia | block, slot 1900000 | withdrawals | +//! | deneb | Hoodi | block, slot 2003 | no transactions | +//! | electra | Hoodi | block, slot 70001 | no requests | +//! | fulu | mainnet | block, slot 15684701 | 238 transactions, 3 requests (2 types) | +//! | gloas | Plataberget | envelope, slot 593101 | | +//! +//! The fulu block is finalized and canonical; its bytes are the same from +//! ethlambda's and Lodestar's Beacon API. It carries one withdrawal request +//! and two consolidation requests, so its requests hash is over two request +//! types. + +use ethlambda_state_transition::beacon::gossip::{QueueReason, RejectReason}; +use ethlambda_types::beacon::containers::electra; +use ethlambda_types::beacon::fork::ForkName; +use libssz::SszDecode as _; + +use super::*; + +const BELLATRIX: &[u8] = + include_bytes!("../testdata/payload_hash/sepolia_bellatrix_slot_500000.ssz"); +const CAPELLA: &[u8] = include_bytes!("../testdata/payload_hash/sepolia_capella_slot_1900000.ssz"); +const DENEB: &[u8] = include_bytes!("../testdata/payload_hash/hoodi_deneb_slot_2003.ssz"); +const ELECTRA: &[u8] = include_bytes!("../testdata/payload_hash/hoodi_electra_slot_70001.ssz"); +const FULU: &[u8] = include_bytes!("../testdata/payload_hash/mainnet_fulu_slot_15684701.ssz"); +const GLOAS: &[u8] = + include_bytes!("../testdata/payload_hash/plataberget_gloas_envelope_slot_593101.ssz"); + +fn block(fork: ForkName, bytes: &[u8]) -> SignedBeaconBlock { + SignedBeaconBlock::from_ssz(fork, bytes).expect("a real block decodes") +} + +fn envelope() -> gloas::SignedExecutionPayloadEnvelope { + gloas::SignedExecutionPayloadEnvelope::from_ssz_bytes(GLOAS).expect("a real envelope decodes") +} + +fn bellatrix_payload(block: &mut SignedBeaconBlock) -> &mut bellatrix::ExecutionPayload { + let SignedBeaconBlock::Bellatrix(inner) = block else { + panic!("bellatrix"); + }; + &mut inner.message.body.execution_payload +} + +fn fulu_block(block: &mut SignedBeaconBlock) -> &mut electra::SignedBeaconBlock { + let SignedBeaconBlock::Fulu(inner) = block else { + panic!("fulu"); + }; + inner +} + +/// The real fulu block with one header field changed. +fn changed_fulu_block() -> SignedBeaconBlock { + let mut changed = block(ForkName::Fulu, FULU); + fulu_block(&mut changed) + .message + .body + .execution_payload + .gas_used += 1; + changed +} + +/// An all-zero request: only its presence matters to the requests hash. +fn consolidation() -> electra::ConsolidationRequest { + electra::ConsolidationRequest::from_ssz_bytes(&[0; 116]).expect("a fixed-size container") +} + +/// An all-zero withdrawal request, for blocks whose consolidation list (two +/// at most) may already be full. +fn withdrawal_request() -> electra::WithdrawalRequest { + electra::WithdrawalRequest::from_ssz_bytes(&[0; 76]).expect("a fixed-size container") +} + +fn withdrawal() -> capella::Withdrawal { + capella::Withdrawal::from_ssz_bytes(&[0; 44]).expect("a fixed-size container") +} + +fn assert_refused(result: Result<(), HashMismatch>, what: &str) { + assert!(result.is_err(), "{what}: the hash must no longer match"); +} + +#[test] +fn every_real_payload_matches_its_hash() { + for (fork, bytes) in [ + (ForkName::Bellatrix, BELLATRIX), + (ForkName::Capella, CAPELLA), + (ForkName::Deneb, DENEB), + (ForkName::Electra, ELECTRA), + (ForkName::Fulu, FULU), + ] { + let block = block(fork, bytes); + assert_eq!(verify_block(&block), Ok(()), "{fork:?}"); + } + assert_eq!(verify_envelope(&envelope()), Ok(()), "gloas"); +} + +#[test] +fn the_vectors_exercise_what_each_fork_adds() { + let SignedBeaconBlock::Capella(capella) = block(ForkName::Capella, CAPELLA) else { + panic!("capella"); + }; + assert!( + !capella + .message + .body + .execution_payload + .withdrawals + .is_empty(), + "capella's vector must carry withdrawals" + ); + let SignedBeaconBlock::Bellatrix(bellatrix) = block(ForkName::Bellatrix, BELLATRIX) else { + panic!("bellatrix"); + }; + // Past the merge: a pre-merge block carries the empty default payload. + assert_ne!( + bellatrix.message.body.execution_payload, + bellatrix::ExecutionPayload::default() + ); + + let mut fulu = block(ForkName::Fulu, FULU); + let fulu = &fulu_block(&mut fulu).message.body; + // The transactions trie keys a transaction by `rlp(index)`: one byte up to + // index 127, two from index 128 (`0x81 0x80`), so the vector needs more + // than 128 transactions to reach a two-byte key. + assert_eq!(128usize.encode_to_vec(), [0x81, 0x80]); + assert!( + fulu.execution_payload.transactions.len() > 128, + "fulu's vector must reach a two-byte transaction key" + ); + // Real requests of more than one type, so the requests hash is over + // several typed entries and not only the empty list's. + let requests = ethlambda_state_transition::beacon::stf::electra::get_execution_requests_list( + &fulu.execution_requests, + ); + assert!( + requests.len() >= 2, + "fulu's vector must carry requests of two types" + ); + + // The empty cases on real blocks: deneb's has no transactions, so its + // hash covers the empty transactions trie, and electra's has no requests, + // so its hash covers the requests hash of an empty list. + let SignedBeaconBlock::Deneb(deneb) = block(ForkName::Deneb, DENEB) else { + panic!("deneb"); + }; + assert!(deneb.message.body.execution_payload.transactions.is_empty()); + let SignedBeaconBlock::Electra(electra_block) = block(ForkName::Electra, ELECTRA) else { + panic!("electra"); + }; + let requests = ethlambda_state_transition::beacon::stf::electra::get_execution_requests_list( + &electra_block.message.body.execution_requests, + ); + assert!( + requests.is_empty(), + "electra's vector must carry no requests" + ); + + assert!(!envelope().message.payload.transactions.is_empty()); +} + +/// No vector has an empty withdrawals list (every one from capella on carries +/// 16), so the empty withdrawals root is pinned against the execution layer's +/// empty trie root, `keccak256(rlp(""))`, instead. +#[test] +fn an_empty_withdrawals_list_has_the_empty_trie_root() { + let empty_trie_root = + hex::decode("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") + .expect("hex"); + assert_eq!(keccak256(&[0x80]).to_vec(), empty_trie_root); + assert_eq!( + compute_withdrawals_root(&[], &NativeCrypto).0.to_vec(), + empty_trie_root + ); +} + +/// An absent optional header field writes nothing, so a present one after it +/// would shift into its place: `computed_hash` asserts the fields a fork adds +/// only ever extend the header's tail. +#[test] +#[cfg(debug_assertions)] +#[should_panic(expected = "a header field is present after an absent earlier one")] +fn a_header_field_after_an_absent_one_is_caught() { + let block = block(ForkName::Electra, ELECTRA); + let SignedBeaconBlock::Electra(inner) = &block else { + panic!("electra"); + }; + let message = &inner.message; + let payload = &message.body.execution_payload; + let mut fields = Fields::deneb(payload, message.parent_root, Some(Vec::new())); + fields.blob_gas = None; + let _ = fields.computed_hash(); +} + +#[test] +fn a_changed_bellatrix_field_is_refused() { + let mut changed = block(ForkName::Bellatrix, BELLATRIX); + bellatrix_payload(&mut changed).gas_used += 1; + assert_refused(verify_block(&changed), "gas_used"); + + let mut changed = block(ForkName::Bellatrix, BELLATRIX); + bellatrix_payload(&mut changed).block_hash.0[0] ^= 1; + assert_refused(verify_block(&changed), "block_hash"); +} + +#[test] +fn a_changed_capella_withdrawal_is_refused() { + let mut changed = block(ForkName::Capella, CAPELLA); + let SignedBeaconBlock::Capella(inner) = &mut changed else { + panic!("capella"); + }; + inner.message.body.execution_payload.withdrawals[0].amount += 1; + assert_refused(verify_block(&changed), "withdrawal amount"); +} + +#[test] +fn a_changed_deneb_field_is_refused() { + for what in ["excess_blob_gas", "parent_root", "timestamp"] { + let mut changed = block(ForkName::Deneb, DENEB); + let SignedBeaconBlock::Deneb(inner) = &mut changed else { + panic!("deneb"); + }; + match what { + "excess_blob_gas" => inner.message.body.execution_payload.excess_blob_gas += 1, + // The header commits to the parent beacon block root, which the + // payload does not carry: it comes from the block. + "parent_root" => inner.message.parent_root.0[0] ^= 1, + _ => inner.message.body.execution_payload.timestamp += 1, + } + assert_refused(verify_block(&changed), what); + } +} + +#[test] +fn a_changed_electra_or_fulu_field_is_refused() { + for (fork, bytes) in [(ForkName::Electra, ELECTRA), (ForkName::Fulu, FULU)] { + for what in [ + "transaction", + "parent_root", + "request", + "existing_request", + "extra_data", + ] { + let mut changed = block(fork, bytes); + let (SignedBeaconBlock::Electra(inner) | SignedBeaconBlock::Fulu(inner)) = &mut changed + else { + panic!("electra or fulu"); + }; + match what { + "transaction" => { + let payload = &mut inner.message.body.execution_payload; + let last = payload.transactions.len() - 1; + let tx = &mut payload.transactions[last]; + let end = tx.len() - 1; + tx[end] ^= 1; + } + "parent_root" => inner.message.parent_root.0[0] ^= 1, + // The requests hash covers a request however empty its + // payload: one more withdrawal request moves it. + "request" => inner + .message + .body + .execution_requests + .withdrawals + .push(withdrawal_request()) + .expect("room for one more request"), + // A changed field inside a request the block already + // carries; only the fulu vector has one. + "existing_request" => { + let requests = &mut inner.message.body.execution_requests; + if requests.consolidations.is_empty() { + continue; + } + requests.consolidations[0].source_address.0[0] ^= 1; + } + _ => inner + .message + .body + .execution_payload + .extra_data + .push(1) + .expect("room for one more byte"), + } + assert_refused(verify_block(&changed), &format!("{fork:?} {what}")); + } + } +} + +#[test] +fn a_changed_gloas_field_is_refused() { + for what in [ + "slot_number", + "block_access_list", + "parent_beacon_block_root", + "execution_requests", + "withdrawal", + "transaction", + "base_fee_per_gas", + ] { + let mut changed = envelope(); + let message = &mut changed.message; + match what { + "slot_number" => message.payload.slot_number += 1, + "block_access_list" => message.payload.block_access_list.push(0), + "parent_beacon_block_root" => message.parent_beacon_block_root.0[0] ^= 1, + "execution_requests" => message + .execution_requests + .consolidations + .push(consolidation()), + "withdrawal" => message.payload.withdrawals.push(withdrawal()), + "transaction" => { + let last = message.payload.transactions.len() - 1; + let tx = &mut message.payload.transactions[last]; + let end = tx.len() - 1; + tx[end] ^= 1; + } + _ => message.payload.base_fee_per_gas.0[31] ^= 1, + } + assert_refused(verify_envelope(&changed), what); + } +} + +#[test] +fn a_pre_merge_bellatrix_payload_has_nothing_to_check() { + // Before the merge a block carries the empty default payload. + let mut empty = block(ForkName::Bellatrix, BELLATRIX); + *bellatrix_payload(&mut empty) = bellatrix::ExecutionPayload::default(); + assert_eq!(verify_block(&empty), Ok(())); +} + +/// The pre-merge skip is the whole default payload, as the specification's +/// `is_execution_enabled` has it, not two zero hashes. +#[test] +fn a_payload_with_zero_hashes_but_other_fields_is_checked() { + let mut zero_hashes = block(ForkName::Bellatrix, BELLATRIX); + let payload = bellatrix_payload(&mut zero_hashes); + payload.block_hash = Default::default(); + payload.parent_hash = Default::default(); + assert_refused( + verify_block(&zero_hashes), + "zero hashes over a real payload", + ); +} + +#[test] +fn a_block_mismatch_is_ignored_once_its_checks_accept() { + let changed = changed_fulu_block(); + let root = Root::ZERO; + let outcome = block_verdict(Outcome::Accept, &changed, root); + // IGNORE, never REJECT: other clients relay such a block. + assert_eq!(outcome.labels(), ("ignore", "payload_block_hash_mismatch")); + assert_eq!( + block_verdict(Outcome::Accept, &block(ForkName::Fulu, FULU), root), + Outcome::Accept + ); +} + +/// Anything but `Accept` passes through unhashed: a queued block may not have +/// had its signature checked yet, and a rejected one is refused already. +#[test] +fn a_block_that_was_not_accepted_keeps_its_verdict() { + let changed = changed_fulu_block(); + for outcome in [ + Outcome::Queue(QueueReason::ParentUnknown), + Outcome::Ignore(IgnoreReason::Overloaded), + Outcome::Reject(RejectReason::BadSignature), + ] { + assert_eq!(block_verdict(outcome, &changed, Root::ZERO), outcome); + } +} + +/// Gloas only counts a mismatch until the Amsterdam header is final: an +/// encoding this build gets wrong must not refuse every payload. +#[test] +fn a_gloas_mismatch_is_counted_but_not_refused() { + let mut changed = envelope(); + changed.message.payload.gas_used += 1; + assert!(verify_envelope(&changed).is_err()); + for source in [EnvelopeSource::Gossip, EnvelopeSource::Fetch] { + let accepted = envelope_verdict(Outcome::Accept, &changed, source); + assert_eq!(accepted, Outcome::Accept, "{source:?}"); + let queued = Outcome::Queue(QueueReason::StateNotCached); + assert_eq!(envelope_verdict(queued, &changed, source), queued); + } +} + +/// Not a check: how long [`verify_block`] takes, copies included, on the real +/// fulu vector and on fulu blocks filled to the gossip size limit. `#[ignore]`d +/// because it only prints. +/// +/// ```text +/// cargo test -p ethlambda-p2p --profile release-fast --lib \ +/// payload_hash::tests::verify_block_cost -- --ignored --nocapture --test-threads=1 +/// ``` +#[test] +#[ignore = "benchmark: prints how long verify_block takes"] +fn verify_block_cost() { + use std::hint::black_box; + use std::time::{Duration, Instant}; + + use ethlambda_types::beacon::containers::bellatrix::{Transaction, Transactions}; + use ethlambda_types::beacon::preset; + + use crate::req_resp::MAX_PAYLOAD_SIZE; + + fn time(label: &str, block: &SignedBeaconBlock, runs: u32) { + let SignedBeaconBlock::Fulu(inner) = block else { + panic!("fulu"); + }; + let transactions = inner.message.body.execution_payload.transactions.len(); + let bytes = block.to_ssz().len(); + assert!(bytes <= MAX_PAYLOAD_SIZE, "{label}: over the gossip limit"); + let _ = black_box(verify_block(black_box(block))); + let mut fastest = Duration::MAX; + let mut total = Duration::ZERO; + for _ in 0..runs { + let start = Instant::now(); + let _ = black_box(verify_block(black_box(block))); + let elapsed = start.elapsed(); + fastest = fastest.min(elapsed); + total += elapsed; + } + println!( + "verify_block_cost {label}: {transactions} transactions, {bytes} SSZ bytes, \ + mean {:?}, min {fastest:?} over {runs} runs", + total / runs + ); + } + + /// The fulu vector with its transactions replaced by `count` of `len` + /// bytes each. + fn filled(count: usize, len: usize) -> SignedBeaconBlock { + let mut filled = block(ForkName::Fulu, FULU); + let transactions: Vec = (0..count) + .map(|index| { + let mut tx = vec![0x02u8; len]; + let tail = (index as u32).to_le_bytes(); + let keep = len.min(tail.len()); + tx[len - keep..].copy_from_slice(&tail[..keep]); + Transaction::try_from(tx).expect("within MAX_BYTES_PER_TRANSACTION") + }) + .collect(); + fulu_block(&mut filled) + .message + .body + .execution_payload + .transactions = + Transactions::try_from(transactions).expect("within MAX_TRANSACTIONS_PER_PAYLOAD"); + filled + } + + let real = block(ForkName::Fulu, FULU); + time("mainnet vector", &real, 200); + + // What the block takes besides its transactions; each transaction adds + // its bytes and a four-byte offset. + let empty = filled(0, 0); + let room = MAX_PAYLOAD_SIZE - empty.to_ssz().len(); + + // As many transactions as the list allows, as small as the room leaves: + // the most trie insertions a block at the limit can ask for. + let count = preset::MAX_TRANSACTIONS_PER_PAYLOAD; + time( + "limit, most transactions", + &filled(count, room / count - 4), + 5, + ); + + // The mainnet vector's mean transaction size. + let SignedBeaconBlock::Fulu(inner) = &real else { + panic!("fulu"); + }; + let real_transactions = &inner.message.body.execution_payload.transactions; + let mean = real_transactions.iter().map(|tx| tx.len()).sum::() / real_transactions.len(); + time( + "limit, mainnet-sized transactions", + &filled(room / (mean + 4), mean), + 10, + ); + + // One transaction holding the whole room: keccak over ten megabytes. + time("limit, one transaction", &filled(1, room - 4), 10); +} diff --git a/crates/net/p2p/src/beacon/testdata/payload_hash/hoodi_deneb_slot_2003.ssz b/crates/net/p2p/src/beacon/testdata/payload_hash/hoodi_deneb_slot_2003.ssz new file mode 100644 index 000000000..0520bc7e8 Binary files /dev/null and b/crates/net/p2p/src/beacon/testdata/payload_hash/hoodi_deneb_slot_2003.ssz differ diff --git a/crates/net/p2p/src/beacon/testdata/payload_hash/hoodi_electra_slot_70001.ssz b/crates/net/p2p/src/beacon/testdata/payload_hash/hoodi_electra_slot_70001.ssz new file mode 100644 index 000000000..9d745df6c Binary files /dev/null and b/crates/net/p2p/src/beacon/testdata/payload_hash/hoodi_electra_slot_70001.ssz differ diff --git a/crates/net/p2p/src/beacon/testdata/payload_hash/mainnet_fulu_slot_15684701.ssz b/crates/net/p2p/src/beacon/testdata/payload_hash/mainnet_fulu_slot_15684701.ssz new file mode 100644 index 000000000..16e6e25ee Binary files /dev/null and b/crates/net/p2p/src/beacon/testdata/payload_hash/mainnet_fulu_slot_15684701.ssz differ diff --git a/crates/net/p2p/src/beacon/testdata/payload_hash/plataberget_gloas_envelope_slot_593101.ssz b/crates/net/p2p/src/beacon/testdata/payload_hash/plataberget_gloas_envelope_slot_593101.ssz new file mode 100644 index 000000000..b541030e9 Binary files /dev/null and b/crates/net/p2p/src/beacon/testdata/payload_hash/plataberget_gloas_envelope_slot_593101.ssz differ diff --git a/crates/net/p2p/src/beacon/testdata/payload_hash/sepolia_bellatrix_slot_500000.ssz b/crates/net/p2p/src/beacon/testdata/payload_hash/sepolia_bellatrix_slot_500000.ssz new file mode 100644 index 000000000..3f721faea Binary files /dev/null and b/crates/net/p2p/src/beacon/testdata/payload_hash/sepolia_bellatrix_slot_500000.ssz differ diff --git a/crates/net/p2p/src/beacon/testdata/payload_hash/sepolia_capella_slot_1900000.ssz b/crates/net/p2p/src/beacon/testdata/payload_hash/sepolia_capella_slot_1900000.ssz new file mode 100644 index 000000000..e8edef812 Binary files /dev/null and b/crates/net/p2p/src/beacon/testdata/payload_hash/sepolia_capella_slot_1900000.ssz differ diff --git a/crates/net/p2p/src/beacon/verdict.rs b/crates/net/p2p/src/beacon/verdict.rs index ab5651c26..f2b327a0b 100644 --- a/crates/net/p2p/src/beacon/verdict.rs +++ b/crates/net/p2p/src/beacon/verdict.rs @@ -40,6 +40,7 @@ use spawned_concurrency::message::Message; use spawned_concurrency::tasks::{Context, Handler}; use tracing::{debug, error, warn}; +use crate::beacon::payload_hash::{self, EnvelopeSource}; use crate::beacon::{builder_market, column_checks}; use crate::gossipsub::operation_kind; use crate::{P2PServer, metrics}; @@ -121,8 +122,12 @@ impl Validated { /// them. fn stateful_checks(&mut self, store: &Store) -> Outcome { match self { + // The payload block hash rule (`payload_hash`), stricter than the + // specification, runs only once the topic's own rules accept, + // that is once the proposer's or builder's signature verified. Self::Block { block, block_root } => { - gossip::block::stateful_checks(store, block, *block_root) + let outcome = gossip::block::stateful_checks(store, block, *block_root); + payload_hash::block_verdict(outcome, block, *block_root) } Self::Column(sidecar) => match &**sidecar { DataColumnSidecar::Fulu(sidecar) => gossip::column::stateful_checks(store, sidecar), @@ -144,7 +149,10 @@ impl Validated { attestation, subnet_id, } => gossip::attestation::stateful_checks(store, attestation, *subnet_id), - Self::Envelope(envelope) => gossip::envelope::stateful_checks(store, envelope), + Self::Envelope(envelope) => { + let outcome = gossip::envelope::stateful_checks(store, envelope); + payload_hash::envelope_verdict(outcome, envelope, EnvelopeSource::Gossip) + } Self::ExecutionPayloadBid { bid, market } => { gossip::execution_payload_bid::stateful_checks(store, market, bid) } @@ -537,9 +545,21 @@ impl Message for GossipVerdict { /// validation on separate blocking threads at once, and only the first /// `Accept` to reach the actor stands, which is the specification's "first /// valid". Any other outcome passes through unrecorded, since only an Accept -/// is a candidate for the seen cache in the first place. +/// is a candidate for the seen cache in the first place, with one exception. +/// +/// A block IGNOREd for its payload block hash passed every rule the +/// specification has, signature included, so it is the specification's "first +/// block with a valid signature" for its slot and proposer, and is recorded +/// too, as `AlreadySeen` when another one got there first. Otherwise a +/// proposer could send any number of signed blocks for one slot, each +/// verified and hashed in full, where the cheap checks would IGNORE every one +/// after the first. fn settle(server: &mut P2PServer, outcome: Outcome, object: &Validated) -> Outcome { - if outcome == Outcome::Accept && !object.record_seen(server) { + let seen_candidate = matches!( + outcome, + Outcome::Accept | Outcome::Ignore(IgnoreReason::PayloadBlockHashMismatch) + ); + if seen_candidate && !object.record_seen(server) { Outcome::Ignore(IgnoreReason::AlreadySeen) } else { outcome @@ -553,11 +573,18 @@ impl Handler for P2PServer { outcome, object, } = msg; - let outcome = settle(self, outcome, &object); - let received_at = id.received_at; - if report(self, id, outcome) { - object.forward(self, received_at, outcome); - } + conclude(self, id, outcome, object); + } +} + +/// What the actor does with a blocking task's verdict: settle it against the +/// seen cache, report it to gossipsub, and hand the object on only when the +/// outcome allows it ([`disposition`]). +fn conclude(server: &mut P2PServer, id: GossipId, outcome: Outcome, object: Validated) { + let outcome = settle(server, outcome, &object); + let received_at = id.received_at; + if report(server, id, outcome) { + object.forward(server, received_at, outcome); } } @@ -943,6 +970,78 @@ mod tests { ); } + /// A block IGNOREd for its payload hash had a valid signature, so its slot + /// and proposer are marked seen: another block for them is IGNOREd by the + /// cheap checks, before any signature or hash work, and a later `Accept` + /// for them is `AlreadySeen`. So is a later mismatch, which was checked + /// concurrently and lost the race to the first. + #[tokio::test] + async fn a_payload_hash_mismatch_marks_its_slot_and_proposer_seen() { + let mut server = unconnected_beacon_server(Config::mainnet(), 0).await; + let block = |root: u8| Validated::Block { + block: Box::new(fulu_block(5, 1)), + block_root: Root::repeat_byte(root), + }; + let mismatch = Outcome::Ignore(IgnoreReason::PayloadBlockHashMismatch); + + assert_eq!(settle(&mut server, mismatch, &block(1)), mismatch); + + let now_ms = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .expect("after the epoch") + .as_millis() as u64; + let second = fulu_block(5, 1); + assert_eq!( + gossip::block::cheap_checks(&server.seen_blocks, &server.store, &second, now_ms), + Err(Outcome::Ignore(IgnoreReason::AlreadySeen)) + ); + assert_eq!( + settle(&mut server, Outcome::Accept, &block(2)), + Outcome::Ignore(IgnoreReason::AlreadySeen) + ); + assert_eq!( + settle(&mut server, mismatch, &block(3)), + Outcome::Ignore(IgnoreReason::AlreadySeen) + ); + } + + /// A block whose payload block hash is not its header's ends in + /// `Ignore(PayloadBlockHashMismatch)`, which neither relays it nor hands + /// it to the chain actor. `Ignore(Overloaded)` is the one ignore that + /// still forwards a block, and only from `spawn_stateful_checks`, when no + /// permit was free and no check ran. + #[tokio::test] + async fn a_block_with_a_mismatched_payload_hash_is_not_imported_from_gossip() { + let mut server = unconnected_beacon_server(Config::mainnet(), 0).await; + let chain = Arc::new(RecordingChain::default()); + server.blockchain = Some(chain.clone()); + let id = |byte: u8| GossipId { + message_id: MessageId::new(&[byte]), + propagation_source: PeerId::random(), + received_at: Instant::now(), + kind: "beacon_block", + }; + let block = |slot| Validated::Block { + block: Box::new(fulu_block(slot, 1)), + block_root: Root::repeat_byte(slot as u8), + }; + + let mismatch = Outcome::Ignore(IgnoreReason::PayloadBlockHashMismatch); + assert!(matches!( + disposition(mismatch), + (MessageAcceptance::Ignore, false) + )); + conclude(&mut server, id(1), mismatch, block(5)); + assert!(chain.announcements.lock().unwrap().is_empty()); + + // The same path does forward an accepted block. + conclude(&mut server, id(2), Outcome::Accept, block(6)); + assert_eq!( + *chain.announcements.lock().unwrap(), + [BlockAnnouncement::Announce] + ); + } + #[tokio::test] async fn an_overloaded_aggregate_is_not_forwarded() { let mut server = unconnected_beacon_server(Config::mainnet(), 0).await; diff --git a/crates/net/p2p/src/gossipsub/handler.rs b/crates/net/p2p/src/gossipsub/handler.rs index d6b90bce0..3e13f7e5b 100644 --- a/crates/net/p2p/src/gossipsub/handler.rs +++ b/crates/net/p2p/src/gossipsub/handler.rs @@ -1780,6 +1780,24 @@ mod tests { )); } + /// Triage never hashes a payload: an envelope whose block hash is not its + /// header's still goes on to the stateful checks, where the payload hash + /// is looked at only after the builder's signature verified. + #[tokio::test] + async fn a_wrong_payload_block_hash_is_not_judged_in_triage() { + let server = unconnected_beacon_server(Config::mainnet(), 0).await; + let envelope = crate::test_support::envelope(1, 3); + assert!( + crate::beacon::payload_hash::verify_envelope(&envelope).is_err(), + "the test envelope's block hash is arbitrary" + ); + + assert!(matches!( + triage_envelope(&server, &envelope.to_ssz()), + Dispatch::Validate(Validated::Envelope(_)) + )); + } + /// A payload attestation for a slot before gloas is rejected by the cheap /// checks; mainnet's gloas epoch is not zero, so slot zero is before it. #[tokio::test] diff --git a/crates/net/p2p/src/metrics.rs b/crates/net/p2p/src/metrics.rs index f2e9114be..5fb152e86 100644 --- a/crates/net/p2p/src/metrics.rs +++ b/crates/net/p2p/src/metrics.rs @@ -476,6 +476,24 @@ pub fn inc_beacon_gossip_verdict_expired(kind: &str) { .inc(); } +static LEAN_BEACON_PAYLOAD_BLOCK_HASH_MISMATCH_TOTAL: LazyLock = LazyLock::new( + || { + register_int_counter_vec!( + "lean_beacon_payload_block_hash_mismatch_total", + "Signed execution payloads whose block hash is not the hash of the header rebuilt from their fields, by kind (block: a gossip block, IGNOREd; envelope: counted, not refused)", + &["kind"] + ) + .unwrap() + }, +); + +/// Count one signed payload whose block hash is not its header's. +pub fn inc_beacon_payload_block_hash_mismatch(kind: &str) { + LEAN_BEACON_PAYLOAD_BLOCK_HASH_MISMATCH_TOTAL + .with_label_values(&[kind]) + .inc(); +} + pub fn inc_beacon_status_digest_mismatch() { LEAN_BEACON_STATUS_DIGEST_MISMATCH_TOTAL.inc(); } diff --git a/docs/beacon_wire.md b/docs/beacon_wire.md index e88914c25..3370a7431 100644 --- a/docs/beacon_wire.md +++ b/docs/beacon_wire.md @@ -212,6 +212,19 @@ an unbounded progressive bitlist, so one longer than electra's type bound is REJECTed first, reading its length without expanding it, since expanding is what the seen-set check does before any other. +One rule on `beacon_block` (bellatrix to fulu) is stricter than the +specification: the payload's `block_hash` must equal the hash of the execution +header rebuilt from the payload's own fields, or the block is IGNOREd +(`beacon/payload_hash.rs`; the reason is `payload_block_hash_mismatch`, and +`lean_beacon_payload_block_hash_mismatch_total{kind}` counts it). It runs at the +end of the stateful half, on a block the topic's rules accepted, so only a block +whose proposer signature verified is hashed. IGNORE rather than REJECT, since +other clients have no such rule and relay the block. A block answered `Queue` +or forwarded under `Overloaded` is not hashed. `execution_payload` envelopes +are hashed the same way once accepted, but a mismatch is only counted and +logged until the Amsterdam header is final. See +[spec_deviations.md](./spec_deviations.md#a-payloads-block-hash-must-be-its-headers). + Three deliberate departures from the gloas gossip rules, each for want of state this node does not keep. Where the specification rejects a message whose block failed validation, this node (having no bad-block cache) cannot tell that from @@ -641,7 +654,8 @@ not imported only briefly and in small numbers, so a batch's envelopes must arrive before the next batch's blocks do. Everything fetched goes through the same rules as an `execution_payload` gossip message (`beacon/envelope_checks.rs`) before the actor sees it; a `Queue` verdict is -forwarded too. +forwarded too. An accepted one also has its payload block hash checked, as +above, which counts a mismatch without refusing the envelope. ### Data column sidecars @@ -903,6 +917,7 @@ as a query filter, so a `quic`-only record is invisible to it. | `lean_beacon_gossip_messages_total{topic,result}` | Gossip received, by topic and by `decoded` / `decode_failed` / `decompress_failed` / `unsupported_fork` | | `lean_beacon_gossip_validation_total{kind,outcome,reason}` | Gossip verdicts, now including `beacon_aggregate_and_proof` and `beacon_attestation`; see [metrics.md](./metrics.md#beacon-gossip-validation) | | `lean_beacon_gossip_verdict_expired_total{kind}` | Verdicts that came too late to propagate anything; should stay at zero | +| `lean_beacon_payload_block_hash_mismatch_total{kind}` | Signed payloads whose `block_hash` is not the hash of the header rebuilt from their fields: `block` (a gossip block, IGNOREd) / `envelope` (counted, not refused) | | `lean_beacon_status_digest_mismatch_total` | Handshakes seen from another fork digest | | `lean_beacon_fork_digest{digest}` | The digest computed at startup, as a label | | `lean_beacon_aggregate_decode_seconds` | Time spent decoding one aggregate off the wire | diff --git a/docs/metrics.md b/docs/metrics.md index e1a19b128..4ab588709 100644 --- a/docs/metrics.md +++ b/docs/metrics.md @@ -455,7 +455,10 @@ and `subcommittee_index` (contribution only) on the reject side; they reuse `not_current_slot`, `already_seen`, `covered_bits`, `no_participants`, `not_aggregator`, `not_in_committee`, `unknown_validator`, `selection_proof`, `aggregator_signature`, `aggregate_signature`, `wrong_subnet` and -`bad_signature`. `already_seen`, `overloaded` and `unsupported_fork` are shared with the +`bad_signature`. `beacon_block` adds `payload_block_hash_mismatch` on the ignore +side, a rule the specification does not have (see +[spec_deviations.md](./spec_deviations.md#a-payloads-block-hash-must-be-its-headers)). +`already_seen`, `overloaded` and `unsupported_fork` are shared with the other topics: `unsupported_fork` is an ignore reason for a message of a fork this build has no gossip rules for, so an honest peer past the fork epoch is not scored as a bad decoder. Gloas blocks, data columns, aggregates and attestations diff --git a/docs/spec_deviations.md b/docs/spec_deviations.md index 9e32cfeb0..6b8920b7e 100644 --- a/docs/spec_deviations.md +++ b/docs/spec_deviations.md @@ -527,6 +527,89 @@ tolerances so that the tests compare like with like. - **Equivalence:** each is the specification's answer wherever the specification has one; they differ only where it would have raised. +## A payload's block hash must be its header's + +A `beacon_block` gossip message (bellatrix to fulu) whose payload `block_hash` +is not `keccak256(rlp(header))`, for the execution block header rebuilt from the +payload's own fields, is IGNOREd once the topic's own rules have accepted it. +Gloas envelopes, from gossip or fetched, are checked the same way but only +counted. + +- **The specification:** has no such rule. A hash that does not match is the + execution client's to find, when the chain actor hands it the payload. +- **ethlambda:** rebuilds the header in `ethlambda-p2p` + (`beacon/payload_hash.rs`) at the end of the stateful half, and only for a + message those rules answered `Accept`, so the proposer's (or the builder's) + signature has verified: an unsigned message never makes the node build a + transaction trie. The header takes the payload's fields, the transactions + and withdrawals tries, the parent beacon block root (the block's + `parent_root`; the envelope's own field in gloas), the EIP-7685 requests hash + (the Engine API's request list, `get_execution_requests_list`), and in gloas + the keccak of the block access list bytes (EIP-7928) and the slot number + (EIP-7843). It is run per fork against real chain data, including a mainnet + block with more than 128 transactions and requests of two types: see the + vector table in the module's tests. A bellatrix payload that is the whole empty + default (before the merge, the specification's `is_execution_enabled`) is + skipped. +- **IGNORE, not REJECT:** other clients have no such rule and relay a validly + signed block whatever its `block_hash`, so a REJECT would count an invalid + delivery against every honest peer that forwarded one, and a proposer + publishing such blocks could drive those peers toward the graylist, + isolating only this node. IGNORE keeps the block from being imported or + relayed from gossip without scoring anyone. +- **Gloas is counted, not refused:** the Amsterdam header (the block access + list hash, the slot number) may still change between devnets, and envelopes + are judged on gossip and on fetch alike, so an encoding this build gets wrong + would refuse every payload and leave the node unable to follow the chain. + Refusing waits until the header is final. +- **Why:** the Engine API already requires the execution client to validate + `blockHash` on every `engine_newPayload`, even while syncing (execution-apis + `paris.md`), so while an import waits for that answer this finds nothing the + execution client would not. It exists for the early fork-choice import being + worked on in lambdaclass/ethlambda#773, which applies a payload before the + execution client answers and reverts it on `INVALID`: a gossip block caught + here saves that path a revert. Until that lands there is no revert to save: + what the rule changes is that such a block is neither relayed (an `Accept` + verdict comes before the execution client is asked) nor handed to the chain + actor from gossip, whose import would have failed on the execution client's + answer anyway. +- **Not covered:** a block fetched by `beacon_blocks_by_range/root`, and a + gossip block answered `Queue` or forwarded under `Ignore(Overloaded)`, reach + the chain actor unhashed. The execution client still checks them. That + includes a block this rule IGNOREd on gossip: if a child arrives naming it + as its parent, it is fetched by root and imported unhashed, so for that block + the early import's revert is delayed rather than saved. +- **Cost and counter:** one transactions trie plus the RLP and keccak of the + header, on the blocking validation thread and inside its permit + (`gossip_validation_permits` on gossip, `column_check_permits` for a fetched + envelope). Measured with the `#[ignore]`d + `payload_hash::tests::verify_block_cost` (`release-fast`, Apple M3 Pro, load + average about 20 on 11 cores; reruns under a load near 100 were up to five + times slower): about 0.24 ms on the mainnet vector (238 transactions, + 114 kB). At the 10 MiB gossip limit: 11 ms for one transaction, 19 ms for + 14,882 transactions of about 700 bytes (a mainnet block's mean size), and + 0.6 s for the worst case, 1,048,576 five-byte transactions (the most the + list allows). Only a block whose proposer signature verified can ask for + that, and once its verdict reaches the p2p actor its slot and proposer are + marked seen, so the cheap checks IGNORE any other block for them. Blocks + already in validation are not covered: a proposer that publishes several + signed equivocations at once has each hashed until the first verdict lands, + up to the 128 `gossip_validation_permits` at a time (a message arriving with + none free is forwarded unhashed under `Overloaded`). That pool is shared with + data columns and envelopes only: the attestation, sync committee and builder + topics have pools of their own. The signature check has the same window; + only its per-block cost is smaller. + `lean_beacon_payload_block_hash_mismatch_total{kind}` counts mismatches + (`block` IGNOREd, `envelope` counted only). A `warn!` names the slot, + proposer and root of a block, and the slot and root of a gossip envelope; a + fetched envelope's mismatch is logged at `debug`, so a sync does not log one + per envelope. +- **Equivalence:** a stricter filter, never a looser one, as long as this + header encoding is the execution layer's: every block it ignores then + carries a payload the execution client would call `INVALID`. The real-chain + vectors are what pin the encoding per fork, and gloas only counts a mismatch + because its header is not final yet. + ## The envelope check reads the cached state root `verify_execution_payload_envelope` compares `envelope.beacon_block_root` with