-
Notifications
You must be signed in to change notification settings - Fork 1.3k
SSZ-QL: Add endpoints (BeaconState/BeaconBlock)
#15888
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b12a510
Move ssz_query objects into testing folder (ensuring test objects onl…
syjn99 da219b1
Add containers for response
syjn99 554f29c
Export sszInfo
syjn99 19230d9
Add QueryBeaconState/Block
syjn99 086d349
Add comments and few refactor
syjn99 a7bf9f6
Merge branch 'develop' into feat/ssz-query-api
syjn99 6ff9029
Fix merge conflict issues
syjn99 5f02a53
Return 500 when calculate offset fails
syjn99 a4d4288
Add test for QueryBeaconState
syjn99 875c39d
Add test for QueryBeaconBlock
syjn99 de38cf6
Changelog :)
syjn99 48bbf0f
Merge branch 'develop' into feat/ssz-query-api
syjn99 f0e3116
Rename `QuerySSZRequest` to `SSZQueryRequest`
syjn99 0cd36b7
Fix middleware hooks for RPC to accept JSON from client and return SSZ
syjn99 ceee243
Convert to `SSZObject` directly from proto
syjn99 183fe78
Move marshalling/calculating hash tree root part after `CalculateOffs…
syjn99 70605b1
Make nogo happy
syjn99 3e458d1
Add informing comment for using proto unsafe conversion
syjn99 03d2748
Merge branch 'develop' into feat/ssz-query-api
rkapka 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
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
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,202 @@ | ||
| package beacon | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "io" | ||
| "net/http" | ||
|
|
||
| "github.com/OffchainLabs/prysm/v6/api" | ||
| "github.com/OffchainLabs/prysm/v6/api/server/structs" | ||
| "github.com/OffchainLabs/prysm/v6/beacon-chain/rpc/eth/shared" | ||
| "github.com/OffchainLabs/prysm/v6/beacon-chain/rpc/lookup" | ||
| "github.com/OffchainLabs/prysm/v6/encoding/ssz/query" | ||
| "github.com/OffchainLabs/prysm/v6/monitoring/tracing/trace" | ||
| "github.com/OffchainLabs/prysm/v6/network/httputil" | ||
| sszquerypb "github.com/OffchainLabs/prysm/v6/proto/ssz_query" | ||
| "github.com/OffchainLabs/prysm/v6/runtime/version" | ||
| ) | ||
|
|
||
| // QueryBeaconState handles SSZ Query request for BeaconState. | ||
| // Returns as bytes serialized SSZQueryResponse. | ||
| func (s *Server) QueryBeaconState(w http.ResponseWriter, r *http.Request) { | ||
| ctx, span := trace.StartSpan(r.Context(), "beacon.QueryBeaconState") | ||
| defer span.End() | ||
|
|
||
| stateID := r.PathValue("state_id") | ||
| if stateID == "" { | ||
| httputil.HandleError(w, "state_id is required in URL params", http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| // Validate path before lookup: it might be expensive. | ||
| var req structs.SSZQueryRequest | ||
| err := json.NewDecoder(r.Body).Decode(&req) | ||
| switch { | ||
| case errors.Is(err, io.EOF): | ||
| httputil.HandleError(w, "No data submitted", http.StatusBadRequest) | ||
| return | ||
| case err != nil: | ||
| httputil.HandleError(w, "Could not decode request body: "+err.Error(), http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| if len(req.Query) == 0 { | ||
| httputil.HandleError(w, "Empty query submitted", http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| path, err := query.ParsePath(req.Query) | ||
| if err != nil { | ||
| httputil.HandleError(w, "Could not parse path '"+req.Query+"': "+err.Error(), http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| stateRoot, err := s.Stater.StateRoot(ctx, []byte(stateID)) | ||
| if err != nil { | ||
| var rootNotFoundErr *lookup.StateRootNotFoundError | ||
| if errors.As(err, &rootNotFoundErr) { | ||
| httputil.HandleError(w, "State root not found: "+rootNotFoundErr.Error(), http.StatusNotFound) | ||
| return | ||
| } | ||
| httputil.HandleError(w, "Could not get state root: "+err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| st, err := s.Stater.State(ctx, []byte(stateID)) | ||
| if err != nil { | ||
| shared.WriteStateFetchError(w, err) | ||
| return | ||
| } | ||
|
|
||
| // NOTE: Using unsafe conversion to proto is acceptable here, | ||
| // as we play with a copy of the state returned by Stater. | ||
| sszObject, ok := st.ToProtoUnsafe().(query.SSZObject) | ||
| if !ok { | ||
| httputil.HandleError(w, "Unsupported state version for querying: "+version.String(st.Version()), http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| info, err := query.AnalyzeObject(sszObject) | ||
| if err != nil { | ||
| httputil.HandleError(w, "Could not analyze state object: "+err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| _, offset, length, err := query.CalculateOffsetAndLength(info, path) | ||
| if err != nil { | ||
| httputil.HandleError(w, "Could not calculate offset and length for path '"+req.Query+"': "+err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| encodedState, err := st.MarshalSSZ() | ||
| if err != nil { | ||
| httputil.HandleError(w, "Could not marshal state to SSZ: "+err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| response := &sszquerypb.SSZQueryResponse{ | ||
| Root: stateRoot, | ||
| Result: encodedState[offset : offset+length], | ||
| } | ||
|
|
||
| responseSsz, err := response.MarshalSSZ() | ||
| if err != nil { | ||
| httputil.HandleError(w, "Could not marshal response to SSZ: "+err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| w.Header().Set(api.VersionHeader, version.String(st.Version())) | ||
| httputil.WriteSsz(w, responseSsz) | ||
| } | ||
|
|
||
| // QueryBeaconState handles SSZ Query request for BeaconState. | ||
| // Returns as bytes serialized SSZQueryResponse. | ||
| func (s *Server) QueryBeaconBlock(w http.ResponseWriter, r *http.Request) { | ||
| ctx, span := trace.StartSpan(r.Context(), "beacon.QueryBeaconBlock") | ||
| defer span.End() | ||
|
|
||
| blockId := r.PathValue("block_id") | ||
| if blockId == "" { | ||
| httputil.HandleError(w, "block_id is required in URL params", http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| // Validate path before lookup: it might be expensive. | ||
| var req structs.SSZQueryRequest | ||
| err := json.NewDecoder(r.Body).Decode(&req) | ||
| switch { | ||
| case errors.Is(err, io.EOF): | ||
| httputil.HandleError(w, "No data submitted", http.StatusBadRequest) | ||
| return | ||
| case err != nil: | ||
| httputil.HandleError(w, "Could not decode request body: "+err.Error(), http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| if len(req.Query) == 0 { | ||
| httputil.HandleError(w, "Empty query submitted", http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| path, err := query.ParsePath(req.Query) | ||
| if err != nil { | ||
| httputil.HandleError(w, "Could not parse path '"+req.Query+"': "+err.Error(), http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| signedBlock, err := s.Blocker.Block(ctx, []byte(blockId)) | ||
| if !shared.WriteBlockFetchError(w, signedBlock, err) { | ||
| return | ||
| } | ||
|
|
||
| protoBlock, err := signedBlock.Block().Proto() | ||
| if err != nil { | ||
| httputil.HandleError(w, "Could not convert block to proto: "+err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| block, ok := protoBlock.(query.SSZObject) | ||
| if !ok { | ||
| httputil.HandleError(w, "Unsupported block version for querying: "+version.String(signedBlock.Version()), http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| info, err := query.AnalyzeObject(block) | ||
| if err != nil { | ||
| httputil.HandleError(w, "Could not analyze block object: "+err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| _, offset, length, err := query.CalculateOffsetAndLength(info, path) | ||
| if err != nil { | ||
| httputil.HandleError(w, "Could not calculate offset and length for path '"+req.Query+"': "+err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| encodedBlock, err := signedBlock.Block().MarshalSSZ() | ||
| if err != nil { | ||
| httputil.HandleError(w, "Could not marshal block to SSZ: "+err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| blockRoot, err := block.HashTreeRoot() | ||
| if err != nil { | ||
| httputil.HandleError(w, "Could not compute block root: "+err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| response := &sszquerypb.SSZQueryResponse{ | ||
| Root: blockRoot[:], | ||
| Result: encodedBlock[offset : offset+length], | ||
| } | ||
|
|
||
| responseSsz, err := response.MarshalSSZ() | ||
| if err != nil { | ||
| httputil.HandleError(w, "Could not marshal response to SSZ: "+err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| w.Header().Set(api.VersionHeader, version.String(signedBlock.Version())) | ||
| httputil.WriteSsz(w, responseSsz) | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.