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