mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
refactor: simplify SystemCaller API (#14578)
This commit is contained in:
@ -23,18 +23,16 @@ use revm::context_interface::result::ResultAndState;
|
||||
#[inline]
|
||||
pub(crate) fn transact_blockhashes_contract_call<Halt>(
|
||||
chain_spec: impl EthereumHardforks,
|
||||
block_timestamp: u64,
|
||||
block_number: u64,
|
||||
parent_block_hash: B256,
|
||||
evm: &mut impl Evm<HaltReason = Halt>,
|
||||
) -> Result<Option<ResultAndState<Halt>>, BlockExecutionError> {
|
||||
if !chain_spec.is_prague_active_at_timestamp(block_timestamp) {
|
||||
if !chain_spec.is_prague_active_at_timestamp(evm.block().timestamp) {
|
||||
return Ok(None)
|
||||
}
|
||||
|
||||
// if the block number is zero (genesis block) then no system transaction may occur as per
|
||||
// EIP-2935
|
||||
if block_number == 0 {
|
||||
if evm.block().number == 0 {
|
||||
return Ok(None)
|
||||
}
|
||||
|
||||
|
||||
@ -20,12 +20,10 @@ use revm::context_interface::result::ResultAndState;
|
||||
#[inline]
|
||||
pub(crate) fn transact_beacon_root_contract_call<Halt>(
|
||||
chain_spec: impl EthereumHardforks,
|
||||
block_timestamp: u64,
|
||||
block_number: u64,
|
||||
parent_beacon_block_root: Option<B256>,
|
||||
evm: &mut impl Evm<Error: Display, HaltReason = Halt>,
|
||||
) -> Result<Option<ResultAndState<Halt>>, BlockExecutionError> {
|
||||
if !chain_spec.is_cancun_active_at_timestamp(block_timestamp) {
|
||||
if !chain_spec.is_cancun_active_at_timestamp(evm.block().timestamp) {
|
||||
return Ok(None)
|
||||
}
|
||||
|
||||
@ -34,7 +32,7 @@ pub(crate) fn transact_beacon_root_contract_call<Halt>(
|
||||
|
||||
// if the block number is zero (genesis block) then the parent beacon block root must
|
||||
// be 0x0 and no system transaction may occur as per EIP-4788
|
||||
if block_number == 0 {
|
||||
if evm.block().number == 0 {
|
||||
if !parent_beacon_block_root.is_zero() {
|
||||
return Err(BlockValidationError::CancunGenesisParentBeaconBlockRootNotZero {
|
||||
parent_beacon_block_root,
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
//! System contract call functions.
|
||||
|
||||
use crate::{ConfigureEvm, Database, Evm, EvmEnv};
|
||||
use alloc::{boxed::Box, sync::Arc};
|
||||
use crate::Evm;
|
||||
use alloc::boxed::Box;
|
||||
use alloy_consensus::BlockHeader;
|
||||
use alloy_eips::{
|
||||
eip7002::WITHDRAWAL_REQUEST_TYPE, eip7251::CONSOLIDATION_REQUEST_TYPE, eip7685::Requests,
|
||||
@ -78,18 +78,17 @@ impl OnStateHook for NoopHook {
|
||||
///
|
||||
/// This can be used to chain system transaction calls.
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct SystemCaller<EvmConfig, Chainspec> {
|
||||
evm_config: EvmConfig,
|
||||
chain_spec: Arc<Chainspec>,
|
||||
pub struct SystemCaller<ChainSpec> {
|
||||
chain_spec: ChainSpec,
|
||||
/// Optional hook to be called after each state change.
|
||||
hook: Option<Box<dyn OnStateHook>>,
|
||||
}
|
||||
|
||||
impl<EvmConfig, Chainspec> SystemCaller<EvmConfig, Chainspec> {
|
||||
impl<ChainSpec> SystemCaller<ChainSpec> {
|
||||
/// Create a new system caller with the given EVM config, database, and chain spec, and creates
|
||||
/// the EVM with the given initialized config and block environment.
|
||||
pub const fn new(evm_config: EvmConfig, chain_spec: Arc<Chainspec>) -> Self {
|
||||
Self { evm_config, chain_spec, hook: None }
|
||||
pub const fn new(chain_spec: ChainSpec) -> Self {
|
||||
Self { chain_spec, hook: None }
|
||||
}
|
||||
|
||||
/// Installs a custom hook to be called after each state change.
|
||||
@ -102,29 +101,18 @@ impl<EvmConfig, Chainspec> SystemCaller<EvmConfig, Chainspec> {
|
||||
pub fn finish(self) {}
|
||||
}
|
||||
|
||||
impl<EvmConfig, Chainspec> SystemCaller<EvmConfig, Chainspec>
|
||||
impl<Chainspec> SystemCaller<Chainspec>
|
||||
where
|
||||
EvmConfig: ConfigureEvm,
|
||||
Chainspec: EthereumHardforks,
|
||||
{
|
||||
/// Apply pre execution changes.
|
||||
pub fn apply_pre_execution_changes(
|
||||
&mut self,
|
||||
header: &EvmConfig::Header,
|
||||
header: impl BlockHeader,
|
||||
evm: &mut impl Evm<DB: DatabaseCommit, Error: Display>,
|
||||
) -> Result<(), BlockExecutionError> {
|
||||
self.apply_blockhashes_contract_call(
|
||||
header.timestamp(),
|
||||
header.number(),
|
||||
header.parent_hash(),
|
||||
evm,
|
||||
)?;
|
||||
self.apply_beacon_root_contract_call(
|
||||
header.timestamp(),
|
||||
header.number(),
|
||||
header.parent_beacon_block_root(),
|
||||
evm,
|
||||
)?;
|
||||
self.apply_blockhashes_contract_call(header.parent_hash(), evm)?;
|
||||
self.apply_beacon_root_contract_call(header.parent_beacon_block_root(), evm)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -151,45 +139,14 @@ where
|
||||
Ok(requests)
|
||||
}
|
||||
|
||||
/// Applies the pre-block call to the EIP-2935 blockhashes contract.
|
||||
pub fn pre_block_blockhashes_contract_call<DB>(
|
||||
&mut self,
|
||||
db: &mut DB,
|
||||
evm_env: &EvmEnv<EvmConfig::Spec>,
|
||||
parent_block_hash: B256,
|
||||
) -> Result<(), BlockExecutionError>
|
||||
where
|
||||
DB: Database + DatabaseCommit,
|
||||
DB::Error: Display,
|
||||
{
|
||||
let evm_config = self.evm_config.clone();
|
||||
let mut evm = evm_config.evm_with_env(db, evm_env.clone());
|
||||
|
||||
self.apply_blockhashes_contract_call(
|
||||
evm_env.block_env.timestamp,
|
||||
evm_env.block_env.number,
|
||||
parent_block_hash,
|
||||
&mut evm,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Applies the pre-block call to the EIP-2935 blockhashes contract.
|
||||
pub fn apply_blockhashes_contract_call(
|
||||
&mut self,
|
||||
timestamp: u64,
|
||||
block_number: u64,
|
||||
parent_block_hash: B256,
|
||||
evm: &mut impl Evm<DB: DatabaseCommit, Error: Display>,
|
||||
evm: &mut impl Evm<DB: DatabaseCommit>,
|
||||
) -> Result<(), BlockExecutionError> {
|
||||
let result_and_state = eip2935::transact_blockhashes_contract_call(
|
||||
&self.chain_spec,
|
||||
timestamp,
|
||||
block_number,
|
||||
parent_block_hash,
|
||||
evm,
|
||||
)?;
|
||||
let result_and_state =
|
||||
eip2935::transact_blockhashes_contract_call(&self.chain_spec, parent_block_hash, evm)?;
|
||||
|
||||
if let Some(res) = result_and_state {
|
||||
if let Some(ref mut hook) = self.hook {
|
||||
@ -204,43 +161,15 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Applies the pre-block call to the EIP-4788 beacon root contract.
|
||||
pub fn pre_block_beacon_root_contract_call<DB>(
|
||||
&mut self,
|
||||
db: &mut DB,
|
||||
evm_env: &EvmEnv<EvmConfig::Spec>,
|
||||
parent_beacon_block_root: Option<B256>,
|
||||
) -> Result<(), BlockExecutionError>
|
||||
where
|
||||
DB: Database + DatabaseCommit,
|
||||
DB::Error: Display,
|
||||
{
|
||||
let evm_config = self.evm_config.clone();
|
||||
let mut evm = evm_config.evm_with_env(db, evm_env.clone());
|
||||
|
||||
self.apply_beacon_root_contract_call(
|
||||
evm_env.block_env.timestamp,
|
||||
evm_env.block_env.number,
|
||||
parent_beacon_block_root,
|
||||
&mut evm,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Applies the pre-block call to the EIP-4788 beacon root contract.
|
||||
pub fn apply_beacon_root_contract_call(
|
||||
&mut self,
|
||||
timestamp: u64,
|
||||
block_number: u64,
|
||||
parent_block_hash: Option<B256>,
|
||||
evm: &mut impl Evm<DB: DatabaseCommit, Error: Display>,
|
||||
parent_beacon_block_root: Option<B256>,
|
||||
evm: &mut impl Evm<DB: DatabaseCommit>,
|
||||
) -> Result<(), BlockExecutionError> {
|
||||
let result_and_state = eip4788::transact_beacon_root_contract_call(
|
||||
&self.chain_spec,
|
||||
timestamp,
|
||||
block_number,
|
||||
parent_block_hash,
|
||||
parent_beacon_block_root,
|
||||
evm,
|
||||
)?;
|
||||
|
||||
@ -257,24 +186,6 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Applies the post-block call to the EIP-7002 withdrawal request contract.
|
||||
pub fn post_block_withdrawal_requests_contract_call<DB>(
|
||||
&mut self,
|
||||
db: &mut DB,
|
||||
evm_env: &EvmEnv<EvmConfig::Spec>,
|
||||
) -> Result<Bytes, BlockExecutionError>
|
||||
where
|
||||
DB: Database + DatabaseCommit,
|
||||
DB::Error: Display,
|
||||
{
|
||||
let evm_config = self.evm_config.clone();
|
||||
let mut evm = evm_config.evm_with_env(db, evm_env.clone());
|
||||
|
||||
let result = self.apply_withdrawal_requests_contract_call(&mut evm)?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Applies the post-block call to the EIP-7002 withdrawal request contract.
|
||||
pub fn apply_withdrawal_requests_contract_call(
|
||||
&mut self,
|
||||
@ -295,23 +206,6 @@ where
|
||||
eip7002::post_commit(result_and_state.result)
|
||||
}
|
||||
|
||||
/// Applies the post-block call to the EIP-7251 consolidation requests contract.
|
||||
pub fn post_block_consolidation_requests_contract_call<DB>(
|
||||
&mut self,
|
||||
db: &mut DB,
|
||||
evm_env: &EvmEnv<EvmConfig::Spec>,
|
||||
) -> Result<Bytes, BlockExecutionError>
|
||||
where
|
||||
DB: Database + DatabaseCommit,
|
||||
{
|
||||
let evm_config = self.evm_config.clone();
|
||||
let mut evm = evm_config.evm_with_env(db, evm_env.clone());
|
||||
|
||||
let res = self.apply_consolidation_requests_contract_call(&mut evm)?;
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
/// Applies the post-block call to the EIP-7251 consolidation requests contract.
|
||||
pub fn apply_consolidation_requests_contract_call(
|
||||
&mut self,
|
||||
|
||||
Reference in New Issue
Block a user