reth_node_core/
version.rs1use std::{borrow::Cow, sync::OnceLock};
3
4use alloy_primitives::Bytes;
5use alloy_rpc_types_engine::ClientCode;
6use reth_db::ClientVersion;
7
8pub const CLIENT_CODE: ClientCode = ClientCode::RH;
10
11static VERSION_METADATA: OnceLock<RethCliVersionConsts> = OnceLock::new();
13
14pub fn try_init_version_metadata(
16 metadata: RethCliVersionConsts,
17) -> Result<(), RethCliVersionConsts> {
18 VERSION_METADATA.set(metadata)
19}
20
21#[derive(Debug, Default)]
25pub struct RethCliVersionConsts {
26 pub name_client: Cow<'static, str>,
28
29 pub cargo_pkg_version: Cow<'static, str>,
31
32 pub vergen_git_sha_long: Cow<'static, str>,
34
35 pub vergen_git_sha: Cow<'static, str>,
37
38 pub vergen_build_timestamp: Cow<'static, str>,
40
41 pub vergen_cargo_target_triple: Cow<'static, str>,
43
44 pub vergen_cargo_features: Cow<'static, str>,
46
47 pub short_version: Cow<'static, str>,
49
50 pub long_version: Cow<'static, str>,
52 pub build_profile_name: Cow<'static, str>,
54
55 pub p2p_client_version: Cow<'static, str>,
67
68 pub extra_data: Cow<'static, str>,
70}
71
72pub fn default_extra_data() -> String {
83 format!("reth/v{}/{}", env!("CARGO_PKG_VERSION"), std::env::consts::OS)
84}
85
86pub fn default_extra_data_bytes() -> Bytes {
89 Bytes::from(default_extra_data().as_bytes().to_vec())
90}
91
92pub 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
102pub fn version_metadata() -> &'static RethCliVersionConsts {
104 VERSION_METADATA.get_or_init(default_reth_version_metadata)
105}
106
107pub 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}