Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Future slot check for state end point #5755

Merged
merged 6 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}