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

evm: use Header AT in ConfigureEvmEnv #10968

Merged
merged 9 commits into from
Sep 17, 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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions crates/engine/invalid-block-hooks/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use reth_evm::{
system_calls::{apply_beacon_root_contract_call, apply_blockhashes_contract_call},
ConfigureEvm,
};
use reth_primitives::{keccak256, Receipt, SealedBlockWithSenders, SealedHeader, B256, U256};
use reth_primitives::{
keccak256, Header, Receipt, SealedBlockWithSenders, SealedHeader, B256, U256,
};
use reth_provider::{BlockExecutionOutput, ChainSpecProvider, StateProviderFactory};
use reth_revm::{
database::StateProviderDatabase,
Expand Down Expand Up @@ -50,7 +52,7 @@ impl<P, EvmConfig> InvalidBlockWitnessHook<P, EvmConfig> {
impl<P, EvmConfig> InvalidBlockWitnessHook<P, EvmConfig>
where
P: StateProviderFactory + ChainSpecProvider<ChainSpec = ChainSpec> + Send + Sync + 'static,
EvmConfig: ConfigureEvm,
EvmConfig: ConfigureEvm<Header = Header>,
{
fn on_invalid_block(
&self,
Expand Down Expand Up @@ -236,7 +238,7 @@ where
impl<P, EvmConfig> InvalidBlockHook for InvalidBlockWitnessHook<P, EvmConfig>
where
P: StateProviderFactory + ChainSpecProvider<ChainSpec = ChainSpec> + Send + Sync + 'static,
EvmConfig: ConfigureEvm,
EvmConfig: ConfigureEvm<Header = Header>,
{
fn on_invalid_block(
&self,
Expand Down
4 changes: 2 additions & 2 deletions crates/engine/util/src/reorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ where
S: Stream<Item = BeaconEngineMessage<Engine>>,
Engine: EngineTypes,
Provider: BlockReader + StateProviderFactory,
Evm: ConfigureEvm,
Evm: ConfigureEvm<Header = Header>,
{
type Item = S::Item;

Expand Down Expand Up @@ -237,7 +237,7 @@ fn create_reorg_head<Provider, Evm>(
) -> RethResult<(ExecutionPayload, Option<CancunPayloadFields>)>
where
Provider: BlockReader + StateProviderFactory,
Evm: ConfigureEvm,
Evm: ConfigureEvm<Header = Header>,
{
let chain_spec = payload_validator.chain_spec();

Expand Down
14 changes: 7 additions & 7 deletions crates/ethereum/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl<EvmConfig> EthExecutorProvider<EvmConfig> {

impl<EvmConfig> EthExecutorProvider<EvmConfig>
where
EvmConfig: ConfigureEvm,
EvmConfig: ConfigureEvm<Header = Header>,
{
fn eth_executor<DB>(&self, db: DB) -> EthBlockExecutor<EvmConfig, DB>
where
Expand All @@ -83,7 +83,7 @@ where

impl<EvmConfig> BlockExecutorProvider for EthExecutorProvider<EvmConfig>
where
EvmConfig: ConfigureEvm,
EvmConfig: ConfigureEvm<Header = Header>,
{
type Executor<DB: Database<Error: Into<ProviderError> + Display>> =
EthBlockExecutor<EvmConfig, DB>;
Expand Down Expand Up @@ -126,7 +126,7 @@ struct EthEvmExecutor<EvmConfig> {

impl<EvmConfig> EthEvmExecutor<EvmConfig>
where
EvmConfig: ConfigureEvm,
EvmConfig: ConfigureEvm<Header = Header>,
{
/// Executes the transactions in the block and returns the receipts of the transactions in the
/// block, the total gas used and the list of EIP-7685 [requests](Request).
Expand Down Expand Up @@ -267,7 +267,7 @@ impl<EvmConfig, DB> EthBlockExecutor<EvmConfig, DB> {

impl<EvmConfig, DB> EthBlockExecutor<EvmConfig, DB>
where
EvmConfig: ConfigureEvm,
EvmConfig: ConfigureEvm<Header = Header>,
DB: Database<Error: Into<ProviderError> + Display>,
{
/// Configures a new evm configuration and block environment for the given block.
Expand Down Expand Up @@ -356,7 +356,7 @@ where

impl<EvmConfig, DB> Executor<DB> for EthBlockExecutor<EvmConfig, DB>
where
EvmConfig: ConfigureEvm,
EvmConfig: ConfigureEvm<Header = Header>,
DB: Database<Error: Into<ProviderError> + Display>,
{
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
Expand Down Expand Up @@ -391,7 +391,7 @@ pub struct BlockAccessListExecutor<EvmConfig, DB> {

impl<EvmConfig, DB> Executor<DB> for BlockAccessListExecutor<EvmConfig, DB>
where
EvmConfig: ConfigureEvm,
EvmConfig: ConfigureEvm<Header = Header>,
DB: Database<Error: Into<ProviderError> + Display>,
{
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
Expand Down Expand Up @@ -485,7 +485,7 @@ impl<EvmConfig, DB> EthBatchExecutor<EvmConfig, DB> {

impl<EvmConfig, DB> BatchExecutor<DB> for EthBatchExecutor<EvmConfig, DB>
where
EvmConfig: ConfigureEvm,
EvmConfig: ConfigureEvm<Header = Header>,
DB: Database<Error: Into<ProviderError> + Display>,
{
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
Expand Down
22 changes: 21 additions & 1 deletion crates/ethereum/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,29 @@ impl ConfigureEvmEnv for EthEvmConfig {
cfg_env.handler_cfg.spec_id = spec_id;
}

fn fill_block_env(&self, block_env: &mut BlockEnv, header: &Self::Header, after_merge: bool) {
block_env.number = U256::from(header.number);
block_env.coinbase = header.beneficiary;
block_env.timestamp = U256::from(header.timestamp);
if after_merge {
block_env.prevrandao = Some(header.mix_hash);
block_env.difficulty = U256::ZERO;
} else {
block_env.difficulty = header.difficulty;
block_env.prevrandao = None;
}
block_env.basefee = U256::from(header.base_fee_per_gas.unwrap_or_default());
block_env.gas_limit = U256::from(header.gas_limit);

// EIP-4844 excess blob gas of this block, introduced in Cancun
if let Some(excess_blob_gas) = header.excess_blob_gas {
block_env.set_blob_excess_gas_and_price(excess_blob_gas);
}
}

fn next_cfg_and_block_env(
&self,
parent: &Header,
parent: &Self::Header,
attributes: NextBlockEnvAttributes,
) -> (CfgEnvWithHandlerCfg, BlockEnv) {
// configure evm env based on parent block
Expand Down
1 change: 1 addition & 0 deletions crates/ethereum/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ reth-beacon-consensus.workspace = true
reth-rpc.workspace = true
reth-node-api.workspace = true
reth-chainspec.workspace = true
reth-primitives.workspace = true

# misc
eyre.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion crates/ethereum/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use reth_node_builder::{
BuilderContext, Node, PayloadBuilderConfig, PayloadTypes,
};
use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService};
use reth_primitives::Header;
use reth_provider::CanonStateSubscriptions;
use reth_rpc::EthApi;
use reth_tracing::tracing::{debug, info};
Expand Down Expand Up @@ -217,7 +218,7 @@ impl EthereumPayloadBuilder {
where
Types: NodeTypesWithEngine<ChainSpec = ChainSpec>,
Node: FullNodeTypes<Types = Types>,
Evm: ConfigureEvm,
Evm: ConfigureEvm<Header = Header>,
Pool: TransactionPool + Unpin + 'static,
Types::Engine: PayloadTypes<
BuiltPayload = EthBuiltPayload,
Expand Down
6 changes: 3 additions & 3 deletions crates/ethereum/payload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl<EvmConfig> EthereumPayloadBuilder<EvmConfig> {

impl<EvmConfig> EthereumPayloadBuilder<EvmConfig>
where
EvmConfig: ConfigureEvm,
EvmConfig: ConfigureEvm<Header = Header>,
{
/// Returns the configured [`CfgEnvWithHandlerCfg`] and [`BlockEnv`] for the targeted payload
/// (that has the `parent` as its parent).
Expand All @@ -88,7 +88,7 @@ where
// Default implementation of [PayloadBuilder] for unit type
impl<EvmConfig, Pool, Client> PayloadBuilder<Pool, Client> for EthereumPayloadBuilder<EvmConfig>
where
EvmConfig: ConfigureEvm,
EvmConfig: ConfigureEvm<Header = Header>,
Client: StateProviderFactory,
Pool: TransactionPool,
{
Expand Down Expand Up @@ -137,7 +137,7 @@ pub fn default_ethereum_payload<EvmConfig, Pool, Client>(
initialized_block_env: BlockEnv,
) -> Result<BuildOutcome<EthBuiltPayload>, PayloadBuilderError>
where
EvmConfig: ConfigureEvm,
EvmConfig: ConfigureEvm<Header = Header>,
Client: StateProviderFactory,
Pool: TransactionPool,
{
Expand Down
30 changes: 5 additions & 25 deletions crates/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ extern crate alloc;
use core::ops::Deref;

use crate::builder::RethEvmBuilder;
use reth_primitives::{
Address, Header, TransactionSigned, TransactionSignedEcRecovered, B256, U256,
};
use reth_primitives::{Address, TransactionSigned, TransactionSignedEcRecovered, B256, U256};
use revm::{Database, Evm, GetInspector};
use revm_primitives::{
BlockEnv, Bytes, CfgEnvWithHandlerCfg, Env, EnvWithHandlerCfg, SpecId, TxEnv,
Expand Down Expand Up @@ -139,30 +137,12 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static {
fn fill_cfg_env(
&self,
cfg_env: &mut CfgEnvWithHandlerCfg,
header: &Header,
header: &Self::Header,
total_difficulty: U256,
);

/// Fill [`BlockEnv`] field according to the chain spec and given header
fn fill_block_env(&self, block_env: &mut BlockEnv, header: &Header, after_merge: bool) {
block_env.number = U256::from(header.number);
block_env.coinbase = header.beneficiary;
block_env.timestamp = U256::from(header.timestamp);
if after_merge {
block_env.prevrandao = Some(header.mix_hash);
block_env.difficulty = U256::ZERO;
} else {
block_env.difficulty = header.difficulty;
block_env.prevrandao = None;
}
block_env.basefee = U256::from(header.base_fee_per_gas.unwrap_or_default());
block_env.gas_limit = U256::from(header.gas_limit);

// EIP-4844 excess blob gas of this block, introduced in Cancun
if let Some(excess_blob_gas) = header.excess_blob_gas {
block_env.set_blob_excess_gas_and_price(excess_blob_gas);
}
}
fn fill_block_env(&self, block_env: &mut BlockEnv, header: &Self::Header, after_merge: bool);

/// Convenience function to call both [`fill_cfg_env`](ConfigureEvmEnv::fill_cfg_env) and
/// [`ConfigureEvmEnv::fill_block_env`].
Expand All @@ -172,7 +152,7 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static {
&self,
cfg: &mut CfgEnvWithHandlerCfg,
block_env: &mut BlockEnv,
header: &Header,
header: &Self::Header,
total_difficulty: U256,
) {
self.fill_cfg_env(cfg, header, total_difficulty);
Expand All @@ -187,7 +167,7 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static {
/// the CL, such as the timestamp, suggested fee recipient, and randomness value.
fn next_cfg_and_block_env(
&self,
parent: &Header,
parent: &Self::Header,
attributes: NextBlockEnvAttributes,
) -> (CfgEnvWithHandlerCfg, BlockEnv);
}
Expand Down
10 changes: 5 additions & 5 deletions crates/evm/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub trait EvmEnvProvider: Send + Sync {
evm_config: EvmConfig,
) -> ProviderResult<()>
where
EvmConfig: ConfigureEvmEnv;
EvmConfig: ConfigureEvmEnv<Header = Header>;

/// Fills the default [`CfgEnvWithHandlerCfg`] and [BlockEnv] fields with values specific to the
/// given [Header].
Expand All @@ -32,7 +32,7 @@ pub trait EvmEnvProvider: Send + Sync {
evm_config: EvmConfig,
) -> ProviderResult<(CfgEnvWithHandlerCfg, BlockEnv)>
where
EvmConfig: ConfigureEvmEnv,
EvmConfig: ConfigureEvmEnv<Header = Header>,
{
let mut cfg = CfgEnvWithHandlerCfg::new_with_spec_id(CfgEnv::default(), SpecId::LATEST);
let mut block_env = BlockEnv::default();
Expand All @@ -50,7 +50,7 @@ pub trait EvmEnvProvider: Send + Sync {
evm_config: EvmConfig,
) -> ProviderResult<()>
where
EvmConfig: ConfigureEvmEnv;
EvmConfig: ConfigureEvmEnv<Header = Header>;

/// Fills the [`CfgEnvWithHandlerCfg`] fields with values specific to the given
/// [BlockHashOrNumber].
Expand All @@ -61,7 +61,7 @@ pub trait EvmEnvProvider: Send + Sync {
evm_config: EvmConfig,
) -> ProviderResult<()>
where
EvmConfig: ConfigureEvmEnv;
EvmConfig: ConfigureEvmEnv<Header = Header>;

/// Fills the [`CfgEnvWithHandlerCfg`] fields with values specific to the given [Header].
fn fill_cfg_env_with_header<EvmConfig>(
Expand All @@ -71,5 +71,5 @@ pub trait EvmEnvProvider: Send + Sync {
evm_config: EvmConfig,
) -> ProviderResult<()>
where
EvmConfig: ConfigureEvmEnv;
EvmConfig: ConfigureEvmEnv<Header = Header>;
}
7 changes: 4 additions & 3 deletions crates/evm/src/system_calls/eip2935.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::ConfigureEvm;
use core::fmt::Display;
use reth_chainspec::{ChainSpec, EthereumHardforks};
use reth_execution_errors::{BlockExecutionError, BlockValidationError};
use reth_primitives::Header;
use revm::{interpreter::Host, Database, DatabaseCommit, Evm};
use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ResultAndState, B256};

Expand All @@ -28,7 +29,7 @@ pub fn pre_block_blockhashes_contract_call<EvmConfig, DB>(
where
DB: Database + DatabaseCommit,
DB::Error: Display,
EvmConfig: ConfigureEvm,
EvmConfig: ConfigureEvm<Header = Header>,
{
// Apply the pre-block EIP-2935 contract call
let mut evm_pre_block = Evm::builder()
Expand Down Expand Up @@ -74,7 +75,7 @@ pub fn transact_blockhashes_contract_call<EvmConfig, EXT, DB>(
where
DB: Database + DatabaseCommit,
DB::Error: core::fmt::Display,
EvmConfig: ConfigureEvm,
EvmConfig: ConfigureEvm<Header = Header>,
{
if !chain_spec.is_prague_active_at_timestamp(block_timestamp) {
return Ok(None)
Expand Down Expand Up @@ -133,7 +134,7 @@ pub fn apply_blockhashes_contract_call<EvmConfig, EXT, DB>(
where
DB: Database + DatabaseCommit,
DB::Error: core::fmt::Display,
EvmConfig: ConfigureEvm,
EvmConfig: ConfigureEvm<Header = Header>,
{
if let Some(res) = transact_blockhashes_contract_call(
evm_config,
Expand Down
Loading
Loading