reth_evm/
either.rs

1//! Helper type that represents one of two possible executor types
2
3use crate::{
4    execute::{BlockExecutorProvider, Executor},
5    Database, OnStateHook,
6};
7
8// re-export Either
9pub use futures_util::future::Either;
10use reth_execution_types::{BlockExecutionOutput, BlockExecutionResult};
11use reth_primitives_traits::{NodePrimitives, RecoveredBlock};
12
13impl<A, B> BlockExecutorProvider for Either<A, B>
14where
15    A: BlockExecutorProvider,
16    B: BlockExecutorProvider<Primitives = A::Primitives>,
17{
18    type Primitives = A::Primitives;
19
20    type Executor<DB: Database> = Either<A::Executor<DB>, B::Executor<DB>>;
21
22    fn executor<DB>(&self, db: DB) -> Self::Executor<DB>
23    where
24        DB: Database,
25    {
26        match self {
27            Self::Left(a) => Either::Left(a.executor(db)),
28            Self::Right(b) => Either::Right(b.executor(db)),
29        }
30    }
31}
32
33impl<A, B, DB> Executor<DB> for Either<A, B>
34where
35    A: Executor<DB>,
36    B: Executor<DB, Primitives = A::Primitives, Error = A::Error>,
37    DB: Database,
38{
39    type Primitives = A::Primitives;
40    type Error = A::Error;
41
42    fn execute_one(
43        &mut self,
44        block: &RecoveredBlock<<Self::Primitives as NodePrimitives>::Block>,
45    ) -> Result<BlockExecutionResult<<Self::Primitives as NodePrimitives>::Receipt>, Self::Error>
46    {
47        match self {
48            Self::Left(a) => a.execute_one(block),
49            Self::Right(b) => b.execute_one(block),
50        }
51    }
52
53    fn execute_one_with_state_hook<F>(
54        &mut self,
55        block: &RecoveredBlock<<Self::Primitives as NodePrimitives>::Block>,
56        state_hook: F,
57    ) -> Result<BlockExecutionResult<<Self::Primitives as NodePrimitives>::Receipt>, Self::Error>
58    where
59        F: OnStateHook + 'static,
60    {
61        match self {
62            Self::Left(a) => a.execute_one_with_state_hook(block, state_hook),
63            Self::Right(b) => b.execute_one_with_state_hook(block, state_hook),
64        }
65    }
66
67    fn execute(
68        self,
69        block: &RecoveredBlock<<Self::Primitives as NodePrimitives>::Block>,
70    ) -> Result<BlockExecutionOutput<<Self::Primitives as NodePrimitives>::Receipt>, Self::Error>
71    {
72        match self {
73            Self::Left(a) => a.execute(block),
74            Self::Right(b) => b.execute(block),
75        }
76    }
77
78    fn execute_with_state_closure<F>(
79        self,
80        block: &RecoveredBlock<<Self::Primitives as NodePrimitives>::Block>,
81        state: F,
82    ) -> Result<BlockExecutionOutput<<Self::Primitives as NodePrimitives>::Receipt>, Self::Error>
83    where
84        F: FnMut(&revm_database::State<DB>),
85    {
86        match self {
87            Self::Left(a) => a.execute_with_state_closure(block, state),
88            Self::Right(b) => b.execute_with_state_closure(block, state),
89        }
90    }
91
92    fn into_state(self) -> revm_database::State<DB> {
93        match self {
94            Self::Left(a) => a.into_state(),
95            Self::Right(b) => b.into_state(),
96        }
97    }
98
99    fn size_hint(&self) -> usize {
100        match self {
101            Self::Left(a) => a.size_hint(),
102            Self::Right(b) => b.size_hint(),
103        }
104    }
105}