From 1c3c6157a604c77d10630c1e2a35d2713666b411 Mon Sep 17 00:00:00 2001 From: ThreeHrSleep Date: Mon, 10 Mar 2025 13:17:27 +0530 Subject: [PATCH 1/4] add version endpoint --- Cargo.lock | 14 ++++++++++++-- Cargo.toml | 3 +++ anchor/common/api_types/Cargo.toml | 7 +++++++ anchor/common/api_types/src/lib.rs | 16 ++++++++++++++++ anchor/common/version/src/lib.rs | 1 - anchor/http_api/Cargo.toml | 3 +++ anchor/http_api/src/router.rs | 15 ++++++++++++--- 7 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 anchor/common/api_types/Cargo.toml create mode 100644 anchor/common/api_types/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 3d44e725c..9476c3a18 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -830,6 +830,13 @@ version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34ac096ce696dc2fcabef30516bb13c0a68a11d30131d3df6f04711467681b04" +[[package]] +name = "api_types" +version = "0.1.0" +dependencies = [ + "serde", +] + [[package]] name = "arbitrary" version = "1.4.1" @@ -3704,12 +3711,15 @@ dependencies = [ name = "http_api" version = "0.1.0" dependencies = [ + "api_types", "axum", "serde", + "serde_json", "slot_clock", "task_executor", "tokio", "tracing", + "version", ] [[package]] @@ -7134,9 +7144,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.138" +version = "1.0.140" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d434192e7da787e94a6ea7e9670b26a036d0ca41e0b7efb2676dd32bae872949" +checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" dependencies = [ "itoa", "memchr", diff --git a/Cargo.toml b/Cargo.toml index 7fbe5806f..3ccf9a3db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ members = [ "anchor/common/ssv_network_config", "anchor/common/ssv_types", "anchor/common/version", + "anchor/common/api_types", "anchor/database", "anchor/eth", "anchor/http_api", @@ -34,6 +35,7 @@ client = { path = "anchor/client" } database = { path = "anchor/database" } eth = { path = "anchor/eth" } http_api = { path = "anchor/http_api" } +api_types = { path = "anchor/common/api_types" } http_metrics = { path = "anchor/http_metrics" } message_sender = { path = "anchor/message_sender" } message_validator = { path = "anchor/message_validator" } @@ -106,6 +108,7 @@ reqwest = "0.12.12" rusqlite = "0.28.0" serde = { version = "1.0.208", features = ["derive"] } serde_yaml = "0.9" +serde_json = "1.0.140" sha2 = "0.10.8" strum = { version = "0.26.3", features = ["derive"] } thiserror = "2.0.11" diff --git a/anchor/common/api_types/Cargo.toml b/anchor/common/api_types/Cargo.toml new file mode 100644 index 000000000..17db4c77f --- /dev/null +++ b/anchor/common/api_types/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "api_types" +version = "0.1.0" +edition = "2024" + +[dependencies] +serde = { workspace = true } \ No newline at end of file diff --git a/anchor/common/api_types/src/lib.rs b/anchor/common/api_types/src/lib.rs new file mode 100644 index 000000000..dcc96035e --- /dev/null +++ b/anchor/common/api_types/src/lib.rs @@ -0,0 +1,16 @@ +use serde::Serialize; + +#[derive(Serialize)] +pub struct VersionData { + pub version: String, +} +#[derive(Serialize)] +pub struct GenericResponse { + pub data: T, +} + +implFrom for GenericResponse { + fn from(data: T) -> Self { + Self { data } + } +} diff --git a/anchor/common/version/src/lib.rs b/anchor/common/version/src/lib.rs index 225e6b7ad..faf5646b6 100644 --- a/anchor/common/version/src/lib.rs +++ b/anchor/common/version/src/lib.rs @@ -45,7 +45,6 @@ pub const COMMIT_PREFIX: &str = git_version!( /// ## Example /// /// `Anchor/v0.1.0-67da032+/x86_64-linux` -#[allow(dead_code)] pub fn version_with_platform() -> String { format!("{}/{}-{}", VERSION, Target::arch(), Target::os()) } diff --git a/anchor/http_api/Cargo.toml b/anchor/http_api/Cargo.toml index 15975bac1..90023496a 100644 --- a/anchor/http_api/Cargo.toml +++ b/anchor/http_api/Cargo.toml @@ -15,3 +15,6 @@ slot_clock = { workspace = true } task_executor = { workspace = true } tokio = { workspace = true } tracing = { workspace = true } +serde_json = { workspace = true } +version = {workspace = true } +api_types = { workspace = true } \ No newline at end of file diff --git a/anchor/http_api/src/router.rs b/anchor/http_api/src/router.rs index a6a5fb5b1..6a5d525ae 100644 --- a/anchor/http_api/src/router.rs +++ b/anchor/http_api/src/router.rs @@ -1,14 +1,23 @@ //! The routes for the HTTP API -use axum::{routing::get, Router}; - +use axum::{routing::get, Router, Json}; +use version::{version_with_platform}; +use api_types::{GenericResponse, VersionData}; /// Creates all the routes for HTTP API pub fn new() -> Router { // Default route - Router::new().route("/", get(root)) + Router::new() + .route("/", get(root)) + .route("/anchor/version", get(get_version)) } // Temporary return value. async fn root() -> &'static str { "Anchor client" } + +async fn get_version() -> Json> { + Json(GenericResponse::from(VersionData { + version: version_with_platform(), + })) +} From 5013177fd5b997bffb228f8e8bda7a1afaf75ce9 Mon Sep 17 00:00:00 2001 From: ThreeHrSleep Date: Mon, 10 Mar 2025 13:36:17 +0530 Subject: [PATCH 2/4] fmt + sort --- Cargo.toml | 2 +- anchor/common/api_types/Cargo.toml | 2 +- anchor/common/api_types/src/lib.rs | 2 +- anchor/http_api/Cargo.toml | 6 +++--- anchor/http_api/src/router.rs | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3ccf9a3db..1c1c3d88d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,12 +3,12 @@ members = [ "anchor", "anchor/client", + "anchor/common/api_types", "anchor/common/bls_lagrange", "anchor/common/qbft", "anchor/common/ssv_network_config", "anchor/common/ssv_types", "anchor/common/version", - "anchor/common/api_types", "anchor/database", "anchor/eth", "anchor/http_api", diff --git a/anchor/common/api_types/Cargo.toml b/anchor/common/api_types/Cargo.toml index 17db4c77f..19fd933f5 100644 --- a/anchor/common/api_types/Cargo.toml +++ b/anchor/common/api_types/Cargo.toml @@ -4,4 +4,4 @@ version = "0.1.0" edition = "2024" [dependencies] -serde = { workspace = true } \ No newline at end of file +serde = { workspace = true } diff --git a/anchor/common/api_types/src/lib.rs b/anchor/common/api_types/src/lib.rs index dcc96035e..347591724 100644 --- a/anchor/common/api_types/src/lib.rs +++ b/anchor/common/api_types/src/lib.rs @@ -9,7 +9,7 @@ pub struct GenericResponse { pub data: T, } -implFrom for GenericResponse { +impl From for GenericResponse { fn from(data: T) -> Self { Self { data } } diff --git a/anchor/http_api/Cargo.toml b/anchor/http_api/Cargo.toml index 90023496a..5058835fc 100644 --- a/anchor/http_api/Cargo.toml +++ b/anchor/http_api/Cargo.toml @@ -9,12 +9,12 @@ name = "http_api" path = "src/lib.rs" [dependencies] +api_types = { workspace = true } axum = { workspace = true } serde = { workspace = true } +serde_json = { workspace = true } slot_clock = { workspace = true } task_executor = { workspace = true } tokio = { workspace = true } tracing = { workspace = true } -serde_json = { workspace = true } -version = {workspace = true } -api_types = { workspace = true } \ No newline at end of file +version = { workspace = true } diff --git a/anchor/http_api/src/router.rs b/anchor/http_api/src/router.rs index 6a5d525ae..95b410fbc 100644 --- a/anchor/http_api/src/router.rs +++ b/anchor/http_api/src/router.rs @@ -1,8 +1,8 @@ //! The routes for the HTTP API -use axum::{routing::get, Router, Json}; -use version::{version_with_platform}; use api_types::{GenericResponse, VersionData}; +use axum::{routing::get, Json, Router}; +use version::version_with_platform; /// Creates all the routes for HTTP API pub fn new() -> Router { // Default route From 3d4233ac23f6c161f552901f4c0badd91c63708f Mon Sep 17 00:00:00 2001 From: ThreeHrSleep Date: Mon, 10 Mar 2025 15:27:14 +0530 Subject: [PATCH 3/4] smol fix --- anchor/common/api_types/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/anchor/common/api_types/Cargo.toml b/anchor/common/api_types/Cargo.toml index 19fd933f5..ac580555b 100644 --- a/anchor/common/api_types/Cargo.toml +++ b/anchor/common/api_types/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "api_types" version = "0.1.0" -edition = "2024" +edition = { workspace = true } [dependencies] serde = { workspace = true } From b59c29ac778d868e919826f3b7f8af14166a8b91 Mon Sep 17 00:00:00 2001 From: ThreeHrSleep Date: Mon, 10 Mar 2025 17:07:41 +0530 Subject: [PATCH 4/4] nit --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1c1c3d88d..7691d06c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,12 +30,12 @@ edition = "2021" # This table has three subsections: first the internal dependencies, then the lighthouse dependencies, then all other. [workspace.dependencies] anchor_validator_store = { path = "anchor/validator_store" } +api_types = { path = "anchor/common/api_types" } bls_lagrange = { path = "anchor/common/bls_lagrange" } client = { path = "anchor/client" } database = { path = "anchor/database" } eth = { path = "anchor/eth" } http_api = { path = "anchor/http_api" } -api_types = { path = "anchor/common/api_types" } http_metrics = { path = "anchor/http_metrics" } message_sender = { path = "anchor/message_sender" } message_validator = { path = "anchor/message_validator" } @@ -107,8 +107,8 @@ rand = "0.8.5" reqwest = "0.12.12" rusqlite = "0.28.0" serde = { version = "1.0.208", features = ["derive"] } -serde_yaml = "0.9" serde_json = "1.0.140" +serde_yaml = "0.9" sha2 = "0.10.8" strum = { version = "0.26.3", features = ["derive"] } thiserror = "2.0.11"