Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip:feat: add SystemCaller helper type #11068

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions crates/evm/src/system_calls/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! System contract call functions.

mod eip2935;

pub use eip2935::*;
use revm_primitives::ResultAndState;

mod eip4788;
pub use eip4788::*;
Expand All @@ -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<F> 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<EvmConfig, DB, Chainspec, Hook = NoopHook> {
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<Hook>,
}

impl<EvmConfig, DB, Chainspec> SystemCaller<EvmConfig, DB, Chainspec> {
/// 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<EvmConfig, DB, Chainspec, Hook> SystemCaller<EvmConfig, DB, Chainspec, Hook> {
/// Installs a custom hook to be called after each state change.
pub fn with_state_hook<H: OnStateHook>(
self,
hook: H,
) -> SystemCaller<EvmConfig, DB, Chainspec, H> {
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<EvmConfig, DB, Chainspec, Hook> SystemCaller<EvmConfig, DB, Chainspec, Hook> {
// 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<Self, BlockExecutionError> {
// TODO:
// transact
// apply hook
// commit
todo!()
}

// TODO add other system calls
}
Loading