Skip to content

Commit

Permalink
Add parent_block_number to payload SSE (#4053)
Browse files Browse the repository at this point in the history
## Issue Addressed

In #4027 I forgot to add the `parent_block_number` to the payload attributes SSE.

## Proposed Changes

Compute the parent block number while computing the pre-payload attributes. Pass it on to the SSE stream.

## Additional Info

Not essential for v3.5.1 as I suspect most builders don't need the `parent_block_root`. I would like to use it for my dummy no-op builder however.
  • Loading branch information
michaelsproul committed Mar 14, 2023
1 parent e190ebb commit 36e163c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
19 changes: 14 additions & 5 deletions beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ pub enum ProduceBlockVerification {
pub struct PrePayloadAttributes {
pub proposer_index: u64,
pub prev_randao: Hash256,
/// The parent block number is not part of the payload attributes sent to the EL, but *is*
/// sent to builders via SSE.
pub parent_block_number: u64,
}

/// Define whether a forkchoiceUpdate needs to be checked for an override (`Yes`) or has already
Expand Down Expand Up @@ -3866,16 +3869,21 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
proposer as u64
};

// Get the `prev_randao` value.
let prev_randao = if proposer_head == parent_block_root {
cached_head.parent_random()
// Get the `prev_randao` and parent block number.
let head_block_number = cached_head.head_block_number()?;
let (prev_randao, parent_block_number) = if proposer_head == parent_block_root {
(
cached_head.parent_random()?,
head_block_number.saturating_sub(1),
)
} else {
cached_head.head_random()
}?;
(cached_head.head_random()?, head_block_number)
};

Ok(Some(PrePayloadAttributes {
proposer_index,
prev_randao,
parent_block_number,
}))
}

Expand Down Expand Up @@ -4865,6 +4873,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
proposal_slot: prepare_slot,
proposer_index: proposer,
parent_block_root: head_root,
parent_block_number: pre_payload_attributes.parent_block_number,
parent_block_hash: forkchoice_update_params.head_hash.unwrap_or_default(),
payload_attributes: payload_attributes.into(),
},
Expand Down
11 changes: 11 additions & 0 deletions beacon_node/beacon_chain/src/canonical_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ impl<E: EthSpec> CachedHead<E> {
.map(|payload| payload.prev_randao())
}

/// Returns the execution block number of the block at the head of the chain.
///
/// Returns an error if the chain is prior to Bellatrix.
pub fn head_block_number(&self) -> Result<u64, BeaconStateError> {
self.snapshot
.beacon_block
.message()
.execution_payload()
.map(|payload| payload.block_number())
}

/// Returns the active validator count for the current epoch of the head state.
///
/// Should only return `None` if the caches have not been built on the head state (this should
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/execution_layer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use types::{

mod block_hash;
mod engine_api;
mod engines;
pub mod engines;
mod keccak;
mod metrics;
pub mod payload_cache;
Expand Down
3 changes: 3 additions & 0 deletions common/eth2/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,8 @@ pub struct SseExtendedPayloadAttributesGeneric<T> {
#[serde(with = "eth2_serde_utils::quoted_u64")]
pub proposer_index: u64,
pub parent_block_root: Hash256,
#[serde(with = "eth2_serde_utils::quoted_u64")]
pub parent_block_number: u64,
pub parent_block_hash: ExecutionBlockHash,
pub payload_attributes: T,
}
Expand Down Expand Up @@ -958,6 +960,7 @@ impl ForkVersionDeserialize for SseExtendedPayloadAttributes {
proposal_slot: helper.proposal_slot,
proposer_index: helper.proposer_index,
parent_block_root: helper.parent_block_root,
parent_block_number: helper.parent_block_number,
parent_block_hash: helper.parent_block_hash,
payload_attributes: SsePayloadAttributes::deserialize_by_fork::<D>(
helper.payload_attributes,
Expand Down

0 comments on commit 36e163c

Please sign in to comment.