feat: update SystemCaller (#11718)

This commit is contained in:
Federico Gimenez
2024-10-14 17:52:34 +02:00
committed by GitHub
parent f684dd4c4c
commit 600a394571
11 changed files with 36 additions and 34 deletions

View File

@ -84,7 +84,8 @@ where
EnvWithHandlerCfg::new_with_cfg_env(cfg, block_env, Default::default()), EnvWithHandlerCfg::new_with_cfg_env(cfg, block_env, Default::default()),
); );
let mut system_caller = SystemCaller::new(&self.evm_config, self.provider.chain_spec()); let mut system_caller =
SystemCaller::new(self.evm_config.clone(), self.provider.chain_spec());
// Apply pre-block system contract calls. // Apply pre-block system contract calls.
system_caller.apply_pre_execution_changes(&block.clone().unseal(), &mut evm)?; system_caller.apply_pre_execution_changes(&block.clone().unseal(), &mut evm)?;

View File

@ -286,7 +286,7 @@ where
let mut evm = evm_config.evm_with_env(&mut state, env); let mut evm = evm_config.evm_with_env(&mut state, env);
// apply eip-4788 pre block contract call // apply eip-4788 pre block contract call
let mut system_caller = SystemCaller::new(evm_config, chain_spec); let mut system_caller = SystemCaller::new(evm_config.clone(), chain_spec);
system_caller.apply_beacon_root_contract_call( system_caller.apply_beacon_root_contract_call(
reorg_target.timestamp, reorg_target.timestamp,

View File

@ -141,10 +141,12 @@ where
where where
DB: Database, DB: Database,
DB::Error: Into<ProviderError> + Display, DB::Error: Into<ProviderError> + Display,
F: OnStateHook, F: OnStateHook + 'static,
{ {
let mut system_caller = let mut system_caller = SystemCaller::new(self.evm_config.clone(), &self.chain_spec);
SystemCaller::new(&self.evm_config, &self.chain_spec).with_state_hook(state_hook); if let Some(hook) = state_hook {
system_caller.with_state_hook(Some(Box::new(hook) as Box<dyn OnStateHook>));
}
system_caller.apply_pre_execution_changes(block, &mut evm)?; system_caller.apply_pre_execution_changes(block, &mut evm)?;
@ -290,7 +292,7 @@ where
state_hook: Option<F>, state_hook: Option<F>,
) -> Result<EthExecuteOutput, BlockExecutionError> ) -> Result<EthExecuteOutput, BlockExecutionError>
where where
F: OnStateHook, F: OnStateHook + 'static,
{ {
// 1. prepare state on new block // 1. prepare state on new block
self.on_new_block(&block.header); self.on_new_block(&block.header);
@ -396,7 +398,7 @@ where
state_hook: F, state_hook: F,
) -> Result<Self::Output, Self::Error> ) -> Result<Self::Output, Self::Error>
where where
F: OnStateHook, F: OnStateHook + 'static,
{ {
let BlockExecutionInput { block, total_difficulty } = input; let BlockExecutionInput { block, total_difficulty } = input;
let EthExecuteOutput { receipts, requests, gas_used } = self let EthExecuteOutput { receipts, requests, gas_used } = self

View File

@ -160,7 +160,7 @@ where
let block_number = initialized_block_env.number.to::<u64>(); let block_number = initialized_block_env.number.to::<u64>();
let mut system_caller = SystemCaller::new(&evm_config, chain_spec.clone()); let mut system_caller = SystemCaller::new(evm_config.clone(), chain_spec.clone());
// apply eip-4788 pre block contract call // apply eip-4788 pre block contract call
system_caller system_caller

View File

@ -97,7 +97,7 @@ where
state_hook: F, state_hook: F,
) -> Result<Self::Output, Self::Error> ) -> Result<Self::Output, Self::Error>
where where
F: OnStateHook, F: OnStateHook + 'static,
{ {
match self { match self {
Self::Left(a) => a.execute_with_state_hook(input, state_hook), Self::Left(a) => a.execute_with_state_hook(input, state_hook),

View File

@ -56,7 +56,7 @@ pub trait Executor<DB> {
state_hook: F, state_hook: F,
) -> Result<Self::Output, Self::Error> ) -> Result<Self::Output, Self::Error>
where where
F: OnStateHook; F: OnStateHook + 'static;
} }
/// A general purpose executor that can execute multiple inputs in sequence, validate the outputs, /// A general purpose executor that can execute multiple inputs in sequence, validate the outputs,

View File

@ -1,7 +1,7 @@
//! System contract call functions. //! System contract call functions.
use crate::ConfigureEvm; use crate::ConfigureEvm;
use alloc::vec::Vec; use alloc::{boxed::Box, vec::Vec};
use core::fmt::Display; use core::fmt::Display;
use reth_chainspec::EthereumHardforks; use reth_chainspec::EthereumHardforks;
use reth_execution_errors::BlockExecutionError; use reth_execution_errors::BlockExecutionError;
@ -42,27 +42,26 @@ impl OnStateHook for NoopHook {
/// ///
/// This can be used to chain system transaction calls. /// This can be used to chain system transaction calls.
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct SystemCaller<'a, EvmConfig, Chainspec, Hook = NoopHook> { pub struct SystemCaller<EvmConfig, Chainspec> {
evm_config: &'a EvmConfig, evm_config: EvmConfig,
chain_spec: Chainspec, chain_spec: Chainspec,
/// Optional hook to be called after each state change. /// Optional hook to be called after each state change.
hook: Option<Hook>, hook: Option<Box<dyn OnStateHook>>,
} }
impl<'a, EvmConfig, Chainspec> SystemCaller<'a, EvmConfig, Chainspec, NoopHook> { impl<EvmConfig, Chainspec> SystemCaller<EvmConfig, Chainspec> {
/// Create a new system caller with the given EVM config, database, and chain spec, and creates /// 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. /// the EVM with the given initialized config and block environment.
pub const fn new(evm_config: &'a EvmConfig, chain_spec: Chainspec) -> Self { pub const fn new(evm_config: EvmConfig, chain_spec: Chainspec) -> Self {
Self { evm_config, chain_spec, hook: None } Self { evm_config, chain_spec, hook: None }
} }
/// Installs a custom hook to be called after each state change. /// Installs a custom hook to be called after each state change.
pub fn with_state_hook<H: OnStateHook>( pub fn with_state_hook(&mut self, hook: Option<Box<dyn OnStateHook>>) -> &mut Self {
self, self.hook = hook;
hook: Option<H>, self
) -> SystemCaller<'a, EvmConfig, Chainspec, H> {
let Self { evm_config, chain_spec, .. } = self;
SystemCaller { evm_config, chain_spec, hook }
} }
/// Convenience method to consume the type and drop borrowed fields /// Convenience method to consume the type and drop borrowed fields
pub fn finish(self) {} pub fn finish(self) {}
} }
@ -85,11 +84,10 @@ where
.build() .build()
} }
impl<EvmConfig, Chainspec, Hook> SystemCaller<'_, EvmConfig, Chainspec, Hook> impl<EvmConfig, Chainspec> SystemCaller<EvmConfig, Chainspec>
where where
EvmConfig: ConfigureEvm<Header = Header>, EvmConfig: ConfigureEvm<Header = Header>,
Chainspec: EthereumHardforks, Chainspec: EthereumHardforks,
Hook: OnStateHook,
{ {
/// Apply pre execution changes. /// Apply pre execution changes.
pub fn apply_pre_execution_changes<DB, Ext>( pub fn apply_pre_execution_changes<DB, Ext>(

View File

@ -121,10 +121,12 @@ where
) -> Result<(Vec<Receipt>, u64), BlockExecutionError> ) -> Result<(Vec<Receipt>, u64), BlockExecutionError>
where where
DB: Database<Error: Into<ProviderError> + Display>, DB: Database<Error: Into<ProviderError> + Display>,
F: OnStateHook, F: OnStateHook + 'static,
{ {
let mut system_caller = let mut system_caller = SystemCaller::new(self.evm_config.clone(), &self.chain_spec);
SystemCaller::new(&self.evm_config, &self.chain_spec).with_state_hook(state_hook); if let Some(hook) = state_hook {
system_caller.with_state_hook(Some(Box::new(hook) as Box<dyn OnStateHook>));
}
// apply pre execution changes // apply pre execution changes
system_caller.apply_beacon_root_contract_call( system_caller.apply_beacon_root_contract_call(
@ -306,7 +308,7 @@ where
state_hook: Option<F>, state_hook: Option<F>,
) -> Result<(Vec<Receipt>, u64), BlockExecutionError> ) -> Result<(Vec<Receipt>, u64), BlockExecutionError>
where where
F: OnStateHook, F: OnStateHook + 'static,
{ {
// 1. prepare state on new block // 1. prepare state on new block
self.on_new_block(&block.header); self.on_new_block(&block.header);
@ -410,7 +412,7 @@ where
state_hook: F, state_hook: F,
) -> Result<Self::Output, Self::Error> ) -> Result<Self::Output, Self::Error>
where where
F: OnStateHook, F: OnStateHook + 'static,
{ {
let BlockExecutionInput { block, total_difficulty } = input; let BlockExecutionInput { block, total_difficulty } = input;
let (receipts, gas_used) = self.execute_without_verification_with_state_hook( let (receipts, gas_used) = self.execute_without_verification_with_state_hook(

View File

@ -201,7 +201,7 @@ where
); );
// apply eip-4788 pre block contract call // apply eip-4788 pre block contract call
let mut system_caller = SystemCaller::new(&evm_config, &chain_spec); let mut system_caller = SystemCaller::new(evm_config.clone(), &chain_spec);
system_caller system_caller
.pre_block_beacon_root_contract_call( .pre_block_beacon_root_contract_call(

View File

@ -260,8 +260,7 @@ pub trait LoadPendingBlock: EthApiTypes {
let chain_spec = self.provider().chain_spec(); let chain_spec = self.provider().chain_spec();
let evm_config = self.evm_config().clone(); let mut system_caller = SystemCaller::new(self.evm_config().clone(), chain_spec.clone());
let mut system_caller = SystemCaller::new(&evm_config, chain_spec.clone());
let parent_beacon_block_root = if origin.is_actual_pending() { let parent_beacon_block_root = if origin.is_actual_pending() {
// apply eip-4788 pre block contract call if we got the block from the CL with the real // apply eip-4788 pre block contract call if we got the block from the CL with the real

View File

@ -199,7 +199,7 @@ pub trait Trace: LoadState {
// apply relevant system calls // apply relevant system calls
let mut system_caller = SystemCaller::new( let mut system_caller = SystemCaller::new(
Trace::evm_config(&this), Trace::evm_config(&this).clone(),
LoadState::provider(&this).chain_spec(), LoadState::provider(&this).chain_spec(),
); );
system_caller system_caller
@ -332,7 +332,7 @@ pub trait Trace: LoadState {
// apply relevant system calls // apply relevant system calls
let mut system_caller = SystemCaller::new( let mut system_caller = SystemCaller::new(
Trace::evm_config(&this), Trace::evm_config(&this).clone(),
LoadState::provider(&this).chain_spec(), LoadState::provider(&this).chain_spec(),
); );
system_caller system_caller