-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Deneb: get-blobs beacon api #12603
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
Deneb: get-blobs beacon api #12603
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
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,44 @@ | ||
| load("@prysm//tools/go:def.bzl", "go_library", "go_test") | ||
|
|
||
| go_library( | ||
| name = "go_default_library", | ||
| srcs = [ | ||
| "handlers.go", | ||
| "server.go", | ||
| "structs.go", | ||
| ], | ||
| importpath = "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/blob", | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "//beacon-chain/blockchain:go_default_library", | ||
| "//beacon-chain/db:go_default_library", | ||
| "//beacon-chain/rpc/eth/helpers:go_default_library", | ||
| "//beacon-chain/rpc/lookup:go_default_library", | ||
| "//config/fieldparams:go_default_library", | ||
| "//config/params:go_default_library", | ||
| "//consensus-types/primitives:go_default_library", | ||
| "//encoding/bytesutil:go_default_library", | ||
| "//network:go_default_library", | ||
| "//proto/prysm/v1alpha1:go_default_library", | ||
| "//time/slots:go_default_library", | ||
| "@com_github_ethereum_go_ethereum//common/hexutil:go_default_library", | ||
| "@com_github_pkg_errors//:go_default_library", | ||
| ], | ||
| ) | ||
|
|
||
| go_test( | ||
| name = "go_default_test", | ||
| srcs = ["handlers_test.go"], | ||
| embed = [":go_default_library"], | ||
| deps = [ | ||
| "//beacon-chain/blockchain/testing:go_default_library", | ||
| "//beacon-chain/db/testing:go_default_library", | ||
| "//config/params:go_default_library", | ||
| "//encoding/bytesutil:go_default_library", | ||
| "//network:go_default_library", | ||
| "//proto/prysm/v1alpha1:go_default_library", | ||
| "//testing/assert:go_default_library", | ||
| "//testing/require:go_default_library", | ||
| "@com_github_ethereum_go_ethereum//common/hexutil:go_default_library", | ||
| ], | ||
| ) |
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,174 @@ | ||
| package blob | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/url" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/ethereum/go-ethereum/common/hexutil" | ||
| "github.com/pkg/errors" | ||
| "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/helpers" | ||
| "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/lookup" | ||
| field_params "github.com/prysmaticlabs/prysm/v4/config/fieldparams" | ||
| "github.com/prysmaticlabs/prysm/v4/config/params" | ||
| "github.com/prysmaticlabs/prysm/v4/consensus-types/primitives" | ||
| "github.com/prysmaticlabs/prysm/v4/encoding/bytesutil" | ||
| "github.com/prysmaticlabs/prysm/v4/network" | ||
| eth "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1" | ||
| "github.com/prysmaticlabs/prysm/v4/time/slots" | ||
| ) | ||
|
|
||
| // Blobs is an HTTP handler for Beacon API getBlobs. | ||
| func (s *Server) Blobs(w http.ResponseWriter, r *http.Request) { | ||
| var sidecars []*eth.BlobSidecar | ||
| var root []byte | ||
|
|
||
| indices := parseIndices(r.URL) | ||
| segments := strings.Split(r.URL.Path, "/") | ||
| blockId := segments[len(segments)-1] | ||
| switch blockId { | ||
| case "genesis": | ||
| errJson := &network.DefaultErrorJson{ | ||
| Message: "blobs are not supported for Phase 0 fork", | ||
| Code: http.StatusBadRequest, | ||
| } | ||
| network.WriteError(w, errJson) | ||
| return | ||
| case "head": | ||
| var err error | ||
| root, err = s.ChainInfoFetcher.HeadRoot(r.Context()) | ||
| if err != nil { | ||
| errJson := &network.DefaultErrorJson{ | ||
| Message: errors.Wrapf(err, "could not retrieve head root").Error(), | ||
| Code: http.StatusInternalServerError, | ||
| } | ||
| network.WriteError(w, errJson) | ||
| return | ||
| } | ||
| case "finalized": | ||
| fcp := s.ChainInfoFetcher.FinalizedCheckpt() | ||
| if fcp == nil { | ||
| errJson := &network.DefaultErrorJson{ | ||
| Message: "received nil finalized checkpoint", | ||
| Code: http.StatusInternalServerError, | ||
| } | ||
| network.WriteError(w, errJson) | ||
| return | ||
| } | ||
| root = fcp.Root | ||
| case "justified": | ||
| jcp := s.ChainInfoFetcher.CurrentJustifiedCheckpt() | ||
| if jcp == nil { | ||
| errJson := &network.DefaultErrorJson{ | ||
| Message: "received nil justified checkpoint", | ||
| Code: http.StatusInternalServerError, | ||
| } | ||
| network.WriteError(w, errJson) | ||
| return | ||
| } | ||
| root = jcp.Root | ||
| default: | ||
| if bytesutil.IsHex([]byte(blockId)) { | ||
| var err error | ||
| root, err = hexutil.Decode(blockId) | ||
| if err != nil { | ||
| errJson := &network.DefaultErrorJson{ | ||
| Message: errors.Wrap(err, "could not decode block ID into hex").Error(), | ||
| Code: http.StatusInternalServerError, | ||
| } | ||
| network.WriteError(w, errJson) | ||
| return | ||
| } | ||
| } else { | ||
| slot, err := strconv.ParseUint(blockId, 10, 64) | ||
| if err != nil { | ||
| errJson := &network.DefaultErrorJson{ | ||
| Message: lookup.NewBlockIdParseError(err).Error(), | ||
| Code: http.StatusBadRequest, | ||
| } | ||
| network.WriteError(w, errJson) | ||
| return | ||
| } | ||
| denebStart, err := slots.EpochStart(params.BeaconConfig().DenebForkEpoch) | ||
| if err != nil { | ||
| errJson := &network.DefaultErrorJson{ | ||
| Message: errors.Wrap(err, "could not calculate Deneb start slot").Error(), | ||
| Code: http.StatusInternalServerError, | ||
| } | ||
| network.WriteError(w, errJson) | ||
| return | ||
| } | ||
| if primitives.Slot(slot) < denebStart { | ||
| errJson := &network.DefaultErrorJson{ | ||
| Message: "blobs are not supported before Deneb fork", | ||
| Code: http.StatusBadRequest, | ||
| } | ||
| network.WriteError(w, errJson) | ||
| return | ||
| } | ||
| sidecars, err = s.BeaconDB.BlobSidecarsBySlot(r.Context(), primitives.Slot(slot), indices...) | ||
| if err != nil { | ||
| errJson := &network.DefaultErrorJson{ | ||
| Message: errors.Wrapf(err, "could not retrieve blobs for slot %d", slot).Error(), | ||
| Code: http.StatusInternalServerError, | ||
| } | ||
| network.WriteError(w, errJson) | ||
| return | ||
| } | ||
| network.WriteJson(w, buildSidecardsResponse(sidecars)) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| var err error | ||
| sidecars, err = s.BeaconDB.BlobSidecarsByRoot(r.Context(), bytesutil.ToBytes32(root), indices...) | ||
| if err != nil { | ||
| errJson := &network.DefaultErrorJson{ | ||
| Message: errors.Wrapf(err, "could not retrieve blobs for root %#x", root).Error(), | ||
| Code: http.StatusInternalServerError, | ||
| } | ||
| network.WriteError(w, errJson) | ||
| return | ||
| } | ||
| network.WriteJson(w, buildSidecardsResponse(sidecars)) | ||
| } | ||
|
|
||
| // parseIndices filters out invalid and duplicate blob indices | ||
| func parseIndices(url *url.URL) []uint64 { | ||
| query := url.Query() | ||
| helpers.NormalizeQueryValues(query) | ||
| rawIndices := query["indices"] | ||
| indices := make([]uint64, 0, field_params.MaxBlobsPerBlock) | ||
| loop: | ||
| for _, raw := range rawIndices { | ||
| ix, err := strconv.ParseUint(raw, 10, 64) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| for i := range indices { | ||
| if ix == indices[i] || ix >= field_params.MaxBlobsPerBlock { | ||
| continue loop | ||
| } | ||
| } | ||
| indices = append(indices, ix) | ||
| } | ||
| return indices | ||
| } | ||
|
|
||
| func buildSidecardsResponse(sidecars []*eth.BlobSidecar) *SidecarsResponse { | ||
| resp := &SidecarsResponse{Data: make([]*Sidecar, len(sidecars))} | ||
| for i, sc := range sidecars { | ||
| resp.Data[i] = &Sidecar{ | ||
| BlockRoot: hexutil.Encode(sc.BlockRoot), | ||
| Index: strconv.FormatUint(sc.Index, 10), | ||
| Slot: strconv.FormatUint(uint64(sc.Slot), 10), | ||
| BlockParentRoot: hexutil.Encode(sc.BlockParentRoot), | ||
| ProposerIndex: strconv.FormatUint(uint64(sc.ProposerIndex), 10), | ||
| Blob: hexutil.Encode(sc.Blob), | ||
| KZGCommitment: hexutil.Encode(sc.KzgCommitment), | ||
| KZGProof: hexutil.Encode(sc.KzgProof), | ||
| } | ||
| } | ||
| return resp | ||
| } | ||
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.
I dont know what the best practice is here and whether we need to copy. Lgtm otherwise
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.
lets keep an eye out on it in testing. we have a few more PRs that will need to touch this too will keep an eyeout on ssz