Skip to content

Commit

Permalink
feat: update SystemCaller (paradigmxyz#11718)
Browse files Browse the repository at this point in the history
  • Loading branch information
fgimenez authored Oct 14, 2024
1 parent f684dd4 commit 600a394
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 34 deletions.
3 changes: 2 additions & 1 deletion crates/engine/invalid-block-hooks/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ where
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.
system_caller.apply_pre_execution_changes(&block.clone().unseal(), &mut evm)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/util/src/reorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ where
let mut evm = evm_config.evm_with_env(&mut state, env);

// 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(
reorg_target.timestamp,
Expand Down
12 changes: 7 additions & 5 deletions crates/ethereum/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,12 @@ where
where
DB: Database,
DB::Error: Into<ProviderError> + Display,
F: OnStateHook,
F: OnStateHook + 'static,
{
let mut system_caller =
SystemCaller::new(&self.evm_config, &self.chain_spec).with_state_hook(state_hook);
let mut system_caller = SystemCaller::new(self.evm_config.clone(), &self.chain_spec);
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)?;

Expand Down Expand Up @@ -290,7 +292,7 @@ where
state_hook: Option<F>,
) -> Result<EthExecuteOutput, BlockExecutionError>
where
F: OnStateHook,
F: OnStateHook + 'static,
{
// 1. prepare state on new block
self.on_new_block(&block.header);
Expand Down Expand Up @@ -396,7 +398,7 @@ where
state_hook: F,
) -> Result<Self::Output, Self::Error>
where
F: OnStateHook,
F: OnStateHook + 'static,
{
let BlockExecutionInput { block, total_difficulty } = input;
let EthExecuteOutput { receipts, requests, gas_used } = self
Expand Down
2 changes: 1 addition & 1 deletion crates/ethereum/payload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ where

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
system_caller
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/src/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ where
state_hook: F,
) -> Result<Self::Output, Self::Error>
where
F: OnStateHook,
F: OnStateHook + 'static,
{
match self {
Self::Left(a) => a.execute_with_state_hook(input, state_hook),
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub trait Executor<DB> {
state_hook: F,
) -> Result<Self::Output, Self::Error>
where
F: OnStateHook;
F: OnStateHook + 'static;
}

/// A general purpose executor that can execute multiple inputs in sequence, validate the outputs,
Expand Down
26 changes: 12 additions & 14 deletions crates/evm/src/system_calls/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! System contract call functions.
use crate::ConfigureEvm;
use alloc::vec::Vec;
use alloc::{boxed::Box, vec::Vec};
use core::fmt::Display;
use reth_chainspec::EthereumHardforks;
use reth_execution_errors::BlockExecutionError;
Expand Down Expand Up @@ -42,27 +42,26 @@ impl OnStateHook for NoopHook {
///
/// This can be used to chain system transaction calls.
#[allow(missing_debug_implementations)]
pub struct SystemCaller<'a, EvmConfig, Chainspec, Hook = NoopHook> {
evm_config: &'a EvmConfig,
pub struct SystemCaller<EvmConfig, Chainspec> {
evm_config: EvmConfig,
chain_spec: Chainspec,
/// 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
/// 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 }
}

/// Installs a custom hook to be called after each state change.
pub fn with_state_hook<H: OnStateHook>(
self,
hook: Option<H>,
) -> SystemCaller<'a, EvmConfig, Chainspec, H> {
let Self { evm_config, chain_spec, .. } = self;
SystemCaller { evm_config, chain_spec, hook }
pub fn with_state_hook(&mut self, hook: Option<Box<dyn OnStateHook>>) -> &mut Self {
self.hook = hook;
self
}

/// Convenience method to consume the type and drop borrowed fields
pub fn finish(self) {}
}
Expand All @@ -85,11 +84,10 @@ where
.build()
}

impl<EvmConfig, Chainspec, Hook> SystemCaller<'_, EvmConfig, Chainspec, Hook>
impl<EvmConfig, Chainspec> SystemCaller<EvmConfig, Chainspec>
where
EvmConfig: ConfigureEvm<Header = Header>,
Chainspec: EthereumHardforks,
Hook: OnStateHook,
{
/// Apply pre execution changes.
pub fn apply_pre_execution_changes<DB, Ext>(
Expand Down
12 changes: 7 additions & 5 deletions crates/optimism/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@ where
) -> Result<(Vec<Receipt>, u64), BlockExecutionError>
where
DB: Database<Error: Into<ProviderError> + Display>,
F: OnStateHook,
F: OnStateHook + 'static,
{
let mut system_caller =
SystemCaller::new(&self.evm_config, &self.chain_spec).with_state_hook(state_hook);
let mut system_caller = SystemCaller::new(self.evm_config.clone(), &self.chain_spec);
if let Some(hook) = state_hook {
system_caller.with_state_hook(Some(Box::new(hook) as Box<dyn OnStateHook>));
}

// apply pre execution changes
system_caller.apply_beacon_root_contract_call(
Expand Down Expand Up @@ -306,7 +308,7 @@ where
state_hook: Option<F>,
) -> Result<(Vec<Receipt>, u64), BlockExecutionError>
where
F: OnStateHook,
F: OnStateHook + 'static,
{
// 1. prepare state on new block
self.on_new_block(&block.header);
Expand Down Expand Up @@ -410,7 +412,7 @@ where
state_hook: F,
) -> Result<Self::Output, Self::Error>
where
F: OnStateHook,
F: OnStateHook + 'static,
{
let BlockExecutionInput { block, total_difficulty } = input;
let (receipts, gas_used) = self.execute_without_verification_with_state_hook(
Expand Down
2 changes: 1 addition & 1 deletion crates/optimism/payload/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ where
);

// 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
.pre_block_beacon_root_contract_call(
Expand Down
3 changes: 1 addition & 2 deletions crates/rpc/rpc-eth-api/src/helpers/pending_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,7 @@ pub trait LoadPendingBlock: EthApiTypes {

let chain_spec = self.provider().chain_spec();

let evm_config = self.evm_config().clone();
let mut system_caller = SystemCaller::new(&evm_config, chain_spec.clone());
let mut system_caller = SystemCaller::new(self.evm_config().clone(), chain_spec.clone());

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
Expand Down
4 changes: 2 additions & 2 deletions crates/rpc/rpc-eth-api/src/helpers/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ pub trait Trace: LoadState {

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

// apply relevant system calls
let mut system_caller = SystemCaller::new(
Trace::evm_config(&this),
Trace::evm_config(&this).clone(),
LoadState::provider(&this).chain_spec(),
);
system_caller
Expand Down

0 comments on commit 600a394

Please sign in to comment.