reth_node_core/
utils.rs

1//! Utility functions for node startup and shutdown, for example path parsing and retrieving single
2//! blocks from the network.
3
4use alloy_consensus::BlockHeader;
5use alloy_eips::BlockHashOrNumber;
6use alloy_rpc_types_engine::{JwtError, JwtSecret};
7use eyre::Result;
8use reth_consensus::Consensus;
9use reth_network_p2p::{
10    bodies::client::BodiesClient, headers::client::HeadersClient, priority::Priority,
11};
12use reth_primitives::SealedBlock;
13use reth_primitives_traits::SealedHeader;
14use std::{
15    env::VarError,
16    path::{Path, PathBuf},
17};
18use tracing::{debug, info};
19
20/// Parses a user-specified path with support for environment variables and common shorthands (e.g.
21/// ~ for the user's home directory).
22pub fn parse_path(value: &str) -> Result<PathBuf, shellexpand::LookupError<VarError>> {
23    shellexpand::full(value).map(|path| PathBuf::from(path.into_owned()))
24}
25
26/// Attempts to retrieve or create a JWT secret from the specified path.
27pub fn get_or_create_jwt_secret_from_path(path: &Path) -> Result<JwtSecret, JwtError> {
28    if path.exists() {
29        debug!(target: "reth::cli", ?path, "Reading JWT auth secret file");
30        JwtSecret::from_file(path)
31    } else {
32        info!(target: "reth::cli", ?path, "Creating JWT auth secret file");
33        JwtSecret::try_create_random(path)
34    }
35}
36
37/// Get a single header from network
38pub async fn get_single_header<Client>(
39    client: Client,
40    id: BlockHashOrNumber,
41) -> Result<SealedHeader<Client::Header>>
42where
43    Client: HeadersClient<Header: reth_primitives_traits::BlockHeader>,
44{
45    let (peer_id, response) = client.get_header_with_priority(id, Priority::High).await?.split();
46
47    let Some(header) = response else {
48        client.report_bad_message(peer_id);
49        eyre::bail!("Invalid number of headers received. Expected: 1. Received: 0")
50    };
51
52    let header = SealedHeader::seal(header);
53
54    let valid = match id {
55        BlockHashOrNumber::Hash(hash) => header.hash() == hash,
56        BlockHashOrNumber::Number(number) => header.number() == number,
57    };
58
59    if !valid {
60        client.report_bad_message(peer_id);
61        eyre::bail!(
62            "Received invalid header. Received: {:?}. Expected: {:?}",
63            header.num_hash(),
64            id
65        );
66    }
67
68    Ok(header)
69}
70
71/// Get a body from network based on header
72pub async fn get_single_body<H, Client>(
73    client: Client,
74    header: SealedHeader<H>,
75    consensus: impl Consensus<H, Client::Body>,
76) -> Result<SealedBlock<H, Client::Body>>
77where
78    Client: BodiesClient,
79{
80    let (peer_id, response) = client.get_block_body(header.hash()).await?.split();
81
82    let Some(body) = response else {
83        client.report_bad_message(peer_id);
84        eyre::bail!("Invalid number of bodies received. Expected: 1. Received: 0")
85    };
86
87    let block = SealedBlock { header, body };
88    consensus.validate_block_pre_execution(&block)?;
89
90    Ok(block)
91}