github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/rpc/eth/v1/debug/debug.go (about)

     1  package debug
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/prysmaticlabs/prysm/beacon-chain/rpc/statefetcher"
     7  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1"
     8  	"go.opencensus.io/trace"
     9  	"google.golang.org/grpc/codes"
    10  	"google.golang.org/grpc/status"
    11  	"google.golang.org/protobuf/types/known/emptypb"
    12  )
    13  
    14  // GetBeaconState returns the full beacon state for a given state id.
    15  func (ds *Server) GetBeaconState(ctx context.Context, req *ethpb.StateRequest) (*ethpb.BeaconStateResponse, error) {
    16  	ctx, span := trace.StartSpan(ctx, "beaconv1.GetBeaconState")
    17  	defer span.End()
    18  
    19  	state, err := ds.StateFetcher.State(ctx, req.StateId)
    20  	if err != nil {
    21  		if stateNotFoundErr, ok := err.(*statefetcher.StateNotFoundError); ok {
    22  			return nil, status.Errorf(codes.NotFound, "State not found: %v", stateNotFoundErr)
    23  		} else if parseErr, ok := err.(*statefetcher.StateIdParseError); ok {
    24  			return nil, status.Errorf(codes.InvalidArgument, "Invalid state ID: %v", parseErr)
    25  		}
    26  		return nil, status.Errorf(codes.Internal, "Invalid state ID: %v", err)
    27  	}
    28  
    29  	protoState, err := state.ToProto()
    30  	if err != nil {
    31  		return nil, status.Errorf(codes.Internal, "Could not convert state to proto: %v", err)
    32  	}
    33  
    34  	return &ethpb.BeaconStateResponse{
    35  		Data: protoState,
    36  	}, nil
    37  }
    38  
    39  // GetBeaconStateSSZ returns the SSZ-serialized version of the full beacon state object for given stateId.
    40  func (ds *Server) GetBeaconStateSSZ(ctx context.Context, req *ethpb.StateRequest) (*ethpb.BeaconStateSSZResponse, error) {
    41  	ctx, span := trace.StartSpan(ctx, "beaconv1.GetBeaconStateSSZ")
    42  	defer span.End()
    43  
    44  	state, err := ds.StateFetcher.State(ctx, req.StateId)
    45  	if err != nil {
    46  		if stateNotFoundErr, ok := err.(*statefetcher.StateNotFoundError); ok {
    47  			return nil, status.Errorf(codes.NotFound, "State not found: %v", stateNotFoundErr)
    48  		} else if parseErr, ok := err.(*statefetcher.StateIdParseError); ok {
    49  			return nil, status.Errorf(codes.InvalidArgument, "Invalid state ID: %v", parseErr)
    50  		}
    51  		return nil, status.Errorf(codes.Internal, "Invalid state ID: %v", err)
    52  	}
    53  
    54  	sszState, err := state.MarshalSSZ()
    55  	if err != nil {
    56  		return nil, status.Errorf(codes.Internal, "Could not marshal state into SSZ: %v", err)
    57  	}
    58  
    59  	return &ethpb.BeaconStateSSZResponse{Data: sszState}, nil
    60  }
    61  
    62  // ListForkChoiceHeads retrieves the fork choice leaves for the current head.
    63  func (ds *Server) ListForkChoiceHeads(ctx context.Context, _ *emptypb.Empty) (*ethpb.ForkChoiceHeadsResponse, error) {
    64  	ctx, span := trace.StartSpan(ctx, "debugv1.ListForkChoiceHeads")
    65  	defer span.End()
    66  
    67  	headRoots, headSlots := ds.HeadFetcher.ChainHeads()
    68  	resp := &ethpb.ForkChoiceHeadsResponse{
    69  		Data: make([]*ethpb.ForkChoiceHead, len(headRoots)),
    70  	}
    71  	for i := range headRoots {
    72  		resp.Data[i] = &ethpb.ForkChoiceHead{
    73  			Root: headRoots[i][:],
    74  			Slot: headSlots[i],
    75  		}
    76  	}
    77  
    78  	return resp, nil
    79  }