From 0c3ccccba9fbee4e7c2e9ea46a50e9785bac3ffa Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Sun, 2 Feb 2025 20:32:16 +0400 Subject: [PATCH] chore: simplify `OpBuiltPayload` (#14152) --- crates/chain-state/src/in_memory.rs | 6 ++ crates/optimism/payload/src/builder.rs | 9 +-- crates/optimism/payload/src/payload.rs | 79 +++++++++----------------- 3 files changed, 34 insertions(+), 60 deletions(-) diff --git a/crates/chain-state/src/in_memory.rs b/crates/chain-state/src/in_memory.rs index 44b0cfaedd..ae32d65f85 100644 --- a/crates/chain-state/src/in_memory.rs +++ b/crates/chain-state/src/in_memory.rs @@ -847,6 +847,12 @@ impl ExecutedBlockWithTrieUpdates { pub fn trie_updates(&self) -> &TrieUpdates { &self.trie } + + /// Converts the value into [`SealedBlock`]. + pub fn into_sealed_block(self) -> SealedBlock { + let block = Arc::unwrap_or_clone(self.block.recovered_block); + block.into_sealed_block() + } } /// Non-empty chain of blocks. diff --git a/crates/optimism/payload/src/builder.rs b/crates/optimism/payload/src/builder.rs index 4d6868c879..4a447bdfc5 100644 --- a/crates/optimism/payload/src/builder.rs +++ b/crates/optimism/payload/src/builder.rs @@ -449,14 +449,7 @@ where let no_tx_pool = ctx.attributes().no_tx_pool; - let payload = OpBuiltPayload::new( - ctx.payload_id(), - sealed_block, - info.total_fees, - ctx.chain_spec.clone(), - ctx.config.attributes, - Some(executed), - ); + let payload = OpBuiltPayload::new(ctx.payload_id(), info.total_fees, executed); if no_tx_pool { // if `no_tx_pool` is set only transactions from the payload attributes will be included diff --git a/crates/optimism/payload/src/payload.rs b/crates/optimism/payload/src/payload.rs index 0cfb11d05e..0088e39504 100644 --- a/crates/optimism/payload/src/payload.rs +++ b/crates/optimism/payload/src/payload.rs @@ -1,29 +1,24 @@ //! Payload related types use alloy_eips::{ - eip1559::BaseFeeParams, eip2718::Decodable2718, eip4844::BlobTransactionSidecar, - eip4895::Withdrawals, eip7685::Requests, + eip1559::BaseFeeParams, eip2718::Decodable2718, eip4895::Withdrawals, eip7685::Requests, }; use alloy_primitives::{keccak256, Address, Bytes, B256, B64, U256}; use alloy_rlp::Encodable; use alloy_rpc_types_engine::{ - ExecutionPayloadEnvelopeV2, ExecutionPayloadFieldV2, ExecutionPayloadV1, ExecutionPayloadV3, - PayloadId, + BlobsBundleV1, ExecutionPayloadEnvelopeV2, ExecutionPayloadFieldV2, ExecutionPayloadV1, + ExecutionPayloadV3, PayloadId, }; use op_alloy_consensus::{encode_holocene_extra_data, EIP1559ParamError}; /// Re-export for use in downstream arguments. pub use op_alloy_rpc_types_engine::OpPayloadAttributes; use op_alloy_rpc_types_engine::{OpExecutionPayloadEnvelopeV3, OpExecutionPayloadEnvelopeV4}; use reth_chain_state::ExecutedBlockWithTrieUpdates; -use reth_chainspec::EthereumHardforks; -use reth_optimism_chainspec::OpChainSpec; use reth_optimism_primitives::{OpBlock, OpPrimitives, OpTransactionSigned}; use reth_payload_builder::EthPayloadBuilderAttributes; use reth_payload_primitives::{BuiltPayload, PayloadBuilderAttributes}; use reth_primitives::{transaction::WithEncoded, SealedBlock}; -use std::sync::Arc; - /// Optimism Payload Builder Attributes #[derive(Debug, Clone, PartialEq, Eq, Default)] pub struct OpPayloadBuilderAttributes { @@ -135,19 +130,10 @@ impl PayloadBuilderAttributes for OpPayloadBuilderAttributes { pub struct OpBuiltPayload { /// Identifier of the payload pub(crate) id: PayloadId, - /// The built block - pub(crate) block: Arc>, /// Block execution data for the payload, if any. - pub(crate) executed_block: Option>, + pub(crate) block: ExecutedBlockWithTrieUpdates, /// The fees of the block 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, - /// The rollup's chainspec. - pub(crate) chain_spec: Arc, - /// The payload attributes. - pub(crate) attributes: OpPayloadBuilderAttributes, } // === impl BuiltPayload === @@ -156,13 +142,10 @@ impl OpBuiltPayload { /// Initializes the payload with the given initial block. pub const fn new( id: PayloadId, - block: Arc>, fees: U256, - chain_spec: Arc, - attributes: OpPayloadBuilderAttributes, - executed_block: Option>, + block: ExecutedBlockWithTrieUpdates, ) -> Self { - Self { id, block, executed_block, fees, sidecars: Vec::new(), chain_spec, attributes } + Self { id, block, fees } } /// Returns the identifier of the payload. @@ -172,25 +155,20 @@ impl OpBuiltPayload { /// Returns the built block(sealed) pub fn block(&self) -> &SealedBlock { - &self.block + self.block.sealed_block() } /// Fees of the block pub const fn fees(&self) -> U256 { self.fees } - - /// Adds sidecars to the payload. - pub fn extend_sidecars(&mut self, sidecars: Vec) { - self.sidecars.extend(sidecars) - } } impl BuiltPayload for OpBuiltPayload { type Primitives = OpPrimitives; fn block(&self) -> &SealedBlock { - &self.block + self.block() } fn fees(&self) -> U256 { @@ -198,7 +176,7 @@ impl BuiltPayload for OpBuiltPayload { } fn executed_block(&self) -> Option> { - self.executed_block.clone() + Some(self.block.clone()) } fn requests(&self) -> Option { @@ -218,7 +196,7 @@ impl BuiltPayload for &OpBuiltPayload { } fn executed_block(&self) -> Option> { - self.executed_block.clone() + Some(self.block.clone()) } fn requests(&self) -> Option { @@ -231,7 +209,7 @@ impl From for ExecutionPayloadV1 { fn from(value: OpBuiltPayload) -> Self { Self::from_block_unchecked( value.block().hash(), - &Arc::unwrap_or_clone(value.block).into_block(), + &value.block.into_sealed_block().into_block(), ) } } @@ -241,11 +219,12 @@ impl From for ExecutionPayloadEnvelopeV2 { fn from(value: OpBuiltPayload) -> Self { let OpBuiltPayload { block, fees, .. } = value; + let block = block.into_sealed_block(); Self { block_value: fees, execution_payload: ExecutionPayloadFieldV2::from_block_unchecked( block.hash(), - &Arc::unwrap_or_clone(block).into_block(), + &block.into_block(), ), } } @@ -253,18 +232,15 @@ impl From for ExecutionPayloadEnvelopeV2 { impl From for OpExecutionPayloadEnvelopeV3 { fn from(value: OpBuiltPayload) -> Self { - let OpBuiltPayload { block, fees, sidecars, chain_spec, attributes, .. } = value; + let OpBuiltPayload { block, fees, .. } = value; let parent_beacon_block_root = - if chain_spec.is_cancun_active_at_timestamp(attributes.timestamp()) { - attributes.parent_beacon_block_root().unwrap_or(B256::ZERO) - } else { - B256::ZERO - }; + block.sealed_block().parent_beacon_block_root.unwrap_or_default(); + Self { execution_payload: ExecutionPayloadV3::from_block_unchecked( - block.hash(), - &Arc::unwrap_or_clone(block).into_block(), + block.sealed_block().hash(), + &block.into_sealed_block().into_block(), ), block_value: fees, // From the engine API spec: @@ -276,25 +252,23 @@ impl From for OpExecutionPayloadEnvelopeV3 { // Spec: // should_override_builder: false, - blobs_bundle: sidecars.into_iter().collect::>().into(), + // No blobs for OP. + blobs_bundle: BlobsBundleV1 { blobs: vec![], commitments: vec![], proofs: vec![] }, parent_beacon_block_root, } } } impl From for OpExecutionPayloadEnvelopeV4 { fn from(value: OpBuiltPayload) -> Self { - let OpBuiltPayload { block, fees, sidecars, chain_spec, attributes, .. } = value; + let OpBuiltPayload { block, fees, .. } = value; let parent_beacon_block_root = - if chain_spec.is_cancun_active_at_timestamp(attributes.timestamp()) { - attributes.parent_beacon_block_root().unwrap_or(B256::ZERO) - } else { - B256::ZERO - }; + block.sealed_block().parent_beacon_block_root.unwrap_or_default(); + Self { execution_payload: ExecutionPayloadV3::from_block_unchecked( - block.hash(), - &Arc::unwrap_or_clone(block).into_block(), + block.sealed_block().hash(), + &block.into_sealed_block().into_block(), ), block_value: fees, // From the engine API spec: @@ -306,7 +280,8 @@ impl From for OpExecutionPayloadEnvelopeV4 { // Spec: // should_override_builder: false, - blobs_bundle: sidecars.into_iter().collect::>().into(), + // No blobs for OP. + blobs_bundle: BlobsBundleV1 { blobs: vec![], commitments: vec![], proofs: vec![] }, parent_beacon_block_root, execution_requests: vec![], }