v1alpha1 validators: catch possible reqState nil case (#9275)

* v1alpha1 validators: catch possible reqState nil case

* Minor format of error log

Co-authored-by: terence tsao <terence@prysmaticlabs.com>
Co-authored-by: Radosław Kapka <rkapka@wp.pl>
This commit is contained in:
Kaan 2021-07-26 07:50:40 +02:00 committed by GitHub
parent 14d0d9195f
commit a8c49c50ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 0 deletions

View File

@ -228,6 +228,9 @@ func (bs *Server) ListValidators(
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get requested state: %v", err)
}
if reqState == nil || reqState.IsNil() {
return nil, status.Error(codes.Internal, "Requested state is nil")
}
s, err := helpers.StartSlot(requestedEpoch)
if err != nil {

View File

@ -433,6 +433,40 @@ func TestServer_ListValidators_CannotRequestFutureEpoch(t *testing.T) {
assert.ErrorContains(t, wanted, err)
}
func TestServer_ListValidators_reqStateIsNil(t *testing.T) {
beaconDB := dbTest.SetupDB(t)
secondsPerEpoch := params.BeaconConfig().SecondsPerSlot * uint64(params.BeaconConfig().SlotsPerEpoch)
bs := &Server{
BeaconDB: beaconDB,
GenesisTimeFetcher: &mock.ChainService{
// We are in epoch 1.
Genesis: time.Now().Add(time.Duration(-1*int64(secondsPerEpoch)) * time.Second),
},
HeadFetcher: &mock.ChainService{
State: nil,
},
StateGen: &stategen.MockStateManager{
StatesBySlot: map[types.Slot]state.BeaconState{
0: nil,
},
},
}
// request uses HeadFetcher to get reqState.
req1 := &ethpb.ListValidatorsRequest{PageToken: strconv.Itoa(1), PageSize: 100}
wanted := "Requested state is nil"
_, err := bs.ListValidators(context.Background(), req1)
assert.ErrorContains(t, wanted, err)
// request uses StateGen to get reqState.
req2 := &ethpb.ListValidatorsRequest{
QueryFilter: &ethpb.ListValidatorsRequest_Genesis{},
PageToken: strconv.Itoa(1),
PageSize: 100,
}
_, err = bs.ListValidators(context.Background(), req2)
assert.ErrorContains(t, wanted, err)
}
func TestServer_ListValidators_NoResults(t *testing.T) {
beaconDB := dbTest.SetupDB(t)