prysm-pulse/beacon-chain/rpc/debug/block.go
Raul Jordan 881ad77c52
Add Debug Block Endpoint + Return SSZ-Encoded (#5926)
* add block endpoint

* handle err

* check for nil block

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
2020-05-20 20:36:05 -05:00

34 lines
900 B
Go

package debug
import (
"context"
"github.com/prysmaticlabs/go-ssz"
pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// GetBlock in an ssz-encoded format by block root.
func (ds *Server) GetBlock(
ctx context.Context,
req *pbrpc.BlockRequest,
) (*pbrpc.SSZResponse, error) {
root := bytesutil.ToBytes32(req.BlockRoot)
signedBlock, err := ds.BeaconDB.Block(ctx, root)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not retrieve block by root: %v", err)
}
if signedBlock == nil {
return &pbrpc.SSZResponse{Encoded: make([]byte, 0)}, nil
}
encoded, err := ssz.Marshal(signedBlock)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not marshal block: %v", err)
}
return &pbrpc.SSZResponse{
Encoded: encoded,
}, nil
}