-
Notifications
You must be signed in to change notification settings - Fork 18
Add JSON-RPC endpoints for blocks #301
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
pablodeymo
wants to merge
1
commit into
main
Choose a base branch
from
feat/issue-75-blocks-rpc
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.
Open
Changes from all commits
Commits
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| use axum::{ | ||
| extract::{Path, State}, | ||
| http::StatusCode, | ||
| response::IntoResponse, | ||
| }; | ||
| use ethlambda_storage::Store; | ||
| use ethlambda_types::primitives::H256; | ||
| use serde_json::json; | ||
|
|
||
| use crate::json_response; | ||
|
|
||
| /// `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( | ||
| Path(block_id): Path<String>, | ||
| State(store): State<Store>, | ||
| ) -> impl IntoResponse { | ||
| let root = match resolve_block_id(&store, &block_id) { | ||
| Ok(root) => root, | ||
| Err(err) => return err.into_response(), | ||
| }; | ||
|
|
||
| match store.get_block(&root) { | ||
| Some(block) => json_response(block), | ||
| None => BlockIdError::NotFound.into_response(), | ||
| } | ||
| } | ||
|
|
||
| /// `GET /lean/v0/blocks/:block_id/header` — returns the block header as JSON. | ||
| pub async fn get_block_header( | ||
| Path(block_id): Path<String>, | ||
| State(store): State<Store>, | ||
| ) -> impl IntoResponse { | ||
| let root = match resolve_block_id(&store, &block_id) { | ||
| Ok(root) => root, | ||
| Err(err) => return err.into_response(), | ||
| }; | ||
|
|
||
| match store.get_block_header(&root) { | ||
| Some(header) => json_response(header), | ||
| None => BlockIdError::NotFound.into_response(), | ||
| } | ||
| } | ||
|
|
||
| /// Resolve a `block_id` (hex root or decimal slot) into a block root. | ||
| /// | ||
| /// Slot lookups use the head state's `historical_block_hashes`, so only | ||
| /// canonical blocks are reachable by slot — blocks on side forks must be | ||
| /// addressed by their root. | ||
| fn resolve_block_id(store: &Store, block_id: &str) -> Result<H256, BlockIdError> { | ||
| if let Some(hex_body) = block_id.strip_prefix("0x") { | ||
| parse_root(hex_body) | ||
| } else if block_id.chars().all(|c| c.is_ascii_digit()) { | ||
| let slot: u64 = block_id.parse().map_err(|_| BlockIdError::Invalid)?; | ||
| resolve_slot(store, slot) | ||
| } else { | ||
| Err(BlockIdError::Invalid) | ||
| } | ||
| } | ||
|
|
||
| fn parse_root(hex_body: &str) -> Result<H256, BlockIdError> { | ||
| let bytes = hex::decode(hex_body).map_err(|_| BlockIdError::Invalid)?; | ||
| if bytes.len() != 32 { | ||
| return Err(BlockIdError::Invalid); | ||
| } | ||
| let mut arr = [0u8; 32]; | ||
| arr.copy_from_slice(&bytes); | ||
| Ok(H256(arr)) | ||
| } | ||
|
|
||
| fn resolve_slot(store: &Store, slot: u64) -> Result<H256, BlockIdError> { | ||
| let head_state = store.head_state(); | ||
| let root = head_state | ||
| .historical_block_hashes | ||
| .get(slot as usize) | ||
| .ok_or(BlockIdError::NotFound)?; | ||
| if root.is_zero() { | ||
| return Err(BlockIdError::NotFound); | ||
| } | ||
| Ok(*root) | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| enum BlockIdError { | ||
| Invalid, | ||
| NotFound, | ||
| } | ||
|
|
||
| impl IntoResponse for BlockIdError { | ||
| fn into_response(self) -> axum::response::Response { | ||
| let (status, message) = match self { | ||
| BlockIdError::Invalid => (StatusCode::BAD_REQUEST, "invalid block_id"), | ||
| BlockIdError::NotFound => (StatusCode::NOT_FOUND, "block not found"), | ||
| }; | ||
| let mut response = json_response(json!({ "error": message })); | ||
| *response.status_mut() = status; | ||
| response | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
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.
store.head_state()deserializes the entire beaconState— includinghistorical_block_hashes, which grows linearly with the chain's age (32 bytes × N slots). On a long-running chain this can be hundreds of MB of SSZ parsing just to index one entry. Since this is a debug endpoint the cost is tolerable for now, but a dedicatedStore::get_historical_block_hash(slot: u64) -> Option<H256>accessor that reads only theStatesentry and pulls the specific index would eliminate the overhead without much extra complexity.Prompt To Fix With AI