mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 21:07:18 +00:00
8ffb95bd9d
* restart waiting for activation on key change * test fixes * wiat for activation comments * regression test * log fatal when validator cast fails * derived keymanager * review comments * add buffer to channel * simplify key refetch logic * reload keys into empty wallet * removed warning on wallet creation * add empty line * export AccountsKeystoreRepresentation type * unit test for handleAccountsChanged * test ctx cancellation * add missing mockRemoteKeymanager interface function * gazelle * gzl * fix panic inside goroutine during runner tests * rename error message variables * Update validator/accounts/accounts_list_test.go * reorder imports Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
381 lines
12 KiB
Go
381 lines
12 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
"github.com/pkg/errors"
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
|
"github.com/prysmaticlabs/prysm/shared/mock"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
walletMock "github.com/prysmaticlabs/prysm/validator/accounts/testing"
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/derived"
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
"github.com/tyler-smith/go-bip39"
|
|
util "github.com/wealdtech/go-eth2-util"
|
|
)
|
|
|
|
func TestWaitActivation_ContextCanceled(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
client := mock.NewMockBeaconNodeValidatorClient(ctrl)
|
|
privKey, err := bls.RandKey()
|
|
require.NoError(t, err)
|
|
pubKey := [48]byte{}
|
|
copy(pubKey[:], privKey.PublicKey().Marshal())
|
|
km := &mockKeymanager{
|
|
keysMap: map[[48]byte]bls.SecretKey{
|
|
pubKey: privKey,
|
|
},
|
|
}
|
|
v := validator{
|
|
validatorClient: client,
|
|
keyManager: km,
|
|
}
|
|
clientStream := mock.NewMockBeaconNodeValidator_WaitForActivationClient(ctrl)
|
|
|
|
client.EXPECT().WaitForActivation(
|
|
gomock.Any(),
|
|
ðpb.ValidatorActivationRequest{
|
|
PublicKeys: [][]byte{pubKey[:]},
|
|
},
|
|
).Return(clientStream, nil)
|
|
clientStream.EXPECT().Recv().Return(
|
|
ðpb.ValidatorActivationResponse{},
|
|
nil,
|
|
)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
assert.ErrorContains(t, cancelledCtx, v.WaitForActivation(ctx, make(chan struct{})))
|
|
}
|
|
|
|
func TestWaitActivation_StreamSetupFails_AttemptsToReconnect(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
client := mock.NewMockBeaconNodeValidatorClient(ctrl)
|
|
privKey, err := bls.RandKey()
|
|
require.NoError(t, err)
|
|
pubKey := [48]byte{}
|
|
copy(pubKey[:], privKey.PublicKey().Marshal())
|
|
km := &mockKeymanager{
|
|
keysMap: map[[48]byte]bls.SecretKey{
|
|
pubKey: privKey,
|
|
},
|
|
}
|
|
v := validator{
|
|
validatorClient: client,
|
|
keyManager: km,
|
|
}
|
|
clientStream := mock.NewMockBeaconNodeValidator_WaitForActivationClient(ctrl)
|
|
client.EXPECT().WaitForActivation(
|
|
gomock.Any(),
|
|
ðpb.ValidatorActivationRequest{
|
|
PublicKeys: [][]byte{pubKey[:]},
|
|
},
|
|
).Return(clientStream, errors.New("failed stream")).Return(clientStream, nil)
|
|
|
|
resp := generateMockStatusResponse([][]byte{pubKey[:]})
|
|
resp.Statuses[0].Status.Status = ethpb.ValidatorStatus_ACTIVE
|
|
clientStream.EXPECT().Recv().Return(resp, nil)
|
|
assert.NoError(t, v.WaitForActivation(context.Background(), make(chan struct{})))
|
|
}
|
|
|
|
func TestWaitForActivation_ReceiveErrorFromStream_AttemptsReconnection(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
client := mock.NewMockBeaconNodeValidatorClient(ctrl)
|
|
|
|
privKey, err := bls.RandKey()
|
|
require.NoError(t, err)
|
|
pubKey := [48]byte{}
|
|
copy(pubKey[:], privKey.PublicKey().Marshal())
|
|
km := &mockKeymanager{
|
|
keysMap: map[[48]byte]bls.SecretKey{
|
|
pubKey: privKey,
|
|
},
|
|
}
|
|
v := validator{
|
|
validatorClient: client,
|
|
keyManager: km,
|
|
}
|
|
clientStream := mock.NewMockBeaconNodeValidator_WaitForActivationClient(ctrl)
|
|
client.EXPECT().WaitForActivation(
|
|
gomock.Any(),
|
|
ðpb.ValidatorActivationRequest{
|
|
PublicKeys: [][]byte{pubKey[:]},
|
|
},
|
|
).Return(clientStream, nil)
|
|
// A stream fails the first time, but succeeds the second time.
|
|
resp := generateMockStatusResponse([][]byte{pubKey[:]})
|
|
resp.Statuses[0].Status.Status = ethpb.ValidatorStatus_ACTIVE
|
|
clientStream.EXPECT().Recv().Return(
|
|
nil,
|
|
errors.New("fails"),
|
|
).Return(resp, nil)
|
|
assert.NoError(t, v.WaitForActivation(context.Background(), make(chan struct{})))
|
|
}
|
|
|
|
func TestWaitActivation_LogsActivationEpochOK(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
client := mock.NewMockBeaconNodeValidatorClient(ctrl)
|
|
privKey, err := bls.RandKey()
|
|
require.NoError(t, err)
|
|
pubKey := [48]byte{}
|
|
copy(pubKey[:], privKey.PublicKey().Marshal())
|
|
km := &mockKeymanager{
|
|
keysMap: map[[48]byte]bls.SecretKey{
|
|
pubKey: privKey,
|
|
},
|
|
}
|
|
v := validator{
|
|
validatorClient: client,
|
|
keyManager: km,
|
|
genesisTime: 1,
|
|
}
|
|
resp := generateMockStatusResponse([][]byte{pubKey[:]})
|
|
resp.Statuses[0].Status.Status = ethpb.ValidatorStatus_ACTIVE
|
|
clientStream := mock.NewMockBeaconNodeValidator_WaitForActivationClient(ctrl)
|
|
client.EXPECT().WaitForActivation(
|
|
gomock.Any(),
|
|
ðpb.ValidatorActivationRequest{
|
|
PublicKeys: [][]byte{pubKey[:]},
|
|
},
|
|
).Return(clientStream, nil)
|
|
clientStream.EXPECT().Recv().Return(
|
|
resp,
|
|
nil,
|
|
)
|
|
assert.NoError(t, v.WaitForActivation(context.Background(), make(chan struct{})), "Could not wait for activation")
|
|
assert.LogsContain(t, hook, "Validator activated")
|
|
}
|
|
|
|
func TestWaitForActivation_Exiting(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
client := mock.NewMockBeaconNodeValidatorClient(ctrl)
|
|
privKey, err := bls.RandKey()
|
|
require.NoError(t, err)
|
|
pubKey := [48]byte{}
|
|
copy(pubKey[:], privKey.PublicKey().Marshal())
|
|
km := &mockKeymanager{
|
|
keysMap: map[[48]byte]bls.SecretKey{
|
|
pubKey: privKey,
|
|
},
|
|
}
|
|
v := validator{
|
|
validatorClient: client,
|
|
keyManager: km,
|
|
genesisTime: 1,
|
|
}
|
|
resp := generateMockStatusResponse([][]byte{pubKey[:]})
|
|
resp.Statuses[0].Status.Status = ethpb.ValidatorStatus_EXITING
|
|
clientStream := mock.NewMockBeaconNodeValidator_WaitForActivationClient(ctrl)
|
|
client.EXPECT().WaitForActivation(
|
|
gomock.Any(),
|
|
ðpb.ValidatorActivationRequest{
|
|
PublicKeys: [][]byte{pubKey[:]},
|
|
},
|
|
).Return(clientStream, nil)
|
|
clientStream.EXPECT().Recv().Return(
|
|
resp,
|
|
nil,
|
|
)
|
|
assert.NoError(t, v.WaitForActivation(context.Background(), make(chan struct{})))
|
|
}
|
|
|
|
func TestWaitForActivation_RefetchKeys(t *testing.T) {
|
|
originalPeriod := keyRefetchPeriod
|
|
defer func() {
|
|
keyRefetchPeriod = originalPeriod
|
|
}()
|
|
keyRefetchPeriod = 1 * time.Second
|
|
|
|
hook := logTest.NewGlobal()
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
client := mock.NewMockBeaconNodeValidatorClient(ctrl)
|
|
privKey, err := bls.RandKey()
|
|
require.NoError(t, err)
|
|
pubKey := [48]byte{}
|
|
copy(pubKey[:], privKey.PublicKey().Marshal())
|
|
km := &mockKeymanager{
|
|
keysMap: map[[48]byte]bls.SecretKey{
|
|
pubKey: privKey,
|
|
},
|
|
fetchNoKeys: true,
|
|
}
|
|
v := validator{
|
|
validatorClient: client,
|
|
keyManager: km,
|
|
genesisTime: 1,
|
|
}
|
|
resp := generateMockStatusResponse([][]byte{pubKey[:]})
|
|
resp.Statuses[0].Status.Status = ethpb.ValidatorStatus_ACTIVE
|
|
clientStream := mock.NewMockBeaconNodeValidator_WaitForActivationClient(ctrl)
|
|
client.EXPECT().WaitForActivation(
|
|
gomock.Any(),
|
|
ðpb.ValidatorActivationRequest{
|
|
PublicKeys: [][]byte{pubKey[:]},
|
|
},
|
|
).Return(clientStream, nil)
|
|
clientStream.EXPECT().Recv().Return(
|
|
resp,
|
|
nil,
|
|
)
|
|
assert.NoError(t, v.WaitForActivation(context.Background(), make(chan struct{})), "Could not wait for activation")
|
|
assert.LogsContain(t, hook, msgNoKeysFetched)
|
|
assert.LogsContain(t, hook, "Validator activated")
|
|
}
|
|
|
|
// Regression test for a scenario where you start with an inactive key and then import an active key.
|
|
func TestWaitForActivation_AccountsChanged(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
t.Run("Imported keymanager", func(t *testing.T) {
|
|
inactivePrivKey, err := bls.RandKey()
|
|
require.NoError(t, err)
|
|
inactivePubKey := [48]byte{}
|
|
copy(inactivePubKey[:], inactivePrivKey.PublicKey().Marshal())
|
|
activePrivKey, err := bls.RandKey()
|
|
require.NoError(t, err)
|
|
activePubKey := [48]byte{}
|
|
copy(activePubKey[:], activePrivKey.PublicKey().Marshal())
|
|
km := &mockKeymanager{
|
|
keysMap: map[[48]byte]bls.SecretKey{
|
|
inactivePubKey: inactivePrivKey,
|
|
},
|
|
}
|
|
client := mock.NewMockBeaconNodeValidatorClient(ctrl)
|
|
v := validator{
|
|
validatorClient: client,
|
|
keyManager: km,
|
|
genesisTime: 1,
|
|
}
|
|
|
|
inactiveResp := generateMockStatusResponse([][]byte{inactivePubKey[:]})
|
|
inactiveResp.Statuses[0].Status.Status = ethpb.ValidatorStatus_UNKNOWN_STATUS
|
|
inactiveClientStream := mock.NewMockBeaconNodeValidator_WaitForActivationClient(ctrl)
|
|
client.EXPECT().WaitForActivation(
|
|
gomock.Any(),
|
|
ðpb.ValidatorActivationRequest{
|
|
PublicKeys: [][]byte{inactivePubKey[:]},
|
|
},
|
|
).Return(inactiveClientStream, nil)
|
|
inactiveClientStream.EXPECT().Recv().Return(
|
|
inactiveResp,
|
|
nil,
|
|
).AnyTimes()
|
|
|
|
activeResp := generateMockStatusResponse([][]byte{inactivePubKey[:], activePubKey[:]})
|
|
activeResp.Statuses[0].Status.Status = ethpb.ValidatorStatus_UNKNOWN_STATUS
|
|
activeResp.Statuses[1].Status.Status = ethpb.ValidatorStatus_ACTIVE
|
|
activeClientStream := mock.NewMockBeaconNodeValidator_WaitForActivationClient(ctrl)
|
|
client.EXPECT().WaitForActivation(
|
|
gomock.Any(),
|
|
ðpb.ValidatorActivationRequest{
|
|
PublicKeys: [][]byte{inactivePubKey[:], activePubKey[:]},
|
|
},
|
|
).Return(activeClientStream, nil)
|
|
activeClientStream.EXPECT().Recv().Return(
|
|
activeResp,
|
|
nil,
|
|
)
|
|
|
|
channel := make(chan struct{})
|
|
go func() {
|
|
// We add the active key into the keymanager and simulate a key refresh.
|
|
time.Sleep(time.Second * 1)
|
|
km.keysMap[activePubKey] = activePrivKey
|
|
channel <- struct{}{}
|
|
}()
|
|
|
|
assert.NoError(t, v.WaitForActivation(context.Background(), channel))
|
|
assert.LogsContain(t, hook, "Waiting for deposit to be observed by beacon node")
|
|
assert.LogsContain(t, hook, "Validator activated")
|
|
})
|
|
|
|
t.Run("Derived keymanager", func(t *testing.T) {
|
|
mnemonic := "tumble turn jewel sudden social great water general cabin jacket bounce dry flip monster advance problem social half flee inform century chicken hard reason"
|
|
seed := bip39.NewSeed(mnemonic, "")
|
|
inactivePrivKey, err :=
|
|
util.PrivateKeyFromSeedAndPath(seed, fmt.Sprintf(derived.ValidatingKeyDerivationPathTemplate, 0))
|
|
require.NoError(t, err)
|
|
inactivePubKey := [48]byte{}
|
|
copy(inactivePubKey[:], inactivePrivKey.PublicKey().Marshal())
|
|
activePrivKey, err :=
|
|
util.PrivateKeyFromSeedAndPath(seed, fmt.Sprintf(derived.ValidatingKeyDerivationPathTemplate, 1))
|
|
require.NoError(t, err)
|
|
activePubKey := [48]byte{}
|
|
copy(activePubKey[:], activePrivKey.PublicKey().Marshal())
|
|
wallet := &walletMock.Wallet{
|
|
Files: make(map[string]map[string][]byte),
|
|
AccountPasswords: make(map[string]string),
|
|
WalletPassword: "secretPassw0rd$1999",
|
|
}
|
|
ctx := context.Background()
|
|
km, err := derived.NewKeymanager(ctx, &derived.SetupConfig{
|
|
Wallet: wallet,
|
|
})
|
|
require.NoError(t, err)
|
|
err = km.RecoverAccountsFromMnemonic(ctx, mnemonic, "", 1)
|
|
require.NoError(t, err)
|
|
client := mock.NewMockBeaconNodeValidatorClient(ctrl)
|
|
v := validator{
|
|
validatorClient: client,
|
|
keyManager: km,
|
|
genesisTime: 1,
|
|
}
|
|
|
|
inactiveResp := generateMockStatusResponse([][]byte{inactivePubKey[:]})
|
|
inactiveResp.Statuses[0].Status.Status = ethpb.ValidatorStatus_UNKNOWN_STATUS
|
|
inactiveClientStream := mock.NewMockBeaconNodeValidator_WaitForActivationClient(ctrl)
|
|
client.EXPECT().WaitForActivation(
|
|
gomock.Any(),
|
|
ðpb.ValidatorActivationRequest{
|
|
PublicKeys: [][]byte{inactivePubKey[:]},
|
|
},
|
|
).Return(inactiveClientStream, nil)
|
|
inactiveClientStream.EXPECT().Recv().Return(
|
|
inactiveResp,
|
|
nil,
|
|
).AnyTimes()
|
|
|
|
activeResp := generateMockStatusResponse([][]byte{inactivePubKey[:], activePubKey[:]})
|
|
activeResp.Statuses[0].Status.Status = ethpb.ValidatorStatus_UNKNOWN_STATUS
|
|
activeResp.Statuses[1].Status.Status = ethpb.ValidatorStatus_ACTIVE
|
|
activeClientStream := mock.NewMockBeaconNodeValidator_WaitForActivationClient(ctrl)
|
|
client.EXPECT().WaitForActivation(
|
|
gomock.Any(),
|
|
ðpb.ValidatorActivationRequest{
|
|
PublicKeys: [][]byte{inactivePubKey[:], activePubKey[:]},
|
|
},
|
|
).Return(activeClientStream, nil)
|
|
activeClientStream.EXPECT().Recv().Return(
|
|
activeResp,
|
|
nil,
|
|
)
|
|
|
|
channel := make(chan struct{})
|
|
go func() {
|
|
// We add the active key into the keymanager and simulate a key refresh.
|
|
time.Sleep(time.Second * 1)
|
|
err = km.RecoverAccountsFromMnemonic(ctx, mnemonic, "", 2)
|
|
require.NoError(t, err)
|
|
channel <- struct{}{}
|
|
}()
|
|
|
|
assert.NoError(t, v.WaitForActivation(context.Background(), channel))
|
|
assert.LogsContain(t, hook, "Waiting for deposit to be observed by beacon node")
|
|
assert.LogsContain(t, hook, "Validator activated")
|
|
})
|
|
}
|