Skip to content

Commit

Permalink
Beacon api: get blobs
Browse files Browse the repository at this point in the history
  • Loading branch information
terencechain committed Jan 9, 2023
1 parent 9649e49 commit 50b672a
Show file tree
Hide file tree
Showing 11 changed files with 634 additions and 153 deletions.
8 changes: 8 additions & 0 deletions beacon-chain/rpc/apimiddleware/custom_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ func handlePostSSZ(m *apimiddleware.ApiProxyMiddleware, endpoint apimiddleware.E
return true
}

func handleGetBlobsSidecarSSZ(m *apimiddleware.ApiProxyMiddleware, endpoint apimiddleware.Endpoint, w http.ResponseWriter, req *http.Request) (handled bool) {
config := sszConfig{
fileName: "blobs_sidecar.ssz",
responseJson: &SszResponseJson{},
}
return handleGetSSZ(m, endpoint, w, req, config)
}

func sszRequested(req *http.Request) (bool, error) {
accept := req.Header.Values("Accept")
if len(accept) == 0 {
Expand Down
34 changes: 34 additions & 0 deletions beacon-chain/rpc/apimiddleware/custom_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,40 @@ type bellatrixProduceBlindedBlockResponseJson struct {
Data *BlindedBeaconBlockBellatrixJson `json:"data"`
}

type tempBlobJson struct {
Data string `json:"data"`
}

type tempBlobsSidecarJson struct {
BeaconBlockRoot string `json:"beacon_block_root"`
BeaconBlockSlot string `json:"beacon_block_slot"`
Blobs []tempBlobJson `json:"blobs"`
AggregatedProof string `json:"kzg_aggregated_proof"`
}

// This takes the blobs list and exposes the data field of each blob as the blob content itself in the json
func prepareBlobsResponse(body []byte, responseContainer interface{}) (apimiddleware.RunDefault, apimiddleware.ErrorJson) {
tempContainer := &tempBlobsSidecarJson{}
if err := json.Unmarshal(body, tempContainer); err != nil {
return false, apimiddleware.InternalServerErrorWithMessage(err, "could not unmarshal response into temp container")
}
container, ok := responseContainer.(*blobsSidecarResponseJson)
if !ok {
return false, apimiddleware.InternalServerError(errors.New("container is not of the correct type"))
}

container.Data = &blobsSidecarJson{
BeaconBlockRoot: tempContainer.BeaconBlockRoot,
BeaconBlockSlot: tempContainer.BeaconBlockSlot,
Blobs: make([]string, len(tempContainer.Blobs)),
AggregatedProof: tempContainer.AggregatedProof,
}
for i, blob := range tempContainer.Blobs {
container.Data.Blobs[i] = blob.Data
}
return false, nil
}

func serializeProducedV2Block(response interface{}) (apimiddleware.RunDefault, []byte, apimiddleware.ErrorJson) {
respContainer, ok := response.(*ProduceBlockResponseV2Json)
if !ok {
Expand Down
7 changes: 7 additions & 0 deletions beacon-chain/rpc/apimiddleware/endpoint_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (_ *BeaconEndpointFactory) Paths() []string {
"/eth/v1/validator/prepare_beacon_proposer",
"/eth/v1/validator/register_validator",
"/eth/v1/validator/liveness/{epoch}",
"/eth/v1/beacon/blobs_sidecars/{block_id}",
}
}

Expand Down Expand Up @@ -136,6 +137,12 @@ func (_ *BeaconEndpointFactory) Create(path string) (*apimiddleware.Endpoint, er
OnPreSerializeMiddlewareResponseIntoJson: serializeV2Block,
}
endpoint.CustomHandlers = []apimiddleware.CustomHandler{handleGetBeaconBlockSSZV2}
case " eth/v1/beacon/blobs_sidecars/{block_id}":
endpoint.GetResponse = &blobsSidecarResponseJson{}
endpoint.CustomHandlers = []apimiddleware.CustomHandler{handleGetBlobsSidecarSSZ}
endpoint.Hooks = apimiddleware.HookCollection{
OnPreDeserializeGrpcResponseBodyIntoContainer: prepareBlobsResponse,
}
case "/eth/v1/beacon/blocks/{block_id}/root":
endpoint.GetResponse = &BlockRootResponseJson{}
case "/eth/v1/beacon/blocks/{block_id}/attestations":
Expand Down
11 changes: 11 additions & 0 deletions beacon-chain/rpc/apimiddleware/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1298,3 +1298,14 @@ type EventErrorJson struct {
StatusCode int `json:"status_code"`
Message string `json:"message"`
}

type blobsSidecarJson struct {
BeaconBlockRoot string `json:"beacon_block_root" hex:"true"`
BeaconBlockSlot string `json:"beacon_block_slot"`
Blobs []string `json:"blobs" hex:"true"`
AggregatedProof string `json:"kzg_aggregated_proof" hex:"true"`
}

type blobsSidecarResponseJson struct {
Data *blobsSidecarJson `json:"data"`
}
1 change: 1 addition & 0 deletions beacon-chain/rpc/eth/beacon/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go_library(
name = "go_default_library",
srcs = [
"blinded_blocks.go",
"blobs.go",
"blocks.go",
"config.go",
"log.go",
Expand Down
45 changes: 45 additions & 0 deletions beacon-chain/rpc/eth/beacon/blobs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package beacon

import (
"context"
"fmt"

"github.com/pkg/errors"
ethpbv1 "github.com/prysmaticlabs/prysm/v3/proto/eth/v1"
)

func (bs *Server) GetBlobsSidecar(ctx context.Context, req *ethpbv1.BlobsRequest) (*ethpbv1.BlobsResponse, error) {
sblk, err := bs.blockFromBlockID(ctx, req.BlockId)
err = handleGetBlockError(sblk, err)
if err != nil {
return nil, errors.Wrap(err, "GetBlobs")
}
block := sblk.Block()
root, err := block.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "failed to htr block")
}
sidecar, err := bs.BeaconDB.BlobsSidecar(ctx, root)
if err != nil {
return nil, fmt.Errorf("failed to get blobs sidecar for block %x", root)
}
var blobs []*ethpbv1.Blob
var aggregatedProof []byte
if sidecar != nil {
aggregatedProof = sidecar.AggregatedProof
for _, b := range sidecar.Blobs {
var data []byte
// go through each element, concat them
for _, el := range b.Data {
data = append(data, el)
}
blobs = append(blobs, &ethpbv1.Blob{Data: data})
}
}
return &ethpbv1.BlobsResponse{
BeaconBlockRoot: root[:],
BeaconBlockSlot: uint64(block.Slot()),
Blobs: blobs,
AggregatedProof: aggregatedProof,
}, nil
}
Loading

0 comments on commit 50b672a

Please sign in to comment.