2021-07-02 15:54:52 +00:00
|
|
|
package debug
|
2021-02-22 22:48:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-06-15 15:28:49 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/rpc/statefetcher"
|
2021-06-02 23:49:52 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1"
|
2021-04-14 17:01:24 +00:00
|
|
|
"go.opencensus.io/trace"
|
2021-03-29 21:04:35 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2021-05-17 18:32:04 +00:00
|
|
|
"google.golang.org/protobuf/types/known/emptypb"
|
2021-02-22 22:48:49 +00:00
|
|
|
)
|
|
|
|
|
2021-03-29 21:04:35 +00:00
|
|
|
// GetBeaconState returns the full beacon state for a given state id.
|
2021-08-10 19:55:24 +00:00
|
|
|
func (ds *Server) GetBeaconState(ctx context.Context, req *ethpb.StateRequest) (*ethpb.BeaconStateResponse, error) {
|
2021-05-20 19:56:53 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "beaconv1.GetBeaconState")
|
|
|
|
defer span.End()
|
|
|
|
|
2021-03-29 21:04:35 +00:00
|
|
|
state, err := ds.StateFetcher.State(ctx, req.StateId)
|
|
|
|
if err != nil {
|
2021-06-15 15:28:49 +00:00
|
|
|
if stateNotFoundErr, ok := err.(*statefetcher.StateNotFoundError); ok {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "State not found: %v", stateNotFoundErr)
|
|
|
|
} else if parseErr, ok := err.(*statefetcher.StateIdParseError); ok {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "Invalid state ID: %v", parseErr)
|
|
|
|
}
|
|
|
|
return nil, status.Errorf(codes.Internal, "Invalid state ID: %v", err)
|
2021-03-29 21:04:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protoState, err := state.ToProto()
|
|
|
|
if err != nil {
|
2021-06-15 15:28:49 +00:00
|
|
|
return nil, status.Errorf(codes.Internal, "Could not convert state to proto: %v", err)
|
2021-03-29 21:04:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 19:55:24 +00:00
|
|
|
return ðpb.BeaconStateResponse{
|
2021-03-29 21:04:35 +00:00
|
|
|
Data: protoState,
|
|
|
|
}, nil
|
2021-02-22 22:48:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 15:28:49 +00:00
|
|
|
// GetBeaconStateSSZ returns the SSZ-serialized version of the full beacon state object for given stateId.
|
2021-08-10 19:55:24 +00:00
|
|
|
func (ds *Server) GetBeaconStateSSZ(ctx context.Context, req *ethpb.StateRequest) (*ethpb.BeaconStateSSZResponse, error) {
|
2021-06-15 15:28:49 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "beaconv1.GetBeaconStateSSZ")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
state, err := ds.StateFetcher.State(ctx, req.StateId)
|
|
|
|
if err != nil {
|
|
|
|
if stateNotFoundErr, ok := err.(*statefetcher.StateNotFoundError); ok {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "State not found: %v", stateNotFoundErr)
|
|
|
|
} else if parseErr, ok := err.(*statefetcher.StateIdParseError); ok {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "Invalid state ID: %v", parseErr)
|
|
|
|
}
|
|
|
|
return nil, status.Errorf(codes.Internal, "Invalid state ID: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sszState, err := state.MarshalSSZ()
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not marshal state into SSZ: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-08-10 19:55:24 +00:00
|
|
|
return ðpb.BeaconStateSSZResponse{Data: sszState}, nil
|
2021-06-02 23:49:52 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 21:04:35 +00:00
|
|
|
// ListForkChoiceHeads retrieves the fork choice leaves for the current head.
|
2021-05-17 18:32:04 +00:00
|
|
|
func (ds *Server) ListForkChoiceHeads(ctx context.Context, _ *emptypb.Empty) (*ethpb.ForkChoiceHeadsResponse, error) {
|
2021-08-10 19:55:24 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "debug.ListForkChoiceHeads")
|
2021-04-14 17:01:24 +00:00
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
headRoots, headSlots := ds.HeadFetcher.ChainHeads()
|
|
|
|
resp := ðpb.ForkChoiceHeadsResponse{
|
|
|
|
Data: make([]*ethpb.ForkChoiceHead, len(headRoots)),
|
|
|
|
}
|
|
|
|
for i := range headRoots {
|
|
|
|
resp.Data[i] = ðpb.ForkChoiceHead{
|
|
|
|
Root: headRoots[i][:],
|
|
|
|
Slot: headSlots[i],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
2021-02-22 22:48:49 +00:00
|
|
|
}
|