Skip to content

Commit

Permalink
Future slot check for state end point (#5755)
Browse files Browse the repository at this point in the history
  • Loading branch information
terencechain authored May 6, 2020
1 parent 840bfc5 commit aea7a8d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
12 changes: 12 additions & 0 deletions beacon-chain/rpc/beacon/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 21 additions & 2 deletions beacon-chain/rpc/beacon/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
}

0 comments on commit aea7a8d

Please sign in to comment.