diff --git a/Cargo.lock b/Cargo.lock index 62c184c41..cb6dd8315 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4495,7 +4495,6 @@ version = "0.1.0" dependencies = [ "futures-core", "futures-util", - "reth-consensus-common", "reth-payload-builder", "reth-primitives", "reth-provider", @@ -4945,8 +4944,10 @@ dependencies = [ "futures-core", "futures-util", "parking_lot 0.12.1", + "reth-consensus-common", "reth-interfaces", "reth-primitives", + "reth-revm-primitives", "reth-rlp", "reth-rpc-types", "revm-primitives", diff --git a/crates/payload/basic/Cargo.toml b/crates/payload/basic/Cargo.toml index 9e9b93bc7..f86bc5fe1 100644 --- a/crates/payload/basic/Cargo.toml +++ b/crates/payload/basic/Cargo.toml @@ -10,7 +10,6 @@ description = "A basic payload builder for reth that uses the txpool API to buil [dependencies] ## reth reth-primitives = { path = "../../primitives" } -reth-consensus-common = { path = "../../consensus/common" } reth-revm = { path = "../../revm" } reth-transaction-pool = { path = "../../transaction-pool" } reth-rlp = { path = "../../rlp" } diff --git a/crates/payload/basic/src/lib.rs b/crates/payload/basic/src/lib.rs index 037865637..306aa37ae 100644 --- a/crates/payload/basic/src/lib.rs +++ b/crates/payload/basic/src/lib.rs @@ -11,7 +11,6 @@ use futures_core::{ready, Stream}; use futures_util::FutureExt; -use reth_consensus_common::validation::calculate_next_block_base_fee; use reth_payload_builder::{ error::PayloadBuilderError, BuiltPayload, PayloadBuilderAttributes, PayloadJob, PayloadJobGenerator, @@ -122,27 +121,8 @@ where .ok_or_else(|| PayloadBuilderError::MissingParentBlock(attributes.parent))?; // configure evm env based on parent block - let initialized_cfg = CfgEnv { - chain_id: U256::from(self.chain_spec.chain().id()), - // ensure we're not missing any timestamp based hardforks - spec_id: revm_spec_by_timestamp_after_merge(&self.chain_spec, attributes.timestamp), - ..Default::default() - }; - - let initialized_block_env = BlockEnv { - number: U256::from(parent_block.number + 1), - coinbase: attributes.suggested_fee_recipient, - timestamp: U256::from(attributes.timestamp), - difficulty: U256::ZERO, - prevrandao: Some(attributes.prev_randao), - gas_limit: U256::from(parent_block.gas_limit), - // calculate basefee based on parent block's gas usage - basefee: U256::from(calculate_next_block_base_fee( - parent_block.gas_used, - parent_block.gas_limit, - parent_block.base_fee_per_gas.unwrap_or_default(), - )), - }; + let (initialized_cfg, initialized_block_env) = + attributes.cfg_and_block_env(&self.chain_spec, &parent_block); let config = PayloadConfig { initialized_block_env, diff --git a/crates/payload/builder/Cargo.toml b/crates/payload/builder/Cargo.toml index e7fbb488e..4e94b8e08 100644 --- a/crates/payload/builder/Cargo.toml +++ b/crates/payload/builder/Cargo.toml @@ -13,6 +13,8 @@ reth-primitives = { path = "../../primitives" } reth-rpc-types = { path = "../../rpc/rpc-types" } reth-rlp = { path = "../../rlp" } reth-interfaces = { path = "../../interfaces" } +reth-consensus-common = { path = "../../consensus/common" } +reth-revm-primitives = { path = "../../revm/revm-primitives" } ## ethereum revm-primitives = "1" diff --git a/crates/payload/builder/src/payload.rs b/crates/payload/builder/src/payload.rs index 2370d092a..13c3aff8e 100644 --- a/crates/payload/builder/src/payload.rs +++ b/crates/payload/builder/src/payload.rs @@ -1,8 +1,11 @@ //! Contains types required for building a payload. -use reth_primitives::{Address, SealedBlock, Withdrawal, H256, U256}; +use reth_consensus_common::validation::calculate_next_block_base_fee; +use reth_primitives::{Address, ChainSpec, Header, SealedBlock, Withdrawal, H256, U256}; +use reth_revm_primitives::config::revm_spec_by_timestamp_after_merge; use reth_rlp::Encodable; use reth_rpc_types::engine::{PayloadAttributes, PayloadId}; +use revm_primitives::{BlockEnv, CfgEnv}; /// Contains the built payload. /// @@ -78,6 +81,42 @@ impl PayloadBuilderAttributes { } } + /// Returns the configured [CfgEnv] and [BlockEnv] for the targeted payload (that has the + /// `parent` as its parent). + /// + /// The `chain_spec` is used to determine the correct chain id and hardfork for the payload + /// based on its timestamp. + /// + /// Block related settings are derived from the `parent` block and the configured attributes. + /// + /// NOTE: This is only intended for beacon consensus (after merge). + pub fn cfg_and_block_env(&self, chain_spec: &ChainSpec, parent: &Header) -> (CfgEnv, BlockEnv) { + // configure evm env based on parent block + let cfg = CfgEnv { + chain_id: U256::from(chain_spec.chain().id()), + // ensure we're not missing any timestamp based hardforks + spec_id: revm_spec_by_timestamp_after_merge(chain_spec, self.timestamp), + ..Default::default() + }; + + let block_env = BlockEnv { + number: U256::from(parent.number + 1), + coinbase: self.suggested_fee_recipient, + timestamp: U256::from(self.timestamp), + difficulty: U256::ZERO, + prevrandao: Some(self.prev_randao), + gas_limit: U256::from(parent.gas_limit), + // calculate basefee based on parent block's gas usage + basefee: U256::from(calculate_next_block_base_fee( + parent.gas_used, + parent.gas_limit, + parent.base_fee_per_gas.unwrap_or_default(), + )), + }; + + (cfg, block_env) + } + /// Returns the identifier of the payload. pub fn payload_id(&self) -> PayloadId { self.id