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, 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> {
44    /// The function to execute when launching the node
45    func: F,
46}
47
48impl<F> FnLauncher<F> {
49    /// Creates a new function launcher adapter.
50    ///
51    /// Type parameters `C` and `Ext` help the compiler infer correct types
52    /// since they're not stored in the struct itself.
53    ///
54    /// # Arguments
55    ///
56    /// * `func` - Function that configures and launches a node
57    pub fn new<C, Ext>(func: F) -> Self
58    where
59        C: ChainSpecParser,
60        F: AsyncFnOnce(
61            WithLaunchContext<NodeBuilder<Arc<DatabaseEnv>, C::ChainSpec>>,
62            Ext,
63        ) -> eyre::Result<()>,
64    {
65        Self { func }
66    }
67}
68
69impl<C, Ext, F> Launcher<C, Ext> for FnLauncher<F>
70where
71    C: ChainSpecParser,
72    Ext: clap::Args + fmt::Debug,
73    F: AsyncFnOnce(
74        WithLaunchContext<NodeBuilder<Arc<DatabaseEnv>, C::ChainSpec>>,
75        Ext,
76    ) -> eyre::Result<()>,
77{
78    fn entrypoint(
79        self,
80        builder: WithLaunchContext<NodeBuilder<Arc<DatabaseEnv>, C::ChainSpec>>,
81        builder_args: Ext,
82    ) -> impl Future<Output = eyre::Result<()>> {
83        (self.func)(builder, builder_args)
84    }
85}