2021-07-02 15:54:52 +00:00
|
|
|
package beacon
|
2020-10-05 00:15:27 +00:00
|
|
|
|
|
|
|
import (
|
2021-02-02 14:44:28 +00:00
|
|
|
"bytes"
|
2020-10-05 00:15:27 +00:00
|
|
|
"context"
|
2021-08-20 21:32:56 +00:00
|
|
|
"strconv"
|
2020-10-05 00:15:27 +00:00
|
|
|
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/rpc/eth/helpers"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/rpc/statefetcher"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/state"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/config/params"
|
|
|
|
types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v3/proto/eth/v1"
|
|
|
|
eth "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/time/slots"
|
2021-02-02 14:44:28 +00:00
|
|
|
"go.opencensus.io/trace"
|
|
|
|
"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"
|
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2020-10-05 00:15:27 +00:00
|
|
|
)
|
|
|
|
|
2021-08-20 21:32:56 +00:00
|
|
|
type stateRequest struct {
|
|
|
|
epoch *types.Epoch
|
|
|
|
stateId []byte
|
|
|
|
}
|
|
|
|
|
2020-10-05 00:15:27 +00:00
|
|
|
// GetGenesis retrieves details of the chain's genesis which can be used to identify chain.
|
2021-05-17 18:32:04 +00:00
|
|
|
func (bs *Server) GetGenesis(ctx context.Context, _ *emptypb.Empty) (*ethpb.GenesisResponse, error) {
|
2022-07-14 17:00:33 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "beacon.GetGenesis")
|
2021-02-02 14:44:28 +00:00
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
genesisTime := bs.GenesisTimeFetcher.GenesisTime()
|
|
|
|
if genesisTime.IsZero() {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "Chain genesis info is not yet known")
|
|
|
|
}
|
2022-02-14 13:34:38 +00:00
|
|
|
validatorRoot := bs.ChainInfoFetcher.GenesisValidatorsRoot()
|
2021-02-02 14:44:28 +00:00
|
|
|
if bytes.Equal(validatorRoot[:], params.BeaconConfig().ZeroHash[:]) {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "Chain genesis info is not yet known")
|
|
|
|
}
|
|
|
|
forkVersion := params.BeaconConfig().GenesisForkVersion
|
|
|
|
|
|
|
|
return ðpb.GenesisResponse{
|
2021-02-09 10:05:22 +00:00
|
|
|
Data: ðpb.GenesisResponse_Genesis{
|
2021-05-17 18:32:04 +00:00
|
|
|
GenesisTime: ×tamppb.Timestamp{
|
2021-02-09 10:05:22 +00:00
|
|
|
Seconds: genesisTime.Unix(),
|
|
|
|
Nanos: 0,
|
|
|
|
},
|
|
|
|
GenesisValidatorsRoot: validatorRoot[:],
|
|
|
|
GenesisForkVersion: forkVersion,
|
2021-02-02 14:44:28 +00:00
|
|
|
},
|
|
|
|
}, nil
|
2020-10-05 00:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetStateRoot calculates HashTreeRoot for state with given 'stateId'. If stateId is root, same value will be returned.
|
|
|
|
func (bs *Server) GetStateRoot(ctx context.Context, req *ethpb.StateRequest) (*ethpb.StateRootResponse, error) {
|
2021-09-22 17:59:06 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "beacon.GetStateRoot")
|
2021-02-11 21:08:36 +00:00
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
var (
|
2022-03-25 16:31:50 +00:00
|
|
|
stateRoot []byte
|
|
|
|
err error
|
2021-02-17 15:26:39 +00:00
|
|
|
)
|
|
|
|
|
2022-03-25 16:31:50 +00:00
|
|
|
stateRoot, err = bs.StateFetcher.StateRoot(ctx, req.StateId)
|
2021-02-17 15:26:39 +00:00
|
|
|
if err != nil {
|
2021-06-15 15:28:49 +00:00
|
|
|
if rootNotFoundErr, ok := err.(*statefetcher.StateRootNotFoundError); ok {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "State root not found: %v", rootNotFoundErr)
|
|
|
|
} else if parseErr, ok := err.(*statefetcher.StateIdParseError); ok {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "Invalid state ID: %v", parseErr)
|
|
|
|
}
|
2021-03-17 16:47:44 +00:00
|
|
|
return nil, status.Errorf(codes.Internal, "Could not get state root: %v", err)
|
2021-02-17 15:26:39 +00:00
|
|
|
}
|
2022-03-25 16:31:50 +00:00
|
|
|
st, err := bs.StateFetcher.State(ctx, req.StateId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not get state: %v", err)
|
|
|
|
}
|
2022-05-12 17:23:45 +00:00
|
|
|
isOptimistic, err := helpers.IsOptimistic(ctx, st, bs.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-02-17 15:26:39 +00:00
|
|
|
|
|
|
|
return ðpb.StateRootResponse{
|
|
|
|
Data: ðpb.StateRootResponse_StateRoot{
|
2022-03-25 16:31:50 +00:00
|
|
|
Root: stateRoot,
|
2021-02-17 15:26:39 +00:00
|
|
|
},
|
2022-03-25 16:31:50 +00:00
|
|
|
ExecutionOptimistic: isOptimistic,
|
2021-02-17 15:26:39 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStateFork returns Fork object for state with given 'stateId'.
|
|
|
|
func (bs *Server) GetStateFork(ctx context.Context, req *ethpb.StateRequest) (*ethpb.StateForkResponse, error) {
|
2021-09-22 17:59:06 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "beacon.GetStateFork")
|
2021-02-17 15:26:39 +00:00
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
var (
|
2021-09-22 17:59:06 +00:00
|
|
|
st state.BeaconState
|
|
|
|
err error
|
2021-02-11 21:08:36 +00:00
|
|
|
)
|
|
|
|
|
2021-09-22 17:59:06 +00:00
|
|
|
st, err = bs.StateFetcher.State(ctx, req.StateId)
|
2021-02-17 15:26:39 +00:00
|
|
|
if err != nil {
|
2021-08-20 21:32:56 +00:00
|
|
|
return nil, helpers.PrepareStateFetchGRPCError(err)
|
2021-02-17 15:26:39 +00:00
|
|
|
}
|
2021-09-22 17:59:06 +00:00
|
|
|
fork := st.Fork()
|
2022-05-12 17:23:45 +00:00
|
|
|
isOptimistic, err := helpers.IsOptimistic(ctx, st, bs.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-02-17 15:26:39 +00:00
|
|
|
return ðpb.StateForkResponse{
|
|
|
|
Data: ðpb.Fork{
|
|
|
|
PreviousVersion: fork.PreviousVersion,
|
|
|
|
CurrentVersion: fork.CurrentVersion,
|
|
|
|
Epoch: fork.Epoch,
|
|
|
|
},
|
2022-03-25 16:31:50 +00:00
|
|
|
ExecutionOptimistic: isOptimistic,
|
2021-02-17 15:26:39 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFinalityCheckpoints returns finality checkpoints for state with given 'stateId'. In case finality is
|
|
|
|
// not yet achieved, checkpoint should return epoch 0 and ZERO_HASH as root.
|
|
|
|
func (bs *Server) GetFinalityCheckpoints(ctx context.Context, req *ethpb.StateRequest) (*ethpb.StateFinalityCheckpointResponse, error) {
|
2021-09-22 17:59:06 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "beacon.GetFinalityCheckpoints")
|
2021-02-18 15:46:17 +00:00
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
var (
|
2021-09-22 17:59:06 +00:00
|
|
|
st state.BeaconState
|
|
|
|
err error
|
2021-02-18 15:46:17 +00:00
|
|
|
)
|
|
|
|
|
2021-09-22 17:59:06 +00:00
|
|
|
st, err = bs.StateFetcher.State(ctx, req.StateId)
|
2021-02-18 15:46:17 +00:00
|
|
|
if err != nil {
|
2022-03-23 22:54:07 +00:00
|
|
|
return nil, helpers.PrepareStateFetchGRPCError(err)
|
2021-02-18 15:46:17 +00:00
|
|
|
}
|
2022-05-12 17:23:45 +00:00
|
|
|
isOptimistic, err := helpers.IsOptimistic(ctx, st, bs.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-02-18 15:46:17 +00:00
|
|
|
|
|
|
|
return ðpb.StateFinalityCheckpointResponse{
|
|
|
|
Data: ðpb.StateFinalityCheckpointResponse_StateFinalityCheckpoint{
|
2021-09-22 17:59:06 +00:00
|
|
|
PreviousJustified: checkpoint(st.PreviousJustifiedCheckpoint()),
|
|
|
|
CurrentJustified: checkpoint(st.CurrentJustifiedCheckpoint()),
|
|
|
|
Finalized: checkpoint(st.FinalizedCheckpoint()),
|
2021-02-18 15:46:17 +00:00
|
|
|
},
|
2022-03-25 16:31:50 +00:00
|
|
|
ExecutionOptimistic: isOptimistic,
|
2021-02-18 15:46:17 +00:00
|
|
|
}, nil
|
2021-02-17 15:26:39 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 21:32:56 +00:00
|
|
|
func (bs *Server) stateFromRequest(ctx context.Context, req *stateRequest) (state.BeaconState, error) {
|
|
|
|
if req.epoch != nil {
|
2021-10-01 20:17:57 +00:00
|
|
|
slot, err := slots.EpochStart(*req.epoch)
|
2021-08-20 21:32:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.Internal,
|
|
|
|
"Could not calculate start slot for epoch %d: %v",
|
|
|
|
*req.epoch,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
st, err := bs.StateFetcher.State(ctx, []byte(strconv.FormatUint(uint64(slot), 10)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, helpers.PrepareStateFetchGRPCError(err)
|
|
|
|
}
|
|
|
|
return st, nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
st, err := bs.StateFetcher.State(ctx, req.stateId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, helpers.PrepareStateFetchGRPCError(err)
|
|
|
|
}
|
|
|
|
return st, nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 15:46:17 +00:00
|
|
|
func checkpoint(sourceCheckpoint *eth.Checkpoint) *ethpb.Checkpoint {
|
|
|
|
if sourceCheckpoint != nil {
|
|
|
|
return ðpb.Checkpoint{
|
|
|
|
Epoch: sourceCheckpoint.Epoch,
|
|
|
|
Root: sourceCheckpoint.Root,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ðpb.Checkpoint{
|
|
|
|
Epoch: 0,
|
|
|
|
Root: params.BeaconConfig().ZeroHash[:],
|
|
|
|
}
|
|
|
|
}
|