reth_cli_commands/
init_cmd.rs

1//! Command that initializes the node from a genesis file.
2
3use crate::common::{AccessRights, CliNodeTypes, Environment, EnvironmentArgs};
4use clap::Parser;
5use reth_chainspec::{EthChainSpec, EthereumHardforks};
6use reth_cli::chainspec::ChainSpecParser;
7use reth_provider::BlockHashReader;
8use std::sync::Arc;
9use tracing::info;
10
11/// Initializes the database with the genesis block.
12#[derive(Debug, Parser)]
13pub struct InitCommand<C: ChainSpecParser> {
14    #[command(flatten)]
15    env: EnvironmentArgs<C>,
16}
17
18impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> InitCommand<C> {
19    /// Execute the `init` command
20    pub async fn execute<N: CliNodeTypes<ChainSpec = C::ChainSpec>>(self) -> eyre::Result<()> {
21        info!(target: "reth::cli", "reth init starting");
22
23        let Environment { provider_factory, .. } = self.env.init::<N>(AccessRights::RW)?;
24
25        let hash = provider_factory
26            .block_hash(0)?
27            .ok_or_else(|| eyre::eyre!("Genesis hash not found."))?;
28
29        info!(target: "reth::cli", hash = ?hash, "Genesis block written");
30        Ok(())
31    }
32
33    /// Returns the underlying chain being used to run this command
34    pub fn chain_spec(&self) -> Option<&Arc<C::ChainSpec>> {
35        Some(&self.env.chain)
36    }
37}