From b4bce7c726e3d3aa46d64971cbf7b75657652397 Mon Sep 17 00:00:00 2001 From: dv8silencer <15720668+dv8silencer@users.noreply.github.com> Date: Mon, 9 Nov 2020 22:46:28 -0600 Subject: [PATCH] Correct how AllValidatorsAreExited creates status request (#7758) * fix and regression test * address feedback * gofmt * improve test -- feedback Co-authored-by: dv8silencer <15720668+dv8silencer@users.noreply.github.com> --- validator/client/validator.go | 3 ++- validator/client/validator_test.go | 41 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/validator/client/validator.go b/validator/client/validator.go index d37f78f9e..06fe46510 100644 --- a/validator/client/validator.go +++ b/validator/client/validator.go @@ -597,7 +597,8 @@ func (v *validator) AllValidatorsAreExited(ctx context.Context) (bool, error) { } var publicKeys [][]byte for _, key := range validatingKeys { - publicKeys = append(publicKeys, key[:]) + copyKey := key + publicKeys = append(publicKeys, copyKey[:]) } request := ðpb.MultipleValidatorStatusRequest{ PublicKeys: publicKeys, diff --git a/validator/client/validator_test.go b/validator/client/validator_test.go index df9f1626b..d3c7ba454 100644 --- a/validator/client/validator_test.go +++ b/validator/client/validator_test.go @@ -1015,3 +1015,44 @@ func TestAllValidatorsAreExited_NotAllExited(t *testing.T) { require.NoError(t, err) assert.Equal(t, false, exited) } + +// TestAllValidatorsAreExited_CorrectRequest is a regression test that checks if the request contains the correct keys +func TestAllValidatorsAreExited_CorrectRequest(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + client := mock.NewMockBeaconNodeValidatorClient(ctrl) + + // Create two different public keys + pubKey0 := [48]byte{1, 2, 3, 4} + pubKey1 := [48]byte{6, 7, 8, 9} + // This is the request expected from AllValidatorsAreExited() + request := ðpb.MultipleValidatorStatusRequest{ + PublicKeys: [][]byte{ + pubKey0[:], + pubKey1[:], + }, + } + statuses := []*ethpb.ValidatorStatusResponse{ + {Status: ethpb.ValidatorStatus_ACTIVE}, + {Status: ethpb.ValidatorStatus_EXITED}, + } + + client.EXPECT().MultipleValidatorStatus( + gomock.Any(), // ctx + request, // request + ).Return(ðpb.MultipleValidatorStatusResponse{Statuses: statuses}, nil /*err*/) + + keysMap := make(map[[48]byte]bls.SecretKey) + // secretKey below is just filler and is used multiple times + secretKeyBytes := [32]byte{1} + secretKey, err := bls.SecretKeyFromBytes(secretKeyBytes[:]) + require.NoError(t, err) + keysMap[pubKey0] = secretKey + keysMap[pubKey1] = secretKey + + // If AllValidatorsAreExited does not create the expected request, this test will fail + v := validator{keyManager: &mockKeymanager{keysMap: keysMap}, validatorClient: client} + exited, err := v.AllValidatorsAreExited(context.Background()) + require.NoError(t, err) + assert.Equal(t, false, exited) +}