diff --git a/beacon-chain/rpc/prysm/v1alpha1/validator/server.go b/beacon-chain/rpc/prysm/v1alpha1/validator/server.go index 03a36117c4..ebea1b1dca 100644 --- a/beacon-chain/rpc/prysm/v1alpha1/validator/server.go +++ b/beacon-chain/rpc/prysm/v1alpha1/validator/server.go @@ -123,6 +123,9 @@ func (vs *Server) ValidatorIndex(ctx context.Context, req *ethpb.ValidatorIndexR if err != nil { return nil, status.Errorf(codes.Internal, "Could not determine head state: %v", err) } + if st == nil || st.IsNil() { + return nil, status.Errorf(codes.Internal, "head state is empty") + } index, ok := st.ValidatorIndexByPubkey(bytesutil.ToBytes48(req.PublicKey)) if !ok { return nil, status.Errorf(codes.NotFound, "Could not find validator index for public key %#x", req.PublicKey) diff --git a/beacon-chain/rpc/prysm/v1alpha1/validator/server_test.go b/beacon-chain/rpc/prysm/v1alpha1/validator/server_test.go index c93ec99019..0701918c7f 100644 --- a/beacon-chain/rpc/prysm/v1alpha1/validator/server_test.go +++ b/beacon-chain/rpc/prysm/v1alpha1/validator/server_test.go @@ -48,6 +48,18 @@ func TestValidatorIndex_OK(t *testing.T) { assert.NoError(t, err, "Could not get validator index") } +func TestValidatorIndex_StateEmpty(t *testing.T) { + Server := &Server{ + HeadFetcher: &mockChain.ChainService{}, + } + pubKey := pubKey(1) + req := ðpb.ValidatorIndexRequest{ + PublicKey: pubKey, + } + _, err := Server.ValidatorIndex(context.Background(), req) + assert.ErrorContains(t, "head state is empty", err) +} + func TestWaitForActivation_ContextClosed(t *testing.T) { beaconState, err := v1.InitializeFromProto(ðpb.BeaconState{ Slot: 0,