Skip to content

Commit

Permalink
chore: simplify evm setup (#13864)
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr authored Jan 19, 2025
1 parent 88de40a commit f28c71c
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 130 deletions.
42 changes: 9 additions & 33 deletions crates/ethereum/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,12 @@ impl ConfigureEvmEnv for EthEvmConfig {
impl ConfigureEvm for EthEvmConfig {
type Evm<'a, DB: Database + 'a, I: 'a> = EthEvm<'a, I, DB>;

fn evm_with_env<DB: Database>(
&self,
db: DB,
evm_env: EvmEnv,
tx: TxEnv,
) -> Self::Evm<'_, DB, ()> {
fn evm_with_env<DB: Database>(&self, db: DB, evm_env: EvmEnv) -> Self::Evm<'_, DB, ()> {
EthEvm(
EvmBuilder::default()
.with_db(db)
.with_cfg_env_with_handler_cfg(evm_env.cfg_env_with_handler_cfg)
.with_block_env(evm_env.block_env)
.with_tx_env(tx)
.build(),
)
}
Expand All @@ -250,7 +244,6 @@ impl ConfigureEvm for EthEvmConfig {
&self,
db: DB,
evm_env: EvmEnv,
tx: TxEnv,
inspector: I,
) -> Self::Evm<'_, DB, I>
where
Expand All @@ -263,7 +256,6 @@ impl ConfigureEvm for EthEvmConfig {
.with_external_context(inspector)
.with_cfg_env_with_handler_cfg(evm_env.cfg_env_with_handler_cfg)
.with_block_env(evm_env.block_env)
.with_tx_env(tx)
.append_handler_register(inspector_handle_register)
.build(),
)
Expand Down Expand Up @@ -319,7 +311,7 @@ mod tests {

let evm_env = EvmEnv::default();

let evm = evm_config.evm_with_env(db, evm_env.clone(), Default::default());
let evm = evm_config.evm_with_env(db, evm_env.clone());

// Check that the EVM environment
assert_eq!(evm.context.evm.env.block, evm_env.block_env);
Expand Down Expand Up @@ -350,7 +342,7 @@ mod tests {
..Default::default()
};

let evm = evm_config.evm_with_env(db, evm_env, Default::default());
let evm = evm_config.evm_with_env(db, evm_env);

// Check that the EVM environment is initialized with the custom environment
assert_eq!(evm.context.evm.inner.env.cfg, cfg);
Expand All @@ -376,7 +368,6 @@ mod tests {
number: U256::from(42),
..Default::default()
};
let tx = TxEnv { gas_limit: 5_000_000, gas_price: U256::from(50), ..Default::default() };

let evm_env = EvmEnv {
cfg_env_with_handler_cfg: CfgEnvWithHandlerCfg {
Expand All @@ -386,11 +377,10 @@ mod tests {
block_env: block,
};

let evm = evm_config.evm_with_env(db, evm_env.clone(), tx.clone());
let evm = evm_config.evm_with_env(db, evm_env.clone());

// Verify that the block and transaction environments are set correctly
assert_eq!(evm.context.evm.env.block, evm_env.block_env);
assert_eq!(evm.context.evm.env.tx, tx);

// Default spec ID
assert_eq!(evm.handler.spec_id(), SpecId::LATEST);
Expand All @@ -416,7 +406,7 @@ mod tests {
..Default::default()
};

let evm = evm_config.evm_with_env(db, evm_env, Default::default());
let evm = evm_config.evm_with_env(db, evm_env);

// Check that the spec ID is setup properly
assert_eq!(evm.handler.spec_id(), SpecId::PETERSBURG);
Expand All @@ -436,12 +426,7 @@ mod tests {

let evm_env = EvmEnv::default();

let evm = evm_config.evm_with_env_and_inspector(
db,
evm_env.clone(),
Default::default(),
NoOpInspector,
);
let evm = evm_config.evm_with_env_and_inspector(db, evm_env.clone(), NoOpInspector);

// Check that the EVM environment is set to default values
assert_eq!(evm.context.evm.env.block, evm_env.block_env);
Expand All @@ -461,7 +446,6 @@ mod tests {

let cfg_env = CfgEnv::default().with_chain_id(111);
let block = BlockEnv::default();
let tx = TxEnv::default();
let evm_env = EvmEnv {
cfg_env_with_handler_cfg: CfgEnvWithHandlerCfg {
cfg_env: cfg_env.clone(),
Expand All @@ -470,7 +454,7 @@ mod tests {
block_env: block,
};

let evm = evm_config.evm_with_env_and_inspector(db, evm_env, tx, NoOpInspector);
let evm = evm_config.evm_with_env_and_inspector(db, evm_env, NoOpInspector);

// Check that the EVM environment is set with custom configuration
assert_eq!(evm.context.evm.env.cfg, cfg_env);
Expand All @@ -494,15 +478,12 @@ mod tests {
number: U256::from(42),
..Default::default()
};
let tx = TxEnv { gas_limit: 5_000_000, gas_price: U256::from(50), ..Default::default() };
let evm_env = EvmEnv { block_env: block, ..Default::default() };

let evm =
evm_config.evm_with_env_and_inspector(db, evm_env.clone(), tx.clone(), NoOpInspector);
let evm = evm_config.evm_with_env_and_inspector(db, evm_env.clone(), NoOpInspector);

// Verify that the block and transaction environments are set correctly
assert_eq!(evm.context.evm.env.block, evm_env.block_env);
assert_eq!(evm.context.evm.env.tx, tx);
assert_eq!(evm.context.external, NoOpInspector);
assert_eq!(evm.handler.spec_id(), SpecId::LATEST);

Expand All @@ -525,12 +506,7 @@ mod tests {
..Default::default()
};

let evm = evm_config.evm_with_env_and_inspector(
db,
evm_env.clone(),
Default::default(),
NoOpInspector,
);
let evm = evm_config.evm_with_env_and_inspector(db, evm_env.clone(), NoOpInspector);

// Check that the spec ID is set properly
assert_eq!(evm.handler.spec_id(), SpecId::PETERSBURG);
Expand Down
2 changes: 1 addition & 1 deletion crates/ethereum/payload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ where
PayloadBuilderError::Internal(err.into())
})?;

let mut evm = evm_config.evm_with_env(&mut db, evm_env, Default::default());
let mut evm = evm_config.evm_with_env(&mut db, evm_env);

let mut receipts = Vec::new();
while let Some(pool_tx) = best_txs.next() {
Expand Down
22 changes: 5 additions & 17 deletions crates/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,7 @@ pub trait ConfigureEvm: ConfigureEvmEnv {
/// including the spec id and transaction environment.
///
/// This will preserve any handler modifications
fn evm_with_env<DB: Database>(
&self,
db: DB,
evm_env: EvmEnv,
tx: TxEnv,
) -> Self::Evm<'_, DB, ()>;
fn evm_with_env<DB: Database>(&self, db: DB, evm_env: EvmEnv) -> Self::Evm<'_, DB, ()>;

/// Returns a new EVM with the given database configured with `cfg` and `block_env`
/// configuration derived from the given header. Relies on
Expand All @@ -106,7 +101,7 @@ pub trait ConfigureEvm: ConfigureEvmEnv {
/// This does not initialize the tx environment.
fn evm_for_block<DB: Database>(&self, db: DB, header: &Self::Header) -> Self::Evm<'_, DB, ()> {
let evm_env = self.cfg_and_block_env(header);
self.evm_with_env(db, evm_env, Default::default())
self.evm_with_env(db, evm_env)
}

/// Returns a new EVM with the given database configured with the given environment settings,
Expand All @@ -119,7 +114,6 @@ pub trait ConfigureEvm: ConfigureEvmEnv {
&self,
db: DB,
evm_env: EvmEnv,
tx: TxEnv,
inspector: I,
) -> Self::Evm<'_, DB, I>
where
Expand All @@ -138,27 +132,21 @@ where
(*self).evm_for_block(db, header)
}

fn evm_with_env<DB: Database>(
&self,
db: DB,
evm_env: EvmEnv,
tx: TxEnv,
) -> Self::Evm<'_, DB, ()> {
(*self).evm_with_env(db, evm_env, tx)
fn evm_with_env<DB: Database>(&self, db: DB, evm_env: EvmEnv) -> Self::Evm<'_, DB, ()> {
(*self).evm_with_env(db, evm_env)
}

fn evm_with_env_and_inspector<DB, I>(
&self,
db: DB,
evm_env: EvmEnv,
tx_env: TxEnv,
inspector: I,
) -> Self::Evm<'_, DB, I>
where
DB: Database,
I: GetInspector<DB>,
{
(*self).evm_with_env_and_inspector(db, evm_env, tx_env, inspector)
(*self).evm_with_env_and_inspector(db, evm_env, inspector)
}
}

Expand Down
8 changes: 4 additions & 4 deletions crates/evm/src/system_calls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ where
DB::Error: Display,
{
let evm_config = self.evm_config.clone();
let mut evm = evm_config.evm_with_env(db, evm_env.clone(), Default::default());
let mut evm = evm_config.evm_with_env(db, evm_env.clone());

self.apply_blockhashes_contract_call(
evm_env.block_env.timestamp.to(),
Expand Down Expand Up @@ -181,7 +181,7 @@ where
DB::Error: Display,
{
let evm_config = self.evm_config.clone();
let mut evm = evm_config.evm_with_env(db, evm_env.clone(), Default::default());
let mut evm = evm_config.evm_with_env(db, evm_env.clone());

self.apply_beacon_root_contract_call(
evm_env.block_env.timestamp.to(),
Expand Down Expand Up @@ -230,7 +230,7 @@ where
DB::Error: Display,
{
let evm_config = self.evm_config.clone();
let mut evm = evm_config.evm_with_env(db, evm_env.clone(), Default::default());
let mut evm = evm_config.evm_with_env(db, evm_env.clone());

let result = self.apply_withdrawal_requests_contract_call(&mut evm)?;

Expand Down Expand Up @@ -263,7 +263,7 @@ where
DB::Error: Display,
{
let evm_config = self.evm_config.clone();
let mut evm = evm_config.evm_with_env(db, evm_env.clone(), Default::default());
let mut evm = evm_config.evm_with_env(db, evm_env.clone());

let res = self.apply_consolidation_requests_contract_call(&mut evm)?;

Expand Down
Loading

0 comments on commit f28c71c

Please sign in to comment.