revm/handler/handle_types/
pre_execution.rs

1// Includes.
2use super::{GenericContextHandle, GenericContextHandleRet};
3use crate::{
4    handler::mainnet,
5    primitives::{db::Database, EVMError, Spec},
6    Context, ContextPrecompiles,
7};
8use std::sync::Arc;
9
10/// Loads precompiles into Evm
11pub type LoadPrecompilesHandle<'a, DB> = Arc<dyn Fn() -> ContextPrecompiles<DB> + 'a>;
12
13/// Load access list accounts and beneficiary.
14/// There is no need to load Caller as it is assumed that
15/// it will be loaded in DeductCallerHandle.
16pub type LoadAccountsHandle<'a, EXT, DB> = GenericContextHandle<'a, EXT, DB>;
17
18/// Deduct the caller to its limit.
19pub type DeductCallerHandle<'a, EXT, DB> = GenericContextHandle<'a, EXT, DB>;
20
21/// Load Auth list for EIP-7702, and returns number of created accounts.
22pub type ApplyEIP7702AuthListHandle<'a, EXT, DB> = GenericContextHandleRet<'a, EXT, DB, u64>;
23
24/// Handles related to pre execution before the stack loop is started.
25pub struct PreExecutionHandler<'a, EXT, DB: Database> {
26    /// Load precompiles
27    pub load_precompiles: LoadPrecompilesHandle<'a, DB>,
28    /// Main load handle
29    pub load_accounts: LoadAccountsHandle<'a, EXT, DB>,
30    /// Deduct max value from the caller.
31    pub deduct_caller: DeductCallerHandle<'a, EXT, DB>,
32    /// Apply EIP-7702 auth list
33    pub apply_eip7702_auth_list: ApplyEIP7702AuthListHandle<'a, EXT, DB>,
34}
35
36impl<'a, EXT: 'a, DB: Database + 'a> PreExecutionHandler<'a, EXT, DB> {
37    /// Creates mainnet MainHandles.
38    pub fn new<SPEC: Spec + 'a>() -> Self {
39        Self {
40            load_precompiles: Arc::new(mainnet::load_precompiles::<SPEC, DB>),
41            load_accounts: Arc::new(mainnet::load_accounts::<SPEC, EXT, DB>),
42            deduct_caller: Arc::new(mainnet::deduct_caller::<SPEC, EXT, DB>),
43            apply_eip7702_auth_list: Arc::new(mainnet::apply_eip7702_auth_list::<SPEC, EXT, DB>),
44        }
45    }
46}
47
48impl<EXT, DB: Database> PreExecutionHandler<'_, EXT, DB> {
49    /// Deduct caller to its limit.
50    pub fn deduct_caller(&self, context: &mut Context<EXT, DB>) -> Result<(), EVMError<DB::Error>> {
51        (self.deduct_caller)(context)
52    }
53
54    /// Main load
55    pub fn load_accounts(&self, context: &mut Context<EXT, DB>) -> Result<(), EVMError<DB::Error>> {
56        (self.load_accounts)(context)
57    }
58
59    /// Apply EIP-7702 auth list and return gas refund on account that were already present.
60    pub fn apply_eip7702_auth_list(
61        &self,
62        context: &mut Context<EXT, DB>,
63    ) -> Result<u64, EVMError<DB::Error>> {
64        (self.apply_eip7702_auth_list)(context)
65    }
66
67    /// Load precompiles
68    pub fn load_precompiles(&self) -> ContextPrecompiles<DB> {
69        (self.load_precompiles)()
70    }
71}