Skip to main content

reth_evm/
either.rs

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