reth_cli_commands/
launcher.rs

1use futures::Future;
2use reth_cli::chainspec::ChainSpecParser;
3use reth_db::DatabaseEnv;
4use reth_node_builder::{NodeBuilder, WithLaunchContext};
5use std::{fmt, marker::PhantomData, sync::Arc};
6
7/// A trait for launching a reth node with custom configuration strategies.
8///
9/// This trait allows defining node configuration through various object types rather than just
10/// functions. By implementing this trait on your own structures, you can:
11///
12/// - Create flexible configurations that connect necessary components without creating separate
13///   closures
14/// - Take advantage of decomposition to break complex configurations into a series of methods
15/// - Encapsulate configuration logic in dedicated types with their own state and behavior
16/// - Reuse configuration patterns across different parts of your application
17pub trait Launcher<C, Ext>
18where
19    C: ChainSpecParser,
20    Ext: clap::Args + fmt::Debug,
21{
22    /// Entry point for launching a node with custom configuration.
23    ///
24    /// Consumes `self` to use pre-configured state, takes a builder and arguments,
25    /// and returns an async future.
26    ///
27    /// # Arguments
28    ///
29    /// * `builder` - Node builder with launch context
30    /// * `builder_args` - Extension arguments for configuration
31    fn entrypoint(
32        self,
33        builder: WithLaunchContext<NodeBuilder<Arc<DatabaseEnv>, C::ChainSpec>>,
34        builder_args: Ext,
35    ) -> impl Future<Output = eyre::Result<()>>;
36}
37
38/// A function-based adapter implementation of the [`Launcher`] trait.
39///
40/// This struct adapts existing closures to work with the new [`Launcher`] trait,
41/// maintaining backward compatibility with current node implementations while
42/// enabling the transition to the more flexible trait-based approach.
43pub struct FnLauncher<F, Fut> {
44    /// The function to execute when launching the node
45    func: F,
46    /// Phantom data to track the future type
47    _result: PhantomData<Fut>,
48}
49
50impl<F, Fut> FnLauncher<F, Fut> {
51    /// Creates a new function launcher adapter.
52    ///
53    /// Type parameters `C` and `Ext` help the compiler infer correct types
54    /// since they're not stored in the struct itself.
55    ///
56    /// # Arguments
57    ///
58    /// * `func` - Function that configures and launches a node
59    pub fn new<C, Ext>(func: F) -> Self
60    where
61        C: ChainSpecParser,
62        F: FnOnce(WithLaunchContext<NodeBuilder<Arc<DatabaseEnv>, C::ChainSpec>>, Ext) -> Fut,
63    {
64        Self { func, _result: PhantomData }
65    }
66}
67
68impl<C, Ext, F, Fut> Launcher<C, Ext> for FnLauncher<F, Fut>
69where
70    C: ChainSpecParser,
71    Ext: clap::Args + fmt::Debug,
72    F: FnOnce(WithLaunchContext<NodeBuilder<Arc<DatabaseEnv>, C::ChainSpec>>, Ext) -> Fut,
73    Fut: Future<Output = eyre::Result<()>>,
74{
75    fn entrypoint(
76        self,
77        builder: WithLaunchContext<NodeBuilder<Arc<DatabaseEnv>, C::ChainSpec>>,
78        builder_args: Ext,
79    ) -> impl Future<Output = eyre::Result<()>> {
80        (self.func)(builder, builder_args)
81    }
82}