From 3ad3bbc593782280c4d34ed41388373b77f7b3bd Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 25 Apr 2024 21:23:21 +0200 Subject: [PATCH] chore: more launch builder style function (#7897) --- crates/node-builder/src/launch/common.rs | 22 ++++++++++++++++++++++ crates/node-builder/src/launch/mod.rs | 23 +++++++++++++---------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/crates/node-builder/src/launch/common.rs b/crates/node-builder/src/launch/common.rs index f4d2a931c4..c57e12cf66 100644 --- a/crates/node-builder/src/launch/common.rs +++ b/crates/node-builder/src/launch/common.rs @@ -145,6 +145,16 @@ impl LaunchContextWith { attachment: Attached::new(self.attachment, attachment), } } + + /// Consumes the type and calls a function with a reference to the context. + // Returns the context again + pub fn inspect(self, f: F) -> Self + where + F: FnOnce(&Self), + { + f(&self); + self + } } impl LaunchContextWith> { @@ -338,6 +348,12 @@ where ) } + /// Convenience function to [Self::init_genesis] + pub fn with_genesis(self) -> Result { + init_genesis(self.provider_factory().clone())?; + Ok(self) + } + /// Write the genesis block and state if it has not already been written pub fn init_genesis(&self) -> Result { init_genesis(self.provider_factory().clone()) @@ -352,6 +368,12 @@ where self.node_config().max_block(client, self.provider_factory().clone()).await } + /// Convenience function to [Self::start_prometheus_endpoint] + pub async fn with_prometheus(self) -> eyre::Result { + self.start_prometheus_endpoint().await?; + Ok(self) + } + /// Starts the prometheus endpoint. pub async fn start_prometheus_endpoint(&self) -> eyre::Result<()> { let prometheus_handle = self.node_config().install_prometheus_recorder()?; diff --git a/crates/node-builder/src/launch/mod.rs b/crates/node-builder/src/launch/mod.rs index 00304816cf..408e47cd7a 100644 --- a/crates/node-builder/src/launch/mod.rs +++ b/crates/node-builder/src/launch/mod.rs @@ -95,6 +95,7 @@ where config, } = target; + // setup the launch context let ctx = ctx .with_configured_globals() // load the toml config @@ -104,16 +105,18 @@ where // ensure certain settings take effect .with_adjusted_configs() // Create the provider factory - .with_provider_factory()?; - - info!(target: "reth::cli", "Database opened"); - - ctx.start_prometheus_endpoint().await?; - - debug!(target: "reth::cli", chain=%ctx.chain_id(), genesis=?ctx.genesis_hash(), "Initializing genesis"); - ctx.init_genesis()?; - - info!(target: "reth::cli", "\n{}", ctx.chain_spec().display_hardforks()); + .with_provider_factory()? + .inspect(|_| { + info!(target: "reth::cli", "Database opened"); + }) + .with_prometheus().await? + .inspect(|this| { + debug!(target: "reth::cli", chain=%this.chain_id(), genesis=?this.genesis_hash(), "Initializing genesis"); + }) + .with_genesis()? + .inspect(|this| { + info!(target: "reth::cli", "\n{}", this.chain_spec().display_hardforks()); + }); // setup the consensus instance let consensus: Arc = if ctx.is_dev() {