reth_node_builder/
exex.rs

1//! Types for launching execution extensions (ExEx).
2
3use std::future::Future;
4
5use futures::{future::BoxFuture, FutureExt};
6use reth_exex::ExExContext;
7use reth_node_api::FullNodeComponents;
8
9/// A trait for launching an `ExEx`.
10pub trait LaunchExEx<Node: FullNodeComponents>: Send {
11    /// Launches the `ExEx`.
12    ///
13    /// The `ExEx` should be able to run independently and emit events on the channels provided in
14    /// the [`ExExContext`].
15    fn launch(
16        self,
17        ctx: ExExContext<Node>,
18    ) -> impl Future<Output = eyre::Result<impl Future<Output = eyre::Result<()>> + Send>> + Send;
19}
20
21/// A boxed exex future.
22pub type BoxExEx = BoxFuture<'static, eyre::Result<()>>;
23
24/// A version of [`LaunchExEx`] that returns a boxed future. Makes the trait object-safe.
25pub trait BoxedLaunchExEx<Node: FullNodeComponents>: Send {
26    /// Launches the `ExEx` and returns a boxed future.
27    fn launch(self: Box<Self>, ctx: ExExContext<Node>)
28        -> BoxFuture<'static, eyre::Result<BoxExEx>>;
29}
30
31/// Implements [`BoxedLaunchExEx`] for any [`LaunchExEx`] that is [Send] and `'static`.
32///
33/// Returns a [`BoxFuture`] that resolves to a [`BoxExEx`].
34impl<E, Node> BoxedLaunchExEx<Node> for E
35where
36    E: LaunchExEx<Node> + Send + 'static,
37    Node: FullNodeComponents,
38{
39    fn launch(
40        self: Box<Self>,
41        ctx: ExExContext<Node>,
42    ) -> BoxFuture<'static, eyre::Result<BoxExEx>> {
43        async move {
44            let exex = LaunchExEx::launch(*self, ctx).await?;
45            Ok(Box::pin(exex) as BoxExEx)
46        }
47        .boxed()
48    }
49}
50
51/// Implements `LaunchExEx` for any closure that takes an [`ExExContext`] and returns a future
52/// resolving to an `ExEx`.
53impl<Node, F, Fut, E> LaunchExEx<Node> for F
54where
55    Node: FullNodeComponents,
56    F: FnOnce(ExExContext<Node>) -> Fut + Send,
57    Fut: Future<Output = eyre::Result<E>> + Send,
58    E: Future<Output = eyre::Result<()>> + Send,
59{
60    fn launch(
61        self,
62        ctx: ExExContext<Node>,
63    ) -> impl Future<Output = eyre::Result<impl Future<Output = eyre::Result<()>> + Send>> + Send
64    {
65        self(ctx)
66    }
67}