reth_node_core/
version.rs

1//! Version information for reth.
2use alloy_primitives::Bytes;
3use alloy_rpc_types_engine::ClientCode;
4use reth_db::ClientVersion;
5
6/// The client code for Reth
7pub const CLIENT_CODE: ClientCode = ClientCode::RH;
8
9/// The human readable name of the client
10pub const NAME_CLIENT: &str = "Reth";
11
12/// The latest version from Cargo.toml.
13pub const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
14
15/// The full SHA of the latest commit.
16pub const VERGEN_GIT_SHA_LONG: &str = env!("VERGEN_GIT_SHA");
17
18/// The 8 character short SHA of the latest commit.
19pub const VERGEN_GIT_SHA: &str = const_format::str_index!(VERGEN_GIT_SHA_LONG, ..8);
20
21/// The build timestamp.
22pub const VERGEN_BUILD_TIMESTAMP: &str = env!("VERGEN_BUILD_TIMESTAMP");
23
24/// The target triple.
25pub const VERGEN_CARGO_TARGET_TRIPLE: &str = env!("VERGEN_CARGO_TARGET_TRIPLE");
26
27/// The build features.
28pub const VERGEN_CARGO_FEATURES: &str = env!("VERGEN_CARGO_FEATURES");
29
30/// The short version information for reth.
31///
32/// - The latest version from Cargo.toml
33/// - The short SHA of the latest commit.
34///
35/// # Example
36///
37/// ```text
38/// 0.1.0 (defa64b2)
39/// ```
40pub const SHORT_VERSION: &str = const_format::concatcp!(
41    env!("CARGO_PKG_VERSION"),
42    env!("RETH_VERSION_SUFFIX"),
43    " (",
44    VERGEN_GIT_SHA,
45    ")"
46);
47
48/// The long version information for reth.
49///
50/// - The latest version from Cargo.toml
51/// - The long SHA of the latest commit.
52/// - The build datetime
53/// - The build features
54/// - The build profile
55///
56/// # Example:
57///
58/// ```text
59/// Version: 0.1.0
60/// Commit SHA: defa64b2
61/// Build Timestamp: 2023-05-19T01:47:19.815651705Z
62/// Build Features: jemalloc
63/// Build Profile: maxperf
64/// ```
65pub const LONG_VERSION: &str = const_format::concatcp!(
66    "Version: ",
67    env!("CARGO_PKG_VERSION"),
68    env!("RETH_VERSION_SUFFIX"),
69    "\n",
70    "Commit SHA: ",
71    VERGEN_GIT_SHA_LONG,
72    "\n",
73    "Build Timestamp: ",
74    env!("VERGEN_BUILD_TIMESTAMP"),
75    "\n",
76    "Build Features: ",
77    env!("VERGEN_CARGO_FEATURES"),
78    "\n",
79    "Build Profile: ",
80    BUILD_PROFILE_NAME
81);
82
83/// The build profile name.
84pub const BUILD_PROFILE_NAME: &str = {
85    // Derived from https://stackoverflow.com/questions/73595435/how-to-get-profile-from-cargo-toml-in-build-rs-or-at-runtime
86    // We split on the path separator of the *host* machine, which may be different from
87    // `std::path::MAIN_SEPARATOR_STR`.
88    const OUT_DIR: &str = env!("OUT_DIR");
89    let unix_parts = const_format::str_split!(OUT_DIR, '/');
90    if unix_parts.len() >= 4 {
91        unix_parts[unix_parts.len() - 4]
92    } else {
93        let win_parts = const_format::str_split!(OUT_DIR, '\\');
94        win_parts[win_parts.len() - 4]
95    }
96};
97
98/// The version information for reth formatted for P2P (devp2p).
99///
100/// - The latest version from Cargo.toml
101/// - The target triple
102///
103/// # Example
104///
105/// ```text
106/// reth/v{major}.{minor}.{patch}-{sha1}/{target}
107/// ```
108/// e.g.: `reth/v0.1.0-alpha.1-428a6dc2f/aarch64-apple-darwin`
109pub(crate) const P2P_CLIENT_VERSION: &str = const_format::concatcp!(
110    "reth/v",
111    env!("CARGO_PKG_VERSION"),
112    "-",
113    VERGEN_GIT_SHA,
114    "/",
115    env!("VERGEN_CARGO_TARGET_TRIPLE")
116);
117
118/// The default extra data used for payload building.
119///
120/// - The latest version from Cargo.toml
121/// - The OS identifier
122///
123/// # Example
124///
125/// ```text
126/// reth/v{major}.{minor}.{patch}/{OS}
127/// ```
128pub fn default_extra_data() -> String {
129    format!("reth/v{}/{}", env!("CARGO_PKG_VERSION"), std::env::consts::OS)
130}
131
132/// The default extra data in bytes.
133/// See [`default_extra_data`].
134pub fn default_extra_data_bytes() -> Bytes {
135    Bytes::from(default_extra_data().as_bytes().to_vec())
136}
137
138/// The default client version accessing the database.
139pub fn default_client_version() -> ClientVersion {
140    ClientVersion {
141        version: CARGO_PKG_VERSION.to_string(),
142        git_sha: VERGEN_GIT_SHA.to_string(),
143        build_timestamp: VERGEN_BUILD_TIMESTAMP.to_string(),
144    }
145}
146
147#[cfg(test)]
148mod tests {
149    use super::*;
150
151    #[test]
152    fn assert_extradata_less_32bytes() {
153        let extradata = default_extra_data();
154        assert!(extradata.len() <= 32, "extradata must be less than 32 bytes: {extradata}")
155    }
156}