mirror of
https://github.com/sigp/lighthouse.git
synced 2026-06-30 03:14:25 +00:00
Add payload to a cache for later signing
This commit is contained in:
@@ -14,17 +14,18 @@ use state_processing::{
|
||||
};
|
||||
use state_processing::{VerifyOperation, state_advance::complete_state_advance};
|
||||
use tracing::{Span, debug, debug_span, error, trace, warn};
|
||||
use tree_hash::TreeHash;
|
||||
use types::{
|
||||
Attestation, AttestationElectra, AttesterSlashing, AttesterSlashingElectra, BeaconBlock,
|
||||
BeaconBlockBodyGloas, BeaconBlockGloas, BeaconState, Deposit, Eth1Data, EthSpec, FullPayload,
|
||||
Graffiti, Hash256, PayloadAttestation, ProposerSlashing, RelativeEpoch, SignedBeaconBlock,
|
||||
SignedBlsToExecutionChange, SignedExecutionPayloadBid, SignedVoluntaryExit, Slot,
|
||||
SyncAggregate,
|
||||
BeaconBlockBodyGloas, BeaconBlockGloas, BeaconState, Deposit, Eth1Data, EthSpec,
|
||||
ExecutionPayloadEnvelope, FullPayload, Graffiti, Hash256, PayloadAttestation, ProposerSlashing,
|
||||
RelativeEpoch, SignedBeaconBlock, SignedBlsToExecutionChange, SignedExecutionPayloadBid,
|
||||
SignedVoluntaryExit, Slot, SyncAggregate,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
BeaconChain, BeaconChainTypes, BlockProductionError, ProduceBlockVerification,
|
||||
graffiti_calculator::GraffitiSettings, metrics,
|
||||
execution_payload_bid::ExecutionPayloadData, graffiti_calculator::GraffitiSettings, metrics,
|
||||
};
|
||||
|
||||
pub struct PartialBeaconBlock<E: EthSpec> {
|
||||
@@ -147,7 +148,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
// Produce the execution payload bid.
|
||||
// TODO(gloas) this is strictly for building local bids
|
||||
// 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()
|
||||
.produce_execution_payload_bid(state, state_root_opt, produce_at_slot, 0, u64::MAX)
|
||||
.await?;
|
||||
@@ -165,6 +166,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
chain.complete_partial_beacon_block_gloas(
|
||||
partial_beacon_block,
|
||||
execution_payload_bid,
|
||||
payload_data,
|
||||
state,
|
||||
verification,
|
||||
)
|
||||
@@ -417,6 +419,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
&self,
|
||||
partial_beacon_block: PartialBeaconBlock<T::EthSpec>,
|
||||
signed_execution_payload_bid: SignedExecutionPayloadBid<T::EthSpec>,
|
||||
payload_data: Option<ExecutionPayloadData<T::EthSpec>>,
|
||||
mut state: BeaconState<T::EthSpec>,
|
||||
verification: ProduceBlockVerification,
|
||||
) -> Result<
|
||||
@@ -572,6 +575,32 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
let (mut block, _) = signed_beacon_block.deconstruct();
|
||||
*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)
|
||||
// metrics::inc_counter(&metrics::BLOCK_PRODUCTION_SUCCESSES);
|
||||
|
||||
|
||||
@@ -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::pending_payload_envelopes::PendingPayloadEnvelopes;
|
||||
use crate::persisted_beacon_chain::PersistedBeaconChain;
|
||||
use crate::persisted_custody::persist_custody_context;
|
||||
use crate::persisted_fork_choice::PersistedForkChoice;
|
||||
@@ -419,6 +420,9 @@ pub struct BeaconChain<T: BeaconChainTypes> {
|
||||
RwLock<ObservedDataSidecars<DataColumnSidecar<T::EthSpec>, T::EthSpec>>,
|
||||
/// Maintains a record of slashable message seen over the gossip network or RPC.
|
||||
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.
|
||||
pub observed_voluntary_exits: Mutex<ObservedOperations<SignedVoluntaryExit, T::EthSpec>>,
|
||||
/// Maintains a record of which validators we've seen proposer slashings for.
|
||||
|
||||
@@ -1009,6 +1009,7 @@ where
|
||||
observed_column_sidecars: RwLock::new(ObservedDataSidecars::new(self.spec.clone())),
|
||||
observed_blob_sidecars: RwLock::new(ObservedDataSidecars::new(self.spec.clone())),
|
||||
observed_slashable: <_>::default(),
|
||||
pending_payload_envelopes: <_>::default(),
|
||||
observed_voluntary_exits: <_>::default(),
|
||||
observed_proposer_slashings: <_>::default(),
|
||||
observed_attester_slashings: <_>::default(),
|
||||
|
||||
@@ -4,8 +4,8 @@ use bls::Signature;
|
||||
use execution_layer::{BlockProposalContentsType, BuilderParams};
|
||||
use tracing::instrument;
|
||||
use types::{
|
||||
Address, BeaconState, BlockProductionVersion, BuilderIndex, ExecutionPayloadBid, Hash256,
|
||||
SignedExecutionPayloadBid, Slot,
|
||||
Address, BeaconState, BlockProductionVersion, BuilderIndex, ExecutionPayloadBid,
|
||||
ExecutionPayloadGloas, ExecutionRequests, Hash256, SignedExecutionPayloadBid, Slot,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
@@ -13,11 +13,27 @@ use crate::{
|
||||
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> {
|
||||
// TODO(gloas) introduce `ProposerPreferences` so we can build out trustless
|
||||
// bid building. Right now this only works for local building.
|
||||
/// Produce an `ExecutionPayloadBid` for some `slot` upon the given `state`.
|
||||
/// 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)]
|
||||
pub async fn produce_execution_payload_bid(
|
||||
self: Arc<Self>,
|
||||
@@ -30,6 +46,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
(
|
||||
SignedExecutionPayloadBid<T::EthSpec>,
|
||||
BeaconState<T::EthSpec>,
|
||||
Option<ExecutionPayloadData<T::EthSpec>>,
|
||||
),
|
||||
BlockProductionError,
|
||||
> {
|
||||
@@ -82,33 +99,41 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
.map_err(BlockProductionError::TokioJoin)?
|
||||
.ok_or(BlockProductionError::ShuttingDown)??;
|
||||
|
||||
let (execution_payload, blob_kzg_commitments) = match block_contents_type {
|
||||
BlockProposalContentsType::Full(block_proposal_contents) => {
|
||||
let blob_kzg_commitments =
|
||||
block_proposal_contents.blob_kzg_commitments().cloned();
|
||||
let (execution_payload, blob_kzg_commitments, execution_requests) =
|
||||
match block_contents_type {
|
||||
BlockProposalContentsType::Full(block_proposal_contents) => {
|
||||
let (payload, blob_kzg_commitments, _, execution_requests, _) =
|
||||
block_proposal_contents.deconstruct();
|
||||
|
||||
if let Some(blob_kzg_commitments) = blob_kzg_commitments {
|
||||
(
|
||||
block_proposal_contents.to_payload().execution_payload(),
|
||||
blob_kzg_commitments,
|
||||
)
|
||||
} else {
|
||||
return Err(BlockProductionError::MissingKzgCommitment(
|
||||
"No KZG commitments from the payload".to_owned(),
|
||||
));
|
||||
if let Some(blob_kzg_commitments) = blob_kzg_commitments
|
||||
&& let Some(execution_requests) = execution_requests
|
||||
{
|
||||
(
|
||||
payload.execution_payload(),
|
||||
blob_kzg_commitments,
|
||||
execution_requests,
|
||||
)
|
||||
} else {
|
||||
return Err(BlockProductionError::MissingKzgCommitment(
|
||||
"No KZG commitments from the payload".to_owned(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
// TODO(gloas) we should never receive a blinded response.
|
||||
// Should return some type of `Unexpected` error variant as this should never happen
|
||||
// in the V4 block production flow
|
||||
BlockProposalContentsType::Blinded(_) => {
|
||||
return Err(BlockProductionError::GloasNotImplemented);
|
||||
}
|
||||
};
|
||||
// TODO(gloas) we should never receive a blinded response.
|
||||
// Should return some type of `Unexpected` error variant as this should never happen
|
||||
// in the V4 block production flow
|
||||
BlockProposalContentsType::Blinded(_) => {
|
||||
return Err(BlockProductionError::GloasNotImplemented);
|
||||
}
|
||||
};
|
||||
|
||||
let state_root = state_root_opt.ok_or_else(|| {
|
||||
BlockProductionError::MissingStateRoot
|
||||
})?;
|
||||
let state_root = state_root_opt.ok_or_else(|| 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> {
|
||||
parent_block_hash: state.latest_block_hash()?.to_owned(),
|
||||
@@ -124,6 +149,15 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
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
|
||||
// we'll need to implement builder signature for the trustless path
|
||||
Ok((
|
||||
@@ -134,6 +168,9 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
.map_err(|_| BlockProductionError::GloasNotImplemented)?,
|
||||
},
|
||||
state,
|
||||
// Local building always returns payload data.
|
||||
// Trustless building would return None here.
|
||||
Some(payload_data),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,6 +44,7 @@ pub mod observed_block_producers;
|
||||
pub mod observed_data_sidecars;
|
||||
pub mod observed_operations;
|
||||
mod observed_slashable;
|
||||
pub mod pending_payload_envelopes;
|
||||
pub mod persisted_beacon_chain;
|
||||
pub mod persisted_custody;
|
||||
mod persisted_fork_choice;
|
||||
|
||||
150
beacon_node/beacon_chain/src/pending_payload_envelopes.rs
Normal file
150
beacon_node/beacon_chain/src/pending_payload_envelopes.rs
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user