2021-03-12 17:23:56 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/pkg/errors"
|
2023-03-17 18:52:56 +00:00
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/v4/config/fieldparams"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/require"
|
2023-03-20 16:32:32 +00:00
|
|
|
validatormock "github.com/prysmaticlabs/prysm/v4/testing/validator-mock"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/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()
|
|
|
|
|
2023-04-27 05:25:17 +00:00
|
|
|
inactive := randKeypair(t)
|
|
|
|
active := randKeypair(t)
|
|
|
|
|
2023-03-20 16:32:32 +00:00
|
|
|
client := validatormock.NewMockValidatorClient(ctrl)
|
|
|
|
beaconClient := validatormock.NewMockBeaconChainClient(ctrl)
|
2021-03-12 17:23:56 +00:00
|
|
|
v := validator{
|
|
|
|
validatorClient: client,
|
2023-04-27 05:25:17 +00:00
|
|
|
keyManager: newMockKeymanager(t, inactive),
|
2021-03-12 17:23:56 +00:00
|
|
|
genesisTime: 1,
|
2022-08-16 18:05:56 +00:00
|
|
|
beaconClient: beaconClient,
|
2021-03-12 17:23:56 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 05:25:17 +00:00
|
|
|
resp := testutil.GenerateMultipleValidatorStatusResponse([][]byte{inactive.pub[:], active.pub[:]})
|
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{
|
2023-04-27 05:25:17 +00:00
|
|
|
PublicKeys: [][]byte{inactive.pub[:], active.pub[:]},
|
2021-03-12 17:23:56 +00:00
|
|
|
},
|
|
|
|
).Return(resp, nil)
|
2022-08-16 18:05:56 +00:00
|
|
|
beaconClient.EXPECT().ListValidators(gomock.Any(), gomock.Any()).Return(ðpb.Validators{}, nil)
|
2021-03-12 17:23:56 +00:00
|
|
|
|
2023-04-27 05:25:17 +00:00
|
|
|
anyActive, err := v.HandleKeyReload(context.Background(), [][fieldparams.BLSPubkeyLength]byte{inactive.pub, active.pub})
|
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()
|
|
|
|
|
2023-03-20 16:32:32 +00:00
|
|
|
client := validatormock.NewMockValidatorClient(ctrl)
|
|
|
|
beaconClient := validatormock.NewMockBeaconChainClient(ctrl)
|
2023-04-27 05:25:17 +00:00
|
|
|
kp := randKeypair(t)
|
2021-03-12 17:23:56 +00:00
|
|
|
v := validator{
|
|
|
|
validatorClient: client,
|
2023-04-27 05:25:17 +00:00
|
|
|
keyManager: newMockKeymanager(t, kp),
|
2021-03-12 17:23:56 +00:00
|
|
|
genesisTime: 1,
|
2022-08-16 18:05:56 +00:00
|
|
|
beaconClient: beaconClient,
|
2021-03-12 17:23:56 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 05:25:17 +00:00
|
|
|
resp := testutil.GenerateMultipleValidatorStatusResponse([][]byte{kp.pub[:]})
|
2021-03-12 17:23:56 +00:00
|
|
|
resp.Statuses[0].Status = ethpb.ValidatorStatus_UNKNOWN_STATUS
|
|
|
|
client.EXPECT().MultipleValidatorStatus(
|
|
|
|
gomock.Any(),
|
|
|
|
ðpb.MultipleValidatorStatusRequest{
|
2023-04-27 05:25:17 +00:00
|
|
|
PublicKeys: [][]byte{kp.pub[:]},
|
2021-03-12 17:23:56 +00:00
|
|
|
},
|
|
|
|
).Return(resp, nil)
|
2022-08-16 18:05:56 +00:00
|
|
|
beaconClient.EXPECT().ListValidators(gomock.Any(), gomock.Any()).Return(ðpb.Validators{}, nil)
|
2021-03-12 17:23:56 +00:00
|
|
|
|
2023-04-27 05:25:17 +00:00
|
|
|
anyActive, err := v.HandleKeyReload(context.Background(), [][fieldparams.BLSPubkeyLength]byte{kp.pub})
|
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) {
|
2023-04-27 05:25:17 +00:00
|
|
|
kp := randKeypair(t)
|
2023-03-20 16:32:32 +00:00
|
|
|
client := validatormock.NewMockValidatorClient(ctrl)
|
2021-03-12 17:23:56 +00:00
|
|
|
v := validator{
|
|
|
|
validatorClient: client,
|
2023-04-27 05:25:17 +00:00
|
|
|
keyManager: newMockKeymanager(t, kp),
|
2021-03-12 17:23:56 +00:00
|
|
|
genesisTime: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
client.EXPECT().MultipleValidatorStatus(
|
|
|
|
gomock.Any(),
|
|
|
|
ðpb.MultipleValidatorStatusRequest{
|
2023-04-27 05:25:17 +00:00
|
|
|
PublicKeys: [][]byte{kp.pub[:]},
|
2021-03-12 17:23:56 +00:00
|
|
|
},
|
|
|
|
).Return(nil, errors.New("error"))
|
|
|
|
|
2023-04-27 05:25:17 +00:00
|
|
|
_, err := v.HandleKeyReload(context.Background(), [][fieldparams.BLSPubkeyLength]byte{kp.pub})
|
2021-03-12 17:23:56 +00:00
|
|
|
assert.ErrorContains(t, "error", err)
|
|
|
|
})
|
|
|
|
}
|