Skip to main content

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>>(
22        self,
23        runtime: reth_tasks::Runtime,
24    ) -> eyre::Result<()> {
25        info!(target: "reth::cli", "reth init starting");
26
27        let Environment { provider_factory, .. } = self.env.init::<N>(AccessRights::RW, runtime)?;
28
29        let genesis_block_number = provider_factory.chain_spec().genesis_header().number();
30        let hash = provider_factory
31            .block_hash(genesis_block_number)?
32            .ok_or_else(|| eyre::eyre!("Genesis hash not found."))?;
33
34        info!(target: "reth::cli", hash = ?hash, "Genesis block written");
35        Ok(())
36    }
37}
38
39impl<C: ChainSpecParser> InitCommand<C> {
40    /// Returns the underlying chain being used to run this command
41    pub fn chain_spec(&self) -> Option<&Arc<C::ChainSpec>> {
42        Some(&self.env.chain)
43    }
44}