diff --git a/crates/cli/commands/src/import.rs b/crates/cli/commands/src/import.rs index 05434de4c21..68c56edb792 100644 --- a/crates/cli/commands/src/import.rs +++ b/crates/cli/commands/src/import.rs @@ -6,7 +6,7 @@ use crate::{ use clap::Parser; use reth_chainspec::{ChainSpecProvider, EthChainSpec, EthereumHardforks}; use reth_cli::chainspec::ChainSpecParser; -use reth_node_core::version::SHORT_VERSION; +use reth_node_core::version::version_metadata; use std::{path::PathBuf, sync::Arc}; use tracing::info; @@ -44,7 +44,7 @@ impl> ImportComm N: CliNodeTypes, Comp: CliNodeComponents, { - info!(target: "reth::cli", "reth {} starting", SHORT_VERSION); + info!(target: "reth::cli", "reth {} starting", version_metadata().short_version); let Environment { provider_factory, config, .. } = self.env.init::(AccessRights::RW)?; diff --git a/crates/cli/commands/src/import_era.rs b/crates/cli/commands/src/import_era.rs index 7920fda3131..2bf1fe1c9f7 100644 --- a/crates/cli/commands/src/import_era.rs +++ b/crates/cli/commands/src/import_era.rs @@ -10,7 +10,7 @@ use reth_era_downloader::{read_dir, EraClient, EraStream, EraStreamConfig}; use reth_era_utils as era; use reth_etl::Collector; use reth_fs_util as fs; -use reth_node_core::version::SHORT_VERSION; +use reth_node_core::version::version_metadata; use reth_provider::StaticFileProviderFactory; use reth_static_file_types::StaticFileSegment; use std::{path::PathBuf, sync::Arc}; @@ -68,7 +68,7 @@ impl> ImportEraC where N: CliNodeTypes, { - info!(target: "reth::cli", "reth {} starting", SHORT_VERSION); + info!(target: "reth::cli", "reth {} starting", version_metadata().short_version); let Environment { provider_factory, config, .. } = self.env.init::(AccessRights::RW)?; diff --git a/crates/cli/commands/src/node.rs b/crates/cli/commands/src/node.rs index 3c7b98cbe1c..1714a06d678 100644 --- a/crates/cli/commands/src/node.rs +++ b/crates/cli/commands/src/node.rs @@ -148,7 +148,7 @@ where where L: Launcher, { - tracing::info!(target: "reth::cli", version = ?version::SHORT_VERSION, "Starting reth"); + tracing::info!(target: "reth::cli", version = ?version::version_metadata().short_version, "Starting reth"); let Self { datadir, diff --git a/crates/cli/commands/src/stage/run.rs b/crates/cli/commands/src/stage/run.rs index f608a18fff4..aad1ac8f3ad 100644 --- a/crates/cli/commands/src/stage/run.rs +++ b/crates/cli/commands/src/stage/run.rs @@ -21,10 +21,7 @@ use reth_network::BlockDownloaderProvider; use reth_network_p2p::HeadersClient; use reth_node_core::{ args::{NetworkArgs, StageEnum}, - version::{ - BUILD_PROFILE_NAME, CARGO_PKG_VERSION, VERGEN_BUILD_TIMESTAMP, VERGEN_CARGO_FEATURES, - VERGEN_CARGO_TARGET_TRIPLE, VERGEN_GIT_SHA, - }, + version::version_metadata, }; use reth_node_metrics::{ chain::ChainSpecInfo, @@ -123,12 +120,12 @@ impl let config = MetricServerConfig::new( listen_addr, VersionInfo { - version: CARGO_PKG_VERSION, - build_timestamp: VERGEN_BUILD_TIMESTAMP, - cargo_features: VERGEN_CARGO_FEATURES, - git_sha: VERGEN_GIT_SHA, - target_triple: VERGEN_CARGO_TARGET_TRIPLE, - build_profile: BUILD_PROFILE_NAME, + version: version_metadata().cargo_pkg_version.as_ref(), + build_timestamp: version_metadata().vergen_build_timestamp.as_ref(), + cargo_features: version_metadata().vergen_cargo_features.as_ref(), + git_sha: version_metadata().vergen_git_sha.as_ref(), + target_triple: version_metadata().vergen_cargo_target_triple.as_ref(), + build_profile: version_metadata().build_profile_name.as_ref(), }, ChainSpecInfo { name: provider_factory.chain_spec().chain().to_string() }, ctx.task_executor, diff --git a/crates/ethereum/cli/src/interface.rs b/crates/ethereum/cli/src/interface.rs index ee4fbdfd60f..83b79abe811 100644 --- a/crates/ethereum/cli/src/interface.rs +++ b/crates/ethereum/cli/src/interface.rs @@ -15,10 +15,7 @@ use reth_cli_runner::CliRunner; use reth_db::DatabaseEnv; use reth_node_api::NodePrimitives; use reth_node_builder::{NodeBuilder, WithLaunchContext}; -use reth_node_core::{ - args::LogArgs, - version::{LONG_VERSION, SHORT_VERSION}, -}; +use reth_node_core::{args::LogArgs, version::version_metadata}; use reth_node_ethereum::{consensus::EthBeaconConsensus, EthEvmConfig, EthereumNode}; use reth_node_metrics::recorder::install_prometheus_recorder; use reth_tracing::FileWorkerGuard; @@ -29,7 +26,7 @@ use tracing::info; /// /// This is the entrypoint to the executable. #[derive(Debug, Parser)] -#[command(author, version = SHORT_VERSION, long_version = LONG_VERSION, about = "Reth", long_about = None)] +#[command(author, version =version_metadata().short_version.as_ref(), long_version = version_metadata().long_version.as_ref(), about = "Reth", long_about = None)] pub struct Cli { /// The command to run diff --git a/crates/node/builder/src/launch/common.rs b/crates/node/builder/src/launch/common.rs index e72fcff6ff0..cd2c97e8f87 100644 --- a/crates/node/builder/src/launch/common.rs +++ b/crates/node/builder/src/launch/common.rs @@ -56,10 +56,7 @@ use reth_node_core::{ dirs::{ChainPath, DataDirPath}, node_config::NodeConfig, primitives::BlockHeader, - version::{ - BUILD_PROFILE_NAME, CARGO_PKG_VERSION, VERGEN_BUILD_TIMESTAMP, VERGEN_CARGO_FEATURES, - VERGEN_CARGO_TARGET_TRIPLE, VERGEN_GIT_SHA, - }, + version::version_metadata, }; use reth_node_metrics::{ chain::ChainSpecInfo, @@ -589,12 +586,12 @@ where let config = MetricServerConfig::new( addr, VersionInfo { - version: CARGO_PKG_VERSION, - build_timestamp: VERGEN_BUILD_TIMESTAMP, - cargo_features: VERGEN_CARGO_FEATURES, - git_sha: VERGEN_GIT_SHA, - target_triple: VERGEN_CARGO_TARGET_TRIPLE, - build_profile: BUILD_PROFILE_NAME, + version: version_metadata().cargo_pkg_version.as_ref(), + build_timestamp: version_metadata().vergen_build_timestamp.as_ref(), + cargo_features: version_metadata().vergen_cargo_features.as_ref(), + git_sha: version_metadata().vergen_git_sha.as_ref(), + target_triple: version_metadata().vergen_cargo_target_triple.as_ref(), + build_profile: version_metadata().build_profile_name.as_ref(), }, ChainSpecInfo { name: self.left().config.chain.chain().to_string() }, self.task_executor().clone(), diff --git a/crates/node/builder/src/rpc.rs b/crates/node/builder/src/rpc.rs index 6a0e62be8c2..d8bf73e4855 100644 --- a/crates/node/builder/src/rpc.rs +++ b/crates/node/builder/src/rpc.rs @@ -19,7 +19,7 @@ use reth_node_api::{ }; use reth_node_core::{ node_config::NodeConfig, - version::{CARGO_PKG_VERSION, CLIENT_CODE, NAME_CLIENT, VERGEN_GIT_SHA}, + version::{version_metadata, CLIENT_CODE}, }; use reth_payload_builder::{PayloadBuilderHandle, PayloadStore}; use reth_rpc::eth::{core::EthRpcConverterFor, EthApiTypes, FullEthApiServer}; @@ -1233,9 +1233,9 @@ where let engine_validator = payload_validator_builder.build(ctx).await?; let client = ClientVersionV1 { code: CLIENT_CODE, - name: NAME_CLIENT.to_string(), - version: CARGO_PKG_VERSION.to_string(), - commit: VERGEN_GIT_SHA.to_string(), + name: version_metadata().name_client.to_string(), + version: version_metadata().cargo_pkg_version.to_string(), + commit: version_metadata().vergen_git_sha.to_string(), }; Ok(EngineApi::new( ctx.node.provider().clone(), diff --git a/crates/node/core/src/args/network.rs b/crates/node/core/src/args/network.rs index 2f5908aaf46..a8e67f45268 100644 --- a/crates/node/core/src/args/network.rs +++ b/crates/node/core/src/args/network.rs @@ -6,6 +6,7 @@ use std::{ path::PathBuf, }; +use crate::version::version_metadata; use clap::Args; use reth_chainspec::EthChainSpec; use reth_config::Config; @@ -37,8 +38,6 @@ use reth_network_peers::{mainnet_nodes, TrustedPeer}; use secp256k1::SecretKey; use tracing::error; -use crate::version::P2P_CLIENT_VERSION; - /// Parameters for configuring the network more granularity via CLI #[derive(Debug, Clone, Args, PartialEq, Eq)] #[command(next_help_heading = "Networking")] @@ -74,7 +73,7 @@ pub struct NetworkArgs { pub peers_file: Option, /// Custom node identity - #[arg(long, value_name = "IDENTITY", default_value = P2P_CLIENT_VERSION)] + #[arg(long, value_name = "IDENTITY", default_value = version_metadata().p2p_client_version.as_ref())] pub identity: String, /// Secret key to use for this node. @@ -325,7 +324,7 @@ impl Default for NetworkArgs { bootnodes: None, dns_retries: 0, peers_file: None, - identity: P2P_CLIENT_VERSION.to_string(), + identity: version_metadata().p2p_client_version.to_string(), p2p_secret_key: None, no_persist_peers: false, nat: NatResolver::Any, diff --git a/crates/node/core/src/version.rs b/crates/node/core/src/version.rs index a526301a224..85a6077709f 100644 --- a/crates/node/core/src/version.rs +++ b/crates/node/core/src/version.rs @@ -1,4 +1,6 @@ //! Version information for reth. +use std::{borrow::Cow, sync::OnceLock}; + use alloy_primitives::Bytes; use alloy_rpc_types_engine::ClientCode; use reth_db::ClientVersion; @@ -6,58 +8,66 @@ use reth_db::ClientVersion; /// The client code for Reth pub const CLIENT_CODE: ClientCode = ClientCode::RH; -/// The human readable name of the client -pub const NAME_CLIENT: &str = "Reth"; - -/// The latest version from Cargo.toml. -pub const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION"); - -/// The full SHA of the latest commit. -pub const VERGEN_GIT_SHA_LONG: &str = env!("VERGEN_GIT_SHA"); - -/// The 8 character short SHA of the latest commit. -pub const VERGEN_GIT_SHA: &str = env!("VERGEN_GIT_SHA_SHORT"); - -/// The build timestamp. -pub const VERGEN_BUILD_TIMESTAMP: &str = env!("VERGEN_BUILD_TIMESTAMP"); - -/// The target triple. -pub const VERGEN_CARGO_TARGET_TRIPLE: &str = env!("VERGEN_CARGO_TARGET_TRIPLE"); +/// Global static version metadata +static VERSION_METADATA: OnceLock = OnceLock::new(); -/// The build features. -pub const VERGEN_CARGO_FEATURES: &str = env!("VERGEN_CARGO_FEATURES"); - -/// The short version information for reth. -pub const SHORT_VERSION: &str = env!("RETH_SHORT_VERSION"); - -/// The long version information for reth. -pub const LONG_VERSION: &str = concat!( - env!("RETH_LONG_VERSION_0"), - "\n", - env!("RETH_LONG_VERSION_1"), - "\n", - env!("RETH_LONG_VERSION_2"), - "\n", - env!("RETH_LONG_VERSION_3"), - "\n", - env!("RETH_LONG_VERSION_4") -); - -/// The build profile name. -pub const BUILD_PROFILE_NAME: &str = env!("RETH_BUILD_PROFILE"); +/// Initialize the global version metadata. +pub fn try_init_version_metadata( + metadata: RethCliVersionConsts, +) -> Result<(), RethCliVersionConsts> { + VERSION_METADATA.set(metadata) +} -/// The version information for reth formatted for P2P (devp2p). -/// -/// - The latest version from Cargo.toml -/// - The target triple -/// -/// # Example +/// Constants for reth-cli /// -/// ```text -/// reth/v{major}.{minor}.{patch}-{sha1}/{target} -/// ``` -/// e.g.: `reth/v0.1.0-alpha.1-428a6dc2f/aarch64-apple-darwin` -pub(crate) const P2P_CLIENT_VERSION: &str = env!("RETH_P2P_CLIENT_VERSION"); +/// Global defaults can be set via [`try_init_version_metadata`]. +#[derive(Debug, Default)] +pub struct RethCliVersionConsts { + /// The human readable name of the client + pub name_client: Cow<'static, str>, + + /// The latest version from Cargo.toml. + pub cargo_pkg_version: Cow<'static, str>, + + /// The full SHA of the latest commit. + pub vergen_git_sha_long: Cow<'static, str>, + + /// The 8 character short SHA of the latest commit. + pub vergen_git_sha: Cow<'static, str>, + + /// The build timestamp. + pub vergen_build_timestamp: Cow<'static, str>, + + /// The target triple. + pub vergen_cargo_target_triple: Cow<'static, str>, + + /// The build features. + pub vergen_cargo_features: Cow<'static, str>, + + /// The short version information for reth. + pub short_version: Cow<'static, str>, + + /// The long version information for reth. + pub long_version: Cow<'static, str>, + /// The build profile name. + pub build_profile_name: Cow<'static, str>, + + /// The version information for reth formatted for P2P (devp2p). + /// + /// - The latest version from Cargo.toml + /// - The target triple + /// + /// # Example + /// + /// ```text + /// reth/v{major}.{minor}.{patch}-{sha1}/{target} + /// ``` + /// e.g.: `reth/v0.1.0-alpha.1-428a6dc2f/aarch64-apple-darwin` + pub p2p_client_version: Cow<'static, str>, + + /// extra data used for payload building + pub extra_data: Cow<'static, str>, +} /// The default extra data used for payload building. /// @@ -81,10 +91,42 @@ pub fn default_extra_data_bytes() -> Bytes { /// The default client version accessing the database. pub fn default_client_version() -> ClientVersion { + let meta = version_metadata(); ClientVersion { - version: CARGO_PKG_VERSION.to_string(), - git_sha: VERGEN_GIT_SHA.to_string(), - build_timestamp: VERGEN_BUILD_TIMESTAMP.to_string(), + version: meta.cargo_pkg_version.to_string(), + git_sha: meta.vergen_git_sha.to_string(), + build_timestamp: meta.vergen_build_timestamp.to_string(), + } +} + +/// Get a reference to the global version metadata +pub fn version_metadata() -> &'static RethCliVersionConsts { + VERSION_METADATA.get_or_init(default_reth_version_metadata) +} + +/// default reth version metadata using compile-time env! macros. +pub fn default_reth_version_metadata() -> RethCliVersionConsts { + RethCliVersionConsts { + name_client: Cow::Borrowed("Reth"), + cargo_pkg_version: Cow::Owned(env!("CARGO_PKG_VERSION").to_string()), + vergen_git_sha_long: Cow::Owned(env!("VERGEN_GIT_SHA").to_string()), + vergen_git_sha: Cow::Owned(env!("VERGEN_GIT_SHA_SHORT").to_string()), + vergen_build_timestamp: Cow::Owned(env!("VERGEN_BUILD_TIMESTAMP").to_string()), + vergen_cargo_target_triple: Cow::Owned(env!("VERGEN_CARGO_TARGET_TRIPLE").to_string()), + vergen_cargo_features: Cow::Owned(env!("VERGEN_CARGO_FEATURES").to_string()), + short_version: Cow::Owned(env!("RETH_SHORT_VERSION").to_string()), + long_version: Cow::Owned(format!( + "{}\n{}\n{}\n{}\n{}", + env!("RETH_LONG_VERSION_0"), + env!("RETH_LONG_VERSION_1"), + env!("RETH_LONG_VERSION_2"), + env!("RETH_LONG_VERSION_3"), + env!("RETH_LONG_VERSION_4"), + )), + + build_profile_name: Cow::Owned(env!("RETH_BUILD_PROFILE").to_string()), + p2p_client_version: Cow::Owned(env!("RETH_P2P_CLIENT_VERSION").to_string()), + extra_data: Cow::Owned(default_extra_data()), } } diff --git a/crates/optimism/cli/src/commands/import.rs b/crates/optimism/cli/src/commands/import.rs index 2fd0c56758a..0fd1d64ac12 100644 --- a/crates/optimism/cli/src/commands/import.rs +++ b/crates/optimism/cli/src/commands/import.rs @@ -10,7 +10,7 @@ use reth_consensus::noop::NoopConsensus; use reth_db_api::{tables, transaction::DbTx}; use reth_downloaders::file_client::{ChunkedFileReader, DEFAULT_BYTE_LEN_CHUNK_CHAIN_FILE}; use reth_node_builder::BlockTy; -use reth_node_core::version::SHORT_VERSION; +use reth_node_core::version::version_metadata; use reth_optimism_chainspec::OpChainSpec; use reth_optimism_evm::OpExecutorProvider; use reth_optimism_primitives::{bedrock::is_dup_tx, OpPrimitives}; @@ -44,7 +44,7 @@ impl> ImportOpCommand { pub async fn execute>( self, ) -> eyre::Result<()> { - info!(target: "reth::cli", "reth {} starting", SHORT_VERSION); + info!(target: "reth::cli", "reth {} starting", version_metadata().short_version); info!(target: "reth::cli", "Disabled stages requiring state, since cannot execute OVM state changes" diff --git a/crates/optimism/cli/src/commands/import_receipts.rs b/crates/optimism/cli/src/commands/import_receipts.rs index 38503bc2d33..f6a2214b643 100644 --- a/crates/optimism/cli/src/commands/import_receipts.rs +++ b/crates/optimism/cli/src/commands/import_receipts.rs @@ -12,7 +12,7 @@ use reth_downloaders::{ }; use reth_execution_types::ExecutionOutcome; use reth_node_builder::ReceiptTy; -use reth_node_core::version::SHORT_VERSION; +use reth_node_core::version::version_metadata; use reth_optimism_chainspec::OpChainSpec; use reth_optimism_primitives::{bedrock::is_dup_tx, OpPrimitives, OpReceipt}; use reth_primitives_traits::NodePrimitives; @@ -52,7 +52,7 @@ impl> ImportReceiptsOpCommand { pub async fn execute>( self, ) -> eyre::Result<()> { - info!(target: "reth::cli", "reth {} starting", SHORT_VERSION); + info!(target: "reth::cli", "reth {} starting", version_metadata().short_version); debug!(target: "reth::cli", chunk_byte_len=self.chunk_len.unwrap_or(DEFAULT_BYTE_LEN_CHUNK_CHAIN_FILE), diff --git a/crates/optimism/cli/src/lib.rs b/crates/optimism/cli/src/lib.rs index 4fca639fb28..4d1d22aa4d0 100644 --- a/crates/optimism/cli/src/lib.rs +++ b/crates/optimism/cli/src/lib.rs @@ -47,10 +47,7 @@ use reth_cli_commands::launcher::FnLauncher; use reth_cli_runner::CliRunner; use reth_db::DatabaseEnv; use reth_node_builder::{NodeBuilder, WithLaunchContext}; -use reth_node_core::{ - args::LogArgs, - version::{LONG_VERSION, SHORT_VERSION}, -}; +use reth_node_core::{args::LogArgs, version::version_metadata}; use reth_optimism_node::args::RollupArgs; // This allows us to manually enable node metrics features, required for proper jemalloc metric @@ -61,7 +58,7 @@ use reth_node_metrics as _; /// /// This is the entrypoint to the executable. #[derive(Debug, Parser)] -#[command(author, version = SHORT_VERSION, long_version = LONG_VERSION, about = "Reth", long_about = None)] +#[command(author, version = version_metadata().short_version.as_ref(), long_version = version_metadata().long_version.as_ref(), about = "Reth", long_about = None)] pub struct Cli { /// The command to run diff --git a/crates/optimism/node/src/rpc.rs b/crates/optimism/node/src/rpc.rs index a8776e1e4e6..db811a7f921 100644 --- a/crates/optimism/node/src/rpc.rs +++ b/crates/optimism/node/src/rpc.rs @@ -88,7 +88,7 @@ use reth_node_api::{ AddOnsContext, EngineApiValidator, EngineTypes, FullNodeComponents, NodeTypes, }; use reth_node_builder::rpc::{EngineApiBuilder, PayloadValidatorBuilder}; -use reth_node_core::version::{CARGO_PKG_VERSION, CLIENT_CODE, VERGEN_GIT_SHA}; +use reth_node_core::version::{version_metadata, CLIENT_CODE}; use reth_optimism_rpc::engine::OP_ENGINE_CAPABILITIES; use reth_payload_builder::PayloadStore; use reth_rpc_engine_api::{EngineApi, EngineCapabilities}; @@ -125,8 +125,8 @@ where let client = ClientVersionV1 { code: CLIENT_CODE, name: OP_NAME_CLIENT.to_string(), - version: CARGO_PKG_VERSION.to_string(), - commit: VERGEN_GIT_SHA.to_string(), + version: version_metadata().cargo_pkg_version.to_string(), + commit: version_metadata().vergen_git_sha.to_string(), }; let inner = EngineApi::new( ctx.node.provider().clone(),