-
Notifications
You must be signed in to change notification settings - Fork 26
feat(rpc): add GET /lean/v0/genesis #455
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
Open
MegaRedHand
wants to merge
4
commits into
main
Choose a base branch
from
feat/lean-api-genesis
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+80
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fc1b23f
feat(rpc): add GET /lean/v0/genesis
MegaRedHand b9422c3
fix(rpc): address review feedback on genesis (non-vacuous validator_c…
MegaRedHand 81d6f40
Merge remote-tracking branch 'origin/main' into feat/lean-api-genesis
MegaRedHand 5ea9b98
docs(rpc): document GET /lean/v0/genesis
MegaRedHand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| use axum::{Router, extract::State, response::IntoResponse, routing::get}; | ||
| use ethlambda_storage::Store; | ||
| use serde::Serialize; | ||
|
|
||
| use crate::json_response; | ||
|
|
||
| #[derive(Serialize)] | ||
| struct GenesisResponse { | ||
| genesis_time: u64, | ||
| validator_count: u64, | ||
| } | ||
|
|
||
| async fn get_genesis(State(store): State<Store>) -> impl IntoResponse { | ||
| let genesis_time = store.config().genesis_time; | ||
| // Lean validators are fixed at genesis (no churn), so the current head | ||
| // state's validator registry always equals the genesis validator count. | ||
| let validator_count = store.head_state().validators.len() as u64; | ||
|
MegaRedHand marked this conversation as resolved.
|
||
| json_response(GenesisResponse { | ||
| genesis_time, | ||
| validator_count, | ||
| }) | ||
| } | ||
|
|
||
| pub(crate) fn routes() -> Router<Store> { | ||
| Router::new().route("/lean/v0/genesis", get(get_genesis)) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use axum::{ | ||
| body::Body, | ||
| http::{Request, StatusCode}, | ||
| }; | ||
| use ethlambda_storage::{Store, backend::InMemoryBackend}; | ||
| use ethlambda_types::state::{State, Validator}; | ||
| use http_body_util::BodyExt; | ||
| use std::sync::Arc; | ||
| use tower::ServiceExt; | ||
|
|
||
| #[tokio::test] | ||
| async fn genesis_returns_time_and_validator_count() { | ||
| // Build a state with 3 validators so the assertion is non-vacuous. | ||
| let dummy_validator = |index: u64| Validator { | ||
| attestation_pubkey: [0u8; 52], | ||
| proposal_pubkey: [0u8; 52], | ||
| index, | ||
| }; | ||
| let validators = vec![dummy_validator(0), dummy_validator(1), dummy_validator(2)]; | ||
| let state = State::from_genesis(1000, validators); | ||
|
|
||
| let store = Store::from_anchor_state(Arc::new(InMemoryBackend::new()), state); | ||
| let app = routes().with_state(store); | ||
| let resp = app | ||
| .oneshot( | ||
| Request::builder() | ||
| .uri("/lean/v0/genesis") | ||
| .body(Body::empty()) | ||
| .unwrap(), | ||
| ) | ||
| .await | ||
| .unwrap(); | ||
| assert_eq!(resp.status(), StatusCode::OK); | ||
| let body = resp.into_body().collect().await.unwrap().to_bytes(); | ||
| let json: serde_json::Value = serde_json::from_slice(&body).unwrap(); | ||
| assert_eq!(json["genesis_time"], 1000); | ||
| assert_eq!(json["validator_count"], 3); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.