Skip to content

Commit

Permalink
single entry point to apply pre block execution changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fgimenez committed Sep 29, 2024
1 parent 7c2c3f9 commit 4d553cc
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
24 changes: 23 additions & 1 deletion crates/evm/src/system_calls/eip2935.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use alloy_primitives::B256;
use core::fmt::Display;
use reth_chainspec::EthereumHardforks;
use reth_execution_errors::{BlockExecutionError, BlockValidationError};
use reth_primitives::Header;
use reth_primitives::{Block, Header};
use revm::{interpreter::Host, Database, DatabaseCommit, Evm};
use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ResultAndState};

Expand Down Expand Up @@ -116,6 +116,28 @@ where
Ok(Some(res))
}

#[inline]
pub(crate) fn transact<EvmConfig, EXT, DB>(
evm_config: &EvmConfig,
chain_spec: impl EthereumHardforks,
block: &Block,
evm: &mut Evm<'_, EXT, DB>,
) -> Result<Option<ResultAndState>, BlockExecutionError>
where
DB: Database + DatabaseCommit,
DB::Error: core::fmt::Display,
EvmConfig: ConfigureEvm<Header = Header>,
{
transact_blockhashes_contract_call(
evm_config,
chain_spec,
block.timestamp,
block.number,
block.parent_hash,
evm,
)
}

/// Applies the pre-block call to the [EIP-2935] blockhashes contract, using the given block,
/// chain specification, and EVM and commits the relevant state changes.
///
Expand Down
24 changes: 23 additions & 1 deletion crates/evm/src/system_calls/eip4788.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use alloy_eips::eip4788::BEACON_ROOTS_ADDRESS;
use alloy_primitives::B256;
use reth_chainspec::EthereumHardforks;
use reth_execution_errors::{BlockExecutionError, BlockValidationError};
use reth_primitives::Header;
use reth_primitives::{Block, Header};
use revm::{interpreter::Host, Database, DatabaseCommit, Evm};
use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ResultAndState};

Expand Down Expand Up @@ -126,6 +126,28 @@ where
Ok(Some(res))
}

#[inline]
pub(crate) fn transact<EvmConfig, EXT, DB>(
evm_config: &EvmConfig,
chain_spec: impl EthereumHardforks,
block: &Block,
evm: &mut Evm<'_, EXT, DB>,
) -> Result<Option<ResultAndState>, BlockExecutionError>
where
DB: Database + DatabaseCommit,
DB::Error: core::fmt::Display,
EvmConfig: ConfigureEvm<Header = Header>,
{
transact_beacon_root_contract_call(
evm_config,
&chain_spec,
block.timestamp,
block.number,
block.parent_beacon_block_root,
evm,
)
}

/// Applies the pre-block call to the [EIP-4788] beacon block root contract, using the given block,
/// chain spec, EVM.
///
Expand Down
33 changes: 30 additions & 3 deletions crates/evm/src/system_calls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use alloy_primitives::{Address, FixedBytes};
use core::fmt::Display;
use reth_chainspec::ChainSpec;
use reth_execution_errors::{BlockExecutionError, BlockValidationError};
use reth_primitives::{Buf, Header, Request};
use reth_primitives::{Block, Buf, Header, Request};
use revm::{Database, DatabaseCommit, Evm};
use revm_primitives::{
BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ExecutionResult, ResultAndState, B256,
Expand Down Expand Up @@ -107,6 +107,33 @@ where
Chainspec: AsRef<ChainSpec>,
Hook: OnStateHook,
{
/// Apply pre execution changes.
pub fn apply_pre_execution_changes<DB, Ext>(
&mut self,
block: &Block,
evm: &mut Evm<'_, Ext, DB>,
) -> Result<(), BlockExecutionError>
where
DB: Database + DatabaseCommit,
DB::Error: Display,
{
let transacts = vec![eip2935::transact, eip4788::transact];

for tx in transacts {
let result_and_state =
tx(&self.evm_config.clone(), self.chain_spec.as_ref(), block, evm)?;

if let Some(res) = result_and_state {
if let Some(ref mut hook) = self.hook {
hook.on_state(&res);
}
evm.context.evm.db.commit(res.state);
}
}

Ok(())
}

/// Applies the pre-block call to the EIP-2935 blockhashes contract.
pub fn pre_block_blockhashes_contract_call<DB>(
&mut self,
Expand Down Expand Up @@ -167,7 +194,7 @@ where
db: &mut DB,
initialized_cfg: &CfgEnvWithHandlerCfg,
initialized_block_env: &BlockEnv,
parent_block_hash: Option<B256>,
parent_beacon_block_root: Option<B256>,
) -> Result<(), BlockExecutionError>
where
DB: Database + DatabaseCommit,
Expand All @@ -178,7 +205,7 @@ where
self.apply_beacon_root_contract_call(
initialized_block_env.timestamp.to(),
initialized_block_env.number.to(),
parent_block_hash,
parent_beacon_block_root,
&mut evm,
)?;

Expand Down

0 comments on commit 4d553cc

Please sign in to comment.