reth_evm/system_calls/
eip4788.rsuse alloc::{boxed::Box, string::ToString};
use crate::ConfigureEvm;
use alloy_eips::eip4788::BEACON_ROOTS_ADDRESS;
use alloy_primitives::B256;
use reth_chainspec::EthereumHardforks;
use reth_execution_errors::{BlockExecutionError, BlockValidationError};
use revm::{interpreter::Host, Database, Evm};
use revm_primitives::ResultAndState;
#[inline]
pub(crate) 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<Option<ResultAndState>, BlockExecutionError>
where
DB: Database,
DB::Error: core::fmt::Display,
EvmConfig: ConfigureEvm,
Spec: EthereumHardforks,
{
if !chain_spec.is_cancun_active_at_timestamp(block_timestamp) {
return Ok(None)
}
let parent_beacon_block_root =
parent_beacon_block_root.ok_or(BlockValidationError::MissingParentBeaconBlockRoot)?;
if block_number == 0 {
if !parent_beacon_block_root.is_zero() {
return Err(BlockValidationError::CancunGenesisParentBeaconBlockRootNotZero {
parent_beacon_block_root,
}
.into())
}
return Ok(None)
}
let previous_env = Box::new(evm.context.env().clone());
evm_config.fill_tx_env_system_contract_call(
&mut evm.context.evm.env,
alloy_eips::eip4788::SYSTEM_ADDRESS,
BEACON_ROOTS_ADDRESS,
parent_beacon_block_root.0.into(),
);
let mut res = match evm.transact() {
Ok(res) => res,
Err(e) => {
evm.context.evm.env = previous_env;
return Err(BlockValidationError::BeaconRootContractCall {
parent_beacon_block_root: Box::new(parent_beacon_block_root),
message: e.to_string(),
}
.into())
}
};
res.state.remove(&alloy_eips::eip4788::SYSTEM_ADDRESS);
res.state.remove(&evm.block().coinbase);
evm.context.evm.env = previous_env;
Ok(Some(res))
}