diff --git a/beacon-chain/rpc/prysm/v1alpha1/beacon/validators.go b/beacon-chain/rpc/prysm/v1alpha1/beacon/validators.go index ed0a3aae7..df354ae16 100644 --- a/beacon-chain/rpc/prysm/v1alpha1/beacon/validators.go +++ b/beacon-chain/rpc/prysm/v1alpha1/beacon/validators.go @@ -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 { diff --git a/beacon-chain/rpc/prysm/v1alpha1/beacon/validators_test.go b/beacon-chain/rpc/prysm/v1alpha1/beacon/validators_test.go index 2aa43fc6e..a54be77a9 100644 --- a/beacon-chain/rpc/prysm/v1alpha1/beacon/validators_test.go +++ b/beacon-chain/rpc/prysm/v1alpha1/beacon/validators_test.go @@ -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 := ðpb.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 := ðpb.ListValidatorsRequest{ + QueryFilter: ðpb.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)