reth_cli_commands/
dump_genesis.rs

1//! Command that dumps genesis block JSON configuration to stdout
2use std::sync::Arc;
3
4use clap::Parser;
5use reth_chainspec::EthChainSpec;
6use reth_cli::chainspec::ChainSpecParser;
7
8/// Dumps genesis block JSON configuration to stdout
9#[derive(Debug, Parser)]
10pub struct DumpGenesisCommand<C: ChainSpecParser> {
11    /// The chain this node is running.
12    ///
13    /// Possible values are either a built-in chain or the path to a chain specification file.
14    #[arg(
15        long,
16        value_name = "CHAIN_OR_PATH",
17        long_help = C::help_message(),
18        default_value = C::SUPPORTED_CHAINS[0],
19        value_parser = C::parser()
20    )]
21    chain: Arc<C::ChainSpec>,
22}
23
24impl<C: ChainSpecParser<ChainSpec: EthChainSpec>> DumpGenesisCommand<C> {
25    /// Execute the `dump-genesis` command
26    pub async fn execute(self) -> eyre::Result<()> {
27        println!("{}", serde_json::to_string_pretty(self.chain.genesis())?);
28        Ok(())
29    }
30    /// Returns the underlying chain being used to run this command
31    pub fn chain_spec(&self) -> Option<&Arc<C::ChainSpec>> {
32        Some(&self.chain)
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39    use reth_ethereum_cli::chainspec::{EthereumChainSpecParser, SUPPORTED_CHAINS};
40
41    #[test]
42    fn parse_dump_genesis_command_chain_args() {
43        for chain in SUPPORTED_CHAINS {
44            let args: DumpGenesisCommand<EthereumChainSpecParser> =
45                DumpGenesisCommand::parse_from(["reth", "--chain", chain]);
46            assert_eq!(
47                Ok(args.chain.chain),
48                chain.parse::<reth_chainspec::Chain>(),
49                "failed to parse chain {chain}"
50            );
51        }
52    }
53}