From 43c24d3ee2168768c9f21fdb8c7c8bb982f5b50a Mon Sep 17 00:00:00 2001 From: Eitan Seri- Levi Date: Tue, 10 Feb 2026 09:15:50 -0800 Subject: [PATCH 1/9] init payload processing --- beacon_node/beacon_chain/src/beacon_chain.rs | 18 + .../beacon_chain/src/block_verification.rs | 2 +- .../beacon_chain/src/execution_payload.rs | 1 + beacon_node/beacon_chain/src/lib.rs | 2 + .../src/payload_envelope_verification.rs | 419 ++++++++++++++++++ .../payload_envelope_verification_types.rs | 31 ++ 6 files changed, 472 insertions(+), 1 deletion(-) create mode 100644 beacon_node/beacon_chain/src/payload_envelope_verification.rs create mode 100644 beacon_node/beacon_chain/src/payload_envelope_verification_types.rs diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index ec791537854..a59508a37a0 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -1147,6 +1147,24 @@ impl BeaconChain { } } + + /// Returns the full block at the given root, if it's available in the database. + /// + /// Should always return a full block for pre-merge and post-gloas blocks. + pub fn get_full_block( + &self, + block_root: &Hash256, + ) -> Result>, Error> { + match self.store.try_get_full_block(block_root)? { + Some(DatabaseBlock::Full(block)) => Ok(Some(block)), + Some(DatabaseBlock::Blinded(_)) => { + // TODO(gloas) this should error out + todo!() + } + None => Ok(None), + } + } + /// Returns the block at the given root, if any. /// /// ## Errors diff --git a/beacon_node/beacon_chain/src/block_verification.rs b/beacon_node/beacon_chain/src/block_verification.rs index 9bb6757341d..9d1b8e0d3d8 100644 --- a/beacon_node/beacon_chain/src/block_verification.rs +++ b/beacon_node/beacon_chain/src/block_verification.rs @@ -718,7 +718,7 @@ pub struct SignatureVerifiedBlock { } /// Used to await the result of executing payload with an EE. -type PayloadVerificationHandle = JoinHandle>>; +pub type PayloadVerificationHandle = JoinHandle>>; /// A wrapper around a `SignedBeaconBlock` that indicates that this block is fully verified and /// ready to import into the `BeaconChain`. The validation includes: diff --git a/beacon_node/beacon_chain/src/execution_payload.rs b/beacon_node/beacon_chain/src/execution_payload.rs index bdf3ab95949..370f044c7aa 100644 --- a/beacon_node/beacon_chain/src/execution_payload.rs +++ b/beacon_node/beacon_chain/src/execution_payload.rs @@ -7,6 +7,7 @@ //! So, this module contains functions that one might expect to find in other crates, but they live //! here for good reason. +use crate::payload_envelope_verification::EnvelopeError; use crate::{ BeaconChain, BeaconChainError, BeaconChainTypes, BlockError, BlockProductionError, ExecutionPayloadError, diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index e77739e2d53..7aa64deb1ca 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -42,6 +42,8 @@ pub mod observed_block_producers; pub mod observed_data_sidecars; pub mod observed_operations; mod observed_slashable; +pub mod payload_envelope_verification; +pub mod payload_envelope_verification_types; pub mod persisted_beacon_chain; pub mod persisted_custody; mod persisted_fork_choice; diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification.rs b/beacon_node/beacon_chain/src/payload_envelope_verification.rs new file mode 100644 index 00000000000..b22acd69fbe --- /dev/null +++ b/beacon_node/beacon_chain/src/payload_envelope_verification.rs @@ -0,0 +1,419 @@ +//! The incremental processing steps (e.g., signatures verified but not the state transition) is +//! represented as a sequence of wrapper-types around the block. There is a linear progression of +//! types, starting at a `SignedBeaconBlock` and finishing with a `Fully VerifiedBlock` (see +//! diagram below). +//! +//! ```ignore +//! START +//! | +//! ▼ +//! SignedExecutionPayloadEnvelope +//! | +//! |--------------- +//! | | +//! | ▼ +//! | GossipVerifiedEnvelope +//! | | +//! |--------------- +//! | +//! ▼ +//! ExecutionPendingEnvelope +//! | +//! await +//! | +//! ▼ +//! END +//! +//! ``` + +use crate::NotifyExecutionLayer; +use crate::block_verification::{PayloadVerificationHandle, PayloadVerificationOutcome}; +use crate::payload_envelope_verification_types::{EnvelopeImportData, MaybeAvailableEnvelope}; +use crate::execution_payload::PayloadNotifier; +use crate::{BeaconChain, BeaconChainError, BeaconChainTypes}; +use educe::Educe; +use slot_clock::SlotClock; +use state_processing::envelope_processing::{EnvelopeProcessingError, process_execution_payload_envelope}; +use state_processing::{BlockProcessingError, VerifySignatures}; +use std::sync::Arc; +use tracing::{debug, instrument}; +use types::{ + BeaconState, BeaconStateError, EthSpec, ExecutionBlockHash, Hash256, SignedBeaconBlock, + SignedExecutionPayloadEnvelope, Slot, +}; + +#[derive(Debug, Clone)] +pub enum EnvelopeError { + /// The envelope's block root is unknown. + BlockRootUnknown { + block_root: Hash256, + }, + /// The signature is invalid. + BadSignature, + /// The builder index doesn't match the committed bid + BuilderIndexMismatch { + committed_bid: u64, + envelope: u64, + }, + // The envelope slot doesn't match the block + SlotMismatch { + block: Slot, + envelope: Slot, + }, + // The validator index is unknown + UnknownValidator { + builder_index: u64, + }, + // The block hash doesn't match the committed bid + BlockHashMismatch { + committed_bid: ExecutionBlockHash, + envelope: ExecutionBlockHash, + }, + // Some Beacon Chain Error + BeaconChainError(Arc), + // Some Beacon State error + BeaconStateError(BeaconStateError), + // Some BlockProcessingError (for electra operations) + BlockProcessingError(BlockProcessingError), + // Some EnvelopeProcessingError + EnvelopeProcessingError(EnvelopeProcessingError), +} + +impl From for EnvelopeError { + fn from(e: BeaconChainError) -> Self { + EnvelopeError::BeaconChainError(Arc::new(e)) + } +} + +impl From for EnvelopeError { + fn from(e: BeaconStateError) -> Self { + EnvelopeError::BeaconStateError(e) + } +} + +/// Pull errors up from EnvelopeProcessingError to EnvelopeError +impl From for EnvelopeError { + fn from(e: EnvelopeProcessingError) -> Self { + match e { + EnvelopeProcessingError::BadSignature => EnvelopeError::BadSignature, + EnvelopeProcessingError::BeaconStateError(e) => EnvelopeError::BeaconStateError(e), + EnvelopeProcessingError::BlockHashMismatch { + committed_bid, + envelope, + } => EnvelopeError::BlockHashMismatch { + committed_bid, + envelope, + }, + EnvelopeProcessingError::BlockProcessingError(e) => { + EnvelopeError::BlockProcessingError(e) + } + e => EnvelopeError::EnvelopeProcessingError(e), + } + } +} + +/// This snapshot is to be used for verifying a envelope of the block. +#[derive(Debug, Clone)] +pub struct EnvelopeProcessingSnapshot { + /// This state is equivalent to the `self.beacon_block.state_root()` before applying the envelope. + pub pre_state: BeaconState, + pub state_root: Hash256, + pub beacon_block_root: Hash256, +} + +#[allow(clippy::type_complexity)] +#[instrument(skip_all, level = "debug", fields(beacon_block_root = %envelope.beacon_block_root()))] +fn load_snapshot( + envelope: &SignedExecutionPayloadEnvelope, + chain: &BeaconChain, +) -> Result, EnvelopeError> { + // Reject any block if its block is not known to fork choice. + // + // A block that is not in fork choice is either: + // + // - Not yet imported: we should reject this block because we should only import a child + // envelope after its parent has been fully imported. + // - Pre-finalized: if the block is _prior_ to finalization, we should ignore the envelope + // because it will revert finalization. Note that the finalized block is stored in fork + // choice, so we will not reject any child of the finalized block (this is relevant during + // genesis). + + let fork_choice_read_lock = chain.canonical_head.fork_choice_read_lock(); + let beacon_block_root = envelope.beacon_block_root(); + let Some(proto_beacon_block) = fork_choice_read_lock.get_block(&beacon_block_root) else { + return Err(EnvelopeError::BlockRootUnknown { + block_root: beacon_block_root, + }); + }; + drop(fork_choice_read_lock); + + // TODO(EIP-7732): add metrics here + + let block_state_root = proto_beacon_block.state_root; + // We can use `get_hot_state` here rather than `get_advanced_hot_state` because the envelope + // must be from the same slot as its block (so no advance is required). + let cache_state = true; + let state = chain + .store + .get_hot_state(&block_state_root, cache_state) + .map_err(|e| EnvelopeError::BeaconChainError(Arc::new(e.into())))? + .ok_or_else(|| { + BeaconChainError::DBInconsistent(format!( + "Missing state for envelope block {block_state_root:?}", + )) + })?; + + Ok(EnvelopeProcessingSnapshot { + pre_state: state, + state_root: block_state_root, + beacon_block_root, + }) +} + +/// A wrapper around a `SignedExecutionPayloadEnvelope` that indicates it has been approved for re-gossiping on +/// the p2p network. +#[derive(Educe)] +#[educe(Debug(bound = "T: BeaconChainTypes"))] +pub struct GossipVerifiedEnvelope { + pub signed_envelope: Arc>, + pub block: Arc>, + pub snapshot: Option>>, +} + +impl GossipVerifiedEnvelope { + pub fn new( + signed_envelope: Arc>, + chain: &BeaconChain, + ) -> Result { + let envelope = &signed_envelope.message; + let payload = &envelope.payload; + let beacon_block_root = envelope.beacon_block_root; + + // Check that we've seen the beacon block for this envelope and that it passes validation. + // TODO(EIP-7732): We need a block status table in order to differentiate between: + // + // 1. Blocks we haven't seen (IGNORE), and + // 2. Blocks we've seen that are invalid (REJECT). + // + // Presently these two cases are conflated. + let fork_choice_read_lock = chain.canonical_head.fork_choice_read_lock(); + let Some(proto_block) = fork_choice_read_lock.get_block(&beacon_block_root) else { + return Err(EnvelopeError::BlockRootUnknown { + block_root: beacon_block_root, + }); + }; + drop(fork_choice_read_lock); + + // TODO(EIP-7732): check that we haven't seen another valid `SignedExecutionPayloadEnvelope` + // for this block root from this builder - envelope status table check + + // TODO(EIP-7732): this could be obtained from the ProtoBlock instead of the DB + // but this means the ProtoBlock needs to include something like the ExecutionBid + // will need to answer this question later. + let block = chain + .get_full_block(&beacon_block_root)? + .ok_or_else(|| { + EnvelopeError::from(BeaconChainError::MissingBeaconBlock(beacon_block_root)) + }) + .map(Arc::new)?; + let execution_bid = &block + .message() + .body() + .signed_execution_payload_bid()? + .message; + + // TODO(EIP-7732): Gossip rules for the beacon block contain the following: + // https://github.com/ethereum/consensus-specs/blob/master/specs/phase0/p2p-interface.md#beacon_block + // [IGNORE] The block is not from a future slot (with a MAXIMUM_GOSSIP_CLOCK_DISPARITY allowance) + // [IGNORE] The block is from a slot greater than the latest finalized slot + // should these kinds of checks be included for envelopes as well? + + // check that the slot of the envelope matches the slot of the parent block + if envelope.slot != block.slot() { + return Err(EnvelopeError::SlotMismatch { + block: block.slot(), + envelope: envelope.slot, + }); + } + + // builder index matches committed bid + if envelope.builder_index != execution_bid.builder_index { + return Err(EnvelopeError::BuilderIndexMismatch { + committed_bid: execution_bid.builder_index, + envelope: envelope.builder_index, + }); + } + + // the block hash should match the block hash of the execution bid + if payload.block_hash != execution_bid.block_hash { + return Err(EnvelopeError::BlockHashMismatch { + committed_bid: execution_bid.block_hash, + envelope: payload.block_hash, + }); + } + + // Get the fork from the proposer cache so we can verify the signature. + // This is currently the most efficient way to implement envelope signature verification + // because the `fork` might depend on advancing the parent state. + let block_slot = envelope.slot; + let block_epoch = block_slot.epoch(T::EthSpec::slots_per_epoch()); + let proposer_shuffling_decision_block = + proto_block.proposer_shuffling_root_for_child_block(block_epoch, &chain.spec); + let mut opt_snapshot = None; + let envelope_ref = signed_envelope.as_ref(); + let proposer = chain.with_proposer_cache::<_, EnvelopeError>( + proposer_shuffling_decision_block, + block_epoch, + |proposers| proposers.get_slot::(block_slot), + || { + debug!( + %beacon_block_root, + block_hash = %envelope_ref.block_hash(), + "Proposer shuffling cache miss for envelope verification" + ); + // The proposer index was *not* cached and we must load the parent in order to + // determine the proposer index. + let snapshot = load_snapshot(envelope_ref, chain)?; + opt_snapshot = Some(Box::new(snapshot.clone())); + Ok((snapshot.state_root, snapshot.pre_state)) + }, + )?; + let fork = proposer.fork; + + // True builder index accounting for self-building. + let proposer_index = block.message().proposer_index(); + let builder_index = envelope.builder_index; + + let signature_is_valid = { + let pubkey_cache = chain.validator_pubkey_cache.read(); + let builder_pubkey = pubkey_cache + .get(builder_index as usize) + .ok_or_else(|| EnvelopeError::UnknownValidator { builder_index })?; + signed_envelope.verify_signature( + builder_pubkey, + &fork, + chain.genesis_validators_root, + &chain.spec, + ) + }; + + if !signature_is_valid { + return Err(EnvelopeError::BadSignature); + } + + Ok(Self { + signed_envelope, + block, + snapshot: opt_snapshot, + }) + } + + pub fn envelope_cloned(&self) -> Arc> { + self.signed_envelope.clone() + } +} + +pub trait IntoExecutionPendingEnvelope: Sized { + fn into_execution_pending_envelope( + self, + chain: &Arc>, + notify_execution_layer: NotifyExecutionLayer, + ) -> Result, EnvelopeError>; +} + +pub struct ExecutionPendingEnvelope { + pub signed_envelope: MaybeAvailableEnvelope, + pub import_data: EnvelopeImportData, + pub payload_verification_handle: PayloadVerificationHandle, +} + +impl IntoExecutionPendingEnvelope for GossipVerifiedEnvelope { + fn into_execution_pending_envelope( + self, + chain: &Arc>, + notify_execution_layer: NotifyExecutionLayer, + ) -> Result, EnvelopeError> { + let signed_envelope = self.signed_envelope; + let envelope = &signed_envelope.message; + let payload = &envelope.payload; + + // Verify the execution payload is valid + let payload_notifier = + PayloadNotifier::from_envelope(chain.clone(), envelope, notify_execution_layer)?; + let block_root = envelope.beacon_block_root; + let slot = self.block.slot(); + + let payload_verification_future = async move { + let chain = payload_notifier.chain.clone(); + // TODO:(gloas): timing metrics + if let Some(started_execution) = chain.slot_clock.now_duration() { + chain.block_times_cache.write().set_time_started_execution( + block_root, + slot, + started_execution, + ); + } + + let payload_verification_status = payload_notifier.notify_new_payload().await?; + Ok(PayloadVerificationOutcome { + payload_verification_status, + // This fork is after the merge so it'll never be the merge transition block + is_valid_merge_transition_block: false, + }) + }; + // Spawn the payload verification future as a new task, but don't wait for it to complete. + // The `payload_verification_future` will be awaited later to ensure verification completed + // successfully. + let payload_verification_handle = chain + .task_executor + .spawn_handle( + payload_verification_future, + "execution_payload_verification", + ) + .ok_or(BeaconChainError::RuntimeShutdown)?; + + let snapshot = if let Some(snapshot) = self.snapshot { + *snapshot + } else { + load_snapshot(signed_envelope.as_ref(), chain)? + }; + let mut state = snapshot.pre_state; + + // All the state modifications are done in envelope_processing + process_execution_payload_envelope( + &mut state, + Some(snapshot.state_root), + &signed_envelope, + // verify signature already done for GossipVerifiedEnvelope + VerifySignatures::False, + &chain.spec, + )?; + + Ok(ExecutionPendingEnvelope { + signed_envelope: MaybeAvailableEnvelope::AvailabilityPending { + block_hash: payload.block_hash, + envelope: signed_envelope, + }, + import_data: EnvelopeImportData { + block_root, + block: self.block, + post_state: Box::new(state), + }, + payload_verification_handle, + }) + } +} + +impl IntoExecutionPendingEnvelope + for Arc> +{ + fn into_execution_pending_envelope( + self, + chain: &Arc>, + notify_execution_layer: NotifyExecutionLayer, + ) -> Result, EnvelopeError> { + // TODO(EIP-7732): figure out how this should be refactored.. + GossipVerifiedEnvelope::new(self, chain)? + .into_execution_pending_envelope(chain, notify_execution_layer) + } +} \ No newline at end of file diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification_types.rs b/beacon_node/beacon_chain/src/payload_envelope_verification_types.rs new file mode 100644 index 00000000000..bd2eb5a34fd --- /dev/null +++ b/beacon_node/beacon_chain/src/payload_envelope_verification_types.rs @@ -0,0 +1,31 @@ +use std::sync::Arc; +use types::{ + BeaconState, ChainSpec, DataColumnSidecarList, EthSpec, ExecutionBlockHash, Hash256, + SignedBeaconBlock, SignedExecutionPayloadEnvelope, +}; + +#[derive(PartialEq)] +pub struct EnvelopeImportData { + pub block_root: Hash256, + pub block: Arc>, + pub post_state: Box>, +} + +#[derive(Debug)] +#[allow(dead_code)] +pub struct AvailableEnvelope { + // TODO(EIP-7732): rename to execution_block_hash + block_hash: ExecutionBlockHash, + envelope: Arc>, + columns: DataColumnSidecarList, + /// Timestamp at which this block first became available (UNIX timestamp, time since 1970). + columns_available_timestamp: Option, + pub spec: Arc, +} +pub enum MaybeAvailableEnvelope { + Available(AvailableEnvelope), + AvailabilityPending { + block_hash: ExecutionBlockHash, + envelope: Arc>, + }, +} \ No newline at end of file From 8204241b456f739c01c14596d35cdbeb10b9ad7f Mon Sep 17 00:00:00 2001 From: Eitan Seri- Levi Date: Tue, 10 Feb 2026 19:57:53 -0800 Subject: [PATCH 2/9] Progress --- beacon_node/beacon_chain/src/beacon_chain.rs | 1 - .../beacon_chain/src/block_verification.rs | 3 +- .../beacon_chain/src/execution_payload.rs | 31 +-- beacon_node/beacon_chain/src/lib.rs | 1 - .../execution_pending_envelope.rs | 26 ++ .../gossip_verified_envelope.rs} | 208 +--------------- .../src/payload_envelope_verification/mod.rs | 229 ++++++++++++++++++ .../payload_notifier.rs | 77 ++++++ .../payload_envelope_verification_types.rs | 31 --- .../src/engine_api/new_payload_request.rs | 3 +- .../src/envelope_processing.rs | 101 ++++++++ 11 files changed, 463 insertions(+), 248 deletions(-) create mode 100644 beacon_node/beacon_chain/src/payload_envelope_verification/execution_pending_envelope.rs rename beacon_node/beacon_chain/src/{payload_envelope_verification.rs => payload_envelope_verification/gossip_verified_envelope.rs} (55%) create mode 100644 beacon_node/beacon_chain/src/payload_envelope_verification/mod.rs create mode 100644 beacon_node/beacon_chain/src/payload_envelope_verification/payload_notifier.rs delete mode 100644 beacon_node/beacon_chain/src/payload_envelope_verification_types.rs diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index a59508a37a0..801ef7696fd 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -1147,7 +1147,6 @@ impl BeaconChain { } } - /// Returns the full block at the given root, if it's available in the database. /// /// Should always return a full block for pre-merge and post-gloas blocks. diff --git a/beacon_node/beacon_chain/src/block_verification.rs b/beacon_node/beacon_chain/src/block_verification.rs index 9d1b8e0d3d8..c11f26aa002 100644 --- a/beacon_node/beacon_chain/src/block_verification.rs +++ b/beacon_node/beacon_chain/src/block_verification.rs @@ -718,7 +718,8 @@ pub struct SignatureVerifiedBlock { } /// Used to await the result of executing payload with an EE. -pub type PayloadVerificationHandle = JoinHandle>>; +pub type PayloadVerificationHandle = + JoinHandle>>; /// A wrapper around a `SignedBeaconBlock` that indicates that this block is fully verified and /// ready to import into the `BeaconChain`. The validation includes: diff --git a/beacon_node/beacon_chain/src/execution_payload.rs b/beacon_node/beacon_chain/src/execution_payload.rs index 370f044c7aa..b8fbbc3c32e 100644 --- a/beacon_node/beacon_chain/src/execution_payload.rs +++ b/beacon_node/beacon_chain/src/execution_payload.rs @@ -113,7 +113,7 @@ impl PayloadNotifier { if let Some(precomputed_status) = self.payload_verification_status { Ok(precomputed_status) } else { - notify_new_payload(&self.chain, self.block.message()).await + notify_new_payload(&self.chain, self.block.message().tree_hash_root(), self.block.message().try_into()?).await } } } @@ -127,17 +127,18 @@ impl PayloadNotifier { /// contains a few extra checks by running `partially_verify_execution_payload` first: /// /// https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/bellatrix/beacon-chain.md#notify_new_payload -async fn notify_new_payload( +pub async fn notify_new_payload( chain: &Arc>, - block: BeaconBlockRef<'_, T::EthSpec>, + beacon_block_root: Hash256, + new_payload_request: NewPayloadRequest<'_, T::EthSpec>, ) -> Result { let execution_layer = chain .execution_layer .as_ref() .ok_or(ExecutionPayloadError::NoExecutionConnection)?; - let execution_block_hash = block.execution_payload()?.block_hash(); - let new_payload_response = execution_layer.notify_new_payload(block.try_into()?).await; + let execution_block_hash = new_payload_request.execution_payload_ref().block_hash(); + let new_payload_response = execution_layer.notify_new_payload(new_payload_request.clone()).await; match new_payload_response { Ok(status) => match status { @@ -153,10 +154,10 @@ async fn notify_new_payload( ?validation_error, ?latest_valid_hash, ?execution_block_hash, - root = ?block.tree_hash_root(), - graffiti = block.body().graffiti().as_utf8_lossy(), - proposer_index = block.proposer_index(), - slot = %block.slot(), + // root = ?block.tree_hash_root(), + // graffiti = block.body().graffiti().as_utf8_lossy(), + // proposer_index = block.proposer_index(), + // slot = %block.slot(), method = "new_payload", "Invalid execution payload" ); @@ -179,11 +180,11 @@ async fn notify_new_payload( { // This block has not yet been applied to fork choice, so the latest block that was // imported to fork choice was the parent. - let latest_root = block.parent_root(); + let latest_root = new_payload_request.parent_beacon_block_root()?; chain .process_invalid_execution_payload(&InvalidationOperation::InvalidateMany { - head_block_root: latest_root, + head_block_root: *latest_root, always_invalidate_head: false, latest_valid_ancestor: latest_valid_hash, }) @@ -198,10 +199,10 @@ async fn notify_new_payload( warn!( ?validation_error, ?execution_block_hash, - root = ?block.tree_hash_root(), - graffiti = block.body().graffiti().as_utf8_lossy(), - proposer_index = block.proposer_index(), - slot = %block.slot(), + // root = ?block.tree_hash_root(), + // graffiti = block.body().graffiti().as_utf8_lossy(), + // proposer_index = block.proposer_index(), + // slot = %block.slot(), method = "new_payload", "Invalid execution payload block hash" ); diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index 7aa64deb1ca..4120ed86fcc 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -43,7 +43,6 @@ pub mod observed_data_sidecars; pub mod observed_operations; mod observed_slashable; pub mod payload_envelope_verification; -pub mod payload_envelope_verification_types; pub mod persisted_beacon_chain; pub mod persisted_custody; mod persisted_fork_choice; diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/execution_pending_envelope.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/execution_pending_envelope.rs new file mode 100644 index 00000000000..956e6ffb8f5 --- /dev/null +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/execution_pending_envelope.rs @@ -0,0 +1,26 @@ +use task_executor::JoinHandle; +use types::{EthSpec, FullPayload}; + +use crate::{BeaconChainTypes, PayloadVerificationOutcome, payload_envelope_verification::PayloadEnvelopeImportData}; + + +/// Used to await the result of executing payload with an EE. +pub type PayloadVerificationHandle = + JoinHandle>>>; + +/// A wrapper around a `SignedBeaconBlock` that indicates that this block is fully verified and +/// ready to import into the `BeaconChain`. The validation includes: +/// +/// - Parent is known +/// - Signatures +/// - State root check +/// - Block processing +/// +/// Note: a `ExecutionPendingEnvelope` is not _forever_ valid to be imported, it may later become invalid +/// due to finality or some other event. A `ExecutionPendingEnvelope` should be imported into the +/// `BeaconChain` immediately after it is instantiated. +pub struct ExecutionPendingEnvelope { + pub block: MaybeAvailableBlock, + pub import_data: PayloadEnvelopeImportData, + pub payload_verification_handle: PayloadVerificationHandle, +} \ No newline at end of file diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs similarity index 55% rename from beacon_node/beacon_chain/src/payload_envelope_verification.rs rename to beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs index b22acd69fbe..2bd5a6c3d79 100644 --- a/beacon_node/beacon_chain/src/payload_envelope_verification.rs +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs @@ -1,174 +1,13 @@ -//! The incremental processing steps (e.g., signatures verified but not the state transition) is -//! represented as a sequence of wrapper-types around the block. There is a linear progression of -//! types, starting at a `SignedBeaconBlock` and finishing with a `Fully VerifiedBlock` (see -//! diagram below). -//! -//! ```ignore -//! START -//! | -//! ▼ -//! SignedExecutionPayloadEnvelope -//! | -//! |--------------- -//! | | -//! | ▼ -//! | GossipVerifiedEnvelope -//! | | -//! |--------------- -//! | -//! ▼ -//! ExecutionPendingEnvelope -//! | -//! await -//! | -//! ▼ -//! END -//! -//! ``` - -use crate::NotifyExecutionLayer; -use crate::block_verification::{PayloadVerificationHandle, PayloadVerificationOutcome}; -use crate::payload_envelope_verification_types::{EnvelopeImportData, MaybeAvailableEnvelope}; -use crate::execution_payload::PayloadNotifier; -use crate::{BeaconChain, BeaconChainError, BeaconChainTypes}; -use educe::Educe; -use slot_clock::SlotClock; -use state_processing::envelope_processing::{EnvelopeProcessingError, process_execution_payload_envelope}; -use state_processing::{BlockProcessingError, VerifySignatures}; use std::sync::Arc; -use tracing::{debug, instrument}; -use types::{ - BeaconState, BeaconStateError, EthSpec, ExecutionBlockHash, Hash256, SignedBeaconBlock, - SignedExecutionPayloadEnvelope, Slot, -}; - -#[derive(Debug, Clone)] -pub enum EnvelopeError { - /// The envelope's block root is unknown. - BlockRootUnknown { - block_root: Hash256, - }, - /// The signature is invalid. - BadSignature, - /// The builder index doesn't match the committed bid - BuilderIndexMismatch { - committed_bid: u64, - envelope: u64, - }, - // The envelope slot doesn't match the block - SlotMismatch { - block: Slot, - envelope: Slot, - }, - // The validator index is unknown - UnknownValidator { - builder_index: u64, - }, - // The block hash doesn't match the committed bid - BlockHashMismatch { - committed_bid: ExecutionBlockHash, - envelope: ExecutionBlockHash, - }, - // Some Beacon Chain Error - BeaconChainError(Arc), - // Some Beacon State error - BeaconStateError(BeaconStateError), - // Some BlockProcessingError (for electra operations) - BlockProcessingError(BlockProcessingError), - // Some EnvelopeProcessingError - EnvelopeProcessingError(EnvelopeProcessingError), -} - -impl From for EnvelopeError { - fn from(e: BeaconChainError) -> Self { - EnvelopeError::BeaconChainError(Arc::new(e)) - } -} - -impl From for EnvelopeError { - fn from(e: BeaconStateError) -> Self { - EnvelopeError::BeaconStateError(e) - } -} - -/// Pull errors up from EnvelopeProcessingError to EnvelopeError -impl From for EnvelopeError { - fn from(e: EnvelopeProcessingError) -> Self { - match e { - EnvelopeProcessingError::BadSignature => EnvelopeError::BadSignature, - EnvelopeProcessingError::BeaconStateError(e) => EnvelopeError::BeaconStateError(e), - EnvelopeProcessingError::BlockHashMismatch { - committed_bid, - envelope, - } => EnvelopeError::BlockHashMismatch { - committed_bid, - envelope, - }, - EnvelopeProcessingError::BlockProcessingError(e) => { - EnvelopeError::BlockProcessingError(e) - } - e => EnvelopeError::EnvelopeProcessingError(e), - } - } -} - -/// This snapshot is to be used for verifying a envelope of the block. -#[derive(Debug, Clone)] -pub struct EnvelopeProcessingSnapshot { - /// This state is equivalent to the `self.beacon_block.state_root()` before applying the envelope. - pub pre_state: BeaconState, - pub state_root: Hash256, - pub beacon_block_root: Hash256, -} - -#[allow(clippy::type_complexity)] -#[instrument(skip_all, level = "debug", fields(beacon_block_root = %envelope.beacon_block_root()))] -fn load_snapshot( - envelope: &SignedExecutionPayloadEnvelope, - chain: &BeaconChain, -) -> Result, EnvelopeError> { - // Reject any block if its block is not known to fork choice. - // - // A block that is not in fork choice is either: - // - // - Not yet imported: we should reject this block because we should only import a child - // envelope after its parent has been fully imported. - // - Pre-finalized: if the block is _prior_ to finalization, we should ignore the envelope - // because it will revert finalization. Note that the finalized block is stored in fork - // choice, so we will not reject any child of the finalized block (this is relevant during - // genesis). - let fork_choice_read_lock = chain.canonical_head.fork_choice_read_lock(); - let beacon_block_root = envelope.beacon_block_root(); - let Some(proto_beacon_block) = fork_choice_read_lock.get_block(&beacon_block_root) else { - return Err(EnvelopeError::BlockRootUnknown { - block_root: beacon_block_root, - }); - }; - drop(fork_choice_read_lock); - - // TODO(EIP-7732): add metrics here - - let block_state_root = proto_beacon_block.state_root; - // We can use `get_hot_state` here rather than `get_advanced_hot_state` because the envelope - // must be from the same slot as its block (so no advance is required). - let cache_state = true; - let state = chain - .store - .get_hot_state(&block_state_root, cache_state) - .map_err(|e| EnvelopeError::BeaconChainError(Arc::new(e.into())))? - .ok_or_else(|| { - BeaconChainError::DBInconsistent(format!( - "Missing state for envelope block {block_state_root:?}", - )) - })?; +use educe::Educe; +use state_processing::{VerifySignatures, envelope_processing::process_execution_payload_envelope}; +use tracing::debug; +use types::{EthSpec, SignedBeaconBlock, SignedExecutionPayloadEnvelope}; - Ok(EnvelopeProcessingSnapshot { - pre_state: state, - state_root: block_state_root, - beacon_block_root, - }) -} +use crate::{ + BeaconChain, BeaconChainError, BeaconChainTypes, NotifyExecutionLayer, PayloadVerificationOutcome, payload_envelope_verification::{EnvelopeError, EnvelopeImportData, EnvelopeProcessingSnapshot, ExecutionPendingEnvelope, IntoExecutionPendingEnvelope, MaybeAvailableEnvelope, load_snapshot, payload_notifier::PayloadNotifier} +}; /// A wrapper around a `SignedExecutionPayloadEnvelope` that indicates it has been approved for re-gossiping on /// the p2p network. @@ -313,20 +152,6 @@ impl GossipVerifiedEnvelope { } } -pub trait IntoExecutionPendingEnvelope: Sized { - fn into_execution_pending_envelope( - self, - chain: &Arc>, - notify_execution_layer: NotifyExecutionLayer, - ) -> Result, EnvelopeError>; -} - -pub struct ExecutionPendingEnvelope { - pub signed_envelope: MaybeAvailableEnvelope, - pub import_data: EnvelopeImportData, - pub payload_verification_handle: PayloadVerificationHandle, -} - impl IntoExecutionPendingEnvelope for GossipVerifiedEnvelope { fn into_execution_pending_envelope( self, @@ -336,10 +161,13 @@ impl IntoExecutionPendingEnvelope for GossipVerifiedEnve let signed_envelope = self.signed_envelope; let envelope = &signed_envelope.message; let payload = &envelope.payload; + + // TODO(gloas) unwrap + let bid = chain.get_full_block(&envelope.beacon_block_root).unwrap().unwrap().message().body().signed_execution_payload_bid().unwrap().message; // Verify the execution payload is valid let payload_notifier = - PayloadNotifier::from_envelope(chain.clone(), envelope, notify_execution_layer)?; + PayloadNotifier::new(chain.clone(), envelope, notify_execution_layer)?; let block_root = envelope.beacon_block_root; let slot = self.block.slot(); @@ -403,17 +231,3 @@ impl IntoExecutionPendingEnvelope for GossipVerifiedEnve }) } } - -impl IntoExecutionPendingEnvelope - for Arc> -{ - fn into_execution_pending_envelope( - self, - chain: &Arc>, - notify_execution_layer: NotifyExecutionLayer, - ) -> Result, EnvelopeError> { - // TODO(EIP-7732): figure out how this should be refactored.. - GossipVerifiedEnvelope::new(self, chain)? - .into_execution_pending_envelope(chain, notify_execution_layer) - } -} \ No newline at end of file diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/mod.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/mod.rs new file mode 100644 index 00000000000..4a739e74c56 --- /dev/null +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/mod.rs @@ -0,0 +1,229 @@ +//! The incremental processing steps (e.g., signatures verified but not the state transition) is +//! represented as a sequence of wrapper-types around the block. There is a linear progression of +//! types, starting at a `SignedBeaconBlock` and finishing with a `Fully VerifiedBlock` (see +//! diagram below). +//! +//! ```ignore +//! START +//! | +//! ▼ +//! SignedExecutionPayloadEnvelope +//! | +//! |--------------- +//! | | +//! | ▼ +//! | GossipVerifiedEnvelope +//! | | +//! |--------------- +//! | +//! ▼ +//! ExecutionPendingEnvelope +//! | +//! await +//! | +//! ▼ +//! END +//! +//! ``` + +use std::sync::Arc; + +use state_processing::{BlockProcessingError, ConsensusContext, envelope_processing::EnvelopeProcessingError}; +use tracing::instrument; +use types::{BeaconState, BeaconStateError, ChainSpec, DataColumnSidecarList, EthSpec, ExecutionBlockHash, Hash256, SignedBeaconBlock, SignedExecutionPayloadEnvelope, Slot}; + +use crate::{BeaconChain, BeaconChainError, BeaconChainTypes, NotifyExecutionLayer, block_verification::PayloadVerificationHandle, payload_envelope_verification::gossip_verified_envelope::GossipVerifiedEnvelope}; + +pub mod execution_pending_envelope; +pub mod gossip_verified_envelope; +mod payload_notifier; + +pub trait IntoExecutionPendingEnvelope: Sized { + fn into_execution_pending_envelope( + self, + chain: &Arc>, + notify_execution_layer: NotifyExecutionLayer, + ) -> Result, EnvelopeError>; +} + +pub struct ExecutionPendingEnvelope { + pub signed_envelope: MaybeAvailableEnvelope, + pub import_data: EnvelopeImportData, + pub payload_verification_handle: PayloadVerificationHandle, +} + +#[derive(PartialEq)] +pub struct EnvelopeImportData { + pub block_root: Hash256, + pub block: Arc>, + pub post_state: Box>, +} + +#[derive(Debug)] +#[allow(dead_code)] +pub struct AvailableEnvelope { + // TODO(EIP-7732): rename to execution_block_hash + block_hash: ExecutionBlockHash, + envelope: Arc>, + columns: DataColumnSidecarList, + /// Timestamp at which this block first became available (UNIX timestamp, time since 1970). + columns_available_timestamp: Option, + pub spec: Arc, +} + +pub enum MaybeAvailableEnvelope { + Available(AvailableEnvelope), + AvailabilityPending { + block_hash: ExecutionBlockHash, + envelope: Arc>, + }, +} + +/// This snapshot is to be used for verifying a envelope of the block. +#[derive(Debug, Clone)] +pub struct EnvelopeProcessingSnapshot { + /// This state is equivalent to the `self.beacon_block.state_root()` before applying the envelope. + pub pre_state: BeaconState, + pub state_root: Hash256, + pub beacon_block_root: Hash256, +} + +#[derive(Debug, Clone)] +pub enum EnvelopeError { + /// The envelope's block root is unknown. + BlockRootUnknown { + block_root: Hash256, + }, + /// The signature is invalid. + BadSignature, + /// The builder index doesn't match the committed bid + BuilderIndexMismatch { + committed_bid: u64, + envelope: u64, + }, + // The envelope slot doesn't match the block + SlotMismatch { + block: Slot, + envelope: Slot, + }, + // The validator index is unknown + UnknownValidator { + builder_index: u64, + }, + // The block hash doesn't match the committed bid + BlockHashMismatch { + committed_bid: ExecutionBlockHash, + envelope: ExecutionBlockHash, + }, + // Some Beacon Chain Error + BeaconChainError(Arc), + // Some Beacon State error + BeaconStateError(BeaconStateError), + // Some BlockProcessingError (for electra operations) + BlockProcessingError(BlockProcessingError), + // Some EnvelopeProcessingError + EnvelopeProcessingError(EnvelopeProcessingError), +} + +impl From for EnvelopeError { + fn from(e: BeaconChainError) -> Self { + EnvelopeError::BeaconChainError(Arc::new(e)) + } +} + +impl From for EnvelopeError { + fn from(e: BeaconStateError) -> Self { + EnvelopeError::BeaconStateError(e) + } +} + +/// Pull errors up from EnvelopeProcessingError to EnvelopeError +impl From for EnvelopeError { + fn from(e: EnvelopeProcessingError) -> Self { + match e { + EnvelopeProcessingError::BadSignature => EnvelopeError::BadSignature, + EnvelopeProcessingError::BeaconStateError(e) => EnvelopeError::BeaconStateError(e), + EnvelopeProcessingError::BlockHashMismatch { + committed_bid, + envelope, + } => EnvelopeError::BlockHashMismatch { + committed_bid, + envelope, + }, + EnvelopeProcessingError::BlockProcessingError(e) => { + EnvelopeError::BlockProcessingError(e) + } + e => EnvelopeError::EnvelopeProcessingError(e), + } + } +} + +#[allow(clippy::type_complexity)] +#[instrument(skip_all, level = "debug", fields(beacon_block_root = %envelope.beacon_block_root()))] +pub(crate) fn load_snapshot( + envelope: &SignedExecutionPayloadEnvelope, + chain: &BeaconChain, +) -> Result, EnvelopeError> { + // Reject any block if its block is not known to fork choice. + // + // A block that is not in fork choice is either: + // + // - Not yet imported: we should reject this block because we should only import a child + // envelope after its parent has been fully imported. + // - Pre-finalized: if the block is _prior_ to finalization, we should ignore the envelope + // because it will revert finalization. Note that the finalized block is stored in fork + // choice, so we will not reject any child of the finalized block (this is relevant during + // genesis). + + let fork_choice_read_lock = chain.canonical_head.fork_choice_read_lock(); + let beacon_block_root = envelope.beacon_block_root(); + let Some(proto_beacon_block) = fork_choice_read_lock.get_block(&beacon_block_root) else { + return Err(EnvelopeError::BlockRootUnknown { + block_root: beacon_block_root, + }); + }; + drop(fork_choice_read_lock); + + // TODO(EIP-7732): add metrics here + + let block_state_root = proto_beacon_block.state_root; + // We can use `get_hot_state` here rather than `get_advanced_hot_state` because the envelope + // must be from the same slot as its block (so no advance is required). + let cache_state = true; + let state = chain + .store + .get_hot_state(&block_state_root, cache_state) + .map_err(|e| EnvelopeError::BeaconChainError(Arc::new(e.into())))? + .ok_or_else(|| { + BeaconChainError::DBInconsistent(format!( + "Missing state for envelope block {block_state_root:?}", + )) + })?; + + Ok(EnvelopeProcessingSnapshot { + pre_state: state, + state_root: block_state_root, + beacon_block_root, + }) +} + +impl IntoExecutionPendingEnvelope + for Arc> +{ + fn into_execution_pending_envelope( + self, + chain: &Arc>, + notify_execution_layer: NotifyExecutionLayer, + ) -> Result, EnvelopeError> { + // TODO(EIP-7732): figure out how this should be refactored.. + GossipVerifiedEnvelope::new(self, chain)? + .into_execution_pending_envelope(chain, notify_execution_layer) + } +} + +#[derive(Clone, Debug, PartialEq)] +pub struct PayloadEnvelopeImportData { + pub block_root: Hash256, + pub state: BeaconState, + pub consensus_context: ConsensusContext, +} diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/payload_notifier.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/payload_notifier.rs new file mode 100644 index 00000000000..15fec7e21dd --- /dev/null +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/payload_notifier.rs @@ -0,0 +1,77 @@ +use std::sync::Arc; + +use execution_layer::NewPayloadRequest; +use fork_choice::PayloadVerificationStatus; +use state_processing::{envelope_processing::partially_verify_payload_envelope, per_block_processing::is_execution_enabled}; +use tracing::warn; +use types::{BeaconState, ExecutionPayloadBid, Hash256, SignedBeaconBlock, SignedExecutionPayloadEnvelope}; + +use crate::{BeaconChain, BeaconChainTypes, BlockError, ExecutionPayloadError, NotifyExecutionLayer, execution_payload::notify_new_payload}; + + +/// Used to await the result of executing payload with a remote EE. +pub struct PayloadNotifier { + pub chain: Arc>, + pub envelope: Arc>, + payload_verification_status: Option, +} + +impl PayloadNotifier { + pub fn new( + chain: Arc>, + bid: &ExecutionPayloadBid, + envelope: Arc>, + state: &BeaconState, + notify_execution_layer: NotifyExecutionLayer, + ) -> Result { + let payload_verification_status = { + // Perform the initial stages of payload verification. + // + // We will duplicate these checks again during `per_block_processing`, however these + // checks are cheap and doing them here ensures we have verified them before marking + // the block as optimistically imported. This is particularly relevant in the case + // where we do not send the block to the EL at all. + let payload_message = &envelope.message; + partially_verify_payload_envelope( + state, + &envelope, + &chain.spec, + ).unwrap(); // TODO(gloas) unwrap + + match notify_execution_layer { + NotifyExecutionLayer::No if chain.config.optimistic_finalized_sync => { + // Create a NewPayloadRequest (no clones required) and check optimistic sync verifications + let new_payload_request: NewPayloadRequest = + payload_message.try_into()?; + if let Err(e) = new_payload_request.perform_optimistic_sync_verifications() { + warn!( + block_number = ?payload_message.payload.block_number, + info = "you can silence this warning with --disable-optimistic-finalized-sync", + error = ?e, + "Falling back to slow block hash verification" + ); + None + } else { + Some(PayloadVerificationStatus::Optimistic) + } + } + _ => None, + } + }; + + Ok(Self { + chain, + envelope, + payload_verification_status, + }) + } + + pub async fn notify_new_payload(self) -> Result { + if let Some(precomputed_status) = self.payload_verification_status { + Ok(precomputed_status) + } else { + // tODO(gloas) fix zero + notify_new_payload(&self.chain, Hash256::ZERO, self.envelope.message.try_into()?).await + } + } +} \ No newline at end of file diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification_types.rs b/beacon_node/beacon_chain/src/payload_envelope_verification_types.rs deleted file mode 100644 index bd2eb5a34fd..00000000000 --- a/beacon_node/beacon_chain/src/payload_envelope_verification_types.rs +++ /dev/null @@ -1,31 +0,0 @@ -use std::sync::Arc; -use types::{ - BeaconState, ChainSpec, DataColumnSidecarList, EthSpec, ExecutionBlockHash, Hash256, - SignedBeaconBlock, SignedExecutionPayloadEnvelope, -}; - -#[derive(PartialEq)] -pub struct EnvelopeImportData { - pub block_root: Hash256, - pub block: Arc>, - pub post_state: Box>, -} - -#[derive(Debug)] -#[allow(dead_code)] -pub struct AvailableEnvelope { - // TODO(EIP-7732): rename to execution_block_hash - block_hash: ExecutionBlockHash, - envelope: Arc>, - columns: DataColumnSidecarList, - /// Timestamp at which this block first became available (UNIX timestamp, time since 1970). - columns_available_timestamp: Option, - pub spec: Arc, -} -pub enum MaybeAvailableEnvelope { - Available(AvailableEnvelope), - AvailabilityPending { - block_hash: ExecutionBlockHash, - envelope: Arc>, - }, -} \ No newline at end of file diff --git a/beacon_node/execution_layer/src/engine_api/new_payload_request.rs b/beacon_node/execution_layer/src/engine_api/new_payload_request.rs index ba94296b859..b88dd7ca3a5 100644 --- a/beacon_node/execution_layer/src/engine_api/new_payload_request.rs +++ b/beacon_node/execution_layer/src/engine_api/new_payload_request.rs @@ -4,8 +4,7 @@ use crate::versioned_hashes::verify_versioned_hashes; use state_processing::per_block_processing::deneb::kzg_commitment_to_versioned_hash; use superstruct::superstruct; use types::{ - BeaconBlockRef, BeaconStateError, EthSpec, ExecutionBlockHash, ExecutionPayload, - ExecutionPayloadRef, Hash256, VersionedHash, + BeaconBlockRef, BeaconStateError, EthSpec, ExecutionBlockHash, ExecutionPayload, ExecutionPayloadEnvelope, ExecutionPayloadRef, Hash256, VersionedHash }; use types::{ ExecutionPayloadBellatrix, ExecutionPayloadCapella, ExecutionPayloadDeneb, diff --git a/consensus/state_processing/src/envelope_processing.rs b/consensus/state_processing/src/envelope_processing.rs index d46728dbbc1..2f511846a53 100644 --- a/consensus/state_processing/src/envelope_processing.rs +++ b/consensus/state_processing/src/envelope_processing.rs @@ -276,3 +276,104 @@ pub fn process_execution_payload_envelope( Ok(()) } + + +/// Performs *partial* verification of the `payload envelope`. +pub fn partially_verify_payload_envelope( + state: &BeaconState, + signed_envelope: &SignedExecutionPayloadEnvelope, + spec: &ChainSpec, +) -> Result<(), EnvelopeProcessingError> { + let envelope = &signed_envelope.message; + let payload = &signed_envelope.message.payload; + + // Verify consistency with the beacon block + let latest_block_header_root = state.latest_block_header().tree_hash_root(); + envelope_verify!( + envelope.beacon_block_root == latest_block_header_root, + EnvelopeProcessingError::LatestBlockHeaderMismatch { + envelope_root: envelope.beacon_block_root, + block_header_root: latest_block_header_root, + } + ); + envelope_verify!( + envelope.slot == state.slot(), + EnvelopeProcessingError::SlotMismatch { + envelope_slot: envelope.slot, + parent_state_slot: state.slot(), + } + ); + + // Verify consistency with the committed bid + let committed_bid = state.latest_execution_payload_bid()?; + envelope_verify!( + envelope.builder_index == committed_bid.builder_index, + EnvelopeProcessingError::BuilderIndexMismatch { + committed_bid: committed_bid.builder_index, + envelope: envelope.builder_index, + } + ); + envelope_verify!( + committed_bid.prev_randao == payload.prev_randao, + EnvelopeProcessingError::PrevRandaoMismatch { + committed_bid: committed_bid.prev_randao, + envelope: payload.prev_randao, + } + ); + + // Verify consistency with expected withdrawals + // NOTE: we don't bother hashing here except in case of error, because we can just compare for + // equality directly. This equality check could be more straight-forward if the types were + // changed to match (currently we are comparing VariableList to List). This could happen + // coincidentally when we adopt ProgressiveList. + envelope_verify!( + payload.withdrawals.len() == state.payload_expected_withdrawals()?.len() + && payload + .withdrawals + .iter() + .eq(state.payload_expected_withdrawals()?.iter()), + EnvelopeProcessingError::WithdrawalsRootMismatch { + state: state.payload_expected_withdrawals()?.tree_hash_root(), + payload: payload.withdrawals.tree_hash_root(), + } + ); + + // Verify the gas limit + envelope_verify!( + committed_bid.gas_limit == payload.gas_limit, + EnvelopeProcessingError::GasLimitMismatch { + committed_bid: committed_bid.gas_limit, + envelope: payload.gas_limit, + } + ); + + // Verify the block hash + envelope_verify!( + committed_bid.block_hash == payload.block_hash, + EnvelopeProcessingError::BlockHashMismatch { + committed_bid: committed_bid.block_hash, + envelope: payload.block_hash, + } + ); + + // Verify consistency of the parent hash with respect to the previous execution payload + envelope_verify!( + payload.parent_hash == *state.latest_block_hash()?, + EnvelopeProcessingError::ParentHashMismatch { + state: *state.latest_block_hash()?, + envelope: payload.parent_hash, + } + ); + + // Verify timestamp + let state_timestamp = compute_timestamp_at_slot(state, state.slot(), spec)?; + envelope_verify!( + payload.timestamp == state_timestamp, + EnvelopeProcessingError::TimestampMismatch { + state: state_timestamp, + envelope: payload.timestamp, + } + ); + + Ok(()) +} From 22f3fd4ccf6571644e6dee9478ab68fc1d17596f Mon Sep 17 00:00:00 2001 From: Eitan Seri- Levi Date: Tue, 10 Feb 2026 22:40:26 -0800 Subject: [PATCH 3/9] Continue --- .../beacon_chain/src/execution_payload.rs | 11 +++- .../execution_pending_envelope.rs | 12 ++-- .../gossip_verified_envelope.rs | 65 +++++++++++++------ .../src/payload_envelope_verification/mod.rs | 25 ++++++- .../payload_notifier.rs | 32 +++++---- .../signature_verified_envelope.rs | 40 ++++++++++++ .../src/engine_api/new_payload_request.rs | 3 +- .../src/envelope_processing.rs | 3 +- 8 files changed, 146 insertions(+), 45 deletions(-) create mode 100644 beacon_node/beacon_chain/src/payload_envelope_verification/signature_verified_envelope.rs diff --git a/beacon_node/beacon_chain/src/execution_payload.rs b/beacon_node/beacon_chain/src/execution_payload.rs index b8fbbc3c32e..631a0180dd0 100644 --- a/beacon_node/beacon_chain/src/execution_payload.rs +++ b/beacon_node/beacon_chain/src/execution_payload.rs @@ -113,7 +113,12 @@ impl PayloadNotifier { if let Some(precomputed_status) = self.payload_verification_status { Ok(precomputed_status) } else { - notify_new_payload(&self.chain, self.block.message().tree_hash_root(), self.block.message().try_into()?).await + notify_new_payload( + &self.chain, + self.block.message().tree_hash_root(), + self.block.message().try_into()?, + ) + .await } } } @@ -138,7 +143,9 @@ pub async fn notify_new_payload( .ok_or(ExecutionPayloadError::NoExecutionConnection)?; let execution_block_hash = new_payload_request.execution_payload_ref().block_hash(); - let new_payload_response = execution_layer.notify_new_payload(new_payload_request.clone()).await; + let new_payload_response = execution_layer + .notify_new_payload(new_payload_request.clone()) + .await; match new_payload_response { Ok(status) => match status { diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/execution_pending_envelope.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/execution_pending_envelope.rs index 956e6ffb8f5..7dffd1c09c6 100644 --- a/beacon_node/beacon_chain/src/payload_envelope_verification/execution_pending_envelope.rs +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/execution_pending_envelope.rs @@ -1,8 +1,10 @@ use task_executor::JoinHandle; use types::{EthSpec, FullPayload}; -use crate::{BeaconChainTypes, PayloadVerificationOutcome, payload_envelope_verification::PayloadEnvelopeImportData}; - +use crate::{ + BeaconChainTypes, PayloadVerificationOutcome, + payload_envelope_verification::{MaybeAvailableEnvelope, PayloadEnvelopeImportData}, +}; /// Used to await the result of executing payload with an EE. pub type PayloadVerificationHandle = @@ -20,7 +22,7 @@ pub type PayloadVerificationHandle = /// due to finality or some other event. A `ExecutionPendingEnvelope` should be imported into the /// `BeaconChain` immediately after it is instantiated. pub struct ExecutionPendingEnvelope { - pub block: MaybeAvailableBlock, + pub block: MaybeAvailableEnvelope, pub import_data: PayloadEnvelopeImportData, - pub payload_verification_handle: PayloadVerificationHandle, -} \ No newline at end of file + pub payload_verification_handle: PayloadVerificationHandle, +} diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs index 2bd5a6c3d79..580c6e92343 100644 --- a/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs @@ -1,12 +1,19 @@ use std::sync::Arc; use educe::Educe; +use slot_clock::SlotClock; use state_processing::{VerifySignatures, envelope_processing::process_execution_payload_envelope}; use tracing::debug; -use types::{EthSpec, SignedBeaconBlock, SignedExecutionPayloadEnvelope}; +use types::{EthSpec, SignedBeaconBlock, SignedExecutionPayloadEnvelope, consts::gloas::BUILDER_INDEX_SELF_BUILD}; use crate::{ - BeaconChain, BeaconChainError, BeaconChainTypes, NotifyExecutionLayer, PayloadVerificationOutcome, payload_envelope_verification::{EnvelopeError, EnvelopeImportData, EnvelopeProcessingSnapshot, ExecutionPendingEnvelope, IntoExecutionPendingEnvelope, MaybeAvailableEnvelope, load_snapshot, payload_notifier::PayloadNotifier} + BeaconChain, BeaconChainError, BeaconChainTypes, NotifyExecutionLayer, + PayloadVerificationOutcome, + payload_envelope_verification::{ + EnvelopeError, EnvelopeImportData, EnvelopeProcessingSnapshot, ExecutionPendingEnvelope, + IntoExecutionPendingEnvelope, MaybeAvailableEnvelope, load_snapshot, + payload_notifier::PayloadNotifier, + }, }; /// A wrapper around a `SignedExecutionPayloadEnvelope` that indicates it has been approved for re-gossiping on @@ -29,7 +36,7 @@ impl GossipVerifiedEnvelope { let beacon_block_root = envelope.beacon_block_root; // Check that we've seen the beacon block for this envelope and that it passes validation. - // TODO(EIP-7732): We need a block status table in order to differentiate between: + // TODO(EIP-7732): We might need some type of status table in order to differentiate between: // // 1. Blocks we haven't seen (IGNORE), and // 2. Blocks we've seen that are invalid (REJECT). @@ -41,14 +48,16 @@ impl GossipVerifiedEnvelope { block_root: beacon_block_root, }); }; + + let latest_finalized_slot = fork_choice_read_lock + .finalized_checkpoint() + .epoch + .start_slot(T::EthSpec::slots_per_epoch()); + drop(fork_choice_read_lock); // TODO(EIP-7732): check that we haven't seen another valid `SignedExecutionPayloadEnvelope` // for this block root from this builder - envelope status table check - - // TODO(EIP-7732): this could be obtained from the ProtoBlock instead of the DB - // but this means the ProtoBlock needs to include something like the ExecutionBid - // will need to answer this question later. let block = chain .get_full_block(&beacon_block_root)? .ok_or_else(|| { @@ -61,11 +70,14 @@ impl GossipVerifiedEnvelope { .signed_execution_payload_bid()? .message; - // TODO(EIP-7732): Gossip rules for the beacon block contain the following: - // https://github.com/ethereum/consensus-specs/blob/master/specs/phase0/p2p-interface.md#beacon_block - // [IGNORE] The block is not from a future slot (with a MAXIMUM_GOSSIP_CLOCK_DISPARITY allowance) - // [IGNORE] The block is from a slot greater than the latest finalized slot - // should these kinds of checks be included for envelopes as well? + // check that the envelopes slot isnt from a slot prior + // to the latest finalized slot. + if envelope.slot < latest_finalized_slot { + return Err(EnvelopeError::PriorToFinalization { + payload_slot: envelope.slot, + latest_finalized_slot, + }); + } // check that the slot of the envelope matches the slot of the parent block if envelope.slot != block.slot() { @@ -119,17 +131,22 @@ impl GossipVerifiedEnvelope { )?; let fork = proposer.fork; - // True builder index accounting for self-building. - let proposer_index = block.message().proposer_index(); let builder_index = envelope.builder_index; + let index = if builder_index == BUILDER_INDEX_SELF_BUILD { + block.message().proposer_index() + } else { + builder_index + }; let signature_is_valid = { + // TODO(gloas) the builder pubkey wont be in the validator pubkey cache + // this will currently only work for local block building. let pubkey_cache = chain.validator_pubkey_cache.read(); - let builder_pubkey = pubkey_cache - .get(builder_index as usize) - .ok_or_else(|| EnvelopeError::UnknownValidator { builder_index })?; + let pubkey = pubkey_cache + .get(index as usize) + .ok_or_else(|| EnvelopeError::UnknownValidator { builder_index: index })?; signed_envelope.verify_signature( - builder_pubkey, + pubkey, &fork, chain.genesis_validators_root, &chain.spec, @@ -161,9 +178,17 @@ impl IntoExecutionPendingEnvelope for GossipVerifiedEnve let signed_envelope = self.signed_envelope; let envelope = &signed_envelope.message; let payload = &envelope.payload; - + // TODO(gloas) unwrap - let bid = chain.get_full_block(&envelope.beacon_block_root).unwrap().unwrap().message().body().signed_execution_payload_bid().unwrap().message; + let bid = chain + .get_full_block(&envelope.beacon_block_root) + .unwrap() + .unwrap() + .message() + .body() + .signed_execution_payload_bid() + .unwrap() + .message; // Verify the execution payload is valid let payload_notifier = diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/mod.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/mod.rs index 4a739e74c56..b166e90fa14 100644 --- a/beacon_node/beacon_chain/src/payload_envelope_verification/mod.rs +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/mod.rs @@ -17,6 +17,9 @@ //! |--------------- //! | //! ▼ +//! SignatureVerifiedEnvelope +//! | +//! ▼ //! ExecutionPendingEnvelope //! | //! await @@ -28,15 +31,25 @@ use std::sync::Arc; -use state_processing::{BlockProcessingError, ConsensusContext, envelope_processing::EnvelopeProcessingError}; +use state_processing::{ + BlockProcessingError, ConsensusContext, envelope_processing::EnvelopeProcessingError, +}; use tracing::instrument; -use types::{BeaconState, BeaconStateError, ChainSpec, DataColumnSidecarList, EthSpec, ExecutionBlockHash, Hash256, SignedBeaconBlock, SignedExecutionPayloadEnvelope, Slot}; +use types::{ + BeaconState, BeaconStateError, ChainSpec, DataColumnSidecarList, EthSpec, ExecutionBlockHash, + Hash256, SignedBeaconBlock, SignedExecutionPayloadEnvelope, Slot, +}; -use crate::{BeaconChain, BeaconChainError, BeaconChainTypes, NotifyExecutionLayer, block_verification::PayloadVerificationHandle, payload_envelope_verification::gossip_verified_envelope::GossipVerifiedEnvelope}; +use crate::{ + BeaconChain, BeaconChainError, BeaconChainTypes, NotifyExecutionLayer, + block_verification::PayloadVerificationHandle, + payload_envelope_verification::gossip_verified_envelope::GossipVerifiedEnvelope, +}; pub mod execution_pending_envelope; pub mod gossip_verified_envelope; mod payload_notifier; +mod signature_verified_envelope; pub trait IntoExecutionPendingEnvelope: Sized { fn into_execution_pending_envelope( @@ -115,6 +128,12 @@ pub enum EnvelopeError { committed_bid: ExecutionBlockHash, envelope: ExecutionBlockHash, }, + // The slot belongs to a block that is from a slot prior than + // the most recently finalized slot + PriorToFinalization { + payload_slot: Slot, + latest_finalized_slot: Slot, + }, // Some Beacon Chain Error BeaconChainError(Arc), // Some Beacon State error diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/payload_notifier.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/payload_notifier.rs index 15fec7e21dd..6bbdd971a51 100644 --- a/beacon_node/beacon_chain/src/payload_envelope_verification/payload_notifier.rs +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/payload_notifier.rs @@ -2,12 +2,19 @@ use std::sync::Arc; use execution_layer::NewPayloadRequest; use fork_choice::PayloadVerificationStatus; -use state_processing::{envelope_processing::partially_verify_payload_envelope, per_block_processing::is_execution_enabled}; +use state_processing::{ + envelope_processing::partially_verify_payload_envelope, + per_block_processing::is_execution_enabled, +}; use tracing::warn; -use types::{BeaconState, ExecutionPayloadBid, Hash256, SignedBeaconBlock, SignedExecutionPayloadEnvelope}; - -use crate::{BeaconChain, BeaconChainTypes, BlockError, ExecutionPayloadError, NotifyExecutionLayer, execution_payload::notify_new_payload}; +use types::{ + BeaconState, ExecutionPayloadBid, Hash256, SignedBeaconBlock, SignedExecutionPayloadEnvelope, +}; +use crate::{ + BeaconChain, BeaconChainTypes, BlockError, ExecutionPayloadError, NotifyExecutionLayer, + execution_payload::notify_new_payload, +}; /// Used to await the result of executing payload with a remote EE. pub struct PayloadNotifier { @@ -24,7 +31,7 @@ impl PayloadNotifier { state: &BeaconState, notify_execution_layer: NotifyExecutionLayer, ) -> Result { - let payload_verification_status = { + let payload_verification_status = { // Perform the initial stages of payload verification. // // We will duplicate these checks again during `per_block_processing`, however these @@ -32,11 +39,7 @@ impl PayloadNotifier { // the block as optimistically imported. This is particularly relevant in the case // where we do not send the block to the EL at all. let payload_message = &envelope.message; - partially_verify_payload_envelope( - state, - &envelope, - &chain.spec, - ).unwrap(); // TODO(gloas) unwrap + partially_verify_payload_envelope(state, &envelope, &chain.spec).unwrap(); // TODO(gloas) unwrap match notify_execution_layer { NotifyExecutionLayer::No if chain.config.optimistic_finalized_sync => { @@ -71,7 +74,12 @@ impl PayloadNotifier { Ok(precomputed_status) } else { // tODO(gloas) fix zero - notify_new_payload(&self.chain, Hash256::ZERO, self.envelope.message.try_into()?).await + notify_new_payload( + &self.chain, + Hash256::ZERO, + self.envelope.message.try_into()?, + ) + .await } } -} \ No newline at end of file +} diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/signature_verified_envelope.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/signature_verified_envelope.rs new file mode 100644 index 00000000000..cd430daf5b3 --- /dev/null +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/signature_verified_envelope.rs @@ -0,0 +1,40 @@ +use std::sync::Arc; + +use state_processing::ConsensusContext; +use types::{BeaconState, Hash256, SignedExecutionPayloadEnvelope}; + +use crate::{BeaconChain, BeaconChainTypes, payload_envelope_verification::{EnvelopeError, MaybeAvailableEnvelope}}; + + +/// A wrapper around a `SignedExecutionPayloadEnvelope` that indicates that all signatures (except the deposit +/// signatures) have been verified. +pub struct SignatureVerifiedEnvelope { + envelope: SignedExecutionPayloadEnvelope, + block_root: Hash256, + state: Option>, + consensus_context: ConsensusContext, +} + + +impl SignatureVerifiedEnvelope { + pub fn new( + envelope: Arc>, + state: &mut BeaconState, + block_root: Hash256, + chain: &BeaconChain, + ) -> Result { + let is_signature_valid = envelope.verify_signature_with_state(state, &chain.spec)?; + + if !is_signature_valid { + return Err(EnvelopeError::BadSignature) + } + + Self { + envelope, + block_root, + state + } + + todo!() + } +} \ No newline at end of file diff --git a/beacon_node/execution_layer/src/engine_api/new_payload_request.rs b/beacon_node/execution_layer/src/engine_api/new_payload_request.rs index b88dd7ca3a5..145d171bbcf 100644 --- a/beacon_node/execution_layer/src/engine_api/new_payload_request.rs +++ b/beacon_node/execution_layer/src/engine_api/new_payload_request.rs @@ -4,7 +4,8 @@ use crate::versioned_hashes::verify_versioned_hashes; use state_processing::per_block_processing::deneb::kzg_commitment_to_versioned_hash; use superstruct::superstruct; use types::{ - BeaconBlockRef, BeaconStateError, EthSpec, ExecutionBlockHash, ExecutionPayload, ExecutionPayloadEnvelope, ExecutionPayloadRef, Hash256, VersionedHash + BeaconBlockRef, BeaconStateError, EthSpec, ExecutionBlockHash, ExecutionPayload, + ExecutionPayloadEnvelope, ExecutionPayloadRef, Hash256, VersionedHash, }; use types::{ ExecutionPayloadBellatrix, ExecutionPayloadCapella, ExecutionPayloadDeneb, diff --git a/consensus/state_processing/src/envelope_processing.rs b/consensus/state_processing/src/envelope_processing.rs index 2f511846a53..0cdb6caaef4 100644 --- a/consensus/state_processing/src/envelope_processing.rs +++ b/consensus/state_processing/src/envelope_processing.rs @@ -277,7 +277,6 @@ pub fn process_execution_payload_envelope( Ok(()) } - /// Performs *partial* verification of the `payload envelope`. pub fn partially_verify_payload_envelope( state: &BeaconState, @@ -286,7 +285,7 @@ pub fn partially_verify_payload_envelope( ) -> Result<(), EnvelopeProcessingError> { let envelope = &signed_envelope.message; let payload = &signed_envelope.message.payload; - + // Verify consistency with the beacon block let latest_block_header_root = state.latest_block_header().tree_hash_root(); envelope_verify!( From 9f972d1743d068ba82199ad8b4c9b410927b3fdb Mon Sep 17 00:00:00 2001 From: Eitan Seri- Levi Date: Wed, 11 Feb 2026 14:53:24 -0800 Subject: [PATCH 4/9] progress --- beacon_node/beacon_chain/src/beacon_chain.rs | 28 +++++ .../beacon_chain/src/execution_payload.rs | 1 - .../gossip_verified_envelope.rs | 102 +++++++++--------- .../src/payload_envelope_verification/mod.rs | 80 ++++++++++++-- .../payload_notifier.rs | 66 +++++++----- .../signature_verified_envelope.rs | 40 ------- .../payload_envelope_verification/tests.rs | 20 ++++ .../src/engine_api/new_payload_request.rs | 2 +- .../src/envelope_processing.rs | 100 ----------------- 9 files changed, 211 insertions(+), 228 deletions(-) delete mode 100644 beacon_node/beacon_chain/src/payload_envelope_verification/signature_verified_envelope.rs create mode 100644 beacon_node/beacon_chain/src/payload_envelope_verification/tests.rs diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 801ef7696fd..7b825be2c3c 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -56,6 +56,7 @@ use crate::observed_block_producers::ObservedBlockProducers; use crate::observed_data_sidecars::ObservedDataSidecars; use crate::observed_operations::{ObservationOutcome, ObservedOperations}; use crate::observed_slashable::ObservedSlashable; +use crate::payload_envelope_verification::{ExecutedEnvelope, ExecutionPendingEnvelope}; use crate::persisted_beacon_chain::PersistedBeaconChain; use crate::persisted_custody::persist_custody_context; use crate::persisted_fork_choice::PersistedForkChoice; @@ -3547,6 +3548,33 @@ impl BeaconChain { )) } + /// Accepts a fully-verified payload envelope and awaits on its payload verification handle to + /// get a fully `ExecutedEnvelope`. + /// + /// An error is returned if the verification handle couldn't be awaited. + #[instrument(skip_all, level = "debug")] + pub async fn into_executed_payload_envelope( + self: Arc, + pending_envelope: ExecutionPendingEnvelope, + ) -> Result, BlockError> { + let ExecutionPendingEnvelope { + signed_envelope, + import_data, + payload_verification_handle, + } = pending_envelope; + + let payload_verification_outcome = payload_verification_handle + .await + .map_err(BeaconChainError::TokioJoin)? + .ok_or(BeaconChainError::RuntimeShutdown)??; + + Ok(ExecutedEnvelope::new( + signed_envelope, + import_data, + payload_verification_outcome, + )) + } + /* Import methods */ /// Checks if the block is available, and imports immediately if so, otherwise caches the block diff --git a/beacon_node/beacon_chain/src/execution_payload.rs b/beacon_node/beacon_chain/src/execution_payload.rs index 631a0180dd0..0be058231a8 100644 --- a/beacon_node/beacon_chain/src/execution_payload.rs +++ b/beacon_node/beacon_chain/src/execution_payload.rs @@ -7,7 +7,6 @@ //! So, this module contains functions that one might expect to find in other crates, but they live //! here for good reason. -use crate::payload_envelope_verification::EnvelopeError; use crate::{ BeaconChain, BeaconChainError, BeaconChainTypes, BlockError, BlockProductionError, ExecutionPayloadError, diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs index 580c6e92343..a7ccc056acf 100644 --- a/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs @@ -4,7 +4,10 @@ use educe::Educe; use slot_clock::SlotClock; use state_processing::{VerifySignatures, envelope_processing::process_execution_payload_envelope}; use tracing::debug; -use types::{EthSpec, SignedBeaconBlock, SignedExecutionPayloadEnvelope, consts::gloas::BUILDER_INDEX_SELF_BUILD}; +use types::{ + EthSpec, SignedBeaconBlock, SignedExecutionPayloadEnvelope, + consts::gloas::BUILDER_INDEX_SELF_BUILD, +}; use crate::{ BeaconChain, BeaconChainError, BeaconChainTypes, NotifyExecutionLayer, @@ -70,7 +73,7 @@ impl GossipVerifiedEnvelope { .signed_execution_payload_bid()? .message; - // check that the envelopes slot isnt from a slot prior + // check that the envelopes slot isnt from a slot prior // to the latest finalized slot. if envelope.slot < latest_finalized_slot { return Err(EnvelopeError::PriorToFinalization { @@ -103,54 +106,58 @@ impl GossipVerifiedEnvelope { }); } - // Get the fork from the proposer cache so we can verify the signature. - // This is currently the most efficient way to implement envelope signature verification - // because the `fork` might depend on advancing the parent state. + // Verify the envelope signature. + // + // For self-build envelopes, we can use the proposer cache for the fork and the + // validator pubkey cache for the proposer's pubkey, avoiding a state load from disk. + // For external builder envelopes, we must load the state to access the builder registry. + let builder_index = envelope.builder_index; let block_slot = envelope.slot; let block_epoch = block_slot.epoch(T::EthSpec::slots_per_epoch()); let proposer_shuffling_decision_block = proto_block.proposer_shuffling_root_for_child_block(block_epoch, &chain.spec); - let mut opt_snapshot = None; - let envelope_ref = signed_envelope.as_ref(); - let proposer = chain.with_proposer_cache::<_, EnvelopeError>( - proposer_shuffling_decision_block, - block_epoch, - |proposers| proposers.get_slot::(block_slot), - || { - debug!( - %beacon_block_root, - block_hash = %envelope_ref.block_hash(), - "Proposer shuffling cache miss for envelope verification" - ); - // The proposer index was *not* cached and we must load the parent in order to - // determine the proposer index. - let snapshot = load_snapshot(envelope_ref, chain)?; - opt_snapshot = Some(Box::new(snapshot.clone())); - Ok((snapshot.state_root, snapshot.pre_state)) - }, - )?; - let fork = proposer.fork; - let builder_index = envelope.builder_index; - let index = if builder_index == BUILDER_INDEX_SELF_BUILD { - block.message().proposer_index() - } else { - builder_index - }; + let (signature_is_valid, opt_snapshot) = if builder_index == BUILDER_INDEX_SELF_BUILD { + // Fast path: self-build envelopes can be verified without loading the state. + let envelope_ref = signed_envelope.as_ref(); + let mut opt_snapshot = None; + let proposer = chain.with_proposer_cache::<_, EnvelopeError>( + proposer_shuffling_decision_block, + block_epoch, + |proposers| proposers.get_slot::(block_slot), + || { + debug!( + %beacon_block_root, + "Proposer shuffling cache miss for envelope verification" + ); + let snapshot = load_snapshot(envelope_ref, chain)?; + opt_snapshot = Some(Box::new(snapshot.clone())); + Ok((snapshot.state_root, snapshot.pre_state)) + }, + )?; + let fork = proposer.fork; - let signature_is_valid = { - // TODO(gloas) the builder pubkey wont be in the validator pubkey cache - // this will currently only work for local block building. let pubkey_cache = chain.validator_pubkey_cache.read(); let pubkey = pubkey_cache - .get(index as usize) - .ok_or_else(|| EnvelopeError::UnknownValidator { builder_index: index })?; - signed_envelope.verify_signature( + .get(block.message().proposer_index() as usize) + .ok_or_else(|| EnvelopeError::UnknownValidator { + builder_index: block.message().proposer_index(), + })?; + let is_valid = signed_envelope.verify_signature( pubkey, &fork, chain.genesis_validators_root, &chain.spec, - ) + ); + (is_valid, opt_snapshot) + } else { + // TODO(gloas) we should probably introduce a builder cache or some type of + // global cache. + // External builder: must load the state to get the builder pubkey. + let snapshot = load_snapshot(signed_envelope.as_ref(), chain)?; + let is_valid = + signed_envelope.verify_signature_with_state(&snapshot.pre_state, &chain.spec)?; + (is_valid, Some(Box::new(snapshot))) }; if !signature_is_valid { @@ -179,20 +186,13 @@ impl IntoExecutionPendingEnvelope for GossipVerifiedEnve let envelope = &signed_envelope.message; let payload = &envelope.payload; - // TODO(gloas) unwrap - let bid = chain - .get_full_block(&envelope.beacon_block_root) - .unwrap() - .unwrap() - .message() - .body() - .signed_execution_payload_bid() - .unwrap() - .message; - // Verify the execution payload is valid - let payload_notifier = - PayloadNotifier::new(chain.clone(), envelope, notify_execution_layer)?; + let payload_notifier = PayloadNotifier::new( + chain.clone(), + signed_envelope.clone(), + self.block.clone(), + notify_execution_layer, + )?; let block_root = envelope.beacon_block_root; let slot = self.block.slot(); diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/mod.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/mod.rs index b166e90fa14..7ddc4b5c64b 100644 --- a/beacon_node/beacon_chain/src/payload_envelope_verification/mod.rs +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/mod.rs @@ -17,9 +17,6 @@ //! |--------------- //! | //! ▼ -//! SignatureVerifiedEnvelope -//! | -//! ▼ //! ExecutionPendingEnvelope //! | //! await @@ -41,15 +38,16 @@ use types::{ }; use crate::{ - BeaconChain, BeaconChainError, BeaconChainTypes, NotifyExecutionLayer, - block_verification::PayloadVerificationHandle, + AvailabilityProcessingStatus, BeaconChain, BeaconChainError, BeaconChainTypes, + ExecutionPayloadError, NotifyExecutionLayer, PayloadVerificationOutcome, + block_verification::PayloadVerificationHandle, block_verification_types::BlockImportData, payload_envelope_verification::gossip_verified_envelope::GossipVerifiedEnvelope, }; pub mod execution_pending_envelope; pub mod gossip_verified_envelope; mod payload_notifier; -mod signature_verified_envelope; +mod tests; pub trait IntoExecutionPendingEnvelope: Sized { fn into_execution_pending_envelope( @@ -101,7 +99,67 @@ pub struct EnvelopeProcessingSnapshot { pub beacon_block_root: Hash256, } -#[derive(Debug, Clone)] +/// A payload envelope that has gone through processing checks and execution by an EL client. +/// This envelope hasn't necessarily completed data availability checks. +/// +/// +/// It contains 2 variants: +/// 1. `Available`: This enelope has been executed and also contains all data to consider it +/// fully available. +/// 2. `AvailabilityPending`: This envelope hasn't received all required blobs to consider it +/// fully available. +pub enum ExecutedEnvelope { + Available(AvailableExecutedEnvelope), + // TODO(gloas) implement availability pending + AvailabilityPending(), +} + +impl ExecutedEnvelope { + pub fn new( + envelope: MaybeAvailableEnvelope, + import_data: EnvelopeImportData, + payload_verification_outcome: PayloadVerificationOutcome, + ) -> Self { + match envelope { + MaybeAvailableEnvelope::Available(available_envelope) => { + Self::Available(AvailableExecutedEnvelope::new( + available_envelope, + import_data, + payload_verification_outcome, + )) + } + // TODO(gloas) implement availability pending + MaybeAvailableEnvelope::AvailabilityPending { + block_hash: _, + envelope: _, + } => Self::AvailabilityPending(), + } + } +} + +/// A payload envelope that has completed all payload processing checks including verification +/// by an EL client **and** has all requisite blob data to be imported into fork choice. +pub struct AvailableExecutedEnvelope { + pub envelope: AvailableEnvelope, + pub import_data: EnvelopeImportData, + pub payload_verification_outcome: PayloadVerificationOutcome, +} + +impl AvailableExecutedEnvelope { + pub fn new( + envelope: AvailableEnvelope, + import_data: EnvelopeImportData, + payload_verification_outcome: PayloadVerificationOutcome, + ) -> Self { + Self { + envelope, + import_data, + payload_verification_outcome, + } + } +} + +#[derive(Debug)] pub enum EnvelopeError { /// The envelope's block root is unknown. BlockRootUnknown { @@ -142,6 +200,8 @@ pub enum EnvelopeError { BlockProcessingError(BlockProcessingError), // Some EnvelopeProcessingError EnvelopeProcessingError(EnvelopeProcessingError), + // Error verifying the execution payload + ExecutionPayloadError(ExecutionPayloadError), } impl From for EnvelopeError { @@ -150,6 +210,12 @@ impl From for EnvelopeError { } } +impl From for EnvelopeError { + fn from(e: ExecutionPayloadError) -> Self { + EnvelopeError::ExecutionPayloadError(e) + } +} + impl From for EnvelopeError { fn from(e: BeaconStateError) -> Self { EnvelopeError::BeaconStateError(e) diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/payload_notifier.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/payload_notifier.rs index 6bbdd971a51..b485af0bfaa 100644 --- a/beacon_node/beacon_chain/src/payload_envelope_verification/payload_notifier.rs +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/payload_notifier.rs @@ -1,15 +1,10 @@ use std::sync::Arc; -use execution_layer::NewPayloadRequest; +use execution_layer::{NewPayloadRequest, NewPayloadRequestGloas}; use fork_choice::PayloadVerificationStatus; -use state_processing::{ - envelope_processing::partially_verify_payload_envelope, - per_block_processing::is_execution_enabled, -}; +use state_processing::per_block_processing::deneb::kzg_commitment_to_versioned_hash; use tracing::warn; -use types::{ - BeaconState, ExecutionPayloadBid, Hash256, SignedBeaconBlock, SignedExecutionPayloadEnvelope, -}; +use types::{SignedBeaconBlock, SignedExecutionPayloadEnvelope}; use crate::{ BeaconChain, BeaconChainTypes, BlockError, ExecutionPayloadError, NotifyExecutionLayer, @@ -19,33 +14,26 @@ use crate::{ /// Used to await the result of executing payload with a remote EE. pub struct PayloadNotifier { pub chain: Arc>, - pub envelope: Arc>, + envelope: Arc>, + block: Arc>, payload_verification_status: Option, } impl PayloadNotifier { pub fn new( chain: Arc>, - bid: &ExecutionPayloadBid, envelope: Arc>, - state: &BeaconState, + block: Arc>, notify_execution_layer: NotifyExecutionLayer, ) -> Result { let payload_verification_status = { - // Perform the initial stages of payload verification. - // - // We will duplicate these checks again during `per_block_processing`, however these - // checks are cheap and doing them here ensures we have verified them before marking - // the block as optimistically imported. This is particularly relevant in the case - // where we do not send the block to the EL at all. let payload_message = &envelope.message; - partially_verify_payload_envelope(state, &envelope, &chain.spec).unwrap(); // TODO(gloas) unwrap match notify_execution_layer { NotifyExecutionLayer::No if chain.config.optimistic_finalized_sync => { - // Create a NewPayloadRequest (no clones required) and check optimistic sync verifications - let new_payload_request: NewPayloadRequest = - payload_message.try_into()?; + // TODO(gloas) unwrap + let new_payload_request = + Self::build_new_payload_request(&envelope, &block).unwrap(); if let Err(e) = new_payload_request.perform_optimistic_sync_verifications() { warn!( block_number = ?payload_message.payload.block_number, @@ -65,6 +53,7 @@ impl PayloadNotifier { Ok(Self { chain, envelope, + block, payload_verification_status, }) } @@ -73,13 +62,34 @@ impl PayloadNotifier { if let Some(precomputed_status) = self.payload_verification_status { Ok(precomputed_status) } else { - // tODO(gloas) fix zero - notify_new_payload( - &self.chain, - Hash256::ZERO, - self.envelope.message.try_into()?, - ) - .await + let block_root = self.envelope.message.beacon_block_root; + let request = Self::build_new_payload_request(&self.envelope, &self.block)?; + notify_new_payload(&self.chain, block_root, request).await } } + + fn build_new_payload_request<'a>( + envelope: &'a SignedExecutionPayloadEnvelope, + block: &'a SignedBeaconBlock, + ) -> Result, BlockError> { + let bid = &block + .message() + .body() + .signed_execution_payload_bid() + .map_err(|e| BlockError::BeaconChainError(Box::new(e.into())))? + .message; + + let versioned_hashes = bid + .blob_kzg_commitments + .iter() + .map(kzg_commitment_to_versioned_hash) + .collect(); + + Ok(NewPayloadRequest::Gloas(NewPayloadRequestGloas { + execution_payload: &envelope.message.payload, + versioned_hashes, + parent_beacon_block_root: block.message().parent_root(), + execution_requests: &envelope.message.execution_requests, + })) + } } diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/signature_verified_envelope.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/signature_verified_envelope.rs deleted file mode 100644 index cd430daf5b3..00000000000 --- a/beacon_node/beacon_chain/src/payload_envelope_verification/signature_verified_envelope.rs +++ /dev/null @@ -1,40 +0,0 @@ -use std::sync::Arc; - -use state_processing::ConsensusContext; -use types::{BeaconState, Hash256, SignedExecutionPayloadEnvelope}; - -use crate::{BeaconChain, BeaconChainTypes, payload_envelope_verification::{EnvelopeError, MaybeAvailableEnvelope}}; - - -/// A wrapper around a `SignedExecutionPayloadEnvelope` that indicates that all signatures (except the deposit -/// signatures) have been verified. -pub struct SignatureVerifiedEnvelope { - envelope: SignedExecutionPayloadEnvelope, - block_root: Hash256, - state: Option>, - consensus_context: ConsensusContext, -} - - -impl SignatureVerifiedEnvelope { - pub fn new( - envelope: Arc>, - state: &mut BeaconState, - block_root: Hash256, - chain: &BeaconChain, - ) -> Result { - let is_signature_valid = envelope.verify_signature_with_state(state, &chain.spec)?; - - if !is_signature_valid { - return Err(EnvelopeError::BadSignature) - } - - Self { - envelope, - block_root, - state - } - - todo!() - } -} \ No newline at end of file diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/tests.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/tests.rs new file mode 100644 index 00000000000..381a5eaa4f4 --- /dev/null +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/tests.rs @@ -0,0 +1,20 @@ +use std::sync::Arc; + +use crate::{AvailabilityProcessingStatus, BeaconChain, BeaconChainTypes, payload_envelope_verification::{ExecutedEnvelope, ExecutionPendingEnvelope}}; + +async fn import_execution_pending_envelope( + chain: Arc>, + execution_pending_envelope: ExecutionPendingEnvelope, +) -> Result { + match chain + .clone() + .into_executed_payload_envelope(execution_pending_envelope) + .await + .unwrap() + { + ExecutedEnvelope::Available(envelope) => todo!(), + ExecutedEnvelope::AvailabilityPending() => { + Err("AvailabilityPending not expected in this test. Block not imported.".to_string()) + } + } +} diff --git a/beacon_node/execution_layer/src/engine_api/new_payload_request.rs b/beacon_node/execution_layer/src/engine_api/new_payload_request.rs index 145d171bbcf..ba94296b859 100644 --- a/beacon_node/execution_layer/src/engine_api/new_payload_request.rs +++ b/beacon_node/execution_layer/src/engine_api/new_payload_request.rs @@ -5,7 +5,7 @@ use state_processing::per_block_processing::deneb::kzg_commitment_to_versioned_h use superstruct::superstruct; use types::{ BeaconBlockRef, BeaconStateError, EthSpec, ExecutionBlockHash, ExecutionPayload, - ExecutionPayloadEnvelope, ExecutionPayloadRef, Hash256, VersionedHash, + ExecutionPayloadRef, Hash256, VersionedHash, }; use types::{ ExecutionPayloadBellatrix, ExecutionPayloadCapella, ExecutionPayloadDeneb, diff --git a/consensus/state_processing/src/envelope_processing.rs b/consensus/state_processing/src/envelope_processing.rs index 0cdb6caaef4..d46728dbbc1 100644 --- a/consensus/state_processing/src/envelope_processing.rs +++ b/consensus/state_processing/src/envelope_processing.rs @@ -276,103 +276,3 @@ pub fn process_execution_payload_envelope( Ok(()) } - -/// Performs *partial* verification of the `payload envelope`. -pub fn partially_verify_payload_envelope( - state: &BeaconState, - signed_envelope: &SignedExecutionPayloadEnvelope, - spec: &ChainSpec, -) -> Result<(), EnvelopeProcessingError> { - let envelope = &signed_envelope.message; - let payload = &signed_envelope.message.payload; - - // Verify consistency with the beacon block - let latest_block_header_root = state.latest_block_header().tree_hash_root(); - envelope_verify!( - envelope.beacon_block_root == latest_block_header_root, - EnvelopeProcessingError::LatestBlockHeaderMismatch { - envelope_root: envelope.beacon_block_root, - block_header_root: latest_block_header_root, - } - ); - envelope_verify!( - envelope.slot == state.slot(), - EnvelopeProcessingError::SlotMismatch { - envelope_slot: envelope.slot, - parent_state_slot: state.slot(), - } - ); - - // Verify consistency with the committed bid - let committed_bid = state.latest_execution_payload_bid()?; - envelope_verify!( - envelope.builder_index == committed_bid.builder_index, - EnvelopeProcessingError::BuilderIndexMismatch { - committed_bid: committed_bid.builder_index, - envelope: envelope.builder_index, - } - ); - envelope_verify!( - committed_bid.prev_randao == payload.prev_randao, - EnvelopeProcessingError::PrevRandaoMismatch { - committed_bid: committed_bid.prev_randao, - envelope: payload.prev_randao, - } - ); - - // Verify consistency with expected withdrawals - // NOTE: we don't bother hashing here except in case of error, because we can just compare for - // equality directly. This equality check could be more straight-forward if the types were - // changed to match (currently we are comparing VariableList to List). This could happen - // coincidentally when we adopt ProgressiveList. - envelope_verify!( - payload.withdrawals.len() == state.payload_expected_withdrawals()?.len() - && payload - .withdrawals - .iter() - .eq(state.payload_expected_withdrawals()?.iter()), - EnvelopeProcessingError::WithdrawalsRootMismatch { - state: state.payload_expected_withdrawals()?.tree_hash_root(), - payload: payload.withdrawals.tree_hash_root(), - } - ); - - // Verify the gas limit - envelope_verify!( - committed_bid.gas_limit == payload.gas_limit, - EnvelopeProcessingError::GasLimitMismatch { - committed_bid: committed_bid.gas_limit, - envelope: payload.gas_limit, - } - ); - - // Verify the block hash - envelope_verify!( - committed_bid.block_hash == payload.block_hash, - EnvelopeProcessingError::BlockHashMismatch { - committed_bid: committed_bid.block_hash, - envelope: payload.block_hash, - } - ); - - // Verify consistency of the parent hash with respect to the previous execution payload - envelope_verify!( - payload.parent_hash == *state.latest_block_hash()?, - EnvelopeProcessingError::ParentHashMismatch { - state: *state.latest_block_hash()?, - envelope: payload.parent_hash, - } - ); - - // Verify timestamp - let state_timestamp = compute_timestamp_at_slot(state, state.slot(), spec)?; - envelope_verify!( - payload.timestamp == state_timestamp, - EnvelopeProcessingError::TimestampMismatch { - state: state_timestamp, - envelope: payload.timestamp, - } - ); - - Ok(()) -} From f637a68e04e9ca3a810245a38cc67ff3475e30ef Mon Sep 17 00:00:00 2001 From: Eitan Seri- Levi Date: Wed, 11 Feb 2026 23:34:53 -0800 Subject: [PATCH 5/9] Import payload flow --- beacon_node/beacon_chain/src/beacon_chain.rs | 6 +- .../beacon_chain/src/execution_payload.rs | 6 +- beacon_node/beacon_chain/src/lib.rs | 1 + .../src/payload_envelope_import/mod.rs | 339 ++++++++++++++++++ .../execution_pending_envelope.rs | 28 -- .../gossip_verified_envelope.rs | 65 +++- .../src/payload_envelope_verification/mod.rs | 58 ++- .../payload_notifier.rs | 2 +- .../payload_envelope_verification/tests.rs | 7 +- .../beacon_chain/src/validator_monitor.rs | 2 +- .../gossip_methods.rs | 145 +++++++- 11 files changed, 599 insertions(+), 60 deletions(-) create mode 100644 beacon_node/beacon_chain/src/payload_envelope_import/mod.rs delete mode 100644 beacon_node/beacon_chain/src/payload_envelope_verification/execution_pending_envelope.rs diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 7b825be2c3c..0c10aaadc4d 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -3555,7 +3555,7 @@ impl BeaconChain { #[instrument(skip_all, level = "debug")] pub async fn into_executed_payload_envelope( self: Arc, - pending_envelope: ExecutionPendingEnvelope, + pending_envelope: ExecutionPendingEnvelope, ) -> Result, BlockError> { let ExecutionPendingEnvelope { signed_envelope, @@ -4168,7 +4168,7 @@ impl BeaconChain { Ok(block_root) } - fn handle_import_block_db_write_error( + pub(crate) fn handle_import_block_db_write_error( &self, // We don't actually need this value, however it's always present when we call this function // and it needs to be dropped to prevent a dead-lock. Requiring it to be passed here is @@ -4202,7 +4202,7 @@ impl BeaconChain { } /// Check block's consistentency with any configured weak subjectivity checkpoint. - fn check_block_against_weak_subjectivity_checkpoint( + pub(crate) fn check_block_against_weak_subjectivity_checkpoint( &self, block: BeaconBlockRef, block_root: Hash256, diff --git a/beacon_node/beacon_chain/src/execution_payload.rs b/beacon_node/beacon_chain/src/execution_payload.rs index 0be058231a8..b0644ac8aad 100644 --- a/beacon_node/beacon_chain/src/execution_payload.rs +++ b/beacon_node/beacon_chain/src/execution_payload.rs @@ -160,7 +160,8 @@ pub async fn notify_new_payload( ?validation_error, ?latest_valid_hash, ?execution_block_hash, - // root = ?block.tree_hash_root(), + // TODO(gloas) are these other logs important? + root = ?beacon_block_root, // graffiti = block.body().graffiti().as_utf8_lossy(), // proposer_index = block.proposer_index(), // slot = %block.slot(), @@ -205,7 +206,8 @@ pub async fn notify_new_payload( warn!( ?validation_error, ?execution_block_hash, - // root = ?block.tree_hash_root(), + // TODO(gloas) are these other logs important? + root = ?beacon_block_root, // graffiti = block.body().graffiti().as_utf8_lossy(), // proposer_index = block.proposer_index(), // slot = %block.slot(), diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index 4120ed86fcc..f0151eed01a 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -42,6 +42,7 @@ pub mod observed_block_producers; pub mod observed_data_sidecars; pub mod observed_operations; mod observed_slashable; +pub mod payload_envelope_import; pub mod payload_envelope_verification; pub mod persisted_beacon_chain; pub mod persisted_custody; diff --git a/beacon_node/beacon_chain/src/payload_envelope_import/mod.rs b/beacon_node/beacon_chain/src/payload_envelope_import/mod.rs new file mode 100644 index 00000000000..ab7de5c15da --- /dev/null +++ b/beacon_node/beacon_chain/src/payload_envelope_import/mod.rs @@ -0,0 +1,339 @@ +use std::sync::Arc; + +use fork_choice::PayloadVerificationStatus; +use logging::crit; +use store::StoreOp; +use tracing::{debug, error, info_span, instrument}; +use types::{BeaconState, BlockImportSource, EthSpec, Hash256, SignedBeaconBlock}; + +use crate::{ + AvailabilityProcessingStatus, BeaconChain, BeaconChainError, BeaconChainTypes, BlockError, + NotifyExecutionLayer, + block_verification_types::{AsBlock, AvailableBlockData}, + payload_envelope_verification::{ + AvailableEnvelope, AvailableExecutedEnvelope, EnvelopeImportData, ExecutedEnvelope, + IntoExecutionPendingEnvelope, + }, + validator_monitor::timestamp_now, +}; + +impl BeaconChain { + /// Returns `Ok(block_root)` if the given `unverified_envelope` was successfully verified and + /// imported into the chain. + /// + /// Items that implement `IntoExecutionPendingEnvelope` include: + /// + /// - `GossipVerifiedEnvelope` + /// + /// ## Errors + /// + /// Returns an `Err` if the given block was invalid, or an error was encountered during + /// verification. + #[instrument(skip_all, fields(block_root = ?block_root, block_source = %block_source))] + pub async fn process_execution_payload_envelope>( + self: &Arc, + block_root: Hash256, + unverified_envelope: P, + notify_execution_layer: NotifyExecutionLayer, + block_source: BlockImportSource, + publish_fn: impl FnOnce() -> Result<(), BlockError>, + ) -> Result { + let block_slot = unverified_envelope.envelope().slot(); + + // TODO(gloas) Set observed time if not already set. Usually this should be set by gossip or RPC, + // but just in case we set it again here (useful for tests). + + // TODO(gloas) if we decide to insert the payload envelope into the new DA checker + // we should insert the pre executed envelope here. + + // TODO(gloas) Start the Prometheus timer. + // let _full_timer = metrics::start_timer(&metrics::BLOCK_PROCESSING_TIMES); + + // TODO(gloas) Increment the Prometheus counter for envelope processing requests. + // metrics::inc_counter(&metrics::BLOCK_PROCESSING_REQUESTS); + + // A small closure to group the verification and import errors. + let chain = self.clone(); + let import_envelope = async move { + let execution_pending = unverified_envelope + .into_execution_pending_envelope(&chain, notify_execution_layer)?; + publish_fn()?; + + // TODO(gloas) Record the time it took to complete consensus verification. + // if let Some(timestamp) = self.slot_clock.now_duration() { + // self.block_times_cache + // .write() + // .set_time_consensus_verified(block_root, block_slot, timestamp) + // } + + let executed_envelope = chain + .into_executed_payload_envelope(execution_pending) + .await + .inspect_err(|_| { + // TODO(gloas) If the envelope fails execution for whatever reason (e.g. engine offline), + // and we keep it in the cache, then the node will NOT perform lookup and + // reprocess this block until the block is evicted from DA checker, causing the + // chain to get stuck temporarily if the block is canonical. Therefore we remove + // it from the cache if execution fails. + + //self.data_availability_checker + // .remove_block_on_execution_error(&block_root); + })?; + + // TODO(gloas) Record the *additional* time it took to wait for execution layer verification. + // if let Some(timestamp) = self.slot_clock.now_duration() { + // self.block_times_cache + // .write() + // .set_time_executed(block_root, block_slot, timestamp) + // } + + match executed_envelope { + ExecutedEnvelope::Available(envelope) => { + self.import_available_execution_payload_envelope(Box::new(envelope)) + .await + } + ExecutedEnvelope::AvailabilityPending() => { + return Err(BlockError::InternalError( + "Pending payload envelope not yet implemented".to_owned(), + )); + } + } + }; + + // Verify and import the block. + match import_envelope.await { + // The block was successfully verified and imported. Yay. + Ok(status @ AvailabilityProcessingStatus::Imported(block_root)) => { + debug!( + ?block_root, + %block_slot, + source = %block_source, + "Envelope imported" + ); + + // TODO(gloas) Increment the Prometheus counter for block processing successes. + // metrics::inc_counter(&metrics::BLOCK_PROCESSING_SUCCESSES); + + Ok(status) + } + Ok(status @ AvailabilityProcessingStatus::MissingComponents(slot, block_root)) => { + debug!(?block_root, %slot, "Beacon block awaiting blobs"); + + Ok(status) + } + Err(BlockError::BeaconChainError(e)) => { + match e.as_ref() { + BeaconChainError::TokioJoin(e) => { + debug!( + error = ?e, + "Envelope processing cancelled" + ); + } + _ => { + // There was an error whilst attempting to verify and import the block. The block might + // be partially verified or partially imported. + crit!( + error = ?e, + "Envelope processing error" + ); + } + }; + Err(BlockError::BeaconChainError(e)) + } + // The block failed verification. + Err(other) => { + debug!(reason = other.to_string(), " Envelope rejected"); + Err(other) + } + } + } + + #[instrument(skip_all)] + pub async fn import_available_execution_payload_envelope( + self: &Arc, + envelope: Box>, + ) -> Result { + let AvailableExecutedEnvelope { + envelope, + import_data, + payload_verification_outcome, + } = *envelope; + + let EnvelopeImportData { + block_root, + block, + post_state, + } = import_data; + + // TODO(gloas) Record the time at which this block's blobs became available. + + let block_root = { + // Capture the current span before moving into the blocking task + let current_span = tracing::Span::current(); + let chain = self.clone(); + self.spawn_blocking_handle( + move || { + // Enter the captured span in the blocking thread + let _guard = current_span.enter(); + chain.import_execution_payload_envelope( + envelope, + block_root, + *post_state, + payload_verification_outcome.payload_verification_status, + block, + ) + }, + "payload_verification_handle", + ) + .await?? + }; + + Ok(AvailabilityProcessingStatus::Imported(block_root)) + } + + /// Accepts a fully-verified and available envelope and imports it into the chain without performing any + /// additional verification. + /// + /// An error is returned if the envelope was unable to be imported. It may be partially imported + /// (i.e., this function is not atomic). + #[allow(clippy::too_many_arguments)] + #[instrument(skip_all)] + fn import_execution_payload_envelope( + &self, + signed_envelope: AvailableEnvelope, + block_root: Hash256, + mut state: BeaconState, + payload_verification_status: PayloadVerificationStatus, + parent_block: Arc>, + ) -> Result { + // ----------------------------- ENVELOPE NOT YET ATTESTABLE ---------------------------------- + // Everything in this initial section is on the hot path between processing the envelope and + // being able to attest to it. DO NOT add any extra processing in this initial section + // unless it must run before fork choice. + // ----------------------------------------------------------------------------------------- + let current_slot = self.slot()?; + let current_epoch = current_slot.epoch(T::EthSpec::slots_per_epoch()); + let envelope = signed_envelope.message(); + + // TODO(gloas) implement metrics + // let post_exec_timer = metrics::start_timer(&metrics::BLOCK_PROCESSING_POST_EXEC_PROCESSING); + + // Check the payloads parent block against weak subjectivity checkpoint. + self.check_block_against_weak_subjectivity_checkpoint( + parent_block.message(), + block_root, + &state, + )?; + + // Take an upgradable read lock on fork choice so we can check if this block has already + // been imported. We don't want to repeat work importing a block that is already imported. + let fork_choice_reader = self.canonical_head.fork_choice_upgradable_read_lock(); + if fork_choice_reader.contains_block(&block_root) { + return Err(BlockError::DuplicateFullyImported(block_root)); + } + + // Take an exclusive write-lock on fork choice. It's very important to prevent deadlocks by + // avoiding taking other locks whilst holding this lock. + let mut fork_choice = parking_lot::RwLockUpgradableReadGuard::upgrade(fork_choice_reader); + + // TODO(gloas) Do we need this check? Do not import a block that doesn't descend from the finalized root. + // let signed_block = check_block_is_finalized_checkpoint_or_descendant(self, &fork_choice, signed_block)?; + + // TODO(gloas)Do we want to use an early attester cache like mechanism for payload enevelopes? + // TODO(gloas) emit SSE event if the payload became the new head payload + // drop(post_exec_timer); + + // ---------------------------- BLOCK PROBABLY ATTESTABLE ---------------------------------- + // Most blocks are now capable of being attested to thanks to the `early_attester_cache` + // cache above. Resume non-essential processing. + // + // It is important NOT to return errors here before the database commit, because the block + // has already been added to fork choice and the database would be left in an inconsistent + // state if we returned early without committing. In other words, an error here would + // corrupt the node's database permanently. + // ----------------------------------------------------------------------------------------- + + // Store the envelope and its state, and execute the confirmation batch for the intermediate + // states, which will delete their temporary flags. + // If the write fails, revert fork choice to the version from disk, else we can + // end up with envelopes in fork choice that are missing from disk. + // See https://github.com/sigp/lighthouse/issues/2028 + let (signed_envelope, columns) = signed_envelope.deconstruct(); + + let mut ops = vec![]; + + match self.get_blobs_or_columns_store_op( + block_root, + signed_envelope.slot(), + AvailableBlockData::DataColumns(columns), + ) { + Ok(Some(blobs_or_columns_store_op)) => { + ops.push(blobs_or_columns_store_op); + } + Ok(None) => {} + Err(e) => { + error!( + msg = "Restoring fork choice from disk", + error = &e, + ?block_root, + "Failed to store data columns into the database" + ); + return Err(self + .handle_import_block_db_write_error(fork_choice) + .err() + .unwrap_or(BlockError::InternalError(e))); + } + } + + // TODO(gloas) metrics + // let db_write_timer = metrics::start_timer(&metrics::BLOCK_PROCESSING_DB_WRITE); + + ops.push(StoreOp::PutPayloadEnvelope( + block_root, + signed_envelope.clone(), + )); + ops.push(StoreOp::PutState( + signed_envelope.message.state_root, + &state, + )); + + let db_span = info_span!("persist_payloads_and_blobs").entered(); + + // TODO(gloas) do i need this + if let Err(e) = self.store.do_atomically_with_block_and_blobs_cache(ops) { + error!( + msg = "Restoring fork choice from disk", + error = ?e, + "Database write failed!" + ); + // TODO(gloas) handle db write failure + // return Err(self + // .handle_import_block_db_write_error(fork_choice) + // .err() + // .unwrap_or(e.into())); + } + + drop(db_span); + + // The fork choice write-lock is dropped *after* the on-disk database has been updated. + // This prevents inconsistency between the two at the expense of concurrency. + drop(fork_choice); + + // We're declaring the envelope "imported" at this point, since fork choice and the DB know + // about it. + let envelope_time_imported = timestamp_now(); + + // TODO(gloas) depending on what happens with light clients + // we might need to do some computations here + + // TODO(gloas) metrics + // metrics::stop_timer(db_write_timer); + + // TODO(gloas) metrics + // metrics::inc_counter(&metrics::BLOCK_PROCESSING_SUCCESSES); + + // TODO(gloas) we might want to implement something similar + // to `import_block_update_metrics_and_events` + Ok(block_root) + } +} diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/execution_pending_envelope.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/execution_pending_envelope.rs deleted file mode 100644 index 7dffd1c09c6..00000000000 --- a/beacon_node/beacon_chain/src/payload_envelope_verification/execution_pending_envelope.rs +++ /dev/null @@ -1,28 +0,0 @@ -use task_executor::JoinHandle; -use types::{EthSpec, FullPayload}; - -use crate::{ - BeaconChainTypes, PayloadVerificationOutcome, - payload_envelope_verification::{MaybeAvailableEnvelope, PayloadEnvelopeImportData}, -}; - -/// Used to await the result of executing payload with an EE. -pub type PayloadVerificationHandle = - JoinHandle>>>; - -/// A wrapper around a `SignedBeaconBlock` that indicates that this block is fully verified and -/// ready to import into the `BeaconChain`. The validation includes: -/// -/// - Parent is known -/// - Signatures -/// - State root check -/// - Block processing -/// -/// Note: a `ExecutionPendingEnvelope` is not _forever_ valid to be imported, it may later become invalid -/// due to finality or some other event. A `ExecutionPendingEnvelope` should be imported into the -/// `BeaconChain` immediately after it is instantiated. -pub struct ExecutionPendingEnvelope { - pub block: MaybeAvailableEnvelope, - pub import_data: PayloadEnvelopeImportData, - pub payload_verification_handle: PayloadVerificationHandle, -} diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs index a7ccc056acf..5af6fe39842 100644 --- a/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs @@ -3,14 +3,14 @@ use std::sync::Arc; use educe::Educe; use slot_clock::SlotClock; use state_processing::{VerifySignatures, envelope_processing::process_execution_payload_envelope}; -use tracing::debug; +use tracing::{Span, debug}; use types::{ EthSpec, SignedBeaconBlock, SignedExecutionPayloadEnvelope, consts::gloas::BUILDER_INDEX_SELF_BUILD, }; use crate::{ - BeaconChain, BeaconChainError, BeaconChainTypes, NotifyExecutionLayer, + BeaconChain, BeaconChainError, BeaconChainTypes, BlockError, NotifyExecutionLayer, PayloadVerificationOutcome, payload_envelope_verification::{ EnvelopeError, EnvelopeImportData, EnvelopeProcessingSnapshot, ExecutionPendingEnvelope, @@ -181,7 +181,7 @@ impl IntoExecutionPendingEnvelope for GossipVerifiedEnve self, chain: &Arc>, notify_execution_layer: NotifyExecutionLayer, - ) -> Result, EnvelopeError> { + ) -> Result, BlockError> { let signed_envelope = self.signed_envelope; let envelope = &signed_envelope.message; let payload = &envelope.payload; @@ -255,4 +255,63 @@ impl IntoExecutionPendingEnvelope for GossipVerifiedEnve payload_verification_handle, }) } + + fn envelope(&self) -> &Arc> { + &self.signed_envelope + } +} + +impl BeaconChain { + /// Returns `Ok(GossipVerifiedEnvelope)` if the supplied `envelope` should be forwarded onto the + /// gossip network. The envelope is not imported into the chain, it is just partially verified. + /// + /// The returned `GossipVerifiedEnvelope` should be provided to `Self::process_execution_payload_envelope` immediately + /// after it is returned, unless some other circumstance decides it should not be imported at + /// all. + /// + /// ## Errors + /// + /// Returns an `Err` if the given envelope was invalid, or an error was encountered during + pub async fn verify_envelope_for_gossip( + self: &Arc, + envelope: Arc>, + ) -> Result, EnvelopeError> { + let chain = self.clone(); + let span = Span::current(); + self.task_executor + .clone() + .spawn_blocking_handle( + move || { + let _guard = span.enter(); + let slot = envelope.slot(); + let beacon_block_root = envelope.message.beacon_block_root; + + match GossipVerifiedEnvelope::new(envelope, &chain) { + Ok(verified) => { + debug!( + %slot, + ?beacon_block_root, + "Successfully verified gossip envelope" + ); + + Ok(verified) + } + Err(e) => { + debug!( + error = e.to_string(), + ?beacon_block_root, + %slot, + "Rejected gossip envelope" + ); + + Err(e) + } + } + }, + "gossip_envelope_verification_handle", + ) + .ok_or(BeaconChainError::RuntimeShutdown)? + .await + .map_err(BeaconChainError::TokioJoin)? + } } diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/mod.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/mod.rs index 7ddc4b5c64b..4dd8d351d72 100644 --- a/beacon_node/beacon_chain/src/payload_envelope_verification/mod.rs +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/mod.rs @@ -34,17 +34,16 @@ use state_processing::{ use tracing::instrument; use types::{ BeaconState, BeaconStateError, ChainSpec, DataColumnSidecarList, EthSpec, ExecutionBlockHash, - Hash256, SignedBeaconBlock, SignedExecutionPayloadEnvelope, Slot, + ExecutionPayloadEnvelope, Hash256, SignedBeaconBlock, SignedExecutionPayloadEnvelope, Slot, }; use crate::{ - AvailabilityProcessingStatus, BeaconChain, BeaconChainError, BeaconChainTypes, - ExecutionPayloadError, NotifyExecutionLayer, PayloadVerificationOutcome, - block_verification::PayloadVerificationHandle, block_verification_types::BlockImportData, + BeaconChain, BeaconChainError, BeaconChainTypes, BlockError, ExecutionPayloadError, + NotifyExecutionLayer, PayloadVerificationOutcome, + block_verification::PayloadVerificationHandle, payload_envelope_verification::gossip_verified_envelope::GossipVerifiedEnvelope, }; -pub mod execution_pending_envelope; pub mod gossip_verified_envelope; mod payload_notifier; mod tests; @@ -54,12 +53,14 @@ pub trait IntoExecutionPendingEnvelope: Sized { self, chain: &Arc>, notify_execution_layer: NotifyExecutionLayer, - ) -> Result, EnvelopeError>; + ) -> Result, BlockError>; + + fn envelope(&self) -> &Arc>; } -pub struct ExecutionPendingEnvelope { - pub signed_envelope: MaybeAvailableEnvelope, - pub import_data: EnvelopeImportData, +pub struct ExecutionPendingEnvelope { + pub signed_envelope: MaybeAvailableEnvelope, + pub import_data: EnvelopeImportData, pub payload_verification_handle: PayloadVerificationHandle, } @@ -82,6 +83,25 @@ pub struct AvailableEnvelope { pub spec: Arc, } +impl AvailableEnvelope { + pub fn message(&self) -> &ExecutionPayloadEnvelope { + &self.envelope.message + } + + #[allow(clippy::type_complexity)] + pub fn deconstruct( + self, + ) -> ( + Arc>, + DataColumnSidecarList, + ) { + let AvailableEnvelope { + envelope, columns, .. + } = self; + (envelope, columns) + } +} + pub enum MaybeAvailableEnvelope { Available(AvailableEnvelope), AvailabilityPending { @@ -204,6 +224,12 @@ pub enum EnvelopeError { ExecutionPayloadError(ExecutionPayloadError), } +impl std::fmt::Display for EnvelopeError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:?}", self) + } +} + impl From for EnvelopeError { fn from(e: BeaconChainError) -> Self { EnvelopeError::BeaconChainError(Arc::new(e)) @@ -248,7 +274,7 @@ impl From for EnvelopeError { pub(crate) fn load_snapshot( envelope: &SignedExecutionPayloadEnvelope, chain: &BeaconChain, -) -> Result, EnvelopeError> { +) -> Result, BlockError> { // Reject any block if its block is not known to fork choice. // // A block that is not in fork choice is either: @@ -263,8 +289,8 @@ pub(crate) fn load_snapshot( let fork_choice_read_lock = chain.canonical_head.fork_choice_read_lock(); let beacon_block_root = envelope.beacon_block_root(); let Some(proto_beacon_block) = fork_choice_read_lock.get_block(&beacon_block_root) else { - return Err(EnvelopeError::BlockRootUnknown { - block_root: beacon_block_root, + return Err(BlockError::ParentUnknown { + parent_root: beacon_block_root, }); }; drop(fork_choice_read_lock); @@ -278,7 +304,7 @@ pub(crate) fn load_snapshot( let state = chain .store .get_hot_state(&block_state_root, cache_state) - .map_err(|e| EnvelopeError::BeaconChainError(Arc::new(e.into())))? + .map_err(|e| BlockError::BeaconChainError(Box::new(e.into())))? .ok_or_else(|| { BeaconChainError::DBInconsistent(format!( "Missing state for envelope block {block_state_root:?}", @@ -299,11 +325,15 @@ impl IntoExecutionPendingEnvelope self, chain: &Arc>, notify_execution_layer: NotifyExecutionLayer, - ) -> Result, EnvelopeError> { + ) -> Result, BlockError> { // TODO(EIP-7732): figure out how this should be refactored.. GossipVerifiedEnvelope::new(self, chain)? .into_execution_pending_envelope(chain, notify_execution_layer) } + + fn envelope(&self) -> &Arc> { + self + } } #[derive(Clone, Debug, PartialEq)] diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/payload_notifier.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/payload_notifier.rs index b485af0bfaa..4ac556f1f79 100644 --- a/beacon_node/beacon_chain/src/payload_envelope_verification/payload_notifier.rs +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/payload_notifier.rs @@ -8,7 +8,7 @@ use types::{SignedBeaconBlock, SignedExecutionPayloadEnvelope}; use crate::{ BeaconChain, BeaconChainTypes, BlockError, ExecutionPayloadError, NotifyExecutionLayer, - execution_payload::notify_new_payload, + execution_payload::notify_new_payload, payload_envelope_verification::EnvelopeError, }; /// Used to await the result of executing payload with a remote EE. diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/tests.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/tests.rs index 381a5eaa4f4..2545a900ad3 100644 --- a/beacon_node/beacon_chain/src/payload_envelope_verification/tests.rs +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/tests.rs @@ -1,10 +1,13 @@ use std::sync::Arc; -use crate::{AvailabilityProcessingStatus, BeaconChain, BeaconChainTypes, payload_envelope_verification::{ExecutedEnvelope, ExecutionPendingEnvelope}}; +use crate::{ + AvailabilityProcessingStatus, BeaconChain, BeaconChainTypes, + payload_envelope_verification::{ExecutedEnvelope, ExecutionPendingEnvelope}, +}; async fn import_execution_pending_envelope( chain: Arc>, - execution_pending_envelope: ExecutionPendingEnvelope, + execution_pending_envelope: ExecutionPendingEnvelope, ) -> Result { match chain .clone() diff --git a/beacon_node/beacon_chain/src/validator_monitor.rs b/beacon_node/beacon_chain/src/validator_monitor.rs index fdc7d273205..d8c161f8c13 100644 --- a/beacon_node/beacon_chain/src/validator_monitor.rs +++ b/beacon_node/beacon_chain/src/validator_monitor.rs @@ -30,7 +30,7 @@ use types::{ Attestation, AttestationData, AttesterSlashingRef, BeaconBlockRef, BeaconState, BeaconStateError, ChainSpec, Epoch, EthSpec, Hash256, IndexedAttestation, IndexedAttestationRef, ProposerSlashing, SignedAggregateAndProof, SignedContributionAndProof, - Slot, SyncCommitteeMessage, VoluntaryExit, + SignedExecutionPayloadEnvelope, Slot, SyncCommitteeMessage, VoluntaryExit, }; /// Used for Prometheus labels. diff --git a/beacon_node/network/src/network_beacon_processor/gossip_methods.rs b/beacon_node/network/src/network_beacon_processor/gossip_methods.rs index a4125f3df07..bdcdd688600 100644 --- a/beacon_node/network/src/network_beacon_processor/gossip_methods.rs +++ b/beacon_node/network/src/network_beacon_processor/gossip_methods.rs @@ -4,7 +4,6 @@ use crate::{ service::NetworkMessage, sync::SyncMessage, }; -use beacon_chain::blob_verification::{GossipBlobError, GossipVerifiedBlob}; use beacon_chain::block_verification_types::AsBlock; use beacon_chain::data_column_verification::{GossipDataColumnError, GossipVerifiedDataColumn}; use beacon_chain::store::Error; @@ -19,6 +18,12 @@ use beacon_chain::{ sync_committee_verification::{self, Error as SyncCommitteeError}, validator_monitor::{get_block_delay_ms, get_slot_delay_ms}, }; +use beacon_chain::{ + blob_verification::{GossipBlobError, GossipVerifiedBlob}, + payload_envelope_verification::{ + EnvelopeError, gossip_verified_envelope::GossipVerifiedEnvelope, + }, +}; use beacon_processor::{Work, WorkEvent}; use lighthouse_network::{Client, MessageAcceptance, MessageId, PeerAction, PeerId, ReportSource}; use logging::crit; @@ -3224,20 +3229,20 @@ impl NetworkBeaconProcessor { } } - pub async fn process_gossip_execution_payload( + pub async fn process_gossip_execution_payload_envelope( self: &Arc, message_id: MessageId, peer_id: PeerId, - execution_payload: SignedExecutionPayloadEnvelope, + envelope: SignedExecutionPayloadEnvelope, ) { // TODO(EIP-7732): Implement proper execution payload envelope gossip processing. // This should integrate with the envelope_verification.rs module once it's implemented. trace!( %peer_id, - builder_index = execution_payload.message.builder_index, - slot = %execution_payload.message.slot, - beacon_block_root = %execution_payload.message.beacon_block_root, + builder_index = envelope.message.builder_index, + slot = %envelope.message.slot, + beacon_block_root = %envelope.message.beacon_block_root, "Processing execution payload envelope" ); @@ -3245,6 +3250,134 @@ impl NetworkBeaconProcessor { self.propagate_validation_result(message_id, peer_id, MessageAcceptance::Ignore); } + async fn process_gossip_unverified_execution_payload_envelope( + self: &Arc, + message_id: MessageId, + peer_id: PeerId, + envelope: Arc>, + ) -> Option> { + // TODO(glos) add payload envelope delay metrics + + let verification_result = self + .chain + .clone() + .verify_envelope_for_gossip(envelope.clone()) + .await; + + // TODO(gloas) delay metrics and write the time that the payload was observed into + // the delay cache + + let verified_envelope = match verification_result { + Ok(verified_envelope) => { + info!( + slot = %verified_envelope.signed_envelope.slot(), + root = ?verified_envelope.signed_envelope.beacon_block_root(), + "New envelope received" + ); + + self.propagate_validation_result(message_id, peer_id, MessageAcceptance::Accept); + + verified_envelope + } + // TODO(gloas) penalize peers accordingly + Err(_) => return None, + }; + + // TODO(gloas) do we need to register the payload with monitored validators? + + let envelope_slot = verified_envelope.signed_envelope.slot(); + let beacon_block_root = verified_envelope.signed_envelope.beacon_block_root(); + match self.chain.slot() { + // We only need to do a simple check about the envelope slot vs the current slot beacuse + // `verify_envelope_for_gossip` already ensuresthat the envelope slot is within tolerance + // for envelope imports. + Ok(current_slot) if envelope_slot > current_slot => { + warn!( + ?envelope_slot, + ?beacon_block_root, + msg = "if this happens consistently, check system clock", + "envelope arrived early" + ); + + // TODO(gloas) update metrics to note how early the envelope arrived + + let inner_self = self.clone(); + let process_fn = Box::pin(async move { + inner_self + .process_gossip_verified_execution_payload_envelope( + peer_id, + verified_envelope, + ) + .await; + }); + + // TODO(gloas) send to reprocess queue + None + } + Ok(_) => Some(verified_envelope), + Err(e) => { + error!( + error = ?e, + %envelope_slot, + ?beacon_block_root, + location = "envelope gossip", + "Failed to defer envelope import" + ); + None + } + } + } + + async fn process_gossip_verified_execution_payload_envelope( + self: Arc, + peer_id: PeerId, + verified_envelope: GossipVerifiedEnvelope, + ) { + let processing_start_time = Instant::now(); + let envelope = verified_envelope.envelope_cloned(); + let beacon_block_root = verified_envelope.signed_envelope.beacon_block_root(); + + let result = self + .chain + .process_execution_payload_envelope( + block_root, + verified_envelope, + notify_execution_layer, + block_source, + publish_fn, + ) + .await; + + register_process_result_metrics(&result, metrics::BlockSource::Gossip, "envelope"); + + match &result { + Ok(AvailabilityProcessingStatus::Imported(block_root)) => { + // TODO(gloas) do we need to send a `PayloadImported` event to the reporcess queue? + + debug!( + ?block_root, + %peer_id, + "Gossipsub envelope processed" + ); + + // TODO(gloas) do we need to recompute head? + // should canonical_head return the block and the payload now? + self.chain.recompute_head_at_current_slot().await; + + // TODO(gloas) metrics + } + Ok(AvailabilityProcessingStatus::MissingComponents(slot, block_root)) => { + trace!( + %slot, + %block_root, + "Processed envelope, waiting for other components" + ) + } + + Err(_) => todo!(), + } + } + pub fn process_gossip_execution_payload_bid( self: &Arc, message_id: MessageId, From 47782a68c31c4b4c10a597562d6a94032546e9d8 Mon Sep 17 00:00:00 2001 From: Eitan Seri- Levi Date: Thu, 12 Feb 2026 21:27:39 -0800 Subject: [PATCH 6/9] delay cache, and remove some todos --- beacon_node/beacon_chain/src/beacon_chain.rs | 16 +- beacon_node/beacon_chain/src/builder.rs | 1 + .../beacon_chain/src/envelope_times_cache.rs | 197 ++++++++++++++++++ beacon_node/beacon_chain/src/lib.rs | 2 +- beacon_node/beacon_chain/src/metrics.rs | 38 ++++ .../gossip_verified_envelope.rs | 17 +- .../import.rs} | 172 ++++++++------- .../src/payload_envelope_verification/mod.rs | 58 +++--- .../payload_notifier.rs | 8 +- .../payload_envelope_verification/tests.rs | 23 -- .../beacon_chain/src/validator_monitor.rs | 2 +- .../beacon_chain/tests/block_verification.rs | 2 +- beacon_node/network/src/metrics.rs | 10 + .../gossip_methods.rs | 82 +++++--- .../src/network_beacon_processor/mod.rs | 8 +- beacon_node/network/src/router.rs | 1 + 16 files changed, 459 insertions(+), 178 deletions(-) create mode 100644 beacon_node/beacon_chain/src/envelope_times_cache.rs rename beacon_node/beacon_chain/src/{payload_envelope_import/mod.rs => payload_envelope_verification/import.rs} (68%) delete mode 100644 beacon_node/beacon_chain/src/payload_envelope_verification/tests.rs diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 59dd0fd1fe4..4af147addb7 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -27,6 +27,7 @@ use crate::data_availability_checker::{ }; use crate::data_column_verification::{GossipDataColumnError, GossipVerifiedDataColumn}; use crate::early_attester_cache::EarlyAttesterCache; +use crate::envelope_times_cache::EnvelopeTimesCache; use crate::errors::{BeaconChainError as Error, BlockProductionError}; use crate::events::ServerSentEventHandler; use crate::execution_payload::{NotifyExecutionLayer, PreparePayloadHandle, get_execution_payload}; @@ -56,7 +57,9 @@ use crate::observed_block_producers::ObservedBlockProducers; use crate::observed_data_sidecars::ObservedDataSidecars; use crate::observed_operations::{ObservationOutcome, ObservedOperations}; use crate::observed_slashable::ObservedSlashable; -use crate::payload_envelope_verification::{ExecutedEnvelope, ExecutionPendingEnvelope}; +use crate::payload_envelope_verification::{ + EnvelopeError, ExecutedEnvelope, ExecutionPendingEnvelope, +}; use crate::persisted_beacon_chain::PersistedBeaconChain; use crate::persisted_custody::persist_custody_context; use crate::persisted_fork_choice::PersistedForkChoice; @@ -460,6 +463,8 @@ pub struct BeaconChain { pub early_attester_cache: EarlyAttesterCache, /// A cache used to keep track of various block timings. pub block_times_cache: Arc>, + /// A cache used to keep track of various envelope timings. + pub envelope_times_cache: Arc>, /// A cache used to track pre-finalization block roots for quick rejection. pub pre_finalization_block_cache: PreFinalizationBlockCache, /// A cache used to produce light_client server messages @@ -1158,8 +1163,8 @@ impl BeaconChain { match self.store.try_get_full_block(block_root)? { Some(DatabaseBlock::Full(block)) => Ok(Some(block)), Some(DatabaseBlock::Blinded(_)) => { - // TODO(gloas) this should error out - todo!() + // TODO(gloas) should we return None here? + Ok(None) } None => Ok(None), } @@ -3556,7 +3561,7 @@ impl BeaconChain { pub async fn into_executed_payload_envelope( self: Arc, pending_envelope: ExecutionPendingEnvelope, - ) -> Result, BlockError> { + ) -> Result, EnvelopeError> { let ExecutionPendingEnvelope { signed_envelope, import_data, @@ -4168,7 +4173,7 @@ impl BeaconChain { Ok(block_root) } - pub(crate) fn handle_import_block_db_write_error( + fn handle_import_block_db_write_error( &self, // We don't actually need this value, however it's always present when we call this function // and it needs to be dropped to prevent a dead-lock. Requiring it to be passed here is @@ -6698,6 +6703,7 @@ impl BeaconChain { // sync anyway). self.naive_aggregation_pool.write().prune(slot); self.block_times_cache.write().prune(slot); + self.envelope_times_cache.write().prune(slot); // Don't run heavy-weight tasks during sync. if self.best_slot() + MAX_PER_SLOT_FORK_CHOICE_DISTANCE < slot { diff --git a/beacon_node/beacon_chain/src/builder.rs b/beacon_node/beacon_chain/src/builder.rs index f673519f5fb..3a912016dc3 100644 --- a/beacon_node/beacon_chain/src/builder.rs +++ b/beacon_node/beacon_chain/src/builder.rs @@ -1055,6 +1055,7 @@ where )), beacon_proposer_cache, block_times_cache: <_>::default(), + envelope_times_cache: <_>::default(), pre_finalization_block_cache: <_>::default(), validator_pubkey_cache: RwLock::new(validator_pubkey_cache), early_attester_cache: <_>::default(), diff --git a/beacon_node/beacon_chain/src/envelope_times_cache.rs b/beacon_node/beacon_chain/src/envelope_times_cache.rs new file mode 100644 index 00000000000..84c936c210b --- /dev/null +++ b/beacon_node/beacon_chain/src/envelope_times_cache.rs @@ -0,0 +1,197 @@ +//! This module provides the `EnvelopeTimesCache` which contains information regarding payload +//! envelope timings. +//! +//! This provides `BeaconChain` and associated functions with access to the timestamps of when a +//! payload envelope was observed, verified, executed, and imported. +//! This allows for better traceability and allows us to determine the root cause for why an +//! envelope was imported late. +//! This allows us to distinguish between the following scenarios: +//! - The envelope was observed late. +//! - Consensus verification was slow. +//! - Execution verification was slow. +//! - The DB write was slow. + +use eth2::types::{Hash256, Slot}; +use std::collections::HashMap; +use std::time::Duration; + +type BlockRoot = Hash256; + +#[derive(Clone, Default)] +pub struct EnvelopeTimestamps { + /// When the envelope was first observed (gossip or RPC). + pub observed: Option, + /// When consensus verification (state transition) completed. + pub consensus_verified: Option, + /// When execution layer verification started. + pub started_execution: Option, + /// When execution layer verification completed. + pub executed: Option, + /// When the envelope was imported into the DB. + pub imported: Option, +} + +/// Delay data for envelope processing, computed relative to the slot start time. +#[derive(Debug, Default)] +pub struct EnvelopeDelays { + /// Time after start of slot we saw the envelope. + pub observed: Option, + /// The time it took to complete consensus verification of the envelope. + pub consensus_verification_time: Option, + /// The time it took to complete execution verification of the envelope. + pub execution_time: Option, + /// Time after execution until the envelope was imported. + pub imported: Option, +} + +impl EnvelopeDelays { + fn new(times: EnvelopeTimestamps, slot_start_time: Duration) -> EnvelopeDelays { + let observed = times + .observed + .and_then(|observed_time| observed_time.checked_sub(slot_start_time)); + let consensus_verification_time = times + .consensus_verified + .and_then(|consensus_verified| consensus_verified.checked_sub(times.observed?)); + let execution_time = times + .executed + .and_then(|executed| executed.checked_sub(times.started_execution?)); + let imported = times + .imported + .and_then(|imported_time| imported_time.checked_sub(times.executed?)); + EnvelopeDelays { + observed, + consensus_verification_time, + execution_time, + imported, + } + } +} + +pub struct EnvelopeTimesCacheValue { + pub slot: Slot, + pub timestamps: EnvelopeTimestamps, + pub peer_id: Option, +} + +impl EnvelopeTimesCacheValue { + fn new(slot: Slot) -> Self { + EnvelopeTimesCacheValue { + slot, + timestamps: Default::default(), + peer_id: None, + } + } +} + +#[derive(Default)] +pub struct EnvelopeTimesCache { + pub cache: HashMap, +} + +impl EnvelopeTimesCache { + /// Set the observation time for `block_root` to `timestamp` if `timestamp` is less than + /// any previous timestamp at which this envelope was observed. + pub fn set_time_observed( + &mut self, + block_root: BlockRoot, + slot: Slot, + timestamp: Duration, + peer_id: Option, + ) { + let entry = self + .cache + .entry(block_root) + .or_insert_with(|| EnvelopeTimesCacheValue::new(slot)); + match entry.timestamps.observed { + Some(existing) if existing <= timestamp => { + // Existing timestamp is earlier, do nothing. + } + _ => { + entry.timestamps.observed = Some(timestamp); + entry.peer_id = peer_id; + } + } + } + + /// Set the timestamp for `field` if that timestamp is less than any previously known value. + fn set_time_if_less( + &mut self, + block_root: BlockRoot, + slot: Slot, + field: impl Fn(&mut EnvelopeTimestamps) -> &mut Option, + timestamp: Duration, + ) { + let entry = self + .cache + .entry(block_root) + .or_insert_with(|| EnvelopeTimesCacheValue::new(slot)); + let existing_timestamp = field(&mut entry.timestamps); + if existing_timestamp.is_none_or(|prev| timestamp < prev) { + *existing_timestamp = Some(timestamp); + } + } + + pub fn set_time_consensus_verified( + &mut self, + block_root: BlockRoot, + slot: Slot, + timestamp: Duration, + ) { + self.set_time_if_less( + block_root, + slot, + |timestamps| &mut timestamps.consensus_verified, + timestamp, + ) + } + + pub fn set_time_started_execution( + &mut self, + block_root: BlockRoot, + slot: Slot, + timestamp: Duration, + ) { + self.set_time_if_less( + block_root, + slot, + |timestamps| &mut timestamps.started_execution, + timestamp, + ) + } + + pub fn set_time_executed(&mut self, block_root: BlockRoot, slot: Slot, timestamp: Duration) { + self.set_time_if_less( + block_root, + slot, + |timestamps| &mut timestamps.executed, + timestamp, + ) + } + + pub fn set_time_imported(&mut self, block_root: BlockRoot, slot: Slot, timestamp: Duration) { + self.set_time_if_less( + block_root, + slot, + |timestamps| &mut timestamps.imported, + timestamp, + ) + } + + pub fn get_envelope_delays( + &self, + block_root: BlockRoot, + slot_start_time: Duration, + ) -> EnvelopeDelays { + if let Some(entry) = self.cache.get(&block_root) { + EnvelopeDelays::new(entry.timestamps.clone(), slot_start_time) + } else { + EnvelopeDelays::default() + } + } + + /// Prune the cache to only store the most recent 2 epochs. + pub fn prune(&mut self, current_slot: Slot) { + self.cache + .retain(|_, entry| entry.slot > current_slot.saturating_sub(64_u64)); + } +} diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index f0151eed01a..5acc3edadd5 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -20,6 +20,7 @@ pub mod custody_context; pub mod data_availability_checker; pub mod data_column_verification; mod early_attester_cache; +pub mod envelope_times_cache; mod errors; pub mod events; pub mod execution_payload; @@ -42,7 +43,6 @@ pub mod observed_block_producers; pub mod observed_data_sidecars; pub mod observed_operations; mod observed_slashable; -pub mod payload_envelope_import; pub mod payload_envelope_verification; pub mod persisted_beacon_chain; pub mod persisted_custody; diff --git a/beacon_node/beacon_chain/src/metrics.rs b/beacon_node/beacon_chain/src/metrics.rs index 9de67ca93fa..775f5a3df02 100644 --- a/beacon_node/beacon_chain/src/metrics.rs +++ b/beacon_node/beacon_chain/src/metrics.rs @@ -21,6 +21,44 @@ pub const VALIDATOR_MONITOR_ATTESTATION_SIMULATOR_SOURCE_ATTESTER_HIT_TOTAL: &st pub const VALIDATOR_MONITOR_ATTESTATION_SIMULATOR_SOURCE_ATTESTER_MISS_TOTAL: &str = "validator_monitor_attestation_simulator_source_attester_miss_total"; +/* +* Execution Payload Envelope Procsesing +*/ + +pub static ENVELOPE_PROCESSING_REQUESTS: LazyLock> = LazyLock::new(|| { + try_create_int_counter( + "payload_envelope_processing_requests_total", + "Count of payload envelopes submitted for processing", + ) +}); +pub static ENVELOPE_PROCESSING_SUCCESSES: LazyLock> = LazyLock::new(|| { + try_create_int_counter( + "payload_envelope_processing_successes_total", + "Count of payload envelopes processed without error", + ) +}); +pub static ENVELOPE_PROCESSING_TIMES: LazyLock> = LazyLock::new(|| { + try_create_histogram( + "payload_envelope_processing_seconds", + "Full runtime of payload envelope processing", + ) +}); +pub static ENVELOPE_PROCESSING_DB_WRITE: LazyLock> = LazyLock::new(|| { + try_create_histogram( + "payload_envelope_processing_db_write_seconds", + "Time spent writing a newly processed payload envelope and state to DB", + ) +}); +pub static ENVELOPE_PROCESSING_POST_EXEC_PROCESSING: LazyLock> = + LazyLock::new(|| { + try_create_histogram_with_buckets( + "payload_envelope_processing_post_exec_pre_attestable_seconds", + "Time between finishing execution processing and the payload envelope + becoming attestable", + linear_buckets(0.01, 0.01, 15), + ) + }); + /* * Block Processing */ diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs index 5af6fe39842..c9bef630aab 100644 --- a/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs @@ -10,7 +10,7 @@ use types::{ }; use crate::{ - BeaconChain, BeaconChainError, BeaconChainTypes, BlockError, NotifyExecutionLayer, + BeaconChain, BeaconChainError, BeaconChainTypes, NotifyExecutionLayer, PayloadVerificationOutcome, payload_envelope_verification::{ EnvelopeError, EnvelopeImportData, EnvelopeProcessingSnapshot, ExecutionPendingEnvelope, @@ -151,8 +151,7 @@ impl GossipVerifiedEnvelope { ); (is_valid, opt_snapshot) } else { - // TODO(gloas) we should probably introduce a builder cache or some type of - // global cache. + // TODO(gloas) if we implement a builder pubkey cache, we'll need to use it here. // External builder: must load the state to get the builder pubkey. let snapshot = load_snapshot(signed_envelope.as_ref(), chain)?; let is_valid = @@ -181,7 +180,7 @@ impl IntoExecutionPendingEnvelope for GossipVerifiedEnve self, chain: &Arc>, notify_execution_layer: NotifyExecutionLayer, - ) -> Result, BlockError> { + ) -> Result, EnvelopeError> { let signed_envelope = self.signed_envelope; let envelope = &signed_envelope.message; let payload = &envelope.payload; @@ -198,13 +197,11 @@ impl IntoExecutionPendingEnvelope for GossipVerifiedEnve let payload_verification_future = async move { let chain = payload_notifier.chain.clone(); - // TODO:(gloas): timing metrics if let Some(started_execution) = chain.slot_clock.now_duration() { - chain.block_times_cache.write().set_time_started_execution( - block_root, - slot, - started_execution, - ); + chain + .envelope_times_cache + .write() + .set_time_started_execution(block_root, slot, started_execution); } let payload_verification_status = payload_notifier.notify_new_payload().await?; diff --git a/beacon_node/beacon_chain/src/payload_envelope_import/mod.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/import.rs similarity index 68% rename from beacon_node/beacon_chain/src/payload_envelope_import/mod.rs rename to beacon_node/beacon_chain/src/payload_envelope_verification/import.rs index ab7de5c15da..36287127210 100644 --- a/beacon_node/beacon_chain/src/payload_envelope_import/mod.rs +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/import.rs @@ -1,20 +1,23 @@ use std::sync::Arc; +use std::time::Duration; use fork_choice::PayloadVerificationStatus; use logging::crit; +use slot_clock::SlotClock; use store::StoreOp; use tracing::{debug, error, info_span, instrument}; -use types::{BeaconState, BlockImportSource, EthSpec, Hash256, SignedBeaconBlock}; +use types::{BeaconState, BlockImportSource, Hash256, SignedBeaconBlock, Slot}; +use super::{ + AvailableEnvelope, AvailableExecutedEnvelope, EnvelopeError, EnvelopeImportData, + ExecutedEnvelope, IntoExecutionPendingEnvelope, +}; use crate::{ - AvailabilityProcessingStatus, BeaconChain, BeaconChainError, BeaconChainTypes, BlockError, + AvailabilityProcessingStatus, BeaconChain, BeaconChainError, BeaconChainTypes, NotifyExecutionLayer, block_verification_types::{AsBlock, AvailableBlockData}, - payload_envelope_verification::{ - AvailableEnvelope, AvailableExecutedEnvelope, EnvelopeImportData, ExecutedEnvelope, - IntoExecutionPendingEnvelope, - }, - validator_monitor::timestamp_now, + metrics, + validator_monitor::{get_slot_delay_ms, timestamp_now}, }; impl BeaconChain { @@ -36,21 +39,26 @@ impl BeaconChain { unverified_envelope: P, notify_execution_layer: NotifyExecutionLayer, block_source: BlockImportSource, - publish_fn: impl FnOnce() -> Result<(), BlockError>, - ) -> Result { + publish_fn: impl FnOnce() -> Result<(), EnvelopeError>, + ) -> Result { let block_slot = unverified_envelope.envelope().slot(); - // TODO(gloas) Set observed time if not already set. Usually this should be set by gossip or RPC, + // Set observed time if not already set. Usually this should be set by gossip or RPC, // but just in case we set it again here (useful for tests). + if let Some(seen_timestamp) = self.slot_clock.now_duration() { + self.envelope_times_cache.write().set_time_observed( + block_root, + block_slot, + seen_timestamp, + None, + ); + } - // TODO(gloas) if we decide to insert the payload envelope into the new DA checker - // we should insert the pre executed envelope here. + // TODO(gloas) insert the pre-executed envelope into some type of cache. - // TODO(gloas) Start the Prometheus timer. - // let _full_timer = metrics::start_timer(&metrics::BLOCK_PROCESSING_TIMES); + let _full_timer = metrics::start_timer(&metrics::ENVELOPE_PROCESSING_TIMES); - // TODO(gloas) Increment the Prometheus counter for envelope processing requests. - // metrics::inc_counter(&metrics::BLOCK_PROCESSING_REQUESTS); + metrics::inc_counter(&metrics::ENVELOPE_PROCESSING_REQUESTS); // A small closure to group the verification and import errors. let chain = self.clone(); @@ -59,12 +67,16 @@ impl BeaconChain { .into_execution_pending_envelope(&chain, notify_execution_layer)?; publish_fn()?; - // TODO(gloas) Record the time it took to complete consensus verification. - // if let Some(timestamp) = self.slot_clock.now_duration() { - // self.block_times_cache - // .write() - // .set_time_consensus_verified(block_root, block_slot, timestamp) - // } + // Record the time it took to complete consensus verification. + if let Some(timestamp) = chain.slot_clock.now_duration() { + chain + .envelope_times_cache + .write() + .set_time_consensus_verified(block_root, block_slot, timestamp); + } + + let envelope_times_cache = chain.envelope_times_cache.clone(); + let slot_clock = chain.slot_clock.clone(); let executed_envelope = chain .into_executed_payload_envelope(execution_pending) @@ -75,28 +87,23 @@ impl BeaconChain { // reprocess this block until the block is evicted from DA checker, causing the // chain to get stuck temporarily if the block is canonical. Therefore we remove // it from the cache if execution fails. - - //self.data_availability_checker - // .remove_block_on_execution_error(&block_root); })?; - // TODO(gloas) Record the *additional* time it took to wait for execution layer verification. - // if let Some(timestamp) = self.slot_clock.now_duration() { - // self.block_times_cache - // .write() - // .set_time_executed(block_root, block_slot, timestamp) - // } + // Record the time it took to wait for execution layer verification. + if let Some(timestamp) = slot_clock.now_duration() { + envelope_times_cache + .write() + .set_time_executed(block_root, block_slot, timestamp); + } match executed_envelope { ExecutedEnvelope::Available(envelope) => { self.import_available_execution_payload_envelope(Box::new(envelope)) .await } - ExecutedEnvelope::AvailabilityPending() => { - return Err(BlockError::InternalError( - "Pending payload envelope not yet implemented".to_owned(), - )); - } + ExecutedEnvelope::AvailabilityPending() => Err(EnvelopeError::InternalError( + "Pending payload envelope not yet implemented".to_owned(), + )), } }; @@ -111,8 +118,7 @@ impl BeaconChain { "Envelope imported" ); - // TODO(gloas) Increment the Prometheus counter for block processing successes. - // metrics::inc_counter(&metrics::BLOCK_PROCESSING_SUCCESSES); + metrics::inc_counter(&metrics::ENVELOPE_PROCESSING_SUCCESSES); Ok(status) } @@ -121,7 +127,7 @@ impl BeaconChain { Ok(status) } - Err(BlockError::BeaconChainError(e)) => { + Err(EnvelopeError::BeaconChainError(e)) => { match e.as_ref() { BeaconChainError::TokioJoin(e) => { debug!( @@ -138,7 +144,7 @@ impl BeaconChain { ); } }; - Err(BlockError::BeaconChainError(e)) + Err(EnvelopeError::BeaconChainError(e)) } // The block failed verification. Err(other) => { @@ -152,7 +158,7 @@ impl BeaconChain { pub async fn import_available_execution_payload_envelope( self: &Arc, envelope: Box>, - ) -> Result { + ) -> Result { let AvailableExecutedEnvelope { envelope, import_data, @@ -165,8 +171,6 @@ impl BeaconChain { post_state, } = import_data; - // TODO(gloas) Record the time at which this block's blobs became available. - let block_root = { // Capture the current span before moving into the blocking task let current_span = tracing::Span::current(); @@ -202,21 +206,18 @@ impl BeaconChain { &self, signed_envelope: AvailableEnvelope, block_root: Hash256, - mut state: BeaconState, - payload_verification_status: PayloadVerificationStatus, + state: BeaconState, + _payload_verification_status: PayloadVerificationStatus, parent_block: Arc>, - ) -> Result { + ) -> Result { // ----------------------------- ENVELOPE NOT YET ATTESTABLE ---------------------------------- // Everything in this initial section is on the hot path between processing the envelope and // being able to attest to it. DO NOT add any extra processing in this initial section // unless it must run before fork choice. // ----------------------------------------------------------------------------------------- - let current_slot = self.slot()?; - let current_epoch = current_slot.epoch(T::EthSpec::slots_per_epoch()); - let envelope = signed_envelope.message(); - // TODO(gloas) implement metrics - // let post_exec_timer = metrics::start_timer(&metrics::BLOCK_PROCESSING_POST_EXEC_PROCESSING); + let post_exec_timer = + metrics::start_timer(&metrics::ENVELOPE_PROCESSING_POST_EXEC_PROCESSING); // Check the payloads parent block against weak subjectivity checkpoint. self.check_block_against_weak_subjectivity_checkpoint( @@ -228,26 +229,24 @@ impl BeaconChain { // Take an upgradable read lock on fork choice so we can check if this block has already // been imported. We don't want to repeat work importing a block that is already imported. let fork_choice_reader = self.canonical_head.fork_choice_upgradable_read_lock(); - if fork_choice_reader.contains_block(&block_root) { - return Err(BlockError::DuplicateFullyImported(block_root)); + if !fork_choice_reader.contains_block(&block_root) { + return Err(EnvelopeError::BlockRootUnknown { block_root }); } + // TODO(gloas) no fork choice logic yet // Take an exclusive write-lock on fork choice. It's very important to prevent deadlocks by // avoiding taking other locks whilst holding this lock. - let mut fork_choice = parking_lot::RwLockUpgradableReadGuard::upgrade(fork_choice_reader); + // let fork_choice = parking_lot::RwLockUpgradableReadGuard::upgrade(fork_choice_reader); // TODO(gloas) Do we need this check? Do not import a block that doesn't descend from the finalized root. // let signed_block = check_block_is_finalized_checkpoint_or_descendant(self, &fork_choice, signed_block)?; - // TODO(gloas)Do we want to use an early attester cache like mechanism for payload enevelopes? + // TODO(gloas) Do we want to use an early attester cache like mechanism for payload enevelopes? // TODO(gloas) emit SSE event if the payload became the new head payload - // drop(post_exec_timer); + drop(post_exec_timer); - // ---------------------------- BLOCK PROBABLY ATTESTABLE ---------------------------------- - // Most blocks are now capable of being attested to thanks to the `early_attester_cache` - // cache above. Resume non-essential processing. - // - // It is important NOT to return errors here before the database commit, because the block + // ---------------------------- ENVELOPE PROBABLY ATTESTABLE ---------------------------------- + // It is important NOT to return errors here before the database commit, because the envelope // has already been added to fork choice and the database would be left in an inconsistent // state if we returned early without committing. In other words, an error here would // corrupt the node's database permanently. @@ -278,15 +277,13 @@ impl BeaconChain { ?block_root, "Failed to store data columns into the database" ); - return Err(self - .handle_import_block_db_write_error(fork_choice) - .err() - .unwrap_or(BlockError::InternalError(e))); + // TODO(gloas) implement failed write handling to fork choice + // let _ = self.handle_import_block_db_write_error(fork_choice); + return Err(EnvelopeError::InternalError(e)); } } - // TODO(gloas) metrics - // let db_write_timer = metrics::start_timer(&metrics::BLOCK_PROCESSING_DB_WRITE); + let db_write_timer = metrics::start_timer(&metrics::ENVELOPE_PROCESSING_DB_WRITE); ops.push(StoreOp::PutPayloadEnvelope( block_root, @@ -299,7 +296,6 @@ impl BeaconChain { let db_span = info_span!("persist_payloads_and_blobs").entered(); - // TODO(gloas) do i need this if let Err(e) = self.store.do_atomically_with_block_and_blobs_cache(ops) { error!( msg = "Restoring fork choice from disk", @@ -315,25 +311,49 @@ impl BeaconChain { drop(db_span); + // TODO(gloas) drop fork choice lock // The fork choice write-lock is dropped *after* the on-disk database has been updated. // This prevents inconsistency between the two at the expense of concurrency. - drop(fork_choice); + // drop(fork_choice); // We're declaring the envelope "imported" at this point, since fork choice and the DB know // about it. let envelope_time_imported = timestamp_now(); // TODO(gloas) depending on what happens with light clients - // we might need to do some computations here + // we might need to do some light client related computations here - // TODO(gloas) metrics - // metrics::stop_timer(db_write_timer); + metrics::stop_timer(db_write_timer); + metrics::inc_counter(&metrics::ENVELOPE_PROCESSING_SUCCESSES); - // TODO(gloas) metrics - // metrics::inc_counter(&metrics::BLOCK_PROCESSING_SUCCESSES); + self.import_envelope_update_metrics_and_events( + block_root, + signed_envelope.slot(), + envelope_time_imported, + ); - // TODO(gloas) we might want to implement something similar - // to `import_block_update_metrics_and_events` Ok(block_root) } + + fn import_envelope_update_metrics_and_events( + &self, + block_root: Hash256, + envelope_slot: Slot, + envelope_time_imported: Duration, + ) { + let envelope_delay_total = + get_slot_delay_ms(envelope_time_imported, envelope_slot, &self.slot_clock); + + // Do not write to the cache for envelopes older than 2 epochs, this helps reduce writes + // to the cache during sync. + if envelope_delay_total < self.slot_clock.slot_duration().saturating_mul(64) { + self.envelope_times_cache.write().set_time_imported( + block_root, + envelope_slot, + envelope_time_imported, + ); + } + + // TODO(gloas) emit SSE event for envelope import (similar to SseBlock for blocks). + } } diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/mod.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/mod.rs index 4dd8d351d72..80e62f93b71 100644 --- a/beacon_node/beacon_chain/src/payload_envelope_verification/mod.rs +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/mod.rs @@ -1,8 +1,9 @@ //! The incremental processing steps (e.g., signatures verified but not the state transition) is //! represented as a sequence of wrapper-types around the block. There is a linear progression of -//! types, starting at a `SignedBeaconBlock` and finishing with a `Fully VerifiedBlock` (see +//! types, starting at a `SignedExecutionPayloadEnvelope` and finishing with an `AvailableExecutedEnvelope` (see //! diagram below). //! +//! // TODO(gloas) we might want to update this diagram to include `AvailabelExecutedEnvelope` //! ```ignore //! START //! | @@ -28,9 +29,9 @@ use std::sync::Arc; -use state_processing::{ - BlockProcessingError, ConsensusContext, envelope_processing::EnvelopeProcessingError, -}; +use store::Error as DBError; + +use state_processing::{BlockProcessingError, envelope_processing::EnvelopeProcessingError}; use tracing::instrument; use types::{ BeaconState, BeaconStateError, ChainSpec, DataColumnSidecarList, EthSpec, ExecutionBlockHash, @@ -45,15 +46,15 @@ use crate::{ }; pub mod gossip_verified_envelope; +pub mod import; mod payload_notifier; -mod tests; pub trait IntoExecutionPendingEnvelope: Sized { fn into_execution_pending_envelope( self, chain: &Arc>, notify_execution_layer: NotifyExecutionLayer, - ) -> Result, BlockError>; + ) -> Result, EnvelopeError>; fn envelope(&self) -> &Arc>; } @@ -74,8 +75,7 @@ pub struct EnvelopeImportData { #[derive(Debug)] #[allow(dead_code)] pub struct AvailableEnvelope { - // TODO(EIP-7732): rename to execution_block_hash - block_hash: ExecutionBlockHash, + execution_block_hash: ExecutionBlockHash, envelope: Arc>, columns: DataColumnSidecarList, /// Timestamp at which this block first became available (UNIX timestamp, time since 1970). @@ -222,6 +222,10 @@ pub enum EnvelopeError { EnvelopeProcessingError(EnvelopeProcessingError), // Error verifying the execution payload ExecutionPayloadError(ExecutionPayloadError), + // An error from block-level checks reused during envelope import + BlockError(BlockError), + // Internal error + InternalError(String), } impl std::fmt::Display for EnvelopeError { @@ -248,6 +252,18 @@ impl From for EnvelopeError { } } +impl From for EnvelopeError { + fn from(e: DBError) -> Self { + EnvelopeError::BeaconChainError(Arc::new(BeaconChainError::DBError(e))) + } +} + +impl From for EnvelopeError { + fn from(e: BlockError) -> Self { + EnvelopeError::BlockError(e) + } +} + /// Pull errors up from EnvelopeProcessingError to EnvelopeError impl From for EnvelopeError { fn from(e: EnvelopeProcessingError) -> Self { @@ -274,14 +290,14 @@ impl From for EnvelopeError { pub(crate) fn load_snapshot( envelope: &SignedExecutionPayloadEnvelope, chain: &BeaconChain, -) -> Result, BlockError> { - // Reject any block if its block is not known to fork choice. +) -> Result, EnvelopeError> { + // Reject any envelope if its block is not known to fork choice. // // A block that is not in fork choice is either: // - // - Not yet imported: we should reject this block because we should only import a child - // envelope after its parent has been fully imported. - // - Pre-finalized: if the block is _prior_ to finalization, we should ignore the envelope + // - Not yet imported: we should reject this envelope because we should only import it after its parent block + // has been fully imported. + // - Pre-finalized: if the parent block is _prior_ to finalization, we should ignore the envelope // because it will revert finalization. Note that the finalized block is stored in fork // choice, so we will not reject any child of the finalized block (this is relevant during // genesis). @@ -289,8 +305,8 @@ pub(crate) fn load_snapshot( let fork_choice_read_lock = chain.canonical_head.fork_choice_read_lock(); let beacon_block_root = envelope.beacon_block_root(); let Some(proto_beacon_block) = fork_choice_read_lock.get_block(&beacon_block_root) else { - return Err(BlockError::ParentUnknown { - parent_root: beacon_block_root, + return Err(EnvelopeError::BlockRootUnknown { + block_root: beacon_block_root, }); }; drop(fork_choice_read_lock); @@ -304,7 +320,7 @@ pub(crate) fn load_snapshot( let state = chain .store .get_hot_state(&block_state_root, cache_state) - .map_err(|e| BlockError::BeaconChainError(Box::new(e.into())))? + .map_err(EnvelopeError::from)? .ok_or_else(|| { BeaconChainError::DBInconsistent(format!( "Missing state for envelope block {block_state_root:?}", @@ -325,8 +341,7 @@ impl IntoExecutionPendingEnvelope self, chain: &Arc>, notify_execution_layer: NotifyExecutionLayer, - ) -> Result, BlockError> { - // TODO(EIP-7732): figure out how this should be refactored.. + ) -> Result, EnvelopeError> { GossipVerifiedEnvelope::new(self, chain)? .into_execution_pending_envelope(chain, notify_execution_layer) } @@ -335,10 +350,3 @@ impl IntoExecutionPendingEnvelope self } } - -#[derive(Clone, Debug, PartialEq)] -pub struct PayloadEnvelopeImportData { - pub block_root: Hash256, - pub state: BeaconState, - pub consensus_context: ConsensusContext, -} diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/payload_notifier.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/payload_notifier.rs index 4ac556f1f79..5b1f332b5ad 100644 --- a/beacon_node/beacon_chain/src/payload_envelope_verification/payload_notifier.rs +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/payload_notifier.rs @@ -7,7 +7,7 @@ use tracing::warn; use types::{SignedBeaconBlock, SignedExecutionPayloadEnvelope}; use crate::{ - BeaconChain, BeaconChainTypes, BlockError, ExecutionPayloadError, NotifyExecutionLayer, + BeaconChain, BeaconChainTypes, BlockError, NotifyExecutionLayer, execution_payload::notify_new_payload, payload_envelope_verification::EnvelopeError, }; @@ -25,15 +25,13 @@ impl PayloadNotifier { envelope: Arc>, block: Arc>, notify_execution_layer: NotifyExecutionLayer, - ) -> Result { + ) -> Result { let payload_verification_status = { let payload_message = &envelope.message; match notify_execution_layer { NotifyExecutionLayer::No if chain.config.optimistic_finalized_sync => { - // TODO(gloas) unwrap - let new_payload_request = - Self::build_new_payload_request(&envelope, &block).unwrap(); + let new_payload_request = Self::build_new_payload_request(&envelope, &block)?; if let Err(e) = new_payload_request.perform_optimistic_sync_verifications() { warn!( block_number = ?payload_message.payload.block_number, diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/tests.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/tests.rs deleted file mode 100644 index 2545a900ad3..00000000000 --- a/beacon_node/beacon_chain/src/payload_envelope_verification/tests.rs +++ /dev/null @@ -1,23 +0,0 @@ -use std::sync::Arc; - -use crate::{ - AvailabilityProcessingStatus, BeaconChain, BeaconChainTypes, - payload_envelope_verification::{ExecutedEnvelope, ExecutionPendingEnvelope}, -}; - -async fn import_execution_pending_envelope( - chain: Arc>, - execution_pending_envelope: ExecutionPendingEnvelope, -) -> Result { - match chain - .clone() - .into_executed_payload_envelope(execution_pending_envelope) - .await - .unwrap() - { - ExecutedEnvelope::Available(envelope) => todo!(), - ExecutedEnvelope::AvailabilityPending() => { - Err("AvailabilityPending not expected in this test. Block not imported.".to_string()) - } - } -} diff --git a/beacon_node/beacon_chain/src/validator_monitor.rs b/beacon_node/beacon_chain/src/validator_monitor.rs index d8c161f8c13..fdc7d273205 100644 --- a/beacon_node/beacon_chain/src/validator_monitor.rs +++ b/beacon_node/beacon_chain/src/validator_monitor.rs @@ -30,7 +30,7 @@ use types::{ Attestation, AttestationData, AttesterSlashingRef, BeaconBlockRef, BeaconState, BeaconStateError, ChainSpec, Epoch, EthSpec, Hash256, IndexedAttestation, IndexedAttestationRef, ProposerSlashing, SignedAggregateAndProof, SignedContributionAndProof, - SignedExecutionPayloadEnvelope, Slot, SyncCommitteeMessage, VoluntaryExit, + Slot, SyncCommitteeMessage, VoluntaryExit, }; /// Used for Prometheus labels. diff --git a/beacon_node/beacon_chain/tests/block_verification.rs b/beacon_node/beacon_chain/tests/block_verification.rs index d214ea6b15e..779e1964959 100644 --- a/beacon_node/beacon_chain/tests/block_verification.rs +++ b/beacon_node/beacon_chain/tests/block_verification.rs @@ -1,5 +1,5 @@ #![cfg(not(debug_assertions))] - +// TODO(gloas) we probably need similar test for payload envelope verification use beacon_chain::block_verification_types::{AsBlock, ExecutedBlock, RpcBlock}; use beacon_chain::data_availability_checker::{AvailabilityCheckError, AvailableBlockData}; use beacon_chain::data_column_verification::CustodyDataColumn; diff --git a/beacon_node/network/src/metrics.rs b/beacon_node/network/src/metrics.rs index 0016f66c01c..098d6d8324c 100644 --- a/beacon_node/network/src/metrics.rs +++ b/beacon_node/network/src/metrics.rs @@ -532,6 +532,16 @@ pub static SYNC_RPC_REQUEST_TIME: LazyLock> = LazyLock::new ) }); +/* + * Execution Payload Envelope Delay Metrics + */ +pub static ENVELOPE_DELAY_GOSSIP: LazyLock> = LazyLock::new(|| { + try_create_int_gauge( + "payload_envelope_delay_gossip", + "The first time we see this payload envelope from gossip as a delay from the start of the slot", + ) +}); + /* * Block Delay Metrics */ diff --git a/beacon_node/network/src/network_beacon_processor/gossip_methods.rs b/beacon_node/network/src/network_beacon_processor/gossip_methods.rs index bdcdd688600..1dcfea96d9a 100644 --- a/beacon_node/network/src/network_beacon_processor/gossip_methods.rs +++ b/beacon_node/network/src/network_beacon_processor/gossip_methods.rs @@ -20,9 +20,7 @@ use beacon_chain::{ }; use beacon_chain::{ blob_verification::{GossipBlobError, GossipVerifiedBlob}, - payload_envelope_verification::{ - EnvelopeError, gossip_verified_envelope::GossipVerifiedEnvelope, - }, + payload_envelope_verification::gossip_verified_envelope::GossipVerifiedEnvelope, }; use beacon_processor::{Work, WorkEvent}; use lighthouse_network::{Client, MessageAcceptance, MessageId, PeerAction, PeerId, ReportSource}; @@ -3230,24 +3228,34 @@ impl NetworkBeaconProcessor { } pub async fn process_gossip_execution_payload_envelope( - self: &Arc, + self: Arc, message_id: MessageId, peer_id: PeerId, - envelope: SignedExecutionPayloadEnvelope, + envelope: Arc>, + seen_timestamp: Duration, ) { - // TODO(EIP-7732): Implement proper execution payload envelope gossip processing. - // This should integrate with the envelope_verification.rs module once it's implemented. + if let Some(gossip_verified_envelope) = self + .process_gossip_unverified_execution_payload_envelope( + message_id, + peer_id, + envelope.clone(), + seen_timestamp, + ) + .await + { + let beacon_block_root = gossip_verified_envelope.signed_envelope.beacon_block_root(); - trace!( - %peer_id, - builder_index = envelope.message.builder_index, - slot = %envelope.message.slot, - beacon_block_root = %envelope.message.beacon_block_root, - "Processing execution payload envelope" - ); + Span::current().record("beacon_block_root", beacon_block_root.to_string()); - // For now, ignore all envelopes since verification is not implemented - self.propagate_validation_result(message_id, peer_id, MessageAcceptance::Ignore); + // TODO(gloas) in process_gossip_block here we check_and_insert on the duplicate cache + // before calling gossip_verified_block + + self.process_gossip_verified_execution_payload_envelope( + peer_id, + gossip_verified_envelope, + ) + .await; + } } async fn process_gossip_unverified_execution_payload_envelope( @@ -3255,8 +3263,10 @@ impl NetworkBeaconProcessor { message_id: MessageId, peer_id: PeerId, envelope: Arc>, + seen_duration: Duration, ) -> Option> { - // TODO(glos) add payload envelope delay metrics + let envelope_delay = + get_slot_delay_ms(seen_duration, envelope.slot(), &self.chain.slot_clock); let verification_result = self .chain @@ -3264,11 +3274,21 @@ impl NetworkBeaconProcessor { .verify_envelope_for_gossip(envelope.clone()) .await; - // TODO(gloas) delay metrics and write the time that the payload was observed into - // the delay cache - let verified_envelope = match verification_result { Ok(verified_envelope) => { + metrics::set_gauge( + &metrics::ENVELOPE_DELAY_GOSSIP, + envelope_delay.as_millis() as i64, + ); + + // Write the time the envelope was observed into the delay cache. + self.chain.envelope_times_cache.write().set_time_observed( + verified_envelope.signed_envelope.beacon_block_root(), + envelope.slot(), + seen_duration, + Some(peer_id.to_string()), + ); + info!( slot = %verified_envelope.signed_envelope.slot(), root = ?verified_envelope.signed_envelope.beacon_block_root(), @@ -3302,7 +3322,7 @@ impl NetworkBeaconProcessor { // TODO(gloas) update metrics to note how early the envelope arrived let inner_self = self.clone(); - let process_fn = Box::pin(async move { + let _process_fn = Box::pin(async move { inner_self .process_gossip_verified_execution_payload_envelope( peer_id, @@ -3333,27 +3353,26 @@ impl NetworkBeaconProcessor { peer_id: PeerId, verified_envelope: GossipVerifiedEnvelope, ) { - let processing_start_time = Instant::now(); - let envelope = verified_envelope.envelope_cloned(); + let _processing_start_time = Instant::now(); let beacon_block_root = verified_envelope.signed_envelope.beacon_block_root(); let result = self .chain .process_execution_payload_envelope( - block_root, + beacon_block_root, verified_envelope, - notify_execution_layer, - block_source, - publish_fn, + NotifyExecutionLayer::Yes, + BlockImportSource::Gossip, + || Ok(()), ) .await; - register_process_result_metrics(&result, metrics::BlockSource::Gossip, "envelope"); + // TODO(gloas) metrics + // register_process_result_metrics(&result, metrics::BlockSource::Gossip, "envelope"); match &result { Ok(AvailabilityProcessingStatus::Imported(block_root)) => { // TODO(gloas) do we need to send a `PayloadImported` event to the reporcess queue? - debug!( ?block_root, %peer_id, @@ -3374,7 +3393,10 @@ impl NetworkBeaconProcessor { ) } - Err(_) => todo!(), + Err(_) => { + // TODO(gloas) implement peer penalties + warn!("process_gossip_verified_execution_payload_envelope_failed") + } } } diff --git a/beacon_node/network/src/network_beacon_processor/mod.rs b/beacon_node/network/src/network_beacon_processor/mod.rs index fd67fcde825..ccd36b528e9 100644 --- a/beacon_node/network/src/network_beacon_processor/mod.rs +++ b/beacon_node/network/src/network_beacon_processor/mod.rs @@ -429,11 +429,17 @@ impl NetworkBeaconProcessor { message_id: MessageId, peer_id: PeerId, execution_payload: Box>, + seen_timestamp: Duration, ) -> Result<(), Error> { let processor = self.clone(); let process_fn = async move { processor - .process_gossip_execution_payload(message_id, peer_id, *execution_payload) + .process_gossip_execution_payload_envelope( + message_id, + peer_id, + Arc::new(*execution_payload), + seen_timestamp, + ) .await }; diff --git a/beacon_node/network/src/router.rs b/beacon_node/network/src/router.rs index 8373dec3227..77d64c92e67 100644 --- a/beacon_node/network/src/router.rs +++ b/beacon_node/network/src/router.rs @@ -493,6 +493,7 @@ impl Router { message_id, peer_id, signed_execution_payload_envelope, + timestamp_now(), ), ) } From 72fe22067ce49b5e3c92a8ce5f74a301f55083c1 Mon Sep 17 00:00:00 2001 From: Eitan Seri- Levi Date: Fri, 13 Feb 2026 23:49:59 -0800 Subject: [PATCH 7/9] Import logs --- .../src/payload_envelope_verification/import.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/import.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/import.rs index 36287127210..71887934927 100644 --- a/beacon_node/beacon_chain/src/payload_envelope_verification/import.rs +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/import.rs @@ -5,7 +5,7 @@ use fork_choice::PayloadVerificationStatus; use logging::crit; use slot_clock::SlotClock; use store::StoreOp; -use tracing::{debug, error, info_span, instrument}; +use tracing::{debug, error, info, info_span, instrument, warn}; use types::{BeaconState, BlockImportSource, Hash256, SignedBeaconBlock, Slot}; use super::{ @@ -111,11 +111,11 @@ impl BeaconChain { match import_envelope.await { // The block was successfully verified and imported. Yay. Ok(status @ AvailabilityProcessingStatus::Imported(block_root)) => { - debug!( + info!( ?block_root, %block_slot, source = %block_source, - "Envelope imported" + "Execution payload envelope imported" ); metrics::inc_counter(&metrics::ENVELOPE_PROCESSING_SUCCESSES); @@ -148,7 +148,7 @@ impl BeaconChain { } // The block failed verification. Err(other) => { - debug!(reason = other.to_string(), " Envelope rejected"); + warn!(reason = other.to_string(), "Execution payload envelope rejected"); Err(other) } } From de2362a8202572c59a5c81ba372de77aa34263d8 Mon Sep 17 00:00:00 2001 From: Eitan Seri- Levi Date: Sun, 22 Feb 2026 10:17:47 -0800 Subject: [PATCH 8/9] Fix compilation error --- .../gossip_verified_envelope.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs index c9bef630aab..504a1d2c705 100644 --- a/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/gossip_verified_envelope.rs @@ -2,7 +2,10 @@ use std::sync::Arc; use educe::Educe; use slot_clock::SlotClock; -use state_processing::{VerifySignatures, envelope_processing::process_execution_payload_envelope}; +use state_processing::{ + VerifySignatures, + envelope_processing::{VerifyStateRoot, process_execution_payload_envelope}, +}; use tracing::{Span, debug}; use types::{ EthSpec, SignedBeaconBlock, SignedExecutionPayloadEnvelope, @@ -236,6 +239,7 @@ impl IntoExecutionPendingEnvelope for GossipVerifiedEnve &signed_envelope, // verify signature already done for GossipVerifiedEnvelope VerifySignatures::False, + VerifyStateRoot::True, &chain.spec, )?; From b525fe055fc854f591c6c5a01f33cf9aef48aa10 Mon Sep 17 00:00:00 2001 From: Eitan Seri- Levi Date: Sun, 22 Feb 2026 16:07:48 -0800 Subject: [PATCH 9/9] Fix --- beacon_node/beacon_chain/src/beacon_chain.rs | 30 ------------------- .../beacon_chain/src/execution_payload.rs | 2 +- .../payload_envelope_verification/import.rs | 28 +++++++++++++++++ 3 files changed, 29 insertions(+), 31 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 1ed5579fea6..a491e8559b7 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -57,9 +57,6 @@ use crate::observed_block_producers::ObservedBlockProducers; use crate::observed_data_sidecars::ObservedDataSidecars; use crate::observed_operations::{ObservationOutcome, ObservedOperations}; use crate::observed_slashable::ObservedSlashable; -use crate::payload_envelope_verification::{ - EnvelopeError, ExecutedEnvelope, ExecutionPendingEnvelope, -}; use crate::pending_payload_envelopes::PendingPayloadEnvelopes; use crate::persisted_beacon_chain::PersistedBeaconChain; use crate::persisted_custody::persist_custody_context; @@ -3557,33 +3554,6 @@ impl BeaconChain { )) } - /// Accepts a fully-verified payload envelope and awaits on its payload verification handle to - /// get a fully `ExecutedEnvelope`. - /// - /// An error is returned if the verification handle couldn't be awaited. - #[instrument(skip_all, level = "debug")] - pub async fn into_executed_payload_envelope( - self: Arc, - pending_envelope: ExecutionPendingEnvelope, - ) -> Result, EnvelopeError> { - let ExecutionPendingEnvelope { - signed_envelope, - import_data, - payload_verification_handle, - } = pending_envelope; - - let payload_verification_outcome = payload_verification_handle - .await - .map_err(BeaconChainError::TokioJoin)? - .ok_or(BeaconChainError::RuntimeShutdown)??; - - Ok(ExecutedEnvelope::new( - signed_envelope, - import_data, - payload_verification_outcome, - )) - } - /* Import methods */ /// Checks if the block is available, and imports immediately if so, otherwise caches the block diff --git a/beacon_node/beacon_chain/src/execution_payload.rs b/beacon_node/beacon_chain/src/execution_payload.rs index b0644ac8aad..a5c2ead427a 100644 --- a/beacon_node/beacon_chain/src/execution_payload.rs +++ b/beacon_node/beacon_chain/src/execution_payload.rs @@ -122,7 +122,7 @@ impl PayloadNotifier { } } -/// Verify that `execution_payload` contained by `block` is considered valid by an execution +/// Verify that `execution_payload` associated with `beacon_block_root` is considered valid by an execution /// engine. /// /// ## Specification diff --git a/beacon_node/beacon_chain/src/payload_envelope_verification/import.rs b/beacon_node/beacon_chain/src/payload_envelope_verification/import.rs index f2633e8d5f6..603e14446a6 100644 --- a/beacon_node/beacon_chain/src/payload_envelope_verification/import.rs +++ b/beacon_node/beacon_chain/src/payload_envelope_verification/import.rs @@ -17,6 +17,7 @@ use crate::{ NotifyExecutionLayer, block_verification_types::{AsBlock, AvailableBlockData}, metrics, + payload_envelope_verification::ExecutionPendingEnvelope, validator_monitor::{get_slot_delay_ms, timestamp_now}, }; @@ -157,6 +158,33 @@ impl BeaconChain { } } + /// Accepts a fully-verified payload envelope and awaits on its payload verification handle to + /// get a fully `ExecutedEnvelope`. + /// + /// An error is returned if the verification handle couldn't be awaited. + #[instrument(skip_all, level = "debug")] + pub async fn into_executed_payload_envelope( + self: Arc, + pending_envelope: ExecutionPendingEnvelope, + ) -> Result, EnvelopeError> { + let ExecutionPendingEnvelope { + signed_envelope, + import_data, + payload_verification_handle, + } = pending_envelope; + + let payload_verification_outcome = payload_verification_handle + .await + .map_err(BeaconChainError::TokioJoin)? + .ok_or(BeaconChainError::RuntimeShutdown)??; + + Ok(ExecutedEnvelope::new( + signed_envelope, + import_data, + payload_verification_outcome, + )) + } + #[instrument(skip_all)] pub async fn import_available_execution_payload_envelope( self: &Arc,