Skip to main content

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.
103///
104/// Returns [`unknown_version_metadata`] when no binary set it with
105/// [`try_init_version_metadata`].
106pub fn version_metadata() -> &'static RethCliVersionConsts {
107    VERSION_METADATA.get_or_init(unknown_version_metadata)
108}
109
110/// Version metadata without anything a build script stamps: the package version and `unknown`
111/// for the rest.
112pub fn unknown_version_metadata() -> RethCliVersionConsts {
113    let version = env!("CARGO_PKG_VERSION");
114    let unknown = || Cow::Borrowed("unknown");
115    RethCliVersionConsts {
116        name_client: Cow::Borrowed("Reth"),
117        cargo_pkg_version: Cow::Borrowed(version),
118        vergen_git_sha_long: unknown(),
119        vergen_git_sha: unknown(),
120        vergen_build_timestamp: unknown(),
121        vergen_cargo_target_triple: unknown(),
122        vergen_cargo_features: unknown(),
123        short_version: Cow::Borrowed(version),
124        long_version: Cow::Owned(format!("Version: {version}")),
125        build_profile_name: unknown(),
126        p2p_client_version: Cow::Owned(format!("reth/v{version}/{}", std::env::consts::OS)),
127        extra_data: Cow::Owned(default_extra_data()),
128    }
129}
130
131/// Reth's version metadata, stamped by the calling binary's build script.
132///
133/// Version metadata changes with every commit, and the build timestamp with every build. Stamped
134/// in this crate, it would rebuild every crate that depends on this one. Instead, a binary calls
135/// `reth_node_version_build::emit_version_metadata` from its build script, and sets the result
136/// with [`try_init_version_metadata`]:
137///
138/// ```ignore
139/// reth_node_core::version::try_init_version_metadata(reth_node_core::reth_version_metadata!())
140///     .expect("version metadata is set once, before anything reads it");
141/// ```
142///
143/// [`try_init_version_metadata`]: crate::version::try_init_version_metadata
144#[macro_export]
145macro_rules! reth_version_metadata {
146    () => {
147        $crate::version::RethCliVersionConsts {
148            name_client: ::std::borrow::Cow::Borrowed("Reth"),
149            cargo_pkg_version: ::std::borrow::Cow::Borrowed(env!("CARGO_PKG_VERSION")),
150            vergen_git_sha_long: ::std::borrow::Cow::Borrowed(env!("VERGEN_GIT_SHA")),
151            vergen_git_sha: ::std::borrow::Cow::Borrowed(env!("VERGEN_GIT_SHA_SHORT")),
152            vergen_build_timestamp: ::std::borrow::Cow::Borrowed(env!("VERGEN_BUILD_TIMESTAMP")),
153            vergen_cargo_target_triple: ::std::borrow::Cow::Borrowed(env!(
154                "VERGEN_CARGO_TARGET_TRIPLE"
155            )),
156            vergen_cargo_features: ::std::borrow::Cow::Borrowed(env!("VERGEN_CARGO_FEATURES")),
157            short_version: ::std::borrow::Cow::Borrowed(env!("RETH_SHORT_VERSION")),
158            long_version: ::std::borrow::Cow::Borrowed(concat!(
159                env!("RETH_LONG_VERSION_0"),
160                "\n",
161                env!("RETH_LONG_VERSION_1"),
162                "\n",
163                env!("RETH_LONG_VERSION_2"),
164                "\n",
165                env!("RETH_LONG_VERSION_3"),
166                "\n",
167                env!("RETH_LONG_VERSION_4"),
168            )),
169            build_profile_name: ::std::borrow::Cow::Borrowed(env!("RETH_BUILD_PROFILE")),
170            p2p_client_version: ::std::borrow::Cow::Borrowed(env!("RETH_P2P_CLIENT_VERSION")),
171            extra_data: ::std::borrow::Cow::Owned($crate::version::default_extra_data()),
172        }
173    };
174}
175
176#[cfg(test)]
177mod tests {
178    use super::*;
179
180    #[test]
181    fn assert_extra_data_less_32bytes() {
182        let extra_data = default_extra_data();
183        assert!(extra_data.len() <= 32, "extra data must be less than 32 bytes: {extra_data}")
184    }
185}