reth_cli_commands/init_state/
mod.rs

1//! Command that initializes the node from a genesis file.
2
3use crate::common::{AccessRights, CliNodeTypes, Environment, EnvironmentArgs};
4use alloy_primitives::{B256, U256};
5use clap::Parser;
6use reth_chainspec::{EthChainSpec, EthereumHardforks};
7use reth_cli::chainspec::ChainSpecParser;
8use reth_db_common::init::init_from_state_dump;
9use reth_node_api::NodePrimitives;
10use reth_primitives_traits::SealedHeader;
11use reth_provider::{
12    BlockNumReader, DatabaseProviderFactory, StaticFileProviderFactory, StaticFileWriter,
13};
14use std::{io::BufReader, path::PathBuf, str::FromStr, sync::Arc};
15use tracing::info;
16
17pub mod without_evm;
18
19/// Initializes the database with the genesis block.
20#[derive(Debug, Parser)]
21pub struct InitStateCommand<C: ChainSpecParser> {
22    #[command(flatten)]
23    pub env: EnvironmentArgs<C>,
24
25    /// JSONL file with state dump.
26    ///
27    /// Must contain accounts in following format, additional account fields are ignored. Must
28    /// also contain { "root": \<state-root\> } as first line.
29    /// {
30    ///     "balance": "\<balance\>",
31    ///     "nonce": \<nonce\>,
32    ///     "code": "\<bytecode\>",
33    ///     "storage": {
34    ///         "\<key\>": "\<value\>",
35    ///         ..
36    ///     },
37    ///     "address": "\<address\>",
38    /// }
39    ///
40    /// Allows init at a non-genesis block. Caution! Blocks must be manually imported up until
41    /// and including the non-genesis block to init chain at. See 'import' command.
42    #[arg(value_name = "STATE_DUMP_FILE", verbatim_doc_comment)]
43    pub state: PathBuf,
44
45    /// Specifies whether to initialize the state without relying on EVM historical data.
46    ///
47    /// When enabled, and before inserting the state, it creates a dummy chain up to the last EVM
48    /// block specified. It then, appends the first block provided block.
49    ///
50    /// - **Note**: **Do not** import receipts and blocks beforehand, or this will fail or be
51    ///   ignored.
52    #[arg(long, default_value = "false")]
53    pub without_evm: bool,
54
55    /// Header file containing the header in an RLP encoded format.
56    #[arg(long, value_name = "HEADER_FILE", verbatim_doc_comment)]
57    pub header: Option<PathBuf>,
58
59    /// Total difficulty of the header.
60    #[arg(long, value_name = "TOTAL_DIFFICULTY", verbatim_doc_comment)]
61    pub total_difficulty: Option<String>,
62
63    /// Hash of the header.
64    #[arg(long, value_name = "HEADER_HASH", verbatim_doc_comment)]
65    pub header_hash: Option<String>,
66}
67
68impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> InitStateCommand<C> {
69    /// Execute the `init` command
70    pub async fn execute<N>(self) -> eyre::Result<()>
71    where
72        N: CliNodeTypes<
73            ChainSpec = C::ChainSpec,
74            Primitives: NodePrimitives<BlockHeader = alloy_consensus::Header>,
75        >,
76    {
77        info!(target: "reth::cli", "Reth init-state starting");
78
79        let Environment { config, provider_factory, .. } = self.env.init::<N>(AccessRights::RW)?;
80
81        let static_file_provider = provider_factory.static_file_provider();
82        let provider_rw = provider_factory.database_provider_rw()?;
83
84        if self.without_evm {
85            // ensure header, total difficulty and header hash are provided
86            let header = self.header.ok_or_else(|| eyre::eyre!("Header file must be provided"))?;
87            let header = without_evm::read_header_from_file(header)?;
88
89            let header_hash =
90                self.header_hash.ok_or_else(|| eyre::eyre!("Header hash must be provided"))?;
91            let header_hash = B256::from_str(&header_hash)?;
92
93            let total_difficulty = self
94                .total_difficulty
95                .ok_or_else(|| eyre::eyre!("Total difficulty must be provided"))?;
96            let total_difficulty = U256::from_str(&total_difficulty)?;
97
98            let last_block_number = provider_rw.last_block_number()?;
99
100            if last_block_number == 0 {
101                without_evm::setup_without_evm(
102                    &provider_rw,
103                    SealedHeader::new(header, header_hash),
104                    total_difficulty,
105                )?;
106
107                // SAFETY: it's safe to commit static files, since in the event of a crash, they
108                // will be unwound according to database checkpoints.
109                //
110                // Necessary to commit, so the header is accessible to provider_rw and
111                // init_state_dump
112                static_file_provider.commit()?;
113            } else if last_block_number > 0 && last_block_number < header.number {
114                return Err(eyre::eyre!(
115                    "Data directory should be empty when calling init-state with --without-evm-history."
116                ));
117            }
118        }
119
120        info!(target: "reth::cli", "Initiating state dump");
121
122        let reader = BufReader::new(reth_fs_util::open(self.state)?);
123
124        let hash = init_from_state_dump(reader, &provider_rw, config.stages.etl)?;
125
126        provider_rw.commit()?;
127
128        info!(target: "reth::cli", hash = ?hash, "Genesis block written");
129        Ok(())
130    }
131}
132
133impl<C: ChainSpecParser> InitStateCommand<C> {
134    /// Returns the underlying chain being used to run this command
135    pub fn chain_spec(&self) -> Option<&Arc<C::ChainSpec>> {
136        Some(&self.env.chain)
137    }
138}