Skip to content

Commit 9de65e3

Browse files
authored
Merge pull request #3742 from ChorusOne/feat-add-version-endpoint
Add version endpoint to get build info
2 parents f125e5a + 3b2a7f7 commit 9de65e3

File tree

7 files changed

+105
-23
lines changed

7 files changed

+105
-23
lines changed

Cargo.lock

Lines changed: 24 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ default-features = false
5454
[workspace.dependencies.anyhow]
5555
version = "1.0"
5656

57+
[workspace.dependencies.built]
58+
version = "0.8"
59+
features = ["git2"]
60+
5761
[workspace.dependencies.clap]
5862
version = "4.4"
5963
default-features = false

node/rest/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,6 @@ workspace = true
113113

114114
[dev-dependencies.base64]
115115
workspace = true
116+
117+
[build-dependencies.built]
118+
workspace = true

node/rest/build.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2019-2025 Provable Inc.
2+
// This file is part of the snarkOS library.
3+
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at:
7+
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
fn main() {
17+
built::write_built_file().expect("Failed to acquire build-time information");
18+
}

node/rest/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub use helpers::*;
2323

2424
mod routes;
2525

26+
mod version;
27+
2628
use snarkos_node_cdn::CdnBlockSync;
2729
use snarkos_node_consensus::Consensus;
2830
use snarkos_node_router::{
@@ -212,6 +214,7 @@ impl<N: Network, C: ConsensusStorage<N>, R: Routing<N>> Rest<N, C, R> {
212214
.route(&format!("/{network}/sync/requests/list"), get(Self::get_sync_requests_list))
213215

214216
// GET misc endpoints.
217+
.route(&format!("/{network}/version"), get(Self::get_version))
215218
.route(&format!("/{network}/blocks"), get(Self::get_blocks))
216219
.route(&format!("/{network}/height/{{hash}}"), get(Self::get_height))
217220
.route(&format!("/{network}/memoryPool/transmissions"), get(Self::get_memory_pool_transmissions))

node/rest/src/routes.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ use std::collections::HashMap;
2929
#[cfg(not(feature = "serial"))]
3030
use rayon::prelude::*;
3131

32+
use version::VersionInfo;
33+
3234
/// The `get_blocks` query object.
3335
#[derive(Deserialize, Serialize)]
3436
pub(crate) struct BlockRange {
@@ -70,6 +72,11 @@ struct SyncStatus<'a> {
7072
}
7173

7274
impl<N: Network, C: ConsensusStorage<N>, R: Routing<N>> Rest<N, C, R> {
75+
// GET /<network>/version
76+
pub(crate) async fn get_version() -> ErasedJson {
77+
ErasedJson::pretty(VersionInfo::get())
78+
}
79+
7380
// Get /<network>/consensus_version
7481
pub(crate) async fn get_consensus_version(State(rest): State<Self>) -> Result<ErasedJson, RestError> {
7582
Ok(ErasedJson::pretty(N::CONSENSUS_VERSION(rest.ledger.latest_height())? as u16))

node/rest/src/version.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2019-2025 Provable Inc.
2+
// This file is part of the snarkOS library.
3+
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at:
7+
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
use serde::Serialize;
17+
use std::sync::OnceLock;
18+
19+
// Include the generated build information
20+
mod built_info {
21+
include!(concat!(env!("OUT_DIR"), "/built.rs"));
22+
}
23+
24+
// Cache for version info to avoid repeated string allocations
25+
static VERSION_INFO: OnceLock<VersionInfo> = OnceLock::new();
26+
27+
#[derive(Clone, Debug, Serialize)]
28+
pub struct VersionInfo {
29+
/// The version from Cargo.toml
30+
pub version: String,
31+
/// Git commit hash
32+
pub git_commit: String,
33+
/// Git branch name
34+
pub git_branch: String,
35+
}
36+
37+
impl VersionInfo {
38+
/// Get the cached version information
39+
pub fn get() -> &'static VersionInfo {
40+
VERSION_INFO.get_or_init(|| VersionInfo {
41+
version: built_info::PKG_VERSION.to_string(),
42+
git_commit: built_info::GIT_COMMIT_HASH.unwrap_or("unknown").to_string(),
43+
git_branch: built_info::GIT_HEAD_REF.unwrap_or("unknown").to_string(),
44+
})
45+
}
46+
}

0 commit comments

Comments
 (0)