2021-07-02 15:54:52 +00:00
|
|
|
package debug
|
2021-02-22 22:48:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-08-20 21:32:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/rpc/eth/helpers"
|
2021-08-14 16:41:03 +00:00
|
|
|
ethpbv1 "github.com/prysmaticlabs/prysm/proto/eth/v1"
|
|
|
|
ethpbv2 "github.com/prysmaticlabs/prysm/proto/eth/v2"
|
2021-09-02 19:28:55 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/proto/migration"
|
2021-09-16 09:46:29 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/runtime/version"
|
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-09-02 19:28:55 +00:00
|
|
|
// GetBeaconState returns the full beacon state for a given state ID.
|
2021-08-14 16:41:03 +00:00
|
|
|
func (ds *Server) GetBeaconState(ctx context.Context, req *ethpbv1.StateRequest) (*ethpbv1.BeaconStateResponse, error) {
|
2021-09-02 19:28:55 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "debug.GetBeaconState")
|
2021-05-20 19:56:53 +00:00
|
|
|
defer span.End()
|
|
|
|
|
2021-09-22 17:59:06 +00:00
|
|
|
beaconSt, err := ds.StateFetcher.State(ctx, req.StateId)
|
2021-03-29 21:04:35 +00:00
|
|
|
if err != nil {
|
2021-08-20 21:32:56 +00:00
|
|
|
return nil, helpers.PrepareStateFetchGRPCError(err)
|
2021-03-29 21:04:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 09:30:06 +00:00
|
|
|
if beaconSt.Version() != version.Phase0 {
|
|
|
|
return nil, status.Error(codes.Internal, "State has incorrect type")
|
2021-09-22 17:59:06 +00:00
|
|
|
}
|
2022-03-07 16:56:54 +00:00
|
|
|
protoSt, err := migration.BeaconStateToProto(beaconSt)
|
2021-03-29 21:04:35 +00:00
|
|
|
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-14 16:41:03 +00:00
|
|
|
return ðpbv1.BeaconStateResponse{
|
2021-09-22 17:59:06 +00:00
|
|
|
Data: protoSt,
|
2021-03-29 21:04:35 +00:00
|
|
|
}, nil
|
2021-02-22 22:48:49 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 19:28:55 +00:00
|
|
|
// GetBeaconStateSSZ returns the SSZ-serialized version of the full beacon state object for given state ID.
|
2022-05-13 20:29:18 +00:00
|
|
|
func (ds *Server) GetBeaconStateSSZ(ctx context.Context, req *ethpbv1.StateRequest) (*ethpbv2.SSZContainer, error) {
|
2021-09-02 19:28:55 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "debug.GetBeaconStateSSZ")
|
2021-06-15 15:28:49 +00:00
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
state, err := ds.StateFetcher.State(ctx, req.StateId)
|
|
|
|
if err != nil {
|
2021-08-20 21:32:56 +00:00
|
|
|
return nil, helpers.PrepareStateFetchGRPCError(err)
|
2021-06-15 15:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sszState, err := state.MarshalSSZ()
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not marshal state into SSZ: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-05-13 20:29:18 +00:00
|
|
|
return ðpbv2.SSZContainer{Data: sszState}, nil
|
2021-08-14 16:41:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 19:28:55 +00:00
|
|
|
// GetBeaconStateV2 returns the full beacon state for a given state ID.
|
|
|
|
func (ds *Server) GetBeaconStateV2(ctx context.Context, req *ethpbv2.StateRequestV2) (*ethpbv2.BeaconStateResponseV2, error) {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "debug.GetBeaconStateV2")
|
|
|
|
defer span.End()
|
|
|
|
|
2021-09-22 17:59:06 +00:00
|
|
|
beaconSt, err := ds.StateFetcher.State(ctx, req.StateId)
|
2021-09-02 19:28:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, helpers.PrepareStateFetchGRPCError(err)
|
|
|
|
}
|
2022-05-12 17:23:45 +00:00
|
|
|
isOptimistic, err := helpers.IsOptimistic(ctx, beaconSt, ds.OptimisticModeFetcher)
|
2022-03-25 16:31:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not check if slot's block is optimistic: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-09-22 17:59:06 +00:00
|
|
|
switch beaconSt.Version() {
|
2021-09-02 19:28:55 +00:00
|
|
|
case version.Phase0:
|
2022-03-07 16:56:54 +00:00
|
|
|
protoSt, err := migration.BeaconStateToProto(beaconSt)
|
2021-09-02 19:28:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not convert state to proto: %v", err)
|
|
|
|
}
|
|
|
|
return ðpbv2.BeaconStateResponseV2{
|
2021-09-09 13:29:09 +00:00
|
|
|
Version: ethpbv2.Version_PHASE0,
|
2021-09-02 19:28:55 +00:00
|
|
|
Data: ðpbv2.BeaconStateContainer{
|
2021-09-22 17:59:06 +00:00
|
|
|
State: ðpbv2.BeaconStateContainer_Phase0State{Phase0State: protoSt},
|
2021-09-02 19:28:55 +00:00
|
|
|
},
|
2022-03-25 16:31:50 +00:00
|
|
|
ExecutionOptimistic: isOptimistic,
|
2021-09-02 19:28:55 +00:00
|
|
|
}, nil
|
|
|
|
case version.Altair:
|
2022-05-20 22:40:03 +00:00
|
|
|
protoState, err := migration.BeaconStateAltairToProto(beaconSt)
|
2021-09-02 19:28:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not convert state to proto: %v", err)
|
|
|
|
}
|
|
|
|
return ðpbv2.BeaconStateResponseV2{
|
2021-09-09 13:29:09 +00:00
|
|
|
Version: ethpbv2.Version_ALTAIR,
|
2021-09-02 19:28:55 +00:00
|
|
|
Data: ðpbv2.BeaconStateContainer{
|
|
|
|
State: ðpbv2.BeaconStateContainer_AltairState{AltairState: protoState},
|
|
|
|
},
|
2022-03-25 16:31:50 +00:00
|
|
|
ExecutionOptimistic: isOptimistic,
|
2021-09-02 19:28:55 +00:00
|
|
|
}, nil
|
2022-03-07 16:56:54 +00:00
|
|
|
case version.Bellatrix:
|
2022-05-20 22:40:03 +00:00
|
|
|
protoState, err := migration.BeaconStateBellatrixToProto(beaconSt)
|
2022-03-07 16:56:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not convert state to proto: %v", err)
|
|
|
|
}
|
|
|
|
return ðpbv2.BeaconStateResponseV2{
|
|
|
|
Version: ethpbv2.Version_BELLATRIX,
|
|
|
|
Data: ðpbv2.BeaconStateContainer{
|
|
|
|
State: ðpbv2.BeaconStateContainer_BellatrixState{BellatrixState: protoState},
|
|
|
|
},
|
2022-03-25 16:31:50 +00:00
|
|
|
ExecutionOptimistic: isOptimistic,
|
2022-03-07 16:56:54 +00:00
|
|
|
}, nil
|
2021-09-02 19:28:55 +00:00
|
|
|
default:
|
|
|
|
return nil, status.Error(codes.Internal, "Unsupported state version")
|
|
|
|
}
|
2021-08-14 16:41:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 19:28:55 +00:00
|
|
|
// GetBeaconStateSSZV2 returns the SSZ-serialized version of the full beacon state object for given state ID.
|
2022-05-13 20:29:18 +00:00
|
|
|
func (ds *Server) GetBeaconStateSSZV2(ctx context.Context, req *ethpbv2.StateRequestV2) (*ethpbv2.SSZContainer, error) {
|
2021-09-02 19:28:55 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "debug.GetBeaconStateSSZV2")
|
|
|
|
defer span.End()
|
|
|
|
|
2022-05-13 20:29:18 +00:00
|
|
|
st, err := ds.StateFetcher.State(ctx, req.StateId)
|
2021-09-02 19:28:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, helpers.PrepareStateFetchGRPCError(err)
|
|
|
|
}
|
|
|
|
|
2022-05-13 20:29:18 +00:00
|
|
|
sszState, err := st.MarshalSSZ()
|
2021-09-02 19:28:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not marshal state into SSZ: %v", err)
|
|
|
|
}
|
2022-05-13 20:29:18 +00:00
|
|
|
var ver ethpbv2.Version
|
|
|
|
switch st.Version() {
|
|
|
|
case version.Phase0:
|
|
|
|
ver = ethpbv2.Version_PHASE0
|
|
|
|
case version.Altair:
|
|
|
|
ver = ethpbv2.Version_ALTAIR
|
|
|
|
case version.Bellatrix:
|
|
|
|
ver = ethpbv2.Version_BELLATRIX
|
|
|
|
default:
|
|
|
|
return nil, status.Error(codes.Internal, "Unsupported state version")
|
|
|
|
}
|
2021-09-02 19:28:55 +00:00
|
|
|
|
2022-05-13 20:29:18 +00:00
|
|
|
return ðpbv2.SSZContainer{Data: sszState, Version: ver}, nil
|
2021-06-02 23:49:52 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 13:08:40 +00:00
|
|
|
// ListForkChoiceHeads retrieves the leaves of the current fork choice tree.
|
2021-08-14 16:41:03 +00:00
|
|
|
func (ds *Server) ListForkChoiceHeads(ctx context.Context, _ *emptypb.Empty) (*ethpbv1.ForkChoiceHeadsResponse, error) {
|
2022-07-14 17:00:33 +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()
|
2021-08-14 16:41:03 +00:00
|
|
|
resp := ðpbv1.ForkChoiceHeadsResponse{
|
|
|
|
Data: make([]*ethpbv1.ForkChoiceHead, len(headRoots)),
|
2021-04-14 17:01:24 +00:00
|
|
|
}
|
|
|
|
for i := range headRoots {
|
2021-08-14 16:41:03 +00:00
|
|
|
resp.Data[i] = ðpbv1.ForkChoiceHead{
|
2021-04-14 17:01:24 +00:00
|
|
|
Root: headRoots[i][:],
|
|
|
|
Slot: headSlots[i],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
2021-02-22 22:48:49 +00:00
|
|
|
}
|
2022-03-24 13:08:40 +00:00
|
|
|
|
|
|
|
// ListForkChoiceHeadsV2 retrieves the leaves of the current fork choice tree.
|
|
|
|
func (ds *Server) ListForkChoiceHeadsV2(ctx context.Context, _ *emptypb.Empty) (*ethpbv2.ForkChoiceHeadsResponse, error) {
|
2022-07-14 17:00:33 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "debug.ListForkChoiceHeadsV2")
|
2022-03-24 13:08:40 +00:00
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
headRoots, headSlots := ds.HeadFetcher.ChainHeads()
|
|
|
|
resp := ðpbv2.ForkChoiceHeadsResponse{
|
|
|
|
Data: make([]*ethpbv2.ForkChoiceHead, len(headRoots)),
|
|
|
|
}
|
|
|
|
for i := range headRoots {
|
2022-05-12 17:23:45 +00:00
|
|
|
isOptimistic, err := ds.OptimisticModeFetcher.IsOptimisticForRoot(ctx, headRoots[i])
|
2022-03-24 13:08:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not check if head is optimistic: %v", err)
|
|
|
|
}
|
|
|
|
resp.Data[i] = ðpbv2.ForkChoiceHead{
|
|
|
|
Root: headRoots[i][:],
|
|
|
|
Slot: headSlots[i],
|
|
|
|
ExecutionOptimistic: isOptimistic,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|