Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/blockchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ pub const INTERVALS_PER_SLOT: u64 = 5;
/// Milliseconds in a slot (derived from interval duration and count).
pub const MILLISECONDS_PER_SLOT: u64 = MILLISECONDS_PER_INTERVAL * INTERVALS_PER_SLOT;
pub use ethlambda_types::block::MAX_ATTESTATIONS_DATA;
/// Slots of head-vs-wall-clock lag above which a node is considered syncing.
pub use sync_status::SYNC_LAG_THRESHOLD;
/// Future-slot tolerance for gossip attestations, expressed in intervals.
///
/// Bounds the clock skew the time check is willing to absorb when admitting a
Expand Down
2 changes: 1 addition & 1 deletion crates/blockchain/src/sync_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::metrics::SyncStatus;
/// Local head lag beyond which the node is considered to be syncing.
///
/// See: leanSpec PR #708.
const SYNC_LAG_THRESHOLD: u64 = 4;
pub const SYNC_LAG_THRESHOLD: u64 = 4;
/// Freshest-known block lag beyond which the network is considered stalled.
///
/// During a network-wide stall the node remains synced so validators can help
Expand Down
17 changes: 14 additions & 3 deletions crates/net/rpc/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,26 @@
//! the role (hot-standby model). See leanSpec PR #636 for the full rationale.

use axum::{
Extension, Json,
Extension, Json, Router,
http::StatusCode,
response::{IntoResponse, Response},
routing::get,
};
use ethlambda_storage::Store;
use ethlambda_types::aggregator::AggregatorController;
use serde::Serialize;
use serde_json::Value;
use tracing::info;

use crate::json_response;

pub(crate) fn routes() -> Router<Store> {
Router::new().route(
"/lean/v0/admin/aggregator",
get(get_aggregator).post(post_aggregator),
)
}

#[derive(Serialize)]
struct StatusResponse {
is_aggregator: bool,
Expand All @@ -44,7 +53,9 @@ struct ToggleResponse {
/// `Extension<T>` would cause axum to short-circuit with a 500 when the
/// extension is missing, whereas `Option` yields `None` and lets us return
/// a clean 503 with a useful message.
pub async fn get_aggregator(controller: Option<Extension<AggregatorController>>) -> Response {
pub(crate) async fn get_aggregator(
controller: Option<Extension<AggregatorController>>,
) -> Response {
match controller {
Some(Extension(controller)) => json_response(StatusResponse {
is_aggregator: controller.is_enabled(),
Expand All @@ -62,7 +73,7 @@ pub async fn get_aggregator(controller: Option<Extension<AggregatorController>>)
/// `Extension<T>` would cause axum to short-circuit with a 500 when the
/// extension is missing, whereas `Option` yields `None` and lets us return
/// a clean 503 with a useful message.
pub async fn post_aggregator(
pub(crate) async fn post_aggregator(
controller: Option<Extension<AggregatorController>>,
body: Option<Json<Value>>,
) -> Response {
Expand Down
12 changes: 10 additions & 2 deletions crates/net/rpc/src/blocks.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
use axum::{
Router,
extract::{Path, State},
http::StatusCode,
response::IntoResponse,
routing::get,
};
use ethlambda_storage::Store;
use ethlambda_types::primitives::H256;
use serde_json::json;

use crate::json_response;

pub(crate) fn routes() -> Router<Store> {
Router::new()
.route("/lean/v0/blocks/{block_id}", get(get_block))
.route("/lean/v0/blocks/{block_id}/header", get(get_block_header))
}

/// `GET /lean/v0/blocks/:block_id` — returns the block as JSON.
///
/// `block_id` can be a `0x`-prefixed 32-byte hex root or a decimal slot.
pub async fn get_block(
pub(crate) async fn get_block(
Path(block_id): Path<String>,
State(store): State<Store>,
) -> impl IntoResponse {
Expand All @@ -28,7 +36,7 @@ pub async fn get_block(
}

/// `GET /lean/v0/blocks/:block_id/header` — returns the block header as JSON.
pub async fn get_block_header(
pub(crate) async fn get_block_header(
Path(block_id): Path<String>,
State(store): State<Store>,
) -> impl IntoResponse {
Expand Down
73 changes: 73 additions & 0 deletions crates/net/rpc/src/core.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use axum::{
Json, Router,
http::{HeaderValue, header},
response::IntoResponse,
routing::get,
};
use ethlambda_storage::Store;
use ethlambda_types::primitives::H256;
use libssz::SszEncode;

pub(crate) fn routes() -> Router<Store> {
Router::new()
.route("/lean/v0/health", get(crate::metrics::get_health))
.route("/lean/v0/states/finalized", get(get_latest_finalized_state))
.route("/lean/v0/blocks/finalized", get(get_latest_finalized_block))
.route(
"/lean/v0/checkpoints/justified",
get(get_latest_justified_state),
)
}

pub(crate) async fn get_latest_finalized_state(
axum::extract::State(store): axum::extract::State<Store>,
) -> impl IntoResponse {
let finalized = store.latest_finalized();
let mut state = store
.get_state(&finalized.root)
.expect("finalized state exists");

// Zero state_root to match the canonical post-state representation.
// The spec's state_transition sets state_root to zero during process_block_header,
// and only fills it in lazily at the next slot's process_slots.
// Serving the canonical form ensures checkpoint sync interoperability.
state.latest_block_header.state_root = H256::ZERO;

ssz_response(state.to_ssz())
}

pub(crate) async fn get_latest_finalized_block(
axum::extract::State(store): axum::extract::State<Store>,
) -> impl IntoResponse {
let finalized = store.latest_finalized();
// Returns 404 for genesis since it doesn't have a valid signature
match store.get_signed_block(&finalized.root) {
Some(block) => ssz_response(block.to_ssz()),
None => axum::http::StatusCode::NOT_FOUND.into_response(),
}
}

pub(crate) async fn get_latest_justified_state(
axum::extract::State(store): axum::extract::State<Store>,
) -> impl IntoResponse {
let checkpoint = store.latest_justified();
json_response(checkpoint)
}

pub(crate) fn json_response<T: serde::Serialize>(value: T) -> axum::response::Response {
let mut response = Json(value).into_response();
response.headers_mut().insert(
header::CONTENT_TYPE,
HeaderValue::from_static(crate::JSON_CONTENT_TYPE),
);
response
}

fn ssz_response(bytes: Vec<u8>) -> axum::response::Response {
let mut response = bytes.into_response();
response.headers_mut().insert(
header::CONTENT_TYPE,
HeaderValue::from_static(crate::SSZ_CONTENT_TYPE),
);
response
}
19 changes: 11 additions & 8 deletions crates/net/rpc/src/fork_choice.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use axum::{http::HeaderValue, http::header, response::IntoResponse};
use axum::{Router, http::HeaderValue, http::header, response::IntoResponse, routing::get};
use ethlambda_storage::Store;
use ethlambda_types::{checkpoint::Checkpoint, primitives::H256};
use serde::Serialize;

use crate::json_response;

pub(crate) fn routes() -> Router<Store> {
Router::new()
.route("/lean/v0/fork_choice", get(get_fork_choice))
.route("/lean/v0/fork_choice/ui", get(get_fork_choice_ui))
}

const HTML_CONTENT_TYPE: &str = "text/html; charset=utf-8";
const FORK_CHOICE_HTML: &str = include_str!("../static/fork_choice.html");

Expand All @@ -27,7 +33,7 @@ pub struct ForkChoiceNode {
weight: u64,
}

pub async fn get_fork_choice(
pub(crate) async fn get_fork_choice(
axum::extract::State(store): axum::extract::State<Store>,
) -> impl IntoResponse {
let blocks = store.get_live_chain();
Expand Down Expand Up @@ -75,7 +81,7 @@ pub async fn get_fork_choice(
json_response(response)
}

pub async fn get_fork_choice_ui() -> impl IntoResponse {
pub(crate) async fn get_fork_choice_ui() -> impl IntoResponse {
let mut response = FORK_CHOICE_HTML.into_response();
response.headers_mut().insert(
header::CONTENT_TYPE,
Expand All @@ -87,7 +93,7 @@ pub async fn get_fork_choice_ui() -> impl IntoResponse {
#[cfg(test)]
mod tests {
use super::*;
use axum::{Router, body::Body, http::Request, http::StatusCode, routing::get};
use axum::{Router, body::Body, http::Request, http::StatusCode};
use ethlambda_storage::{Store, backend::InMemoryBackend};
use http_body_util::BodyExt;
use std::sync::Arc;
Expand All @@ -96,10 +102,7 @@ mod tests {
use crate::test_utils::create_test_state;

fn build_test_router(store: Store) -> Router {
Router::new()
.route("/lean/v0/fork_choice", get(get_fork_choice))
.route("/lean/v0/fork_choice/ui", get(get_fork_choice_ui))
.with_state(store)
routes().with_state(store)
}

#[tokio::test]
Expand Down
95 changes: 12 additions & 83 deletions crates/net/rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
use std::net::{IpAddr, SocketAddr};

use axum::{
Extension, Json, Router,
http::{HeaderValue, StatusCode, header},
response::IntoResponse,
routing::get,
};
use axum::{Extension, Router};
use ethlambda_storage::Store;
use ethlambda_types::aggregator::AggregatorController;
use ethlambda_types::primitives::H256;
use libssz::SszEncode;
use tokio_util::sync::CancellationToken;

pub(crate) const JSON_CONTENT_TYPE: &str = "application/json; charset=utf-8";
pub(crate) const SSZ_CONTENT_TYPE: &str = "application/octet-stream";

mod admin;
mod blocks;
mod core;
mod fork_choice;
mod heap_profiling;
pub mod metrics;
mod node;
pub mod test_driver;

pub(crate) use core::json_response;

#[derive(Debug, Clone)]
pub struct RpcConfig {
pub http_address: IpAddr,
Expand Down Expand Up @@ -100,32 +97,17 @@ pub async fn start_rpc_server(
/// know about it and admin handlers extract it independently.
fn build_api_router(store: Store) -> Router {
Router::new()
.route("/lean/v0/health", get(metrics::get_health))
.route("/lean/v0/states/finalized", get(get_latest_finalized_state))
.route("/lean/v0/blocks/finalized", get(get_latest_finalized_block))
.route(
"/lean/v0/checkpoints/justified",
get(get_latest_justified_state),
)
.route("/lean/v0/fork_choice", get(fork_choice::get_fork_choice))
.route(
"/lean/v0/fork_choice/ui",
get(fork_choice::get_fork_choice_ui),
)
.route("/lean/v0/blocks/{block_id}", get(blocks::get_block))
.route(
"/lean/v0/blocks/{block_id}/header",
get(blocks::get_block_header),
)
.route(
"/lean/v0/admin/aggregator",
get(admin::get_aggregator).post(admin::post_aggregator),
)
.merge(core::routes())
.merge(blocks::routes())
.merge(fork_choice::routes())
.merge(admin::routes())
.merge(node::routes())
.with_state(store)
}

/// Build the debug router for profiling endpoints.
fn build_debug_router() -> Router {
use axum::routing::get;
Router::new()
.route("/debug/pprof/allocs", get(heap_profiling::handle_get_heap))
.route(
Expand All @@ -134,59 +116,6 @@ fn build_debug_router() -> Router {
)
}

async fn get_latest_finalized_state(
axum::extract::State(store): axum::extract::State<Store>,
) -> impl IntoResponse {
let finalized = store.latest_finalized();
let mut state = store
.get_state(&finalized.root)
.expect("finalized state exists");

// Zero state_root to match the canonical post-state representation.
// The spec's state_transition sets state_root to zero during process_block_header,
// and only fills it in lazily at the next slot's process_slots.
// Serving the canonical form ensures checkpoint sync interoperability.
state.latest_block_header.state_root = H256::ZERO;

ssz_response(state.to_ssz())
}

async fn get_latest_finalized_block(
axum::extract::State(store): axum::extract::State<Store>,
) -> impl IntoResponse {
let finalized = store.latest_finalized();
// Returns 404 for genesis since it doesn't have a valid signature
match store.get_signed_block(&finalized.root) {
Some(block) => ssz_response(block.to_ssz()),
None => StatusCode::NOT_FOUND.into_response(),
}
}

async fn get_latest_justified_state(
axum::extract::State(store): axum::extract::State<Store>,
) -> impl IntoResponse {
let checkpoint = store.latest_justified();
json_response(checkpoint)
}

fn json_response<T: serde::Serialize>(value: T) -> axum::response::Response {
let mut response = Json(value).into_response();
response.headers_mut().insert(
header::CONTENT_TYPE,
HeaderValue::from_static(JSON_CONTENT_TYPE),
);
response
}

fn ssz_response(bytes: Vec<u8>) -> axum::response::Response {
let mut response = bytes.into_response();
response.headers_mut().insert(
header::CONTENT_TYPE,
HeaderValue::from_static(SSZ_CONTENT_TYPE),
);
response
}

#[cfg(test)]
pub(crate) mod test_utils {
use ethlambda_storage::{StorageBackend, Table};
Expand Down Expand Up @@ -267,7 +196,7 @@ pub(crate) mod test_utils {
#[cfg(test)]
mod tests {
use super::*;
use axum::{body::Body, http::Request};
use axum::{body::Body, http::Request, http::StatusCode, http::header};
use ethlambda_storage::{ForkCheckpoints, Store, backend::InMemoryBackend};
use http_body_util::BodyExt;
use serde_json::json;
Expand Down
Loading
Loading