2020-05-15 21:54:53 +00:00
|
|
|
package debug
|
2020-05-01 01:47:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-09 19:33:18 +00:00
|
|
|
"fmt"
|
2020-05-01 01:47:10 +00:00
|
|
|
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/encoding/bytesutil"
|
|
|
|
pbrpc "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
|
2020-05-01 01:47:10 +00:00
|
|
|
"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,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-23 22:54:07 +00:00
|
|
|
st, err := ds.ReplayerBuilder.ReplayerForSlot(q.Slot).ReplayBlocks(ctx)
|
2020-05-01 01:47:10 +00:00
|
|
|
if err != nil {
|
2022-03-09 19:33:18 +00:00
|
|
|
return nil, status.Error(codes.Internal, fmt.Sprintf("error replaying blocks for state at slot %d: %v", q.Slot, err))
|
2020-05-01 01:47:10 +00:00
|
|
|
}
|
2022-03-09 19:33:18 +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
|
|
|
}
|
|
|
|
}
|