Repository navigation
refactor(rpc): compose api router from per-feature route modules #454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,23 @@ | ||
| 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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Declaring Prompt To Fix With AIThis is a comment left during a code review.
Path: crates/net/rpc/src/lib.rs
Line: 13
Comment:
**Module name `core` shadows the standard library crate**
Declaring `mod core;` in a Rust crate root shadows the built-in `core` crate for all code in `lib.rs`. Any future use of `core::` within this file will resolve to the local module rather than the standard library, which can cause confusing compilation errors or subtle misbehaviour. A name like `endpoints_core`, `base`, or `common` would sidestep this shadowing entirely.
How can I resolve this? If you propose a fix, please make it concise. |
||
| mod fork_choice; | ||
| mod heap_profiling; | ||
| pub mod metrics; | ||
| pub mod test_driver; | ||
|
|
||
| pub(crate) use core::json_response; | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct RpcConfig { | ||
| pub http_address: IpAddr, | ||
|
|
@@ -100,32 +96,16 @@ 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()) | ||
| .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( | ||
|
|
@@ -134,59 +114,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}; | ||
|
|
@@ -267,7 +194,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; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment says "Returns 404 for genesis since it doesn't have a valid signature", but the integration test
test_get_latest_finalized_block_serves_genesis_with_placeholder_proof(inlib.rs) explicitly documents and asserts that genesis returns 200 with a synthesised placeholder proof. The comment was copied from the originallib.rswithout being updated, so it now misleads anyone reading this handler.Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!