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(evm): turn associated ConfigureEvm fns into methods #9322

Merged
merged 3 commits into from
Jul 5, 2024
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
7 changes: 4 additions & 3 deletions crates/ethereum/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ where
DB::Error: Into<ProviderError> + std::fmt::Display,
{
// apply pre execution changes
apply_beacon_root_contract_call::<EvmConfig, _, _>(
apply_beacon_root_contract_call(
&self.evm_config,
&self.chain_spec,
block.timestamp,
block.number,
Expand Down Expand Up @@ -220,7 +221,7 @@ where

// Collect all EIP-7685 requests
let withdrawal_requests =
apply_withdrawal_requests_contract_call::<EvmConfig, _, _>(&mut evm)?;
apply_withdrawal_requests_contract_call(&self.evm_config, &mut evm)?;

[deposit_requests, withdrawal_requests].concat()
} else {
Expand Down Expand Up @@ -275,7 +276,7 @@ where
fn evm_env_for_block(&self, header: &Header, total_difficulty: U256) -> EnvWithHandlerCfg {
let mut cfg = CfgEnvWithHandlerCfg::new(Default::default(), Default::default());
let mut block_env = BlockEnv::default();
EvmConfig::fill_cfg_and_block_env(
self.executor.evm_config.fill_cfg_and_block_env(
&mut cfg,
&mut block_env,
self.chain_spec(),
Expand Down
4 changes: 3 additions & 1 deletion crates/ethereum/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct EthEvmConfig;

impl ConfigureEvmEnv for EthEvmConfig {
fn fill_cfg_env(
&self,
cfg_env: &mut CfgEnvWithHandlerCfg,
chain_spec: &ChainSpec,
header: &Header,
Expand Down Expand Up @@ -63,6 +64,7 @@ impl ConfigureEvmEnv for EthEvmConfig {
}

fn fill_tx_env_system_contract_call(
&self,
env: &mut Env,
caller: Address,
contract: Address,
Expand Down Expand Up @@ -132,7 +134,7 @@ mod tests {
let chain_spec = ChainSpec::default();
let total_difficulty = U256::ZERO;

EthEvmConfig::fill_cfg_and_block_env(
EthEvmConfig::default().fill_cfg_and_block_env(
&mut cfg_env,
&mut block_env,
&chain_spec,
Expand Down
8 changes: 5 additions & 3 deletions crates/ethereum/payload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ where
// apply eip-4788 pre block contract call
pre_block_beacon_root_contract_call(
&mut db,
self.evm_config.clone(),
&self.evm_config,
&chain_spec,
&initialized_cfg,
&initialized_block_env,
Expand Down Expand Up @@ -201,6 +201,7 @@ where
// We do not calculate the EIP-6110 deposit requests because there are no
// transactions in an empty payload.
let withdrawal_requests = post_block_withdrawal_requests_contract_call::<EvmConfig, _>(
&self.evm_config,
&mut db,
&initialized_cfg,
&initialized_block_env,
Expand Down Expand Up @@ -296,7 +297,7 @@ where
// apply eip-4788 pre block contract call
pre_block_beacon_root_contract_call(
&mut db,
evm_config.clone(),
&evm_config,
&chain_spec,
&initialized_cfg,
&initialized_block_env,
Expand Down Expand Up @@ -443,7 +444,8 @@ where
{
let deposit_requests = parse_deposits_from_receipts(&chain_spec, receipts.iter().flatten())
.map_err(|err| PayloadBuilderError::Internal(RethError::Execution(err.into())))?;
let withdrawal_requests = post_block_withdrawal_requests_contract_call::<EvmConfig, _>(
let withdrawal_requests = post_block_withdrawal_requests_contract_call(
&evm_config,
&mut db,
&initialized_cfg,
&initialized_block_env,
Expand Down
10 changes: 7 additions & 3 deletions crates/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static {

/// Fill transaction environment with a system contract call.
fn fill_tx_env_system_contract_call(
&self,
env: &mut Env,
caller: Address,
contract: Address,
Expand All @@ -130,6 +131,7 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static {

/// Fill [`CfgEnvWithHandlerCfg`] fields according to the chain spec and given header
fn fill_cfg_env(
&self,
cfg_env: &mut CfgEnvWithHandlerCfg,
chain_spec: &ChainSpec,
header: &Header,
Expand All @@ -145,11 +147,12 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static {
after_merge: bool,
) {
let coinbase = block_coinbase(chain_spec, header, after_merge);
Self::fill_block_env_with_coinbase(block_env, header, after_merge, coinbase);
self.fill_block_env_with_coinbase(block_env, header, after_merge, coinbase);
}

/// Fill block environment with coinbase.
fn fill_block_env_with_coinbase(
&self,
block_env: &mut BlockEnv,
header: &Header,
after_merge: bool,
Expand Down Expand Up @@ -177,15 +180,16 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static {
/// Convenience function to call both [`fill_cfg_env`](ConfigureEvmEnv::fill_cfg_env) and
/// [`ConfigureEvmEnv::fill_block_env`].
fn fill_cfg_and_block_env(
&self,
cfg: &mut CfgEnvWithHandlerCfg,
block_env: &mut BlockEnv,
chain_spec: &ChainSpec,
header: &Header,
total_difficulty: U256,
) {
Self::fill_cfg_env(cfg, chain_spec, header, total_difficulty);
self.fill_cfg_env(cfg, chain_spec, header, total_difficulty);
let after_merge = cfg.handler_cfg.spec_id >= SpecId::MERGE;
Self::fill_block_env_with_coinbase(
self.fill_block_env_with_coinbase(
block_env,
header,
after_merge,
Expand Down
14 changes: 9 additions & 5 deletions crates/evm/src/system_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use revm_primitives::{
#[allow(clippy::too_many_arguments)]
pub fn pre_block_beacon_root_contract_call<EvmConfig, DB>(
db: &mut DB,
_emv_config: EvmConfig,
evm_config: &EvmConfig,
chain_spec: &ChainSpec,
initialized_cfg: &CfgEnvWithHandlerCfg,
initialized_block_env: &BlockEnv,
Expand All @@ -48,7 +48,8 @@ where
.build();

// initialize a block from the env, because the pre block call needs the block itself
apply_beacon_root_contract_call::<EvmConfig, _, _>(
apply_beacon_root_contract_call(
evm_config,
chain_spec,
block_timestamp,
block_number,
Expand All @@ -66,6 +67,7 @@ where
/// [EIP-4788]: https://eips.ethereum.org/EIPS/eip-4788
#[inline]
pub fn apply_beacon_root_contract_call<EvmConfig, EXT, DB>(
evm_config: &EvmConfig,
chain_spec: &ChainSpec,
block_timestamp: u64,
block_number: u64,
Expand Down Expand Up @@ -100,7 +102,7 @@ where
let previous_env = Box::new(evm.context.env().clone());

// modify env for pre block call
EvmConfig::fill_tx_env_system_contract_call(
evm_config.fill_tx_env_system_contract_call(
&mut evm.context.evm.env,
alloy_eips::eip4788::SYSTEM_ADDRESS,
BEACON_ROOTS_ADDRESS,
Expand Down Expand Up @@ -138,6 +140,7 @@ where
/// This uses [`apply_withdrawal_requests_contract_call`] to ultimately calculate the
/// [requests](Request).
pub fn post_block_withdrawal_requests_contract_call<EvmConfig, DB>(
evm_config: &EvmConfig,
db: &mut DB,
initialized_cfg: &CfgEnvWithHandlerCfg,
initialized_block_env: &BlockEnv,
Expand All @@ -158,7 +161,7 @@ where
.build();

// initialize a block from the env, because the post block call needs the block itself
apply_withdrawal_requests_contract_call::<EvmConfig, _, _>(&mut evm_post_block)
apply_withdrawal_requests_contract_call::<EvmConfig, _, _>(evm_config, &mut evm_post_block)
}

/// Applies the post-block call to the EIP-7002 withdrawal requests contract.
Expand All @@ -167,6 +170,7 @@ where
/// returned. Otherwise, the withdrawal requests are returned.
#[inline]
pub fn apply_withdrawal_requests_contract_call<EvmConfig, EXT, DB>(
evm_config: &EvmConfig,
evm: &mut Evm<'_, EXT, DB>,
) -> Result<Vec<Request>, BlockExecutionError>
where
Expand All @@ -185,7 +189,7 @@ where
// At the end of processing any execution block where `block.timestamp >= FORK_TIMESTAMP` (i.e.
// after processing all transactions and after performing the block body withdrawal requests
// validations), call the contract as `SYSTEM_ADDRESS`.
EvmConfig::fill_tx_env_system_contract_call(
evm_config.fill_tx_env_system_contract_call(
&mut evm.context.evm.env,
alloy_eips::eip7002::SYSTEM_ADDRESS,
WITHDRAWAL_REQUEST_PREDEPLOY_ADDRESS,
Expand Down
5 changes: 3 additions & 2 deletions crates/optimism/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ where
DB: Database<Error: Into<ProviderError> + std::fmt::Display>,
{
// apply pre execution changes
apply_beacon_root_contract_call::<EvmConfig, _, _>(
apply_beacon_root_contract_call(
&self.evm_config,
&self.chain_spec,
block.timestamp,
block.number,
Expand Down Expand Up @@ -271,7 +272,7 @@ where
fn evm_env_for_block(&self, header: &Header, total_difficulty: U256) -> EnvWithHandlerCfg {
let mut cfg = CfgEnvWithHandlerCfg::new(Default::default(), Default::default());
let mut block_env = BlockEnv::default();
EvmConfig::fill_cfg_and_block_env(
self.executor.evm_config.fill_cfg_and_block_env(
&mut cfg,
&mut block_env,
self.chain_spec(),
Expand Down
4 changes: 3 additions & 1 deletion crates/optimism/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl ConfigureEvmEnv for OptimismEvmConfig {
}

fn fill_tx_env_system_contract_call(
&self,
env: &mut Env,
caller: Address,
contract: Address,
Expand Down Expand Up @@ -83,6 +84,7 @@ impl ConfigureEvmEnv for OptimismEvmConfig {
}

fn fill_cfg_env(
&self,
cfg_env: &mut CfgEnvWithHandlerCfg,
chain_spec: &ChainSpec,
header: &Header,
Expand Down Expand Up @@ -143,7 +145,7 @@ mod tests {
let chain_spec = ChainSpec::default();
let total_difficulty = U256::ZERO;

OptimismEvmConfig::fill_cfg_and_block_env(
OptimismEvmConfig::default().fill_cfg_and_block_env(
&mut cfg_env,
&mut block_env,
&chain_spec,
Expand Down
4 changes: 2 additions & 2 deletions crates/optimism/payload/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ where
// apply eip-4788 pre block contract call
pre_block_beacon_root_contract_call(
&mut db,
self.evm_config.clone(),
&self.evm_config,
&chain_spec,
&initialized_cfg,
&initialized_block_env,
Expand Down Expand Up @@ -288,7 +288,7 @@ where
// apply eip-4788 pre block contract call
pre_block_beacon_root_contract_call(
&mut db,
evm_config.clone(),
&evm_config,
&chain_spec,
&initialized_cfg,
&initialized_block_env,
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/rpc-eth-api/src/helpers/pending_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub trait LoadPendingBlock {
// parent beacon block root
pre_block_beacon_root_contract_call(
&mut db,
self.evm_config().clone(),
self.evm_config(),
chain_spec.as_ref(),
&cfg,
&block_env,
Expand Down
8 changes: 4 additions & 4 deletions crates/storage/provider/src/providers/database/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2040,15 +2040,15 @@ impl<TX: DbTx> EvmEnvProvider for DatabaseProvider<TX> {
cfg: &mut CfgEnvWithHandlerCfg,
block_env: &mut BlockEnv,
header: &Header,
_evm_config: EvmConfig,
evm_config: EvmConfig,
) -> ProviderResult<()>
where
EvmConfig: ConfigureEvmEnv,
{
let total_difficulty = self
.header_td_by_number(header.number)?
.ok_or_else(|| ProviderError::HeaderNotFound(header.number.into()))?;
EvmConfig::fill_cfg_and_block_env(
evm_config.fill_cfg_and_block_env(
cfg,
block_env,
&self.chain_spec,
Expand Down Expand Up @@ -2076,15 +2076,15 @@ impl<TX: DbTx> EvmEnvProvider for DatabaseProvider<TX> {
&self,
cfg: &mut CfgEnvWithHandlerCfg,
header: &Header,
_evm_config: EvmConfig,
evm_config: EvmConfig,
) -> ProviderResult<()>
where
EvmConfig: ConfigureEvmEnv,
{
let total_difficulty = self
.header_td_by_number(header.number)?
.ok_or_else(|| ProviderError::HeaderNotFound(header.number.into()))?;
EvmConfig::fill_cfg_env(cfg, &self.chain_spec, header, total_difficulty);
evm_config.fill_cfg_env(cfg, &self.chain_spec, header, total_difficulty);
Ok(())
}
}
Expand Down
4 changes: 3 additions & 1 deletion examples/custom-evm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl MyEvmConfig {

impl ConfigureEvmEnv for MyEvmConfig {
fn fill_cfg_env(
&self,
cfg_env: &mut CfgEnvWithHandlerCfg,
chain_spec: &ChainSpec,
header: &Header,
Expand Down Expand Up @@ -95,12 +96,13 @@ impl ConfigureEvmEnv for MyEvmConfig {
}

fn fill_tx_env_system_contract_call(
&self,
env: &mut Env,
caller: Address,
contract: Address,
data: Bytes,
) {
EthEvmConfig::fill_tx_env_system_contract_call(env, caller, contract, data)
EthEvmConfig::default().fill_tx_env_system_contract_call(env, caller, contract, data)
}
}

Expand Down
8 changes: 1 addition & 7 deletions examples/exex/rollup/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,7 @@ fn configure_evm<'a>(
);

let mut cfg = CfgEnvWithHandlerCfg::new_with_spec_id(evm.cfg().clone(), evm.spec_id());
EthEvmConfig::fill_cfg_and_block_env(
&mut cfg,
evm.block_mut(),
&CHAIN_SPEC,
header,
U256::ZERO,
);
config.fill_cfg_and_block_env(&mut cfg, evm.block_mut(), &CHAIN_SPEC, header, U256::ZERO);
*evm.cfg_mut() = cfg.cfg_env;

evm
Expand Down
6 changes: 4 additions & 2 deletions examples/stateful-precompile/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,23 @@ impl ConfigureEvmEnv for MyEvmConfig {
}

fn fill_cfg_env(
&self,
cfg_env: &mut CfgEnvWithHandlerCfg,
chain_spec: &ChainSpec,
header: &Header,
total_difficulty: U256,
) {
EthEvmConfig::fill_cfg_env(cfg_env, chain_spec, header, total_difficulty)
EthEvmConfig::default().fill_cfg_env(cfg_env, chain_spec, header, total_difficulty)
}

fn fill_tx_env_system_contract_call(
&self,
env: &mut Env,
caller: Address,
contract: Address,
data: Bytes,
) {
EthEvmConfig::fill_tx_env_system_contract_call(env, caller, contract, data)
EthEvmConfig::default().fill_tx_env_system_contract_call(env, caller, contract, data)
}
}

Expand Down
Loading