From 52d48b328f51556e5b4700ef9e811471840b5d31 Mon Sep 17 00:00:00 2001 From: james-prysm <90280386+james-prysm@users.noreply.github.com> Date: Wed, 31 Aug 2022 11:42:49 -0500 Subject: [PATCH] Improve Validator Index RPC Error Handling (#11363) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * adding in nil check for head * adding changes based on feedback * Update beacon-chain/rpc/prysm/v1alpha1/validator/server_test.go Co-authored-by: RadosÅ‚aw Kapka --- beacon-chain/rpc/prysm/v1alpha1/validator/server.go | 3 +++ .../rpc/prysm/v1alpha1/validator/server_test.go | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/beacon-chain/rpc/prysm/v1alpha1/validator/server.go b/beacon-chain/rpc/prysm/v1alpha1/validator/server.go index 03a36117c..ebea1b1dc 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 c93ec9901..0701918c7 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,