Add payload to a cache for later signing

This commit is contained in:
Eitan Seri- Levi
2026-02-03 17:27:58 -08:00
parent 7cf4eb0396
commit 50dde1585c
11 changed files with 286 additions and 37 deletions

View File

@@ -14,17 +14,18 @@ use state_processing::{
}; };
use state_processing::{VerifyOperation, state_advance::complete_state_advance}; use state_processing::{VerifyOperation, state_advance::complete_state_advance};
use tracing::{Span, debug, debug_span, error, trace, warn}; use tracing::{Span, debug, debug_span, error, trace, warn};
use tree_hash::TreeHash;
use types::{ use types::{
Attestation, AttestationElectra, AttesterSlashing, AttesterSlashingElectra, BeaconBlock, Attestation, AttestationElectra, AttesterSlashing, AttesterSlashingElectra, BeaconBlock,
BeaconBlockBodyGloas, BeaconBlockGloas, BeaconState, Deposit, Eth1Data, EthSpec, FullPayload, BeaconBlockBodyGloas, BeaconBlockGloas, BeaconState, Deposit, Eth1Data, EthSpec,
Graffiti, Hash256, PayloadAttestation, ProposerSlashing, RelativeEpoch, SignedBeaconBlock, ExecutionPayloadEnvelope, FullPayload, Graffiti, Hash256, PayloadAttestation, ProposerSlashing,
SignedBlsToExecutionChange, SignedExecutionPayloadBid, SignedVoluntaryExit, Slot, RelativeEpoch, SignedBeaconBlock, SignedBlsToExecutionChange, SignedExecutionPayloadBid,
SyncAggregate, SignedVoluntaryExit, Slot, SyncAggregate,
}; };
use crate::{ use crate::{
BeaconChain, BeaconChainTypes, BlockProductionError, ProduceBlockVerification, BeaconChain, BeaconChainTypes, BlockProductionError, ProduceBlockVerification,
graffiti_calculator::GraffitiSettings, metrics, execution_payload_bid::ExecutionPayloadData, graffiti_calculator::GraffitiSettings, metrics,
}; };
pub struct PartialBeaconBlock<E: EthSpec> { pub struct PartialBeaconBlock<E: EthSpec> {
@@ -147,7 +148,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
// Produce the execution payload bid. // Produce the execution payload bid.
// TODO(gloas) this is strictly for building local bids // TODO(gloas) this is strictly for building local bids
// We'll need to build out trustless/trusted bid paths. // We'll need to build out trustless/trusted bid paths.
let (execution_payload_bid, state) = self let (execution_payload_bid, state, payload_data) = self
.clone() .clone()
.produce_execution_payload_bid(state, state_root_opt, produce_at_slot, 0, u64::MAX) .produce_execution_payload_bid(state, state_root_opt, produce_at_slot, 0, u64::MAX)
.await?; .await?;
@@ -165,6 +166,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
chain.complete_partial_beacon_block_gloas( chain.complete_partial_beacon_block_gloas(
partial_beacon_block, partial_beacon_block,
execution_payload_bid, execution_payload_bid,
payload_data,
state, state,
verification, verification,
) )
@@ -417,6 +419,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
&self, &self,
partial_beacon_block: PartialBeaconBlock<T::EthSpec>, partial_beacon_block: PartialBeaconBlock<T::EthSpec>,
signed_execution_payload_bid: SignedExecutionPayloadBid<T::EthSpec>, signed_execution_payload_bid: SignedExecutionPayloadBid<T::EthSpec>,
payload_data: Option<ExecutionPayloadData<T::EthSpec>>,
mut state: BeaconState<T::EthSpec>, mut state: BeaconState<T::EthSpec>,
verification: ProduceBlockVerification, verification: ProduceBlockVerification,
) -> Result< ) -> Result<
@@ -572,6 +575,32 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
let (mut block, _) = signed_beacon_block.deconstruct(); let (mut block, _) = signed_beacon_block.deconstruct();
*block.state_root_mut() = state_root; *block.state_root_mut() = state_root;
// Construct and cache the ExecutionPayloadEnvelope if we have payload data.
// For local building, we always have payload data.
// For trustless building, the builder will provide the envelope separately.
if let Some(payload_data) = payload_data {
let beacon_block_root = block.tree_hash_root();
let execution_payload_envelope = ExecutionPayloadEnvelope {
payload: payload_data.payload,
execution_requests: payload_data.execution_requests,
builder_index: payload_data.builder_index,
beacon_block_root,
slot: payload_data.slot,
state_root: payload_data.state_root,
};
// Cache the envelope for later retrieval for signing and publishing.
self.pending_payload_envelopes
.write()
.insert(beacon_block_root, execution_payload_envelope);
debug!(
%beacon_block_root,
slot = %block.slot(),
"Cached pending execution payload envelope"
);
}
// TODO(gloas) // TODO(gloas)
// metrics::inc_counter(&metrics::BLOCK_PRODUCTION_SUCCESSES); // metrics::inc_counter(&metrics::BLOCK_PRODUCTION_SUCCESSES);

View File

@@ -56,6 +56,7 @@ use crate::observed_block_producers::ObservedBlockProducers;
use crate::observed_data_sidecars::ObservedDataSidecars; use crate::observed_data_sidecars::ObservedDataSidecars;
use crate::observed_operations::{ObservationOutcome, ObservedOperations}; use crate::observed_operations::{ObservationOutcome, ObservedOperations};
use crate::observed_slashable::ObservedSlashable; use crate::observed_slashable::ObservedSlashable;
use crate::pending_payload_envelopes::PendingPayloadEnvelopes;
use crate::persisted_beacon_chain::PersistedBeaconChain; use crate::persisted_beacon_chain::PersistedBeaconChain;
use crate::persisted_custody::persist_custody_context; use crate::persisted_custody::persist_custody_context;
use crate::persisted_fork_choice::PersistedForkChoice; use crate::persisted_fork_choice::PersistedForkChoice;
@@ -419,6 +420,9 @@ pub struct BeaconChain<T: BeaconChainTypes> {
RwLock<ObservedDataSidecars<DataColumnSidecar<T::EthSpec>, T::EthSpec>>, RwLock<ObservedDataSidecars<DataColumnSidecar<T::EthSpec>, T::EthSpec>>,
/// Maintains a record of slashable message seen over the gossip network or RPC. /// Maintains a record of slashable message seen over the gossip network or RPC.
pub observed_slashable: RwLock<ObservedSlashable<T::EthSpec>>, pub observed_slashable: RwLock<ObservedSlashable<T::EthSpec>>,
/// Cache of pending execution payload envelopes for local block building.
/// Envelopes are stored here during block production and eventually published.
pub pending_payload_envelopes: RwLock<PendingPayloadEnvelopes<T::EthSpec>>,
/// Maintains a record of which validators have submitted voluntary exits. /// Maintains a record of which validators have submitted voluntary exits.
pub observed_voluntary_exits: Mutex<ObservedOperations<SignedVoluntaryExit, T::EthSpec>>, pub observed_voluntary_exits: Mutex<ObservedOperations<SignedVoluntaryExit, T::EthSpec>>,
/// Maintains a record of which validators we've seen proposer slashings for. /// Maintains a record of which validators we've seen proposer slashings for.

View File

@@ -1009,6 +1009,7 @@ where
observed_column_sidecars: RwLock::new(ObservedDataSidecars::new(self.spec.clone())), observed_column_sidecars: RwLock::new(ObservedDataSidecars::new(self.spec.clone())),
observed_blob_sidecars: RwLock::new(ObservedDataSidecars::new(self.spec.clone())), observed_blob_sidecars: RwLock::new(ObservedDataSidecars::new(self.spec.clone())),
observed_slashable: <_>::default(), observed_slashable: <_>::default(),
pending_payload_envelopes: <_>::default(),
observed_voluntary_exits: <_>::default(), observed_voluntary_exits: <_>::default(),
observed_proposer_slashings: <_>::default(), observed_proposer_slashings: <_>::default(),
observed_attester_slashings: <_>::default(), observed_attester_slashings: <_>::default(),

View File

@@ -4,8 +4,8 @@ use bls::Signature;
use execution_layer::{BlockProposalContentsType, BuilderParams}; use execution_layer::{BlockProposalContentsType, BuilderParams};
use tracing::instrument; use tracing::instrument;
use types::{ use types::{
Address, BeaconState, BlockProductionVersion, BuilderIndex, ExecutionPayloadBid, Hash256, Address, BeaconState, BlockProductionVersion, BuilderIndex, ExecutionPayloadBid,
SignedExecutionPayloadBid, Slot, ExecutionPayloadGloas, ExecutionRequests, Hash256, SignedExecutionPayloadBid, Slot,
}; };
use crate::{ use crate::{
@@ -13,11 +13,27 @@ use crate::{
execution_payload::get_execution_payload, execution_payload::get_execution_payload,
}; };
/// Data needed to construct an ExecutionPayloadEnvelope.
/// The envelope requires the beacon_block_root which can only be computed after the block exists.
pub struct ExecutionPayloadData<E: types::EthSpec> {
pub payload: ExecutionPayloadGloas<E>,
pub execution_requests: ExecutionRequests<E>,
pub builder_index: BuilderIndex,
pub slot: Slot,
pub state_root: Hash256,
}
impl<T: BeaconChainTypes> BeaconChain<T> { impl<T: BeaconChainTypes> BeaconChain<T> {
// TODO(gloas) introduce `ProposerPreferences` so we can build out trustless // TODO(gloas) introduce `ProposerPreferences` so we can build out trustless
// bid building. Right now this only works for local building. // bid building. Right now this only works for local building.
/// Produce an `ExecutionPayloadBid` for some `slot` upon the given `state`. /// Produce an `ExecutionPayloadBid` for some `slot` upon the given `state`.
/// This function assumes we've already done the state advance. /// This function assumes we've already done the state advance.
///
/// Returns the signed bid, the state, and optionally the payload data needed to construct
/// the `ExecutionPayloadEnvelope` after the beacon block is created.
///
/// For local building, payload data is always returned (`Some`).
/// For trustless building, the builder provides the envelope separately, so `None` is returned.
#[instrument(level = "debug", skip_all)] #[instrument(level = "debug", skip_all)]
pub async fn produce_execution_payload_bid( pub async fn produce_execution_payload_bid(
self: Arc<Self>, self: Arc<Self>,
@@ -30,6 +46,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
( (
SignedExecutionPayloadBid<T::EthSpec>, SignedExecutionPayloadBid<T::EthSpec>,
BeaconState<T::EthSpec>, BeaconState<T::EthSpec>,
Option<ExecutionPayloadData<T::EthSpec>>,
), ),
BlockProductionError, BlockProductionError,
> { > {
@@ -82,33 +99,41 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
.map_err(BlockProductionError::TokioJoin)? .map_err(BlockProductionError::TokioJoin)?
.ok_or(BlockProductionError::ShuttingDown)??; .ok_or(BlockProductionError::ShuttingDown)??;
let (execution_payload, blob_kzg_commitments) = match block_contents_type { let (execution_payload, blob_kzg_commitments, execution_requests) =
BlockProposalContentsType::Full(block_proposal_contents) => { match block_contents_type {
let blob_kzg_commitments = BlockProposalContentsType::Full(block_proposal_contents) => {
block_proposal_contents.blob_kzg_commitments().cloned(); let (payload, blob_kzg_commitments, _, execution_requests, _) =
block_proposal_contents.deconstruct();
if let Some(blob_kzg_commitments) = blob_kzg_commitments { if let Some(blob_kzg_commitments) = blob_kzg_commitments
( && let Some(execution_requests) = execution_requests
block_proposal_contents.to_payload().execution_payload(), {
blob_kzg_commitments, (
) payload.execution_payload(),
} else { blob_kzg_commitments,
return Err(BlockProductionError::MissingKzgCommitment( execution_requests,
"No KZG commitments from the payload".to_owned(), )
)); } else {
return Err(BlockProductionError::MissingKzgCommitment(
"No KZG commitments from the payload".to_owned(),
));
}
} }
} // TODO(gloas) we should never receive a blinded response.
// TODO(gloas) we should never receive a blinded response. // Should return some type of `Unexpected` error variant as this should never happen
// Should return some type of `Unexpected` error variant as this should never happen // in the V4 block production flow
// in the V4 block production flow BlockProposalContentsType::Blinded(_) => {
BlockProposalContentsType::Blinded(_) => { return Err(BlockProductionError::GloasNotImplemented);
return Err(BlockProductionError::GloasNotImplemented); }
} };
};
let state_root = state_root_opt.ok_or_else(|| { let state_root = state_root_opt.ok_or_else(|| BlockProductionError::MissingStateRoot)?;
BlockProductionError::MissingStateRoot
})?; // TODO(gloas) this is just a dummy error variant for now
let execution_payload_gloas = execution_payload
.as_gloas()
.map_err(|_| BlockProductionError::GloasNotImplemented)?
.to_owned();
let bid = ExecutionPayloadBid::<T::EthSpec> { let bid = ExecutionPayloadBid::<T::EthSpec> {
parent_block_hash: state.latest_block_hash()?.to_owned(), parent_block_hash: state.latest_block_hash()?.to_owned(),
@@ -124,6 +149,15 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
blob_kzg_commitments, blob_kzg_commitments,
}; };
// Store payload data for envelope construction after block is created
let payload_data = ExecutionPayloadData {
payload: execution_payload_gloas,
execution_requests,
builder_index,
slot: produce_at_slot,
state_root,
};
// TODO(gloas) this is only local building // TODO(gloas) this is only local building
// we'll need to implement builder signature for the trustless path // we'll need to implement builder signature for the trustless path
Ok(( Ok((
@@ -134,6 +168,9 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
.map_err(|_| BlockProductionError::GloasNotImplemented)?, .map_err(|_| BlockProductionError::GloasNotImplemented)?,
}, },
state, state,
// Local building always returns payload data.
// Trustless building would return None here.
Some(payload_data),
)) ))
} }
} }

View File

@@ -44,6 +44,7 @@ pub mod observed_block_producers;
pub mod observed_data_sidecars; pub mod observed_data_sidecars;
pub mod observed_operations; pub mod observed_operations;
mod observed_slashable; mod observed_slashable;
pub mod pending_payload_envelopes;
pub mod persisted_beacon_chain; pub mod persisted_beacon_chain;
pub mod persisted_custody; pub mod persisted_custody;
mod persisted_fork_choice; mod persisted_fork_choice;

View File

@@ -0,0 +1,150 @@
//! Provides the `PendingPayloadEnvelopes` cache for storing execution payload envelopes
//! that have been produced during local block production but not yet imported to fork choice.
//!
//! For local building, the envelope is created during block production.
//! This cache holds the envelopes temporarily until the proposer can sign and publish the payload.
use std::collections::HashMap;
use types::{EthSpec, ExecutionPayloadEnvelope, Hash256, Slot};
/// Cache for pending execution payload envelopes awaiting publishing.
///
/// Envelopes are keyed by beacon block root and pruned based on slot age.
pub struct PendingPayloadEnvelopes<E: EthSpec> {
/// Maximum number of slots to keep envelopes before pruning.
max_slot_age: u64,
/// The envelopes, keyed by beacon block root.
envelopes: HashMap<Hash256, ExecutionPayloadEnvelope<E>>,
}
impl<E: EthSpec> Default for PendingPayloadEnvelopes<E> {
fn default() -> Self {
Self::new(Self::DEFAULT_MAX_SLOT_AGE)
}
}
impl<E: EthSpec> PendingPayloadEnvelopes<E> {
/// Default maximum slot age before pruning (2 slots).
pub const DEFAULT_MAX_SLOT_AGE: u64 = 2;
/// Create a new cache with the specified maximum slot age.
pub fn new(max_slot_age: u64) -> Self {
Self {
max_slot_age,
envelopes: HashMap::new(),
}
}
/// Insert a pending envelope into the cache.
pub fn insert(&mut self, block_root: Hash256, envelope: ExecutionPayloadEnvelope<E>) {
self.envelopes.insert(block_root, envelope);
}
/// Get a pending envelope by block root.
pub fn get(&self, block_root: &Hash256) -> Option<&ExecutionPayloadEnvelope<E>> {
self.envelopes.get(block_root)
}
/// Remove and return a pending envelope by block root.
pub fn remove(&mut self, block_root: &Hash256) -> Option<ExecutionPayloadEnvelope<E>> {
self.envelopes.remove(block_root)
}
/// Check if an envelope exists for the given block root.
pub fn contains(&self, block_root: &Hash256) -> bool {
self.envelopes.contains_key(block_root)
}
/// Prune envelopes older than `current_slot - max_slot_age`.
///
/// This removes stale envelopes from blocks that were never imported.
pub fn prune(&mut self, current_slot: Slot) {
let min_slot = current_slot.saturating_sub(self.max_slot_age);
self.envelopes
.retain(|_, envelope| envelope.slot >= min_slot);
}
/// Returns the number of pending envelopes in the cache.
pub fn len(&self) -> usize {
self.envelopes.len()
}
/// Returns true if the cache is empty.
pub fn is_empty(&self) -> bool {
self.envelopes.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
use types::{ExecutionPayloadGloas, ExecutionRequests, MainnetEthSpec};
type E = MainnetEthSpec;
fn make_envelope(slot: Slot, block_root: Hash256) -> ExecutionPayloadEnvelope<E> {
ExecutionPayloadEnvelope {
payload: ExecutionPayloadGloas::default(),
execution_requests: ExecutionRequests::default(),
builder_index: 0,
beacon_block_root: block_root,
slot,
state_root: Hash256::ZERO,
}
}
#[test]
fn insert_and_get() {
let mut cache = PendingPayloadEnvelopes::<E>::default();
let block_root = Hash256::repeat_byte(1);
let envelope = make_envelope(Slot::new(1), block_root);
assert!(!cache.contains(&block_root));
assert_eq!(cache.len(), 0);
cache.insert(block_root, envelope.clone());
assert!(cache.contains(&block_root));
assert_eq!(cache.len(), 1);
assert_eq!(cache.get(&block_root), Some(&envelope));
}
#[test]
fn remove() {
let mut cache = PendingPayloadEnvelopes::<E>::default();
let block_root = Hash256::repeat_byte(1);
let envelope = make_envelope(Slot::new(1), block_root);
cache.insert(block_root, envelope.clone());
assert!(cache.contains(&block_root));
let removed = cache.remove(&block_root);
assert_eq!(removed, Some(envelope));
assert!(!cache.contains(&block_root));
assert_eq!(cache.len(), 0);
}
#[test]
fn prune_old_envelopes() {
let mut cache = PendingPayloadEnvelopes::<E>::new(2);
// Insert envelope at slot 5
let block_root_1 = Hash256::repeat_byte(1);
let envelope_1 = make_envelope(Slot::new(5), block_root_1);
cache.insert(block_root_1, envelope_1);
// Insert envelope at slot 10
let block_root_2 = Hash256::repeat_byte(2);
let envelope_2 = make_envelope(Slot::new(10), block_root_2);
cache.insert(block_root_2, envelope_2);
assert_eq!(cache.len(), 2);
// Prune at slot 10 with max_slot_age=2, should keep slots >= 8
cache.prune(Slot::new(10));
assert_eq!(cache.len(), 1);
assert!(!cache.contains(&block_root_1)); // slot 5 < 8, pruned
assert!(cache.contains(&block_root_2)); // slot 10 >= 8, kept
}
}

View File

@@ -140,7 +140,8 @@ pub fn build_response_v4<T: BeaconChainTypes>(
.to_ref() .to_ref()
.fork_name(&chain.spec) .fork_name(&chain.spec)
.map_err(inconsistent_fork_rejection)?; .map_err(inconsistent_fork_rejection)?;
let consensus_block_value_wei = Uint256::from(consensus_block_value) * Uint256::from(1_000_000_000u64); let consensus_block_value_wei =
Uint256::from(consensus_block_value) * Uint256::from(1_000_000_000u64);
match accept_header { match accept_header {
Some(api_types::Accept::Ssz) => Response::builder() Some(api_types::Accept::Ssz) => Response::builder()

View File

@@ -1,4 +1,6 @@
use crate::produce_block::{produce_blinded_block_v2, produce_block_v2, produce_block_v3, produce_block_v4}; use crate::produce_block::{
produce_blinded_block_v2, produce_block_v2, produce_block_v3, produce_block_v4,
};
use crate::task_spawner::{Priority, TaskSpawner}; use crate::task_spawner::{Priority, TaskSpawner};
use crate::utils::{ use crate::utils::{
AnyVersionFilter, ChainFilter, EthV1Filter, NetworkTxFilter, NotWhileSyncingFilter, AnyVersionFilter, ChainFilter, EthV1Filter, NetworkTxFilter, NotWhileSyncingFilter,

View File

@@ -3799,6 +3799,19 @@ impl ApiTester {
); );
assert!(!metadata.consensus_block_value.is_zero()); assert!(!metadata.consensus_block_value.is_zero());
// Verify that the execution payload envelope is cached for local building.
// The envelope is stored in the pending cache until publishing.
let block_root = block.tree_hash_root();
let envelope = self
.chain
.pending_payload_envelopes
.read()
.get(&block_root)
.cloned()
.expect("envelope should exist in pending cache for local building");
assert_eq!(envelope.beacon_block_root, block_root);
assert_eq!(envelope.slot, slot);
// Sign and publish the block // Sign and publish the block
let signed_block = block.sign(&sk, &fork, genesis_validators_root, &self.chain.spec); let signed_block = block.sign(&sk, &fork, genesis_validators_root, &self.chain.spec);
let signed_block_request = let signed_block_request =

View File

@@ -2452,7 +2452,13 @@ impl BeaconNodeHttpClient {
graffiti: Option<&Graffiti>, graffiti: Option<&Graffiti>,
builder_booster_factor: Option<u64>, builder_booster_factor: Option<u64>,
graffiti_policy: Option<GraffitiPolicy>, graffiti_policy: Option<GraffitiPolicy>,
) -> Result<(ForkVersionedResponse<BeaconBlock<E>, ProduceBlockV4Metadata>, ProduceBlockV4Metadata), Error> { ) -> Result<
(
ForkVersionedResponse<BeaconBlock<E>, ProduceBlockV4Metadata>,
ProduceBlockV4Metadata,
),
Error,
> {
self.get_validator_blocks_v4_modular( self.get_validator_blocks_v4_modular(
slot, slot,
randao_reveal, randao_reveal,
@@ -2473,7 +2479,13 @@ impl BeaconNodeHttpClient {
skip_randao_verification: SkipRandaoVerification, skip_randao_verification: SkipRandaoVerification,
builder_booster_factor: Option<u64>, builder_booster_factor: Option<u64>,
graffiti_policy: Option<GraffitiPolicy>, graffiti_policy: Option<GraffitiPolicy>,
) -> Result<(ForkVersionedResponse<BeaconBlock<E>, ProduceBlockV4Metadata>, ProduceBlockV4Metadata), Error> { ) -> Result<
(
ForkVersionedResponse<BeaconBlock<E>, ProduceBlockV4Metadata>,
ProduceBlockV4Metadata,
),
Error,
> {
let path = self let path = self
.get_validator_blocks_v4_path( .get_validator_blocks_v4_path(
slot, slot,

View File

@@ -1742,7 +1742,6 @@ pub struct ProduceBlockV3Metadata {
pub consensus_block_value: Uint256, pub consensus_block_value: Uint256,
} }
/// Metadata about a `ProduceBlockV3Response` which is returned in the body & headers. /// Metadata about a `ProduceBlockV3Response` which is returned in the body & headers.
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct ProduceBlockV4Metadata { pub struct ProduceBlockV4Metadata {