2021-03-12 17:23:56 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/pkg/errors"
|
2022-01-06 17:33:08 +00:00
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
|
2021-09-15 22:55:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/crypto/bls"
|
2021-07-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
2021-09-17 19:20:50 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/mock"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
2021-03-16 15:00:05 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/client/testutil"
|
2021-03-12 17:23:56 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidator_HandleKeyReload(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
t.Run("active", func(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
|
|
|
|
inactivePrivKey, err := bls.RandKey()
|
|
|
|
require.NoError(t, err)
|
2022-01-06 17:33:08 +00:00
|
|
|
inactivePubKey := [fieldparams.BLSPubkeyLength]byte{}
|
2021-03-12 17:23:56 +00:00
|
|
|
copy(inactivePubKey[:], inactivePrivKey.PublicKey().Marshal())
|
|
|
|
activePrivKey, err := bls.RandKey()
|
|
|
|
require.NoError(t, err)
|
2022-01-06 17:33:08 +00:00
|
|
|
activePubKey := [fieldparams.BLSPubkeyLength]byte{}
|
2021-03-12 17:23:56 +00:00
|
|
|
copy(activePubKey[:], activePrivKey.PublicKey().Marshal())
|
|
|
|
km := &mockKeymanager{
|
2022-01-06 17:33:08 +00:00
|
|
|
keysMap: map[[fieldparams.BLSPubkeyLength]byte]bls.SecretKey{
|
2021-03-12 17:23:56 +00:00
|
|
|
inactivePubKey: inactivePrivKey,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
client := mock.NewMockBeaconNodeValidatorClient(ctrl)
|
|
|
|
v := validator{
|
|
|
|
validatorClient: client,
|
|
|
|
keyManager: km,
|
|
|
|
genesisTime: 1,
|
|
|
|
}
|
|
|
|
|
2021-03-16 15:00:05 +00:00
|
|
|
resp := testutil.GenerateMultipleValidatorStatusResponse([][]byte{inactivePubKey[:], activePubKey[:]})
|
2021-03-12 17:23:56 +00:00
|
|
|
resp.Statuses[0].Status = ethpb.ValidatorStatus_UNKNOWN_STATUS
|
|
|
|
resp.Statuses[1].Status = ethpb.ValidatorStatus_ACTIVE
|
|
|
|
client.EXPECT().MultipleValidatorStatus(
|
|
|
|
gomock.Any(),
|
|
|
|
ðpb.MultipleValidatorStatusRequest{
|
|
|
|
PublicKeys: [][]byte{inactivePubKey[:], activePubKey[:]},
|
|
|
|
},
|
|
|
|
).Return(resp, nil)
|
|
|
|
|
2022-01-06 17:33:08 +00:00
|
|
|
anyActive, err := v.HandleKeyReload(context.Background(), [][fieldparams.BLSPubkeyLength]byte{inactivePubKey, activePubKey})
|
2021-03-12 17:23:56 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, true, anyActive)
|
|
|
|
assert.LogsContain(t, hook, "Waiting for deposit to be observed by beacon node")
|
|
|
|
assert.LogsContain(t, hook, "Validator activated")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no active", func(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
|
|
|
|
inactivePrivKey, err := bls.RandKey()
|
|
|
|
require.NoError(t, err)
|
2022-01-06 17:33:08 +00:00
|
|
|
inactivePubKey := [fieldparams.BLSPubkeyLength]byte{}
|
2021-03-12 17:23:56 +00:00
|
|
|
copy(inactivePubKey[:], inactivePrivKey.PublicKey().Marshal())
|
|
|
|
km := &mockKeymanager{
|
2022-01-06 17:33:08 +00:00
|
|
|
keysMap: map[[fieldparams.BLSPubkeyLength]byte]bls.SecretKey{
|
2021-03-12 17:23:56 +00:00
|
|
|
inactivePubKey: inactivePrivKey,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
client := mock.NewMockBeaconNodeValidatorClient(ctrl)
|
|
|
|
v := validator{
|
|
|
|
validatorClient: client,
|
|
|
|
keyManager: km,
|
|
|
|
genesisTime: 1,
|
|
|
|
}
|
|
|
|
|
2021-03-16 15:00:05 +00:00
|
|
|
resp := testutil.GenerateMultipleValidatorStatusResponse([][]byte{inactivePubKey[:]})
|
2021-03-12 17:23:56 +00:00
|
|
|
resp.Statuses[0].Status = ethpb.ValidatorStatus_UNKNOWN_STATUS
|
|
|
|
client.EXPECT().MultipleValidatorStatus(
|
|
|
|
gomock.Any(),
|
|
|
|
ðpb.MultipleValidatorStatusRequest{
|
|
|
|
PublicKeys: [][]byte{inactivePubKey[:]},
|
|
|
|
},
|
|
|
|
).Return(resp, nil)
|
|
|
|
|
2022-01-06 17:33:08 +00:00
|
|
|
anyActive, err := v.HandleKeyReload(context.Background(), [][fieldparams.BLSPubkeyLength]byte{inactivePubKey})
|
2021-03-12 17:23:56 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, false, anyActive)
|
|
|
|
assert.LogsContain(t, hook, "Waiting for deposit to be observed by beacon node")
|
|
|
|
assert.LogsDoNotContain(t, hook, "Validator activated")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error when getting status", func(t *testing.T) {
|
|
|
|
inactivePrivKey, err := bls.RandKey()
|
|
|
|
require.NoError(t, err)
|
2022-01-06 17:33:08 +00:00
|
|
|
inactivePubKey := [fieldparams.BLSPubkeyLength]byte{}
|
2021-03-12 17:23:56 +00:00
|
|
|
copy(inactivePubKey[:], inactivePrivKey.PublicKey().Marshal())
|
|
|
|
km := &mockKeymanager{
|
2022-01-06 17:33:08 +00:00
|
|
|
keysMap: map[[fieldparams.BLSPubkeyLength]byte]bls.SecretKey{
|
2021-03-12 17:23:56 +00:00
|
|
|
inactivePubKey: inactivePrivKey,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
client := mock.NewMockBeaconNodeValidatorClient(ctrl)
|
|
|
|
v := validator{
|
|
|
|
validatorClient: client,
|
|
|
|
keyManager: km,
|
|
|
|
genesisTime: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
client.EXPECT().MultipleValidatorStatus(
|
|
|
|
gomock.Any(),
|
|
|
|
ðpb.MultipleValidatorStatusRequest{
|
|
|
|
PublicKeys: [][]byte{inactivePubKey[:]},
|
|
|
|
},
|
|
|
|
).Return(nil, errors.New("error"))
|
|
|
|
|
2022-01-06 17:33:08 +00:00
|
|
|
_, err = v.HandleKeyReload(context.Background(), [][fieldparams.BLSPubkeyLength]byte{inactivePubKey})
|
2021-03-12 17:23:56 +00:00
|
|
|
assert.ErrorContains(t, "error", err)
|
|
|
|
})
|
|
|
|
}
|