Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: relax payload conversions with BlockHeader #1981

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 65 additions & 31 deletions crates/rpc-types-engine/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use alloc::{
vec::Vec,
};
use alloy_consensus::{
constants::MAXIMUM_EXTRA_DATA_SIZE, Blob, Block, BlockBody, Bytes48, Header, Transaction,
EMPTY_OMMER_ROOT_HASH,
constants::MAXIMUM_EXTRA_DATA_SIZE, Blob, Block, BlockBody, BlockHeader, Bytes48, Header,
Transaction, EMPTY_OMMER_ROOT_HASH,
};
use alloy_eips::{
eip2718::{Decodable2718, Encodable2718},
Expand All @@ -16,7 +16,7 @@ use alloy_eips::{
eip7685::Requests,
BlockNumHash,
};
use alloy_primitives::{bytes::BufMut, Address, Bloom, Bytes, B256, B64, U256};
use alloy_primitives::{bytes::BufMut, Address, Bloom, Bytes, Sealable, B256, B64, U256};
use core::iter::{FromIterator, IntoIterator};

/// The execution payload body response that allows for `null` values.
Expand Down Expand Up @@ -73,7 +73,11 @@ impl ExecutionPayloadFieldV2 {
/// If the block body contains withdrawals this returns [`ExecutionPayloadFieldV2::V2`].
///
/// Note: This re-calculates the block hash.
pub fn from_block_slow<T: Encodable2718>(block: &Block<T>) -> Self {
pub fn from_block_slow<T, H>(block: &Block<T, H>) -> Self
where
T: Encodable2718,
H: BlockHeader + Sealable,
{
Self::from_block_unchecked(block.hash_slow(), block)
}

Expand All @@ -85,7 +89,11 @@ impl ExecutionPayloadFieldV2 {
/// - [`ExecutionPayloadV2::from_block_unchecked`].
///
/// If the block body contains withdrawals this returns [`ExecutionPayloadFieldV2::V2`].
pub fn from_block_unchecked<T: Encodable2718>(block_hash: B256, block: &Block<T>) -> Self {
pub fn from_block_unchecked<T, H>(block_hash: B256, block: &Block<T, H>) -> Self
where
T: Encodable2718,
H: BlockHeader,
{
if block.body.withdrawals.is_some() {
Self::V2(ExecutionPayloadV2::from_block_unchecked(block_hash, block))
} else {
Expand Down Expand Up @@ -330,27 +338,35 @@ impl ExecutionPayloadV1 {
/// Converts [`alloy_consensus::Block`] to [`ExecutionPayloadV1`].
///
/// Note: This re-calculates the block hash.
pub fn from_block_slow<T: Encodable2718>(block: &Block<T>) -> Self {
Self::from_block_unchecked(block.hash_slow(), block)
pub fn from_block_slow<T, H>(block: &Block<T, H>) -> Self
where
T: Encodable2718,
H: BlockHeader + Sealable,
{
Self::from_block_unchecked(block.header.hash_slow(), block)
}

/// Converts [`alloy_consensus::Block`] to [`ExecutionPayloadV1`] using the given block hash.
pub fn from_block_unchecked<T: Encodable2718>(block_hash: B256, block: &Block<T>) -> Self {
pub fn from_block_unchecked<T, H>(block_hash: B256, block: &Block<T, H>) -> Self
where
T: Encodable2718,
H: BlockHeader,
{
let transactions =
block.body.transactions().map(|tx| tx.encoded_2718().into()).collect::<Vec<_>>();
Self {
parent_hash: block.parent_hash,
fee_recipient: block.beneficiary,
state_root: block.state_root,
receipts_root: block.receipts_root,
logs_bloom: block.logs_bloom,
prev_randao: block.mix_hash,
block_number: block.number,
gas_limit: block.gas_limit,
gas_used: block.gas_used,
timestamp: block.timestamp,
base_fee_per_gas: U256::from(block.base_fee_per_gas.unwrap_or_default()),
extra_data: block.header.extra_data.clone(),
parent_hash: block.parent_hash(),
fee_recipient: block.beneficiary(),
state_root: block.state_root(),
receipts_root: block.receipts_root(),
logs_bloom: block.logs_bloom(),
prev_randao: block.mix_hash().unwrap_or_default(),
block_number: block.number(),
gas_limit: block.gas_limit(),
gas_used: block.gas_used(),
timestamp: block.timestamp(),
base_fee_per_gas: U256::from(block.base_fee_per_gas().unwrap_or_default()),
extra_data: block.header.extra_data().clone(),
block_hash,
transactions,
}
Expand Down Expand Up @@ -389,16 +405,24 @@ impl ExecutionPayloadV2 {
/// If the block does not have any withdrawals, an empty vector is used.
///
/// Note: This re-calculates the block hash.
pub fn from_block_slow<T: Encodable2718>(block: &Block<T>) -> Self {
Self::from_block_unchecked(block.hash_slow(), block)
pub fn from_block_slow<T, H>(block: &Block<T, H>) -> Self
where
T: Encodable2718,
H: BlockHeader + Sealable,
{
Self::from_block_unchecked(block.header.hash_slow(), block)
}

/// Converts [`alloy_consensus::Block`] to [`ExecutionPayloadV2`] using the given block hash.
///
/// See also [`ExecutionPayloadV1::from_block_unchecked`].
///
/// If the block does not have any withdrawals, an empty vector is used.
pub fn from_block_unchecked<T: Encodable2718>(block_hash: B256, block: &Block<T>) -> Self {
pub fn from_block_unchecked<T, H>(block_hash: B256, block: &Block<T, H>) -> Self
where
T: Encodable2718,
H: BlockHeader,
{
Self {
withdrawals: block
.body
Expand Down Expand Up @@ -571,17 +595,25 @@ impl ExecutionPayloadV3 {
/// See also [`ExecutionPayloadV2::from_block_unchecked`].
///
/// Note: This re-calculates the block hash.
pub fn from_block_slow<T: Encodable2718>(block: &Block<T>) -> Self {
pub fn from_block_slow<T, H>(block: &Block<T, H>) -> Self
where
T: Encodable2718,
H: BlockHeader + Sealable,
{
Self::from_block_unchecked(block.hash_slow(), block)
}

/// Converts [`alloy_consensus::Block`] to [`ExecutionPayloadV3`] using the given block hash.
///
/// See also [`ExecutionPayloadV2::from_block_unchecked`].
pub fn from_block_unchecked<T: Encodable2718>(block_hash: B256, block: &Block<T>) -> Self {
pub fn from_block_unchecked<T, H>(block_hash: B256, block: &Block<T, H>) -> Self
where
T: Encodable2718,
H: BlockHeader,
{
Self {
blob_gas_used: block.blob_gas_used.unwrap_or_default(),
excess_blob_gas: block.excess_blob_gas.unwrap_or_default(),
blob_gas_used: block.blob_gas_used().unwrap_or_default(),
excess_blob_gas: block.excess_blob_gas().unwrap_or_default(),
payload_inner: ExecutionPayloadV2::from_block_unchecked(block_hash, block),
}
}
Expand Down Expand Up @@ -806,9 +838,10 @@ impl ExecutionPayload {
/// See also [`ExecutionPayloadSidecar::from_block`].
///
/// Note: This re-calculates the block hash.
pub fn from_block_slow<T>(block: &Block<T>) -> (Self, ExecutionPayloadSidecar)
pub fn from_block_slow<T, H>(block: &Block<T, H>) -> (Self, ExecutionPayloadSidecar)
where
T: Encodable2718 + Transaction,
H: BlockHeader + Sealable,
{
Self::from_block_unchecked(block.hash_slow(), block)
}
Expand All @@ -818,16 +851,17 @@ impl ExecutionPayload {
///
/// See also [`ExecutionPayloadV3::from_block_unchecked`].
/// See also [`ExecutionPayloadSidecar::from_block`].
pub fn from_block_unchecked<T>(
pub fn from_block_unchecked<T, H>(
block_hash: B256,
block: &Block<T>,
block: &Block<T, H>,
) -> (Self, ExecutionPayloadSidecar)
where
T: Encodable2718 + Transaction,
H: BlockHeader,
{
let sidecar = ExecutionPayloadSidecar::from_block(block);

let execution_payload = if block.header.parent_beacon_block_root.is_some() {
let execution_payload = if block.header.parent_beacon_block_root().is_some() {
// block with parent beacon block root: V3
Self::V3(ExecutionPayloadV3::from_block_unchecked(block_hash, block))
} else if block.body.withdrawals.is_some() {
Expand Down
9 changes: 5 additions & 4 deletions crates/rpc-types-engine/src/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
CancunPayloadFields, MaybeCancunPayloadFields, MaybePraguePayloadFields, PraguePayloadFields,
};
use alloc::vec::Vec;
use alloy_consensus::{Block, Transaction};
use alloy_consensus::{Block, BlockHeader, Transaction};
use alloy_eips::eip7685::Requests;
use alloy_primitives::B256;

Expand All @@ -29,17 +29,18 @@ impl ExecutionPayloadSidecar {
///
/// Note: This returns [`RequestOrHash::Hash`](alloy_eips::eip7685::RequestsOrHash::Hash) for
/// the EIP-7685 requests.
pub fn from_block<T>(block: &Block<T>) -> Self
pub fn from_block<T, H>(block: &Block<T, H>) -> Self
where
T: Transaction,
H: BlockHeader,
{
let cancun =
block.parent_beacon_block_root.map(|parent_beacon_block_root| CancunPayloadFields {
block.parent_beacon_block_root().map(|parent_beacon_block_root| CancunPayloadFields {
parent_beacon_block_root,
versioned_hashes: block.body.blob_versioned_hashes_iter().copied().collect(),
});

let prague = block.requests_hash.map(PraguePayloadFields::new);
let prague = block.requests_hash().map(PraguePayloadFields::new);

match (cancun, prague) {
(Some(cancun), Some(prague)) => Self::v4(cancun, prague),
Expand Down