Skip to content

Commit

Permalink
feat: add transact function to 4788 (#11157)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Sep 24, 2024
1 parent 6e64a14 commit c0f0dd7
Showing 1 changed file with 49 additions and 13 deletions.
62 changes: 49 additions & 13 deletions crates/evm/src/system_calls/eip4788.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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, B256};
use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ResultAndState, B256};

/// Apply the [EIP-4788](https://eips.ethereum.org/EIPS/eip-4788) pre block contract call.
///
Expand Down Expand Up @@ -53,27 +53,29 @@ where
/// Applies the pre-block call to the [EIP-4788] beacon block root contract, using the given block,
/// [`ChainSpec`], EVM.
///
/// If Cancun is not activated or the block is the genesis block, then this is a no-op, and no
/// state changes are made.
/// Note: this does not commit the state changes to the database, it only transact the call.
///
/// Returns `None` if Cancun is not active or the block is the genesis block, otherwise returns the
/// result of the call.
///
/// [EIP-4788]: https://eips.ethereum.org/EIPS/eip-4788
#[inline]
pub fn apply_beacon_root_contract_call<EvmConfig, EXT, DB, Spec>(
pub fn transact_beacon_root_contract_call<EvmConfig, EXT, DB, Spec>(
evm_config: &EvmConfig,
chain_spec: &Spec,
block_timestamp: u64,
block_number: u64,
parent_beacon_block_root: Option<B256>,
evm: &mut Evm<'_, EXT, DB>,
) -> Result<(), BlockExecutionError>
) -> Result<Option<ResultAndState>, BlockExecutionError>
where
DB: Database + DatabaseCommit,
DB::Error: core::fmt::Display,
EvmConfig: ConfigureEvm<Header = Header>,
Spec: EthereumHardforks,
{
if !chain_spec.is_cancun_active_at_timestamp(block_timestamp) {
return Ok(())
return Ok(None)
}

let parent_beacon_block_root =
Expand All @@ -88,7 +90,7 @@ where
}
.into())
}
return Ok(())
return Ok(None)
}

// get previous env
Expand All @@ -102,8 +104,8 @@ where
parent_beacon_block_root.0.into(),
);

let mut state = match evm.transact() {
Ok(res) => res.state,
let mut res = match evm.transact() {
Ok(res) => res,
Err(e) => {
evm.context.evm.env = previous_env;
return Err(BlockValidationError::BeaconRootContractCall {
Expand All @@ -114,13 +116,47 @@ where
}
};

state.remove(&alloy_eips::eip4788::SYSTEM_ADDRESS);
state.remove(&evm.block().coinbase);

evm.context.evm.db.commit(state);
res.state.remove(&alloy_eips::eip4788::SYSTEM_ADDRESS);
res.state.remove(&evm.block().coinbase);

// re-set the previous env
evm.context.evm.env = previous_env;

Ok(Some(res))
}

/// Applies the pre-block call to the [EIP-4788] beacon block root contract, using the given block,
/// [`ChainSpec`], EVM.
///
/// If Cancun is not activated or the block is the genesis block, then this is a no-op, and no
/// state changes are made.
///
/// [EIP-4788]: https://eips.ethereum.org/EIPS/eip-4788
#[inline]
pub fn apply_beacon_root_contract_call<EvmConfig, EXT, DB, Spec>(
evm_config: &EvmConfig,
chain_spec: &Spec,
block_timestamp: u64,
block_number: u64,
parent_beacon_block_root: Option<B256>,
evm: &mut Evm<'_, EXT, DB>,
) -> Result<(), BlockExecutionError>
where
DB: Database + DatabaseCommit,
DB::Error: core::fmt::Display,
EvmConfig: ConfigureEvm<Header = Header>,
Spec: EthereumHardforks,
{
if let Some(res) = transact_beacon_root_contract_call(
evm_config,
chain_spec,
block_timestamp,
block_number,
parent_beacon_block_root,
evm,
)? {
evm.context.evm.db.commit(res.state);
}

Ok(())
}

0 comments on commit c0f0dd7

Please sign in to comment.