Skip to content

Commit

Permalink
Add version endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Sep 10, 2020
1 parent e93f95c commit 0af5795
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions beacon_node/http_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ eth2_libp2p = { path = "../eth2_libp2p" }
eth1 = { path = "../eth1" }
fork_choice = { path = "../../consensus/fork_choice" }
state_processing = { path = "../../consensus/state_processing" }
lighthouse_version = { path = "../../common/lighthouse_version" }

[dev-dependencies]
store = { path = "../store" }
Expand Down
15 changes: 15 additions & 0 deletions beacon_node/http_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use beacon_proposer_cache::BeaconProposerCache;
use block_id::BlockId;
use eth2::types::{self as api_types, ValidatorId};
use eth2_libp2p::{NetworkGlobals, PubsubMessage};
use lighthouse_version::version_with_platform;
use network::NetworkMessage;
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -913,6 +914,19 @@ pub fn serve<T: BeaconChainTypes>(
* node
*/

// GET node/version
let get_node_version = eth1_v1
.and(warp::path("node"))
.and(warp::path("version"))
.and(warp::path::end())
.and_then(|| {
blocking_json_task(move || {
Ok(api_types::GenericResponse::from(api_types::VersionData {
version: version_with_platform().to_string(),
}))
})
});

// GET node/syncing
let get_node_syncing = eth1_v1
.and(warp::path("node"))
Expand Down Expand Up @@ -1208,6 +1222,7 @@ pub fn serve<T: BeaconChainTypes>(
.or(get_config_deposit_contract.boxed())
.or(get_debug_beacon_states.boxed())
.or(get_debug_beacon_heads.boxed())
.or(get_node_version.boxed())
.or(get_node_syncing.boxed())
.or(get_validator_duties_attester.boxed())
.or(get_validator_duties_proposer.boxed())
Expand Down
18 changes: 17 additions & 1 deletion beacon_node/http_api/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,18 @@ impl ApiTester {
self
}

pub async fn test_get_node_version(self) -> Self {
let result = self.client.get_node_version().await.unwrap().data;

let expected = VersionData {
version: lighthouse_version::version_with_platform(),
};

assert_eq!(result, expected);

self
}

pub async fn test_get_node_syncing(self) -> Self {
let result = self.client.get_node_syncing().await.unwrap().data;
let head_slot = self.chain.head_info().unwrap().slot;
Expand Down Expand Up @@ -1581,7 +1593,11 @@ async fn debug_get() {

#[tokio::test(core_threads = 2)]
async fn node_get() {
ApiTester::new().test_get_node_syncing().await;
ApiTester::new()
.test_get_node_version()
.await
.test_get_node_syncing()
.await;
}

#[tokio::test(core_threads = 2)]
Expand Down
20 changes: 20 additions & 0 deletions common/eth2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use reqwest::{IntoUrl, Response, StatusCode};
use serde::{de::DeserializeOwned, Serialize};
use std::convert::TryFrom;

pub use reqwest;
pub use reqwest::Url;

#[derive(Debug)]
Expand Down Expand Up @@ -40,6 +41,13 @@ impl BeaconNodeClient {
})
}

/// Returns `Err(())` if the URL is invalid.
pub fn from_components(mut server: Url, client: reqwest::Client) -> Result<Self, ()> {
server.path_segments_mut()?.push("eth").push("v1");

Ok(Self { client, server })
}

async fn get<T: DeserializeOwned, U: IntoUrl>(&self, url: U) -> Result<T, Error> {
let response = self.client.get(url).send().await.map_err(Error::Reqwest)?;
ok_or_error(response)
Expand Down Expand Up @@ -512,6 +520,18 @@ impl BeaconNodeClient {
self.get(path).await
}

/// `GET node/version`
pub async fn get_node_version(&self) -> Result<GenericResponse<VersionData>, Error> {
let mut path = self.server.clone();

path.path_segments_mut()
.expect("path is base")
.push("node")
.push("version");

self.get(path).await
}

/// `GET node/syncing`
pub async fn get_node_syncing(&self) -> Result<GenericResponse<SyncingData>, Error> {
let mut path = self.server.clone();
Expand Down
5 changes: 5 additions & 0 deletions common/eth2/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ pub struct ChainHeadData {
pub root: Hash256,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct VersionData {
pub version: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SyncingData {
pub is_syncing: bool,
Expand Down

0 comments on commit 0af5795

Please sign in to comment.