Future slot check for state end point (#5755)

This commit is contained in:
terence tsao 2020-05-05 18:42:11 -07:00 committed by GitHub
parent 840bfc5f6b
commit aea7a8d291
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 2 deletions

View File

@ -20,8 +20,20 @@ func (bs *Server) GetBeaconState(
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 := bs.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 := bs.StateGen.StateBySlot(ctx, q.Slot)
if err != nil {
return nil, status.Errorf(codes.Internal, "could not compute state by slot: %v", err)

View File

@ -2,10 +2,12 @@ package beacon
import (
"context"
"strings"
"testing"
"github.com/gogo/protobuf/proto"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stategen"
@ -44,8 +46,9 @@ func TestServer_GetBeaconState(t *testing.T) {
t.Fatal(err)
}
bs := &Server{
BeaconDB: db,
StateGen: gen,
BeaconDB: db,
StateGen: gen,
GenesisTimeFetcher: &mock.ChainService{},
}
if _, err := bs.GetBeaconState(ctx, &pbrpc.BeaconStateRequest{}); err == nil {
t.Errorf("Expected error without a query filter, received nil")
@ -76,3 +79,19 @@ func TestServer_GetBeaconState(t *testing.T) {
t.Errorf("Wanted %v, received %v", wanted, res)
}
}
func TestServer_GetBeaconState_RequestFutureSlot(t *testing.T) {
resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{NewStateMgmt: true})
defer resetCfg()
bs := &Server{GenesisTimeFetcher: &mock.ChainService{}}
req := &pbrpc.BeaconStateRequest{
QueryFilter: &pbrpc.BeaconStateRequest_Slot{
Slot: bs.GenesisTimeFetcher.CurrentSlot() + 1,
},
}
wanted := "Cannot retrieve information about a slot in the future"
if _, err := bs.GetBeaconState(context.Background(), req); err != nil && !strings.Contains(err.Error(), wanted) {
t.Errorf("Expected error %v, received %v", wanted, err)
}
}