2020-05-15 21:54:53 +00:00
|
|
|
package debug
|
2020-05-01 01:47:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2020-05-21 01:36:05 +00:00
|
|
|
// GetBeaconState retrieves an ssz-encoded beacon state
|
2020-05-01 01:47:10 +00:00
|
|
|
// from the beacon node by either a slot or block root.
|
2020-05-15 21:54:53 +00:00
|
|
|
func (ds *Server) GetBeaconState(
|
2020-05-01 01:47:10 +00:00
|
|
|
ctx context.Context,
|
|
|
|
req *pbrpc.BeaconStateRequest,
|
2020-05-21 01:36:05 +00:00
|
|
|
) (*pbrpc.SSZResponse, error) {
|
2020-05-01 01:47:10 +00:00
|
|
|
switch q := req.QueryFilter.(type) {
|
|
|
|
case *pbrpc.BeaconStateRequest_Slot:
|
2020-05-15 21:54:53 +00:00
|
|
|
currentSlot := ds.GenesisTimeFetcher.CurrentSlot()
|
2020-05-06 01:42:11 +00:00
|
|
|
requestedSlot := q.Slot
|
|
|
|
if requestedSlot > currentSlot {
|
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.InvalidArgument,
|
|
|
|
"Cannot retrieve information about a slot in the future, current slot %d, requested slot %d",
|
|
|
|
currentSlot,
|
|
|
|
requestedSlot,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-05-15 21:54:53 +00:00
|
|
|
st, err := ds.StateGen.StateBySlot(ctx, q.Slot)
|
2020-05-01 01:47:10 +00:00
|
|
|
if err != nil {
|
2020-05-15 21:54:53 +00:00
|
|
|
return nil, status.Errorf(codes.Internal, "Could not compute state by slot: %v", err)
|
2020-05-01 01:47:10 +00:00
|
|
|
}
|
2021-03-16 00:43:27 +00:00
|
|
|
encoded, err := st.MarshalSSZ()
|
2020-05-21 01:36:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not ssz encode beacon state: %v", err)
|
|
|
|
}
|
|
|
|
return &pbrpc.SSZResponse{
|
|
|
|
Encoded: encoded,
|
|
|
|
}, nil
|
2020-05-01 01:47:10 +00:00
|
|
|
case *pbrpc.BeaconStateRequest_BlockRoot:
|
2020-05-15 21:54:53 +00:00
|
|
|
st, err := ds.StateGen.StateByRoot(ctx, bytesutil.ToBytes32(q.BlockRoot))
|
2020-05-01 01:47:10 +00:00
|
|
|
if err != nil {
|
2020-05-15 21:54:53 +00:00
|
|
|
return nil, status.Errorf(codes.Internal, "Could not compute state by block root: %v", err)
|
2020-05-01 01:47:10 +00:00
|
|
|
}
|
2021-03-16 00:43:27 +00:00
|
|
|
encoded, err := st.MarshalSSZ()
|
2020-05-21 01:36:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not ssz encode beacon state: %v", err)
|
|
|
|
}
|
|
|
|
return &pbrpc.SSZResponse{
|
|
|
|
Encoded: encoded,
|
|
|
|
}, nil
|
2020-05-01 01:47:10 +00:00
|
|
|
default:
|
2020-05-15 21:54:53 +00:00
|
|
|
return nil, status.Error(codes.InvalidArgument, "Need to specify either a block root or slot to request state")
|
2020-05-01 01:47:10 +00:00
|
|
|
}
|
|
|
|
}
|