revm/handler/handle_types/
validation.rs

1use crate::{
2    handler::mainnet,
3    primitives::{db::Database, EVMError, Env, Spec},
4    Context,
5};
6use std::sync::Arc;
7
8/// Handle that validates env.
9pub type ValidateEnvHandle<'a, DB> =
10    Arc<dyn Fn(&Env) -> Result<(), EVMError<<DB as Database>::Error>> + 'a>;
11
12/// Handle that validates transaction environment against the state.
13/// Second parametar is initial gas.
14pub type ValidateTxEnvAgainstState<'a, EXT, DB> =
15    Arc<dyn Fn(&mut Context<EXT, DB>) -> Result<(), EVMError<<DB as Database>::Error>> + 'a>;
16
17/// Initial gas calculation handle
18pub type ValidateInitialTxGasHandle<'a, DB> =
19    Arc<dyn Fn(&Env) -> Result<u64, EVMError<<DB as Database>::Error>> + 'a>;
20
21/// Handles related to validation.
22pub struct ValidationHandler<'a, EXT, DB: Database> {
23    /// Validate and calculate initial transaction gas.
24    pub initial_tx_gas: ValidateInitialTxGasHandle<'a, DB>,
25    /// Validate transactions against state data.
26    pub tx_against_state: ValidateTxEnvAgainstState<'a, EXT, DB>,
27    /// Validate Env.
28    pub env: ValidateEnvHandle<'a, DB>,
29}
30
31impl<'a, EXT: 'a, DB: Database + 'a> ValidationHandler<'a, EXT, DB> {
32    /// Create new ValidationHandles
33    pub fn new<SPEC: Spec + 'a>() -> Self {
34        Self {
35            initial_tx_gas: Arc::new(mainnet::validate_initial_tx_gas::<SPEC, DB>),
36            env: Arc::new(mainnet::validate_env::<SPEC, DB>),
37            tx_against_state: Arc::new(mainnet::validate_tx_against_state::<SPEC, EXT, DB>),
38        }
39    }
40}
41
42impl<EXT, DB: Database> ValidationHandler<'_, EXT, DB> {
43    /// Validate env.
44    pub fn env(&self, env: &Env) -> Result<(), EVMError<DB::Error>> {
45        (self.env)(env)
46    }
47
48    /// Initial gas
49    pub fn initial_tx_gas(&self, env: &Env) -> Result<u64, EVMError<DB::Error>> {
50        (self.initial_tx_gas)(env)
51    }
52
53    /// Validate ttansaction against the state.
54    pub fn tx_against_state(
55        &self,
56        context: &mut Context<EXT, DB>,
57    ) -> Result<(), EVMError<DB::Error>> {
58        (self.tx_against_state)(context)
59    }
60}