Skip to content

Commit

Permalink
migrating get blob from eip4844 and deneb-integration (#12603)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-prysm committed Jul 10, 2023
1 parent ff92529 commit 49ddcd0
Show file tree
Hide file tree
Showing 10 changed files with 566 additions and 0 deletions.
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),
}
}
return resp
}
Loading

0 comments on commit 49ddcd0

Please sign in to comment.