From a8c49c50adedf15fa82b411533f5a8c53d0b4fe3 Mon Sep 17 00:00:00 2001 From: Kaan Date: Mon, 26 Jul 2021 07:50:40 +0200 Subject: [PATCH] v1alpha1 validators: catch possible reqState nil case (#9275) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * v1alpha1 validators: catch possible reqState nil case * Minor format of error log Co-authored-by: terence tsao Co-authored-by: RadosÅ‚aw Kapka --- .../rpc/prysm/v1alpha1/beacon/validators.go | 3 ++ .../prysm/v1alpha1/beacon/validators_test.go | 34 +++++++++++++++++++ 2 files changed, 37 insertions(+) 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)