reth_evm/
engine.rs

1use crate::{execute::ExecutableTxFor, ConfigureEvm, EvmEnvFor, ExecutionCtxFor};
2use rayon::prelude::*;
3
4/// [`ConfigureEvm`] extension providing methods for executing payloads.
5pub trait ConfigureEngineEvm<ExecutionData>: ConfigureEvm {
6    /// Returns an [`crate::EvmEnv`] for the given payload.
7    fn evm_env_for_payload(&self, payload: &ExecutionData) -> Result<EvmEnvFor<Self>, Self::Error>;
8
9    /// Returns an [`ExecutionCtxFor`] for the given payload.
10    fn context_for_payload<'a>(
11        &self,
12        payload: &'a ExecutionData,
13    ) -> Result<ExecutionCtxFor<'a, Self>, Self::Error>;
14
15    /// Returns an [`ExecutableTxIterator`] for the given payload.
16    fn tx_iterator_for_payload(
17        &self,
18        payload: &ExecutionData,
19    ) -> Result<impl ExecutableTxIterator<Self>, Self::Error>;
20}
21
22/// A helper trait representing a pair of a "raw" transactions iterator and a closure that can be
23/// used to convert them to an executable transaction. This tuple is used in the engine to
24/// parallelize heavy work like decoding or recovery.
25pub trait ExecutableTxTuple: Into<(Self::IntoIter, Self::Convert)> + Send + 'static {
26    /// Raw transaction that can be converted to an [`ExecutableTxTuple::Tx`]
27    ///
28    /// This can be any type that can be converted to an [`ExecutableTxTuple::Tx`]. For example,
29    /// an unrecovered transaction or just the transaction bytes.
30    type RawTx: Send + Sync + 'static;
31    /// The executable transaction type iterator yields.
32    type Tx: Clone + Send + Sync + 'static;
33    /// Errors that may occur while recovering or decoding transactions.
34    type Error: core::error::Error + Send + Sync + 'static;
35
36    /// Iterator over [`ExecutableTxTuple::Tx`].
37    type IntoIter: IntoParallelIterator<Item = Self::RawTx, Iter: IndexedParallelIterator>
38        + Send
39        + 'static;
40    /// Closure that can be used to convert a [`ExecutableTxTuple::RawTx`] to a
41    /// [`ExecutableTxTuple::Tx`]. This might involve heavy work like decoding or recovery
42    /// and will be parallelized in the engine.
43    type Convert: Fn(Self::RawTx) -> Result<Self::Tx, Self::Error> + Send + Sync + 'static;
44}
45
46impl<RawTx, Tx, Err, I, F> ExecutableTxTuple for (I, F)
47where
48    RawTx: Send + Sync + 'static,
49    Tx: Clone + Send + Sync + 'static,
50    Err: core::error::Error + Send + Sync + 'static,
51    I: IntoParallelIterator<Item = RawTx, Iter: IndexedParallelIterator> + Send + 'static,
52    F: Fn(RawTx) -> Result<Tx, Err> + Send + Sync + 'static,
53{
54    type RawTx = RawTx;
55    type Tx = Tx;
56    type Error = Err;
57
58    type IntoIter = I;
59    type Convert = F;
60}
61
62/// Iterator over executable transactions.
63pub trait ExecutableTxIterator<Evm: ConfigureEvm>:
64    ExecutableTxTuple<Tx: ExecutableTxFor<Evm>>
65{
66}
67
68impl<T, Evm: ConfigureEvm> ExecutableTxIterator<Evm> for T where
69    T: ExecutableTxTuple<Tx: ExecutableTxFor<Evm>>
70{
71}