mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-27 21:57:16 +00:00
302b0f8c80
* use fast ssz anywhere applicable * use fastssz clearly * Merge branch 'master' into use-fastssz * Apply suggestions from code review * imports * Update beacon-chain/p2p/fork.go * Merge branch 'master' into use-fastssz * update go-ssz * update go-ssz * Merge refs/heads/master into use-fastssz * Merge refs/heads/master into use-fastssz
63 lines
2.0 KiB
Go
63 lines
2.0 KiB
Go
package debug
|
|
|
|
import (
|
|
"context"
|
|
|
|
pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
// GetBeaconState retrieves an ssz-encoded beacon state
|
|
// from the beacon node by either a slot or block root.
|
|
func (ds *Server) GetBeaconState(
|
|
ctx context.Context,
|
|
req *pbrpc.BeaconStateRequest,
|
|
) (*pbrpc.SSZResponse, error) {
|
|
if !featureconfig.Get().NewStateMgmt {
|
|
return nil, status.Error(codes.FailedPrecondition, "Requires --enable-new-state-mgmt to function")
|
|
}
|
|
|
|
switch q := req.QueryFilter.(type) {
|
|
case *pbrpc.BeaconStateRequest_Slot:
|
|
currentSlot := ds.GenesisTimeFetcher.CurrentSlot()
|
|
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,
|
|
)
|
|
}
|
|
|
|
st, err := ds.StateGen.StateBySlot(ctx, q.Slot)
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Internal, "Could not compute state by slot: %v", err)
|
|
}
|
|
encoded, err := st.CloneInnerState().MarshalSSZ()
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Internal, "Could not ssz encode beacon state: %v", err)
|
|
}
|
|
return &pbrpc.SSZResponse{
|
|
Encoded: encoded,
|
|
}, nil
|
|
case *pbrpc.BeaconStateRequest_BlockRoot:
|
|
st, err := ds.StateGen.StateByRoot(ctx, bytesutil.ToBytes32(q.BlockRoot))
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Internal, "Could not compute state by block root: %v", err)
|
|
}
|
|
encoded, err := st.CloneInnerState().MarshalSSZ()
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Internal, "Could not ssz encode beacon state: %v", err)
|
|
}
|
|
return &pbrpc.SSZResponse{
|
|
Encoded: encoded,
|
|
}, nil
|
|
default:
|
|
return nil, status.Error(codes.InvalidArgument, "Need to specify either a block root or slot to request state")
|
|
}
|
|
}
|