From 3c09d3a65c9d00843333a47e04587e6fe63f5e83 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Tue, 5 May 2020 16:35:04 -0700 Subject: [PATCH 1/4] Check for future slot --- beacon-chain/rpc/beacon/state.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/beacon-chain/rpc/beacon/state.go b/beacon-chain/rpc/beacon/state.go index 7b32c1de9095..24a85baff002 100644 --- a/beacon-chain/rpc/beacon/state.go +++ b/beacon-chain/rpc/beacon/state.go @@ -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 an slot in the future, current slot %d, 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) From d80fa1e17ecf0ad7a475b2f097f33b28126b3b39 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Tue, 5 May 2020 16:35:16 -0700 Subject: [PATCH 2/4] Test check for future slot --- beacon-chain/rpc/beacon/state_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/beacon-chain/rpc/beacon/state_test.go b/beacon-chain/rpc/beacon/state_test.go index 6b2e4fdd6afb..baa68804d153 100644 --- a/beacon-chain/rpc/beacon/state_test.go +++ b/beacon-chain/rpc/beacon/state_test.go @@ -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" @@ -76,3 +78,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 an 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) + } +} From c7caeaccfd8eb10ed9972395ce345f423a95066f Mon Sep 17 00:00:00 2001 From: terence tsao Date: Tue, 5 May 2020 17:00:30 -0700 Subject: [PATCH 3/4] Fixed existing test --- beacon-chain/rpc/beacon/state_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/beacon-chain/rpc/beacon/state_test.go b/beacon-chain/rpc/beacon/state_test.go index baa68804d153..07c42ab26083 100644 --- a/beacon-chain/rpc/beacon/state_test.go +++ b/beacon-chain/rpc/beacon/state_test.go @@ -46,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") From 1d6e2d3a0645d2dd2000c5b9d6418631076658da Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Tue, 5 May 2020 20:39:17 -0400 Subject: [PATCH 4/4] Apply suggestions from code review --- beacon-chain/rpc/beacon/state.go | 2 +- beacon-chain/rpc/beacon/state_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/beacon-chain/rpc/beacon/state.go b/beacon-chain/rpc/beacon/state.go index 24a85baff002..d6d9e5f7c029 100644 --- a/beacon-chain/rpc/beacon/state.go +++ b/beacon-chain/rpc/beacon/state.go @@ -28,7 +28,7 @@ func (bs *Server) GetBeaconState( if requestedSlot > currentSlot { return nil, status.Errorf( codes.InvalidArgument, - "Cannot retrieve information about an slot in the future, current slot %d, slot %d", + "Cannot retrieve information about a slot in the future, current slot %d, requested slot %d", currentSlot, requestedSlot, ) diff --git a/beacon-chain/rpc/beacon/state_test.go b/beacon-chain/rpc/beacon/state_test.go index 07c42ab26083..0fd57c816bf1 100644 --- a/beacon-chain/rpc/beacon/state_test.go +++ b/beacon-chain/rpc/beacon/state_test.go @@ -90,7 +90,7 @@ func TestServer_GetBeaconState_RequestFutureSlot(t *testing.T) { Slot: bs.GenesisTimeFetcher.CurrentSlot() + 1, }, } - wanted := "Cannot retrieve information about an slot in the future" + 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) }