reth_cli_commands/db/
static_file_header.rs

1use clap::{Parser, Subcommand};
2use reth_db_common::DbTool;
3use reth_provider::{providers::ProviderNodeTypes, StaticFileProviderFactory};
4use reth_static_file_types::StaticFileSegment;
5use std::path::PathBuf;
6use tracing::warn;
7
8/// The arguments for the `reth db static-file-header` command
9#[derive(Parser, Debug)]
10pub struct Command {
11    #[command(subcommand)]
12    source: Source,
13}
14
15/// Source for locating the static file
16#[derive(Subcommand, Debug)]
17enum Source {
18    /// Query by segment and block number
19    Block {
20        /// Static file segment
21        #[arg(value_enum)]
22        segment: StaticFileSegment,
23        /// Block number to query
24        block: u64,
25    },
26    /// Query by path to static file
27    Path {
28        /// Path to the static file
29        path: PathBuf,
30    },
31}
32
33impl Command {
34    /// Execute `db static-file-header` command
35    pub fn execute<N: ProviderNodeTypes>(self, tool: &DbTool<N>) -> eyre::Result<()> {
36        let static_file_provider = tool.provider_factory.static_file_provider();
37        if let Err(err) = static_file_provider.check_consistency(&tool.provider_factory.provider()?)
38        {
39            warn!("Error checking consistency of static files: {err}");
40        }
41
42        // Get the provider based on the source
43        let provider = match self.source {
44            Source::Path { path } => {
45                static_file_provider.get_segment_provider_for_path(&path)?.ok_or_else(|| {
46                    eyre::eyre!("Could not find static file segment for path: {}", path.display())
47                })?
48            }
49            Source::Block { segment, block } => {
50                static_file_provider.get_segment_provider(segment, block)?
51            }
52        };
53
54        let header = provider.user_header();
55
56        println!("Segment: {}", header.segment());
57        println!("Expected Block Range: {}", header.expected_block_range());
58        println!("Block Range: {:?}", header.block_range());
59        println!("Transaction Range: {:?}", header.tx_range());
60
61        Ok(())
62    }
63}