reth_node_core/
version.rs

1//! Version information for reth.
2use std::{borrow::Cow, sync::OnceLock};
3
4use alloy_primitives::Bytes;
5use alloy_rpc_types_engine::ClientCode;
6use reth_db::ClientVersion;
7
8/// The client code for Reth
9pub const CLIENT_CODE: ClientCode = ClientCode::RH;
10
11/// Global static version metadata
12static VERSION_METADATA: OnceLock<RethCliVersionConsts> = OnceLock::new();
13
14/// Initialize the global version metadata.
15pub fn try_init_version_metadata(
16    metadata: RethCliVersionConsts,
17) -> Result<(), RethCliVersionConsts> {
18    VERSION_METADATA.set(metadata)
19}
20
21/// Constants for reth-cli
22///
23/// Global defaults can be set via [`try_init_version_metadata`].
24#[derive(Debug, Default)]
25pub struct RethCliVersionConsts {
26    /// The human readable name of the client
27    pub name_client: Cow<'static, str>,
28
29    /// The latest version from Cargo.toml.
30    pub cargo_pkg_version: Cow<'static, str>,
31
32    /// The full SHA of the latest commit.
33    pub vergen_git_sha_long: Cow<'static, str>,
34
35    /// The 8 character short SHA of the latest commit.
36    pub vergen_git_sha: Cow<'static, str>,
37
38    /// The build timestamp.
39    pub vergen_build_timestamp: Cow<'static, str>,
40
41    /// The target triple.
42    pub vergen_cargo_target_triple: Cow<'static, str>,
43
44    /// The build features.
45    pub vergen_cargo_features: Cow<'static, str>,
46
47    /// The short version information for reth.
48    pub short_version: Cow<'static, str>,
49
50    /// The long version information for reth.
51    pub long_version: Cow<'static, str>,
52    /// The build profile name.
53    pub build_profile_name: Cow<'static, str>,
54
55    /// The version information for reth formatted for P2P (devp2p).
56    ///
57    /// - The latest version from Cargo.toml
58    /// - The target triple
59    ///
60    /// # Example
61    ///
62    /// ```text
63    /// reth/v{major}.{minor}.{patch}-{sha1}/{target}
64    /// ```
65    /// e.g.: `reth/v0.1.0-alpha.1-428a6dc2f/aarch64-apple-darwin`
66    pub p2p_client_version: Cow<'static, str>,
67
68    /// extra data used for payload building
69    pub extra_data: Cow<'static, str>,
70}
71
72/// The default extra data used for payload building.
73///
74/// - The latest version from Cargo.toml
75/// - The OS identifier
76///
77/// # Example
78///
79/// ```text
80/// reth/v{major}.{minor}.{patch}/{OS}
81/// ```
82pub fn default_extra_data() -> String {
83    format!("reth/v{}/{}", env!("CARGO_PKG_VERSION"), std::env::consts::OS)
84}
85
86/// The default extra data in bytes.
87/// See [`default_extra_data`].
88pub fn default_extra_data_bytes() -> Bytes {
89    Bytes::from(default_extra_data().as_bytes().to_vec())
90}
91
92/// The default client version accessing the database.
93pub fn default_client_version() -> ClientVersion {
94    let meta = version_metadata();
95    ClientVersion {
96        version: meta.cargo_pkg_version.to_string(),
97        git_sha: meta.vergen_git_sha.to_string(),
98        build_timestamp: meta.vergen_build_timestamp.to_string(),
99    }
100}
101
102/// Get a reference to the global version metadata
103pub fn version_metadata() -> &'static RethCliVersionConsts {
104    VERSION_METADATA.get_or_init(default_reth_version_metadata)
105}
106
107/// default reth version metadata using compile-time env! macros.
108pub fn default_reth_version_metadata() -> RethCliVersionConsts {
109    RethCliVersionConsts {
110        name_client: Cow::Borrowed("Reth"),
111        cargo_pkg_version: Cow::Owned(env!("CARGO_PKG_VERSION").to_string()),
112        vergen_git_sha_long: Cow::Owned(env!("VERGEN_GIT_SHA").to_string()),
113        vergen_git_sha: Cow::Owned(env!("VERGEN_GIT_SHA_SHORT").to_string()),
114        vergen_build_timestamp: Cow::Owned(env!("VERGEN_BUILD_TIMESTAMP").to_string()),
115        vergen_cargo_target_triple: Cow::Owned(env!("VERGEN_CARGO_TARGET_TRIPLE").to_string()),
116        vergen_cargo_features: Cow::Owned(env!("VERGEN_CARGO_FEATURES").to_string()),
117        short_version: Cow::Owned(env!("RETH_SHORT_VERSION").to_string()),
118        long_version: Cow::Owned(format!(
119            "{}\n{}\n{}\n{}\n{}",
120            env!("RETH_LONG_VERSION_0"),
121            env!("RETH_LONG_VERSION_1"),
122            env!("RETH_LONG_VERSION_2"),
123            env!("RETH_LONG_VERSION_3"),
124            env!("RETH_LONG_VERSION_4"),
125        )),
126
127        build_profile_name: Cow::Owned(env!("RETH_BUILD_PROFILE").to_string()),
128        p2p_client_version: Cow::Owned(env!("RETH_P2P_CLIENT_VERSION").to_string()),
129        extra_data: Cow::Owned(default_extra_data()),
130    }
131}
132
133#[cfg(test)]
134mod tests {
135    use super::*;
136
137    #[test]
138    fn assert_extra_data_less_32bytes() {
139        let extra_data = default_extra_data();
140        assert!(extra_data.len() <= 32, "extra data must be less than 32 bytes: {extra_data}")
141    }
142}