Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions beacon-chain/rpc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ go_library(
"//beacon-chain/operations/voluntaryexits:go_default_library",
"//beacon-chain/p2p:go_default_library",
"//beacon-chain/rpc/eth/beacon:go_default_library",
"//beacon-chain/rpc/eth/blob:go_default_library",
"//beacon-chain/rpc/eth/builder:go_default_library",
"//beacon-chain/rpc/eth/debug:go_default_library",
"//beacon-chain/rpc/eth/events:go_default_library",
Expand Down
44 changes: 44 additions & 0 deletions beacon-chain/rpc/eth/blob/BUILD.bazel
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",
],
)
174 changes: 174 additions & 0 deletions beacon-chain/rpc/eth/blob/handlers.go
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),
}
}
Comment on lines +162 to +172
Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
Contributor Author

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

return resp
}
Loading