Skip to content

Commit

Permalink
feat: add cli ext event hooks example (#4935)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Oct 6, 2023
1 parent c825aaf commit 36306ce
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ members = [
"crates/rpc/rpc-types-compat",
"examples",
"examples/additional-rpc-namespace-in-cli",
"examples/cli-extension-event-hooks",
"examples/rpc-db",
"examples/manual-p2p",
]
Expand Down
16 changes: 15 additions & 1 deletion bin/reth/src/cli/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use clap::Args;
use reth_basic_payload_builder::{BasicPayloadJobGenerator, BasicPayloadJobGeneratorConfig};
use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService};
use reth_tasks::TaskSpawner;
use std::fmt;
use std::{fmt, marker::PhantomData};

/// A trait that allows for extending parts of the CLI with additional functionality.
///
Expand Down Expand Up @@ -119,6 +119,14 @@ impl RethNodeCommandConfig for DefaultRethNodeCommandConfig {}

impl RethNodeCommandConfig for () {}

/// A helper type for [RethCliExt] extension that don't require any additional clap Arguments.
#[derive(Debug, Clone, Copy)]
pub struct NoArgsCliExt<Conf>(PhantomData<Conf>);

impl<Conf: RethNodeCommandConfig> RethCliExt for NoArgsCliExt<Conf> {
type Node = NoArgs<Conf>;
}

/// A helper struct that allows for wrapping a [RethNodeCommandConfig] value without providing
/// additional CLI arguments.
///
Expand Down Expand Up @@ -214,6 +222,12 @@ impl<T: RethNodeCommandConfig> RethNodeCommandConfig for NoArgs<T> {
}
}

impl<T> From<T> for NoArgs<T> {
fn from(value: T) -> Self {
Self::with(value)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
20 changes: 20 additions & 0 deletions bin/reth/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ impl<Ext: RethCliExt> Cli<Ext> {
reth_tracing::init(layers);
Ok(guard.flatten())
}

/// Configures the given node extension.
pub fn with_node_extension<C>(mut self, conf: C) -> Self
where
C: Into<Ext::Node>,
{
self.command.set_node_extension(conf.into());
self
}
}

/// Convenience function for parsing CLI options, set up logging and run the chosen command.
Expand Down Expand Up @@ -156,6 +165,17 @@ pub enum Commands<Ext: RethCliExt = ()> {
Recover(recover::Command),
}

impl<Ext: RethCliExt> Commands<Ext> {
/// Sets the node extension if it is the [NodeCommand](node::NodeCommand).
///
/// This is a noop if the command is not the [NodeCommand](node::NodeCommand).
pub fn set_node_extension(&mut self, ext: Ext::Node) {
if let Commands::Node(command) = self {
command.ext = ext
}
}
}

/// The log configuration.
#[derive(Debug, Args)]
#[command(next_help_heading = "Logging")]
Expand Down
11 changes: 11 additions & 0 deletions examples/cli-extension-event-hooks/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "cli-extension-event-hooks"
version = "0.0.0"
publish = false
edition.workspace = true
license.workspace = true

[dependencies]
reth.workspace = true
clap.workspace = true
eyre = "0.6"
51 changes: 51 additions & 0 deletions examples/cli-extension-event-hooks/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//! Example for how hook into the node via the CLI extension mechanism without registering
//! additional arguments
//!
//! Run with
//!
//! ```not_rust
//! cargo run -p cli-extension-event-hooks -- node
//! ```
//!
//! This launch the regular reth node and also print:
//!
//! > "All components initialized"
//! once all components have been initialized and
//!
//! > "Node started"
//! once the node has been started.
use clap::Parser;
use reth::cli::{
components::RethNodeComponents,
ext::{NoArgsCliExt, RethNodeCommandConfig},
Cli,
};

fn main() {
Cli::<NoArgsCliExt<MyRethConfig>>::parse()
.with_node_extension(MyRethConfig::default())
.run()
.unwrap();
}

/// Our custom cli args extension that adds one flag to reth default CLI.
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
struct MyRethConfig;

impl RethNodeCommandConfig for MyRethConfig {
fn on_components_initialized<Reth: RethNodeComponents>(
&mut self,
_components: &Reth,
) -> eyre::Result<()> {
println!("All components initialized");
Ok(())
}
fn on_node_started<Reth: RethNodeComponents>(
&mut self,
_components: &Reth,
) -> eyre::Result<()> {
println!("Node started");
Ok(())
}
}

0 comments on commit 36306ce

Please sign in to comment.