Skip to content

Commit

Permalink
feat: add functions for env creation (#12928)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Nov 27, 2024
1 parent 8d70e89 commit 2179301
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 60 deletions.
10 changes: 3 additions & 7 deletions crates/engine/invalid-block-hooks/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ use reth_primitives::{Receipt, SealedBlockWithSenders, SealedHeader};
use reth_primitives_traits::SignedTransaction;
use reth_provider::{BlockExecutionOutput, ChainSpecProvider, StateProviderFactory};
use reth_revm::{
database::StateProviderDatabase,
db::states::bundle_state::BundleRetention,
primitives::{BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg},
DatabaseCommit, StateBuilder,
database::StateProviderDatabase, db::states::bundle_state::BundleRetention,
primitives::EnvWithHandlerCfg, DatabaseCommit, StateBuilder,
};
use reth_rpc_api::DebugApiClient;
use reth_tracing::tracing::warn;
Expand Down Expand Up @@ -76,9 +74,7 @@ where
.build();

// Setup environment for the execution.
let mut cfg = CfgEnvWithHandlerCfg::new(Default::default(), Default::default());
let mut block_env = BlockEnv::default();
self.evm_config.fill_cfg_and_block_env(&mut cfg, &mut block_env, block.header(), U256::MAX);
let (cfg, block_env) = self.evm_config.cfg_and_block_env(block.header(), U256::MAX);

// Setup EVM
let mut evm = self.evm_config.evm_with_env(
Expand Down
8 changes: 2 additions & 6 deletions crates/engine/util/src/reorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ use reth_revm::{
};
use reth_rpc_types_compat::engine::payload::block_to_payload;
use reth_trie::HashedPostState;
use revm_primitives::{
calc_excess_blob_gas, BlockEnv, CfgEnvWithHandlerCfg, EVMError, EnvWithHandlerCfg,
};
use revm_primitives::{calc_excess_blob_gas, EVMError, EnvWithHandlerCfg};
use std::{
collections::VecDeque,
future::Future,
Expand Down Expand Up @@ -298,9 +296,7 @@ where
.build();

// Configure environments
let mut cfg = CfgEnvWithHandlerCfg::new(Default::default(), Default::default());
let mut block_env = BlockEnv::default();
evm_config.fill_cfg_and_block_env(&mut cfg, &mut block_env, &reorg_target.header, U256::MAX);
let (cfg, block_env) = evm_config.cfg_and_block_env(&reorg_target.header, U256::MAX);
let env = EnvWithHandlerCfg::new_with_cfg_env(cfg, block_env, Default::default());
let mut evm = evm_config.evm_with_env(&mut state, env);

Expand Down
7 changes: 2 additions & 5 deletions crates/ethereum/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use reth_primitives::{BlockWithSenders, Receipt};
use reth_revm::db::State;
use revm_primitives::{
db::{Database, DatabaseCommit},
BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ResultAndState, U256,
EnvWithHandlerCfg, ResultAndState, U256,
};

/// Factory for [`EthExecutionStrategy`].
Expand Down Expand Up @@ -117,10 +117,7 @@ where
header: &alloy_consensus::Header,
total_difficulty: U256,
) -> EnvWithHandlerCfg {
let mut cfg = CfgEnvWithHandlerCfg::new(Default::default(), Default::default());
let mut block_env = BlockEnv::default();
self.evm_config.fill_cfg_and_block_env(&mut cfg, &mut block_env, header, total_difficulty);

let (cfg, block_env) = self.evm_config.cfg_and_block_env(header, total_difficulty);
EnvWithHandlerCfg::new_with_cfg_env(cfg, block_env, Default::default())
}
}
Expand Down
16 changes: 3 additions & 13 deletions crates/ethereum/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,11 @@ mod tests {
primitives::{BlockEnv, CfgEnv, SpecId},
JournaledState,
};
use revm_primitives::{CfgEnvWithHandlerCfg, EnvWithHandlerCfg, HandlerCfg};
use revm_primitives::{EnvWithHandlerCfg, HandlerCfg};
use std::collections::HashSet;

#[test]
fn test_fill_cfg_and_block_env() {
// Create a new configuration environment
let mut cfg_env = CfgEnvWithHandlerCfg::new_with_spec_id(CfgEnv::default(), SpecId::LATEST);

// Create a default block environment
let mut block_env = BlockEnv::default();

// Create a default header
let header = Header::default();

Expand All @@ -236,12 +230,8 @@ mod tests {

// Use the `EthEvmConfig` to fill the `cfg_env` and `block_env` based on the ChainSpec,
// Header, and total difficulty
EthEvmConfig::new(Arc::new(chain_spec.clone())).fill_cfg_and_block_env(
&mut cfg_env,
&mut block_env,
&header,
total_difficulty,
);
let (cfg_env, _) = EthEvmConfig::new(Arc::new(chain_spec.clone()))
.cfg_and_block_env(&header, total_difficulty);

// Assert that the chain ID in the `cfg_env` is correctly set to the chain ID of the
// ChainSpec
Expand Down
24 changes: 21 additions & 3 deletions crates/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@

extern crate alloc;

use crate::builder::RethEvmBuilder;
use alloy_consensus::BlockHeader as _;
use alloy_primitives::{Address, Bytes, B256, U256};
use reth_primitives::TransactionSigned;
use reth_primitives_traits::BlockHeader;
use revm::{Database, Evm, GetInspector};
use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, Env, EnvWithHandlerCfg, SpecId, TxEnv};

use crate::builder::RethEvmBuilder;

pub mod builder;
pub mod either;
pub mod execute;
Expand Down Expand Up @@ -139,9 +138,16 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static {
data: Bytes,
);

/// Returns a [`CfgEnvWithHandlerCfg`] for the given header.
fn cfg_env(&self, header: &Self::Header, total_difficulty: U256) -> CfgEnvWithHandlerCfg {
let mut cfg = CfgEnvWithHandlerCfg::new(Default::default(), Default::default());
self.fill_cfg_env(&mut cfg, header, total_difficulty);
cfg
}

/// Fill [`CfgEnvWithHandlerCfg`] fields according to the chain spec and given header.
///
/// This must set the corresponding spec id in the handler cfg, based on timestamp or total
/// This __must__ set the corresponding spec id in the handler cfg, based on timestamp or total
/// difficulty
fn fill_cfg_env(
&self,
Expand Down Expand Up @@ -171,6 +177,18 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static {
}
}

/// Creates a new [`CfgEnvWithHandlerCfg`] and [`BlockEnv`] for the given header.
fn cfg_and_block_env(
&self,
header: &Self::Header,
total_difficulty: U256,
) -> (CfgEnvWithHandlerCfg, BlockEnv) {
let mut cfg = CfgEnvWithHandlerCfg::new(Default::default(), Default::default());
let mut block_env = BlockEnv::default();
self.fill_cfg_and_block_env(&mut cfg, &mut block_env, header, total_difficulty);
(cfg, block_env)
}

/// Convenience function to call both [`fill_cfg_env`](ConfigureEvmEnv::fill_cfg_env) and
/// [`ConfigureEvmEnv::fill_block_env`].
///
Expand Down
9 changes: 2 additions & 7 deletions crates/optimism/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ use reth_optimism_consensus::validate_block_post_execution;
use reth_optimism_forks::OpHardfork;
use reth_primitives::{BlockWithSenders, Receipt, TxType};
use reth_revm::{Database, State};
use revm_primitives::{
db::DatabaseCommit, BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ResultAndState, U256,
};
use revm_primitives::{db::DatabaseCommit, EnvWithHandlerCfg, ResultAndState, U256};
use tracing::trace;

/// Factory for [`OpExecutionStrategy`].
Expand Down Expand Up @@ -106,10 +104,7 @@ where
///
/// Caution: this does not initialize the tx environment.
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();
self.evm_config.fill_cfg_and_block_env(&mut cfg, &mut block_env, header, total_difficulty);

let (cfg, block_env) = self.evm_config.cfg_and_block_env(header, total_difficulty);
EnvWithHandlerCfg::new_with_cfg_env(cfg, block_env, Default::default())
}
}
Expand Down
15 changes: 4 additions & 11 deletions crates/optimism/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,13 @@ mod tests {
use reth_optimism_chainspec::BASE_MAINNET;
use reth_optimism_primitives::OpPrimitives;
use reth_primitives::{Account, Log, Receipt, Receipts, SealedBlockWithSenders, TxType};

use reth_revm::{
db::{BundleState, CacheDB, EmptyDBTyped},
inspectors::NoOpInspector,
primitives::{AccountInfo, BlockEnv, CfgEnv, SpecId},
JournaledState,
};
use revm_primitives::{CfgEnvWithHandlerCfg, EnvWithHandlerCfg, HandlerCfg};
use revm_primitives::{EnvWithHandlerCfg, HandlerCfg};
use std::{
collections::{HashMap, HashSet},
sync::Arc,
Expand All @@ -232,12 +231,6 @@ mod tests {

#[test]
fn test_fill_cfg_and_block_env() {
// Create a new configuration environment
let mut cfg_env = CfgEnvWithHandlerCfg::new_with_spec_id(CfgEnv::default(), SpecId::LATEST);

// Create a default block environment
let mut block_env = BlockEnv::default();

// Create a default header
let header = Header::default();

Expand All @@ -254,10 +247,10 @@ mod tests {
// Define the total difficulty as zero (default)
let total_difficulty = U256::ZERO;

// Use the `OpEvmConfig` to fill the `cfg_env` and `block_env` based on the ChainSpec,
// Use the `OpEvmConfig` to create the `cfg_env` and `block_env` based on the ChainSpec,
// Header, and total difficulty
OpEvmConfig::new(Arc::new(OpChainSpec { inner: chain_spec.clone() }))
.fill_cfg_and_block_env(&mut cfg_env, &mut block_env, &header, total_difficulty);
let (cfg_env, _) = OpEvmConfig::new(Arc::new(OpChainSpec { inner: chain_spec.clone() }))
.cfg_and_block_env(&header, total_difficulty);

// Assert that the chain ID in the `cfg_env` is correctly set to the chain ID of the
// ChainSpec
Expand Down
10 changes: 2 additions & 8 deletions examples/custom-beacon-withdrawals/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ use reth::{
providers::ProviderError,
revm::{
interpreter::Host,
primitives::{
address, Address, BlockEnv, Bytes, CfgEnvWithHandlerCfg, Env, EnvWithHandlerCfg,
TransactTo, TxEnv, U256,
},
primitives::{address, Address, Bytes, Env, EnvWithHandlerCfg, TransactTo, TxEnv, U256},
Database, DatabaseCommit, Evm, State,
},
};
Expand Down Expand Up @@ -133,10 +130,7 @@ where
header: &alloy_consensus::Header,
total_difficulty: U256,
) -> EnvWithHandlerCfg {
let mut cfg = CfgEnvWithHandlerCfg::new(Default::default(), Default::default());
let mut block_env = BlockEnv::default();
self.evm_config.fill_cfg_and_block_env(&mut cfg, &mut block_env, header, total_difficulty);

let (cfg, block_env) = self.evm_config.cfg_and_block_env(header, total_difficulty);
EnvWithHandlerCfg::new_with_cfg_env(cfg, block_env, Default::default())
}
}
Expand Down

0 comments on commit 2179301

Please sign in to comment.