feat: add functions for env creation (#12928)

This commit is contained in:
Matthias Seitz
2024-11-27 23:35:23 +01:00
committed by GitHub
parent 8d70e8921b
commit 2179301590
8 changed files with 39 additions and 60 deletions

View File

@ -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;
@ -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(

View File

@ -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,
@ -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);

View File

@ -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`].
@ -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())
}
}

View File

@ -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();
@ -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

View File

@ -17,6 +17,7 @@
extern crate alloc;
use crate::builder::RethEvmBuilder;
use alloy_consensus::BlockHeader as _;
use alloy_primitives::{Address, Bytes, B256, U256};
use reth_primitives::TransactionSigned;
@ -24,8 +25,6 @@ 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;
@ -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,
@ -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`].
///

View File

@ -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`].
@ -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())
}
}

View File

@ -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,
@ -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();
@ -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

View File

@ -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,
},
};
@ -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())
}
}