|
| 1 | +use axum::{Router, response::IntoResponse, routing::get}; |
| 2 | +use ethlambda_blockchain::{INTERVALS_PER_SLOT, MILLISECONDS_PER_INTERVAL, MILLISECONDS_PER_SLOT}; |
| 3 | +use ethlambda_storage::Store; |
| 4 | +use ethlambda_types::{constants::FORK_DIGEST, state::HISTORICAL_ROOTS_LIMIT}; |
| 5 | +use serde::Serialize; |
| 6 | + |
| 7 | +use crate::json_response; |
| 8 | + |
| 9 | +#[derive(Serialize)] |
| 10 | +struct SpecResponse { |
| 11 | + #[serde(rename = "MILLISECONDS_PER_SLOT")] |
| 12 | + ms_per_slot: u64, |
| 13 | + #[serde(rename = "INTERVALS_PER_SLOT")] |
| 14 | + intervals_per_slot: u64, |
| 15 | + #[serde(rename = "MILLISECONDS_PER_INTERVAL")] |
| 16 | + ms_per_interval: u64, |
| 17 | + #[serde(rename = "HISTORICAL_ROOTS_LIMIT")] |
| 18 | + historical_roots_limit: u64, |
| 19 | + #[serde(rename = "FORK_DIGEST")] |
| 20 | + fork_digest: &'static str, |
| 21 | +} |
| 22 | + |
| 23 | +async fn get_spec() -> impl IntoResponse { |
| 24 | + json_response(SpecResponse { |
| 25 | + ms_per_slot: MILLISECONDS_PER_SLOT, |
| 26 | + intervals_per_slot: INTERVALS_PER_SLOT, |
| 27 | + ms_per_interval: MILLISECONDS_PER_INTERVAL, |
| 28 | + historical_roots_limit: HISTORICAL_ROOTS_LIMIT as u64, |
| 29 | + fork_digest: FORK_DIGEST, |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +pub(crate) fn routes() -> Router<Store> { |
| 34 | + Router::new().route("/lean/v0/config/spec", get(get_spec)) |
| 35 | +} |
| 36 | + |
| 37 | +#[cfg(test)] |
| 38 | +mod tests { |
| 39 | + use super::FORK_DIGEST; |
| 40 | + use crate::test_utils::create_test_state; |
| 41 | + use axum::{ |
| 42 | + body::Body, |
| 43 | + http::{Request, StatusCode}, |
| 44 | + }; |
| 45 | + use ethlambda_blockchain::{ |
| 46 | + INTERVALS_PER_SLOT, MILLISECONDS_PER_INTERVAL, MILLISECONDS_PER_SLOT, |
| 47 | + }; |
| 48 | + use ethlambda_storage::{Store, backend::InMemoryBackend}; |
| 49 | + use ethlambda_types::state::HISTORICAL_ROOTS_LIMIT; |
| 50 | + use http_body_util::BodyExt; |
| 51 | + use std::sync::Arc; |
| 52 | + use tower::ServiceExt; |
| 53 | + |
| 54 | + #[tokio::test] |
| 55 | + async fn spec_returns_lean_constants() { |
| 56 | + let store = Store::from_anchor_state(Arc::new(InMemoryBackend::new()), create_test_state()); |
| 57 | + let app = crate::build_api_router(store); |
| 58 | + let resp = app |
| 59 | + .oneshot( |
| 60 | + Request::builder() |
| 61 | + .uri("/lean/v0/config/spec") |
| 62 | + .body(Body::empty()) |
| 63 | + .unwrap(), |
| 64 | + ) |
| 65 | + .await |
| 66 | + .unwrap(); |
| 67 | + assert_eq!(resp.status(), StatusCode::OK); |
| 68 | + let body = resp.into_body().collect().await.unwrap().to_bytes(); |
| 69 | + let json: serde_json::Value = serde_json::from_slice(&body).unwrap(); |
| 70 | + assert_eq!(json["MILLISECONDS_PER_SLOT"], MILLISECONDS_PER_SLOT); |
| 71 | + assert_eq!(json["INTERVALS_PER_SLOT"], INTERVALS_PER_SLOT); |
| 72 | + assert_eq!(json["MILLISECONDS_PER_INTERVAL"], MILLISECONDS_PER_INTERVAL); |
| 73 | + assert_eq!( |
| 74 | + json["HISTORICAL_ROOTS_LIMIT"], |
| 75 | + HISTORICAL_ROOTS_LIMIT as u64 |
| 76 | + ); |
| 77 | + assert_eq!(json["FORK_DIGEST"], FORK_DIGEST); |
| 78 | + } |
| 79 | +} |
0 commit comments