Segmenting cfg and block env impl from PayloadBuilderAttributes (#6623)

This commit is contained in:
Aditya Pandey
2024-02-16 22:38:24 +05:30
committed by GitHub
parent 9590faefbb
commit 683b252f83
15 changed files with 169 additions and 85 deletions

View File

@ -164,7 +164,6 @@ optimism = [
"reth-node-ethereum/optimism",
"dep:reth-node-optimism",
"reth-node-core/optimism",
"reth-node-api/optimism",
]
# no-op feature flag for switching between the `optimism` and default functionality in CI matrices

View File

@ -68,5 +68,4 @@ optimism = [
"reth-rpc-types/optimism",
"reth-payload-builder/optimism",
"reth-blockchain-tree/optimism",
"reth-node-api/optimism",
]

View File

@ -19,6 +19,3 @@ thiserror.workspace = true
# io
serde.workspace = true
[features]
optimism = []

View File

@ -1,7 +1,6 @@
use crate::{validate_version_specific_fields, AttributesValidationError, EngineApiMessageVersion};
use reth_primitives::{
revm::config::revm_spec_by_timestamp_after_merge,
revm_primitives::{BlobExcessGasAndPrice, BlockEnv, CfgEnv, CfgEnvWithHandlerCfg, SpecId},
revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg},
Address, ChainSpec, Header, SealedBlock, Withdrawals, B256, U256,
};
use reth_rpc_types::{
@ -87,64 +86,7 @@ pub trait PayloadBuilderAttributes: Send + Sync + std::fmt::Debug {
&self,
chain_spec: &ChainSpec,
parent: &Header,
) -> (CfgEnvWithHandlerCfg, BlockEnv) {
// configure evm env based on parent block
let mut cfg = CfgEnv::default();
cfg.chain_id = chain_spec.chain().id();
// ensure we're not missing any timestamp based hardforks
let spec_id = revm_spec_by_timestamp_after_merge(chain_spec, self.timestamp());
// if the parent block did not have excess blob gas (i.e. it was pre-cancun), but it is
// cancun now, we need to set the excess blob gas to the default value
let blob_excess_gas_and_price = parent
.next_block_excess_blob_gas()
.or_else(|| {
if spec_id == SpecId::CANCUN {
// default excess blob gas is zero
Some(0)
} else {
None
}
})
.map(BlobExcessGasAndPrice::new);
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(
parent
.next_block_base_fee(chain_spec.base_fee_params(self.timestamp()))
.unwrap_or_default(),
),
// calculate excess gas based on parent block's blob gas usage
blob_excess_gas_and_price,
};
let cfg_with_handler_cfg;
#[cfg(feature = "optimism")]
{
cfg_with_handler_cfg = CfgEnvWithHandlerCfg {
cfg_env: cfg,
handler_cfg: revm_primitives::HandlerCfg {
spec_id,
is_optimism: chain_spec.is_optimism(),
},
};
}
#[cfg(not(feature = "optimism"))]
{
cfg_with_handler_cfg = CfgEnvWithHandlerCfg::new(cfg, spec_id);
}
(cfg_with_handler_cfg, block_env)
}
) -> (CfgEnvWithHandlerCfg, BlockEnv);
}
/// The execution payload attribute type the CL node emits via the engine API.

View File

@ -122,7 +122,6 @@ optimism = [
"reth-blockchain-tree/optimism",
"reth-beacon-consensus/optimism",
"reth-optimism-payload-builder/optimism",
"reth-node-api/optimism",
]
jemalloc = ["dep:jemalloc-ctl"]

View File

@ -35,7 +35,6 @@ reth-db.workspace = true
[features]
optimism = [
"reth-node-api/optimism",
"reth-network/optimism",
"reth-primitives/optimism",
"reth-transaction-pool/optimism",
@ -43,5 +42,5 @@ optimism = [
"reth-provider/optimism",
"reth-rpc-types-compat/optimism",
"reth-rpc/optimism",
"reth-optimism-payload-builder/optimism"
"reth-optimism-payload-builder/optimism",
]

View File

@ -55,5 +55,4 @@ optimism = [
"reth-rpc-types-compat/optimism",
"reth-interfaces/optimism",
"reth-transaction-pool/optimism",
"reth-node-api/optimism",
]

View File

@ -1,7 +1,11 @@
use crate::EthPayloadBuilderAttributes;
use alloy_rlp::{Encodable, Error as DecodeError};
use reth_node_api::PayloadBuilderAttributes;
use reth_primitives::{Address, TransactionSigned, Withdrawals, B256};
use reth_primitives::{
revm::config::revm_spec_by_timestamp_after_merge,
revm_primitives::{BlobExcessGasAndPrice, BlockEnv, CfgEnv, CfgEnvWithHandlerCfg, SpecId},
Address, ChainSpec, Header, TransactionSigned, Withdrawals, B256, U256,
};
use reth_rpc_types::engine::{OptimismPayloadAttributes, PayloadId};
use reth_rpc_types_compat::engine::payload::convert_standalone_withdraw_to_withdrawal;
@ -88,6 +92,64 @@ impl PayloadBuilderAttributes for OptimismPayloadBuilderAttributes {
fn withdrawals(&self) -> &Withdrawals {
&self.payload_attributes.withdrawals
}
fn cfg_and_block_env(
&self,
chain_spec: &ChainSpec,
parent: &Header,
) -> (CfgEnvWithHandlerCfg, BlockEnv) {
// configure evm env based on parent block
let mut cfg = CfgEnv::default();
cfg.chain_id = chain_spec.chain().id();
// ensure we're not missing any timestamp based hardforks
let spec_id = revm_spec_by_timestamp_after_merge(chain_spec, self.timestamp());
// if the parent block did not have excess blob gas (i.e. it was pre-cancun), but it is
// cancun now, we need to set the excess blob gas to the default value
let blob_excess_gas_and_price = parent
.next_block_excess_blob_gas()
.or_else(|| {
if spec_id == SpecId::CANCUN {
// default excess blob gas is zero
Some(0)
} else {
None
}
})
.map(BlobExcessGasAndPrice::new);
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(
parent
.next_block_base_fee(chain_spec.base_fee_params(self.timestamp()))
.unwrap_or_default(),
),
// calculate excess gas based on parent block's blob gas usage
blob_excess_gas_and_price,
};
let cfg_with_handler_cfg;
{
cfg_with_handler_cfg = CfgEnvWithHandlerCfg {
cfg_env: cfg,
handler_cfg: revm_primitives::HandlerCfg {
spec_id,
#[cfg(feature = "optimism")]
is_optimism: chain_spec.is_optimism(),
},
};
}
(cfg_with_handler_cfg, block_env)
}
}
/// Generates the payload id for the configured payload from the [OptimismPayloadAttributes].

View File

@ -2,7 +2,10 @@
use alloy_rlp::Encodable;
use reth_node_api::{BuiltPayload, PayloadBuilderAttributes};
use reth_primitives::{Address, BlobTransactionSidecar, SealedBlock, Withdrawals, B256, U256};
use reth_primitives::{
revm::config::revm_spec_by_timestamp_after_merge, Address, BlobTransactionSidecar, ChainSpec,
Header, SealedBlock, Withdrawals, B256, U256,
};
use reth_rpc_types::engine::{
ExecutionPayloadEnvelopeV2, ExecutionPayloadEnvelopeV3, ExecutionPayloadV1, PayloadAttributes,
PayloadId,
@ -11,6 +14,7 @@ use reth_rpc_types_compat::engine::payload::{
block_to_payload_v3, convert_block_to_payload_field_v2,
convert_standalone_withdraw_to_withdrawal, try_block_to_payload_v1,
};
use revm_primitives::{BlobExcessGasAndPrice, BlockEnv, CfgEnv, CfgEnvWithHandlerCfg, SpecId};
use std::convert::Infallible;
/// Contains the built payload.
@ -231,6 +235,52 @@ impl PayloadBuilderAttributes for EthPayloadBuilderAttributes {
fn withdrawals(&self) -> &Withdrawals {
&self.withdrawals
}
fn cfg_and_block_env(
&self,
chain_spec: &ChainSpec,
parent: &Header,
) -> (CfgEnvWithHandlerCfg, BlockEnv) {
// configure evm env based on parent block
let mut cfg = CfgEnv::default();
cfg.chain_id = chain_spec.chain().id();
// ensure we're not missing any timestamp based hardforks
let spec_id = revm_spec_by_timestamp_after_merge(chain_spec, self.timestamp());
// if the parent block did not have excess blob gas (i.e. it was pre-cancun), but it is
// cancun now, we need to set the excess blob gas to the default value
let blob_excess_gas_and_price = parent
.next_block_excess_blob_gas()
.or_else(|| {
if spec_id == SpecId::CANCUN {
// default excess blob gas is zero
Some(0)
} else {
None
}
})
.map(BlobExcessGasAndPrice::new);
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(
parent
.next_block_base_fee(chain_spec.base_fee_params(self.timestamp()))
.unwrap_or_default(),
),
// calculate excess gas based on parent block's blob gas usage
blob_excess_gas_and_price,
};
(CfgEnvWithHandlerCfg::new(cfg, spec_id), block_env)
}
}
/// Generates the payload id for the configured payload from the [PayloadAttributes].

View File

@ -37,6 +37,5 @@ optimism = [
"reth-primitives/optimism",
"reth-consensus-common/optimism",
"reth-interfaces/optimism",
"reth-node-api/optimism",
]
js-tracer = ["revm-inspectors/js-tracer"]

View File

@ -24,4 +24,3 @@ serde_json.workspace = true
[features]
client = ["jsonrpsee/client", "jsonrpsee/async-client"]
optimism = ["reth-node-api/optimism"]

View File

@ -49,8 +49,4 @@ reth-payload-builder = { workspace = true, features = ["test-utils"] }
assert_matches.workspace = true
[features]
optimism = [
"reth-primitives/optimism",
"reth-rpc-types/optimism",
"reth-node-api/optimism",
]
optimism = ["reth-primitives/optimism", "reth-rpc-types/optimism"]

View File

@ -101,5 +101,4 @@ optimism = [
"reth-network/optimism",
"reth-provider/optimism",
"reth-transaction-pool/optimism",
"reth-node-api/optimism",
]

View File

@ -61,8 +61,4 @@ rand.workspace = true
[features]
test-utils = ["alloy-rlp", "reth-db/test-utils"]
optimism = [
"reth-primitives/optimism",
"reth-interfaces/optimism",
"reth-node-api/optimism"
]
optimism = ["reth-primitives/optimism", "reth-interfaces/optimism"]

View File

@ -24,7 +24,11 @@ use reth_node_api::{
};
use reth_node_core::{args::RpcServerArgs, node_config::NodeConfig};
use reth_payload_builder::{EthBuiltPayload, EthPayloadBuilderAttributes};
use reth_primitives::{Address, ChainSpec, Genesis, Withdrawals, B256, U256};
use reth_primitives::{
revm::config::revm_spec_by_timestamp_after_merge,
revm_primitives::{BlobExcessGasAndPrice, BlockEnv, CfgEnv, CfgEnvWithHandlerCfg, SpecId},
Address, ChainSpec, Genesis, Header, Withdrawals, B256, U256,
};
use reth_rpc_api::{EngineApiClient, EthApiClient};
use reth_rpc_types::{
engine::{ForkchoiceState, PayloadAttributes as EthPayloadAttributes, PayloadId},
@ -119,6 +123,51 @@ impl PayloadBuilderAttributes for CustomPayloadBuilderAttributes {
fn withdrawals(&self) -> &Withdrawals {
&self.0.withdrawals
}
fn cfg_and_block_env(
&self,
chain_spec: &ChainSpec,
parent: &Header,
) -> (CfgEnvWithHandlerCfg, BlockEnv) {
// configure evm env based on parent block
let mut cfg = CfgEnv::default();
cfg.chain_id = chain_spec.chain().id();
// ensure we're not missing any timestamp based hardforks
let spec_id = revm_spec_by_timestamp_after_merge(chain_spec, self.timestamp());
// if the parent block did not have excess blob gas (i.e. it was pre-cancun), but it is
// cancun now, we need to set the excess blob gas to the default value
let blob_excess_gas_and_price = parent
.next_block_excess_blob_gas()
.or_else(|| {
if spec_id == SpecId::CANCUN {
// default excess blob gas is zero
Some(0)
} else {
None
}
})
.map(BlobExcessGasAndPrice::new);
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(
parent
.next_block_base_fee(chain_spec.base_fee_params(self.timestamp()))
.unwrap_or_default(),
),
// calculate excess gas based on parent block's blob gas usage
blob_excess_gas_and_price,
};
(CfgEnvWithHandlerCfg::new(cfg, spec_id), block_env)
}
}
/// Custom engine types - uses a custom payload attributes RPC type, but uses the default