chore: simplify OpBuiltPayload (#14152)

This commit is contained in:
Arsenii Kulikov
2025-02-02 20:32:16 +04:00
committed by GitHub
parent 60337077c8
commit 0c3ccccba9
3 changed files with 34 additions and 60 deletions

View File

@ -847,6 +847,12 @@ impl<N: NodePrimitives> ExecutedBlockWithTrieUpdates<N> {
pub fn trie_updates(&self) -> &TrieUpdates { pub fn trie_updates(&self) -> &TrieUpdates {
&self.trie &self.trie
} }
/// Converts the value into [`SealedBlock`].
pub fn into_sealed_block(self) -> SealedBlock<N::Block> {
let block = Arc::unwrap_or_clone(self.block.recovered_block);
block.into_sealed_block()
}
} }
/// Non-empty chain of blocks. /// Non-empty chain of blocks.

View File

@ -449,14 +449,7 @@ where
let no_tx_pool = ctx.attributes().no_tx_pool; let no_tx_pool = ctx.attributes().no_tx_pool;
let payload = OpBuiltPayload::new( let payload = OpBuiltPayload::new(ctx.payload_id(), info.total_fees, executed);
ctx.payload_id(),
sealed_block,
info.total_fees,
ctx.chain_spec.clone(),
ctx.config.attributes,
Some(executed),
);
if no_tx_pool { if no_tx_pool {
// if `no_tx_pool` is set only transactions from the payload attributes will be included // if `no_tx_pool` is set only transactions from the payload attributes will be included

View File

@ -1,29 +1,24 @@
//! Payload related types //! Payload related types
use alloy_eips::{ use alloy_eips::{
eip1559::BaseFeeParams, eip2718::Decodable2718, eip4844::BlobTransactionSidecar, eip1559::BaseFeeParams, eip2718::Decodable2718, eip4895::Withdrawals, eip7685::Requests,
eip4895::Withdrawals, eip7685::Requests,
}; };
use alloy_primitives::{keccak256, Address, Bytes, B256, B64, U256}; use alloy_primitives::{keccak256, Address, Bytes, B256, B64, U256};
use alloy_rlp::Encodable; use alloy_rlp::Encodable;
use alloy_rpc_types_engine::{ use alloy_rpc_types_engine::{
ExecutionPayloadEnvelopeV2, ExecutionPayloadFieldV2, ExecutionPayloadV1, ExecutionPayloadV3, BlobsBundleV1, ExecutionPayloadEnvelopeV2, ExecutionPayloadFieldV2, ExecutionPayloadV1,
PayloadId, ExecutionPayloadV3, PayloadId,
}; };
use op_alloy_consensus::{encode_holocene_extra_data, EIP1559ParamError}; use op_alloy_consensus::{encode_holocene_extra_data, EIP1559ParamError};
/// Re-export for use in downstream arguments. /// Re-export for use in downstream arguments.
pub use op_alloy_rpc_types_engine::OpPayloadAttributes; pub use op_alloy_rpc_types_engine::OpPayloadAttributes;
use op_alloy_rpc_types_engine::{OpExecutionPayloadEnvelopeV3, OpExecutionPayloadEnvelopeV4}; use op_alloy_rpc_types_engine::{OpExecutionPayloadEnvelopeV3, OpExecutionPayloadEnvelopeV4};
use reth_chain_state::ExecutedBlockWithTrieUpdates; use reth_chain_state::ExecutedBlockWithTrieUpdates;
use reth_chainspec::EthereumHardforks;
use reth_optimism_chainspec::OpChainSpec;
use reth_optimism_primitives::{OpBlock, OpPrimitives, OpTransactionSigned}; use reth_optimism_primitives::{OpBlock, OpPrimitives, OpTransactionSigned};
use reth_payload_builder::EthPayloadBuilderAttributes; use reth_payload_builder::EthPayloadBuilderAttributes;
use reth_payload_primitives::{BuiltPayload, PayloadBuilderAttributes}; use reth_payload_primitives::{BuiltPayload, PayloadBuilderAttributes};
use reth_primitives::{transaction::WithEncoded, SealedBlock}; use reth_primitives::{transaction::WithEncoded, SealedBlock};
use std::sync::Arc;
/// Optimism Payload Builder Attributes /// Optimism Payload Builder Attributes
#[derive(Debug, Clone, PartialEq, Eq, Default)] #[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct OpPayloadBuilderAttributes { pub struct OpPayloadBuilderAttributes {
@ -135,19 +130,10 @@ impl PayloadBuilderAttributes for OpPayloadBuilderAttributes {
pub struct OpBuiltPayload { pub struct OpBuiltPayload {
/// Identifier of the payload /// Identifier of the payload
pub(crate) id: PayloadId, pub(crate) id: PayloadId,
/// The built block
pub(crate) block: Arc<SealedBlock<OpBlock>>,
/// Block execution data for the payload, if any. /// Block execution data for the payload, if any.
pub(crate) executed_block: Option<ExecutedBlockWithTrieUpdates<OpPrimitives>>, pub(crate) block: ExecutedBlockWithTrieUpdates<OpPrimitives>,
/// The fees of the block /// The fees of the block
pub(crate) fees: U256, pub(crate) fees: U256,
/// The blobs, proofs, and commitments in the block. If the block is pre-cancun, this will be
/// empty.
pub(crate) sidecars: Vec<BlobTransactionSidecar>,
/// The rollup's chainspec.
pub(crate) chain_spec: Arc<OpChainSpec>,
/// The payload attributes.
pub(crate) attributes: OpPayloadBuilderAttributes,
} }
// === impl BuiltPayload === // === impl BuiltPayload ===
@ -156,13 +142,10 @@ impl OpBuiltPayload {
/// Initializes the payload with the given initial block. /// Initializes the payload with the given initial block.
pub const fn new( pub const fn new(
id: PayloadId, id: PayloadId,
block: Arc<SealedBlock<OpBlock>>,
fees: U256, fees: U256,
chain_spec: Arc<OpChainSpec>, block: ExecutedBlockWithTrieUpdates<OpPrimitives>,
attributes: OpPayloadBuilderAttributes,
executed_block: Option<ExecutedBlockWithTrieUpdates<OpPrimitives>>,
) -> Self { ) -> Self {
Self { id, block, executed_block, fees, sidecars: Vec::new(), chain_spec, attributes } Self { id, block, fees }
} }
/// Returns the identifier of the payload. /// Returns the identifier of the payload.
@ -172,25 +155,20 @@ impl OpBuiltPayload {
/// Returns the built block(sealed) /// Returns the built block(sealed)
pub fn block(&self) -> &SealedBlock<OpBlock> { pub fn block(&self) -> &SealedBlock<OpBlock> {
&self.block self.block.sealed_block()
} }
/// Fees of the block /// Fees of the block
pub const fn fees(&self) -> U256 { pub const fn fees(&self) -> U256 {
self.fees self.fees
} }
/// Adds sidecars to the payload.
pub fn extend_sidecars(&mut self, sidecars: Vec<BlobTransactionSidecar>) {
self.sidecars.extend(sidecars)
}
} }
impl BuiltPayload for OpBuiltPayload { impl BuiltPayload for OpBuiltPayload {
type Primitives = OpPrimitives; type Primitives = OpPrimitives;
fn block(&self) -> &SealedBlock<OpBlock> { fn block(&self) -> &SealedBlock<OpBlock> {
&self.block self.block()
} }
fn fees(&self) -> U256 { fn fees(&self) -> U256 {
@ -198,7 +176,7 @@ impl BuiltPayload for OpBuiltPayload {
} }
fn executed_block(&self) -> Option<ExecutedBlockWithTrieUpdates<OpPrimitives>> { fn executed_block(&self) -> Option<ExecutedBlockWithTrieUpdates<OpPrimitives>> {
self.executed_block.clone() Some(self.block.clone())
} }
fn requests(&self) -> Option<Requests> { fn requests(&self) -> Option<Requests> {
@ -218,7 +196,7 @@ impl BuiltPayload for &OpBuiltPayload {
} }
fn executed_block(&self) -> Option<ExecutedBlockWithTrieUpdates<OpPrimitives>> { fn executed_block(&self) -> Option<ExecutedBlockWithTrieUpdates<OpPrimitives>> {
self.executed_block.clone() Some(self.block.clone())
} }
fn requests(&self) -> Option<Requests> { fn requests(&self) -> Option<Requests> {
@ -231,7 +209,7 @@ impl From<OpBuiltPayload> for ExecutionPayloadV1 {
fn from(value: OpBuiltPayload) -> Self { fn from(value: OpBuiltPayload) -> Self {
Self::from_block_unchecked( Self::from_block_unchecked(
value.block().hash(), value.block().hash(),
&Arc::unwrap_or_clone(value.block).into_block(), &value.block.into_sealed_block().into_block(),
) )
} }
} }
@ -241,11 +219,12 @@ impl From<OpBuiltPayload> for ExecutionPayloadEnvelopeV2 {
fn from(value: OpBuiltPayload) -> Self { fn from(value: OpBuiltPayload) -> Self {
let OpBuiltPayload { block, fees, .. } = value; let OpBuiltPayload { block, fees, .. } = value;
let block = block.into_sealed_block();
Self { Self {
block_value: fees, block_value: fees,
execution_payload: ExecutionPayloadFieldV2::from_block_unchecked( execution_payload: ExecutionPayloadFieldV2::from_block_unchecked(
block.hash(), block.hash(),
&Arc::unwrap_or_clone(block).into_block(), &block.into_block(),
), ),
} }
} }
@ -253,18 +232,15 @@ impl From<OpBuiltPayload> for ExecutionPayloadEnvelopeV2 {
impl From<OpBuiltPayload> for OpExecutionPayloadEnvelopeV3 { impl From<OpBuiltPayload> for OpExecutionPayloadEnvelopeV3 {
fn from(value: OpBuiltPayload) -> Self { fn from(value: OpBuiltPayload) -> Self {
let OpBuiltPayload { block, fees, sidecars, chain_spec, attributes, .. } = value; let OpBuiltPayload { block, fees, .. } = value;
let parent_beacon_block_root = let parent_beacon_block_root =
if chain_spec.is_cancun_active_at_timestamp(attributes.timestamp()) { block.sealed_block().parent_beacon_block_root.unwrap_or_default();
attributes.parent_beacon_block_root().unwrap_or(B256::ZERO)
} else {
B256::ZERO
};
Self { Self {
execution_payload: ExecutionPayloadV3::from_block_unchecked( execution_payload: ExecutionPayloadV3::from_block_unchecked(
block.hash(), block.sealed_block().hash(),
&Arc::unwrap_or_clone(block).into_block(), &block.into_sealed_block().into_block(),
), ),
block_value: fees, block_value: fees,
// From the engine API spec: // From the engine API spec:
@ -276,25 +252,23 @@ impl From<OpBuiltPayload> for OpExecutionPayloadEnvelopeV3 {
// Spec: // Spec:
// <https://github.com/ethereum/execution-apis/blob/fe8e13c288c592ec154ce25c534e26cb7ce0530d/src/engine/cancun.md#specification-2> // <https://github.com/ethereum/execution-apis/blob/fe8e13c288c592ec154ce25c534e26cb7ce0530d/src/engine/cancun.md#specification-2>
should_override_builder: false, should_override_builder: false,
blobs_bundle: sidecars.into_iter().collect::<Vec<_>>().into(), // No blobs for OP.
blobs_bundle: BlobsBundleV1 { blobs: vec![], commitments: vec![], proofs: vec![] },
parent_beacon_block_root, parent_beacon_block_root,
} }
} }
} }
impl From<OpBuiltPayload> for OpExecutionPayloadEnvelopeV4 { impl From<OpBuiltPayload> for OpExecutionPayloadEnvelopeV4 {
fn from(value: OpBuiltPayload) -> Self { fn from(value: OpBuiltPayload) -> Self {
let OpBuiltPayload { block, fees, sidecars, chain_spec, attributes, .. } = value; let OpBuiltPayload { block, fees, .. } = value;
let parent_beacon_block_root = let parent_beacon_block_root =
if chain_spec.is_cancun_active_at_timestamp(attributes.timestamp()) { block.sealed_block().parent_beacon_block_root.unwrap_or_default();
attributes.parent_beacon_block_root().unwrap_or(B256::ZERO)
} else {
B256::ZERO
};
Self { Self {
execution_payload: ExecutionPayloadV3::from_block_unchecked( execution_payload: ExecutionPayloadV3::from_block_unchecked(
block.hash(), block.sealed_block().hash(),
&Arc::unwrap_or_clone(block).into_block(), &block.into_sealed_block().into_block(),
), ),
block_value: fees, block_value: fees,
// From the engine API spec: // From the engine API spec:
@ -306,7 +280,8 @@ impl From<OpBuiltPayload> for OpExecutionPayloadEnvelopeV4 {
// Spec: // Spec:
// <https://github.com/ethereum/execution-apis/blob/fe8e13c288c592ec154ce25c534e26cb7ce0530d/src/engine/cancun.md#specification-2> // <https://github.com/ethereum/execution-apis/blob/fe8e13c288c592ec154ce25c534e26cb7ce0530d/src/engine/cancun.md#specification-2>
should_override_builder: false, should_override_builder: false,
blobs_bundle: sidecars.into_iter().collect::<Vec<_>>().into(), // No blobs for OP.
blobs_bundle: BlobsBundleV1 { blobs: vec![], commitments: vec![], proofs: vec![] },
parent_beacon_block_root, parent_beacon_block_root,
execution_requests: vec![], execution_requests: vec![],
} }