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 tracing::info;
9
10/// Initializes the database with the genesis block.
11#[derive(Debug, Parser)]
12pub struct InitCommand<C: ChainSpecParser> {
13    #[command(flatten)]
14    env: EnvironmentArgs<C>,
15}
16
17impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> InitCommand<C> {
18    /// Execute the `init` command
19    pub async fn execute<N: CliNodeTypes<ChainSpec = C::ChainSpec>>(self) -> eyre::Result<()> {
20        info!(target: "reth::cli", "reth init starting");
21
22        let Environment { provider_factory, .. } = self.env.init::<N>(AccessRights::RW)?;
23
24        let hash = provider_factory
25            .block_hash(0)?
26            .ok_or_else(|| eyre::eyre!("Genesis hash not found."))?;
27
28        info!(target: "reth::cli", hash = ?hash, "Genesis block written");
29        Ok(())
30    }
31}