From 63a4aee524b55b2954fe3968ddf2e1d7b8e8b923 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 20 Sep 2024 11:41:29 +0200 Subject: [PATCH] feat: add SystemCaller helper type --- crates/evm/src/system_calls/mod.rs | 76 ++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/crates/evm/src/system_calls/mod.rs b/crates/evm/src/system_calls/mod.rs index 50d5c4c857ff..ec91779e7c71 100644 --- a/crates/evm/src/system_calls/mod.rs +++ b/crates/evm/src/system_calls/mod.rs @@ -1,7 +1,9 @@ //! System contract call functions. mod eip2935; + pub use eip2935::*; +use revm_primitives::ResultAndState; mod eip4788; pub use eip4788::*; @@ -11,3 +13,77 @@ pub use eip7002::*; mod eip7251; pub use eip7251::*; +use reth_execution_errors::BlockExecutionError; + +/// A hook that is called after each state change. +// TODO impl for &mut +pub trait OnStateHook { + /// Invoked with the result and state after each system call. + fn on_state(&mut self, state: &ResultAndState); +} + +impl OnStateHook for F +where + F: FnMut(&ResultAndState), +{ + fn on_state(&mut self, state: &ResultAndState) { + self(state) + } +} + +/// An [`OnStateHook`] that does nothing. +#[derive(Default, Debug, Clone)] +#[non_exhaustive] +pub struct NoopHook; + +impl OnStateHook for NoopHook { + fn on_state(&mut self, _state: &ResultAndState) {} +} + +/// An ephemeral helper type for executing system calls. +/// +/// This can be used to chain system transaction calls. +#[derive(Debug)] +pub struct SystemCaller { + evm_config: EvmConfig, + db: DB, + chain_spec: Chainspec, + /// Optional hook to be called after each state change. + // TODO do we want this optional? + hook: Option, +} + +impl SystemCaller { + /// Create a new system caller with the given EVM config, database, and chain spec. + pub fn new(evm_config: EvmConfig, db: DB, chain_spec: Chainspec) -> Self { + Self { evm_config, db, chain_spec, hook: None } + } +} + +impl SystemCaller { + /// Installs a custom hook to be called after each state change. + pub fn with_state_hook( + self, + hook: H, + ) -> SystemCaller { + let Self { evm_config, db, chain_spec, .. } = self; + SystemCaller { evm_config, db, chain_spec, hook: Some(hook) } + } + /// Convenience type to consume the type and drop borrowed fields + pub fn finish(self) {} +} + +impl SystemCaller { + // TODO add apply functions that are chainable + + /// Applies the pre-block call to the EIP-2935 blockhashes contract. + pub fn pre_block_blockhashes_contract_call(self) -> Result { + // TODO: + // transact + // apply hook + // commit + todo!() + } + + // TODO add other system calls +}