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>
This commit is contained in:
dv8silencer 2020-11-09 22:46:28 -06:00 committed by GitHub
parent 9e9a913069
commit b4bce7c726
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 1 deletions

View File

@ -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 := &ethpb.MultipleValidatorStatusRequest{
PublicKeys: publicKeys,

View File

@ -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 := &ethpb.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(&ethpb.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)
}