2020-10-15 22:31:52 +00:00
|
|
|
package accounts
|
2020-08-20 17:53:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-02-11 16:50:16 +00:00
|
|
|
"sort"
|
2020-08-20 17:53:09 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-09-09 18:29:17 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2021-06-02 23:49:52 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
2021-05-18 17:17:42 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2020-09-09 18:29:17 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/mock"
|
2020-08-20 17:53:09 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts/wallet"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager"
|
2020-10-16 18:45:14 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/imported"
|
2021-02-01 19:00:06 +00:00
|
|
|
"github.com/sirupsen/logrus/hooks/test"
|
2021-01-27 13:12:48 +00:00
|
|
|
"google.golang.org/grpc/metadata"
|
2021-05-17 18:32:04 +00:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2020-08-20 17:53:09 +00:00
|
|
|
)
|
|
|
|
|
2021-02-11 16:50:16 +00:00
|
|
|
func TestExitAccountsCli_OK(t *testing.T) {
|
2020-09-09 18:29:17 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
mockValidatorClient := mock.NewMockBeaconNodeValidatorClient(ctrl)
|
|
|
|
mockNodeClient := mock.NewMockNodeClient(ctrl)
|
|
|
|
|
|
|
|
mockValidatorClient.EXPECT().
|
|
|
|
ValidatorIndex(gomock.Any(), gomock.Any()).
|
|
|
|
Return(ðpb.ValidatorIndexResponse{Index: 1}, nil)
|
|
|
|
|
|
|
|
// Any time in the past will suffice
|
2021-05-17 18:32:04 +00:00
|
|
|
genesisTime := ×tamppb.Timestamp{
|
2020-09-09 18:29:17 +00:00
|
|
|
Seconds: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Unix(),
|
|
|
|
}
|
|
|
|
|
|
|
|
mockNodeClient.EXPECT().
|
|
|
|
GetGenesis(gomock.Any(), gomock.Any()).
|
|
|
|
Return(ðpb.Genesis{GenesisTime: genesisTime}, nil)
|
|
|
|
|
|
|
|
mockValidatorClient.EXPECT().
|
|
|
|
DomainData(gomock.Any(), gomock.Any()).
|
|
|
|
Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil)
|
|
|
|
|
|
|
|
mockValidatorClient.EXPECT().
|
|
|
|
ProposeExit(gomock.Any(), gomock.AssignableToTypeOf(ðpb.SignedVoluntaryExit{})).
|
|
|
|
Return(ðpb.ProposeExitResponse{}, nil)
|
2020-08-20 17:53:09 +00:00
|
|
|
|
|
|
|
walletDir, _, passwordFilePath := setupWalletAndPasswordsDir(t)
|
|
|
|
// Write a directory where we will import keys from.
|
2020-11-10 22:45:17 +00:00
|
|
|
keysDir := filepath.Join(t.TempDir(), "keysDir")
|
2020-08-20 17:53:09 +00:00
|
|
|
require.NoError(t, os.MkdirAll(keysDir, os.ModePerm))
|
|
|
|
|
|
|
|
// Create keystore file in the keys directory we can then import from in our wallet.
|
|
|
|
keystore, _ := createKeystore(t, keysDir)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
2020-10-16 18:45:14 +00:00
|
|
|
// We initialize a wallet with a imported keymanager.
|
2020-08-20 17:53:09 +00:00
|
|
|
cliCtx := setupWalletCtx(t, &testWalletConfig{
|
|
|
|
// Wallet configuration flags.
|
|
|
|
walletDir: walletDir,
|
2020-10-16 18:45:14 +00:00
|
|
|
keymanagerKind: keymanager.Imported,
|
2020-08-20 17:53:09 +00:00
|
|
|
walletPasswordFile: passwordFilePath,
|
|
|
|
accountPasswordFile: passwordFilePath,
|
|
|
|
// Flag required for ImportAccounts to work.
|
|
|
|
keysDir: keysDir,
|
|
|
|
// Flag required for ExitAccounts to work.
|
|
|
|
voluntaryExitPublicKeys: keystore.Pubkey,
|
|
|
|
})
|
2020-11-10 22:45:17 +00:00
|
|
|
_, err := CreateWalletWithKeymanager(cliCtx.Context, &CreateWalletConfig{
|
2020-09-17 01:34:42 +00:00
|
|
|
WalletCfg: &wallet.Config{
|
2020-08-31 19:46:45 +00:00
|
|
|
WalletDir: walletDir,
|
2020-10-16 18:45:14 +00:00
|
|
|
KeymanagerKind: keymanager.Imported,
|
2020-10-10 02:07:28 +00:00
|
|
|
WalletPassword: password,
|
2020-08-31 19:46:45 +00:00
|
|
|
},
|
|
|
|
})
|
2020-08-20 17:53:09 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-31 19:46:45 +00:00
|
|
|
require.NoError(t, ImportAccountsCli(cliCtx))
|
2020-08-20 17:53:09 +00:00
|
|
|
|
2020-09-09 18:29:17 +00:00
|
|
|
validatingPublicKeys, keymanager, err := prepareWallet(cliCtx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, validatingPublicKeys)
|
|
|
|
require.NotNil(t, keymanager)
|
|
|
|
|
2020-08-20 17:53:09 +00:00
|
|
|
// Prepare user input for final confirmation step
|
|
|
|
var stdin bytes.Buffer
|
2020-09-24 11:00:44 +00:00
|
|
|
stdin.Write([]byte(exitPassphrase))
|
2020-09-09 18:29:17 +00:00
|
|
|
rawPubKeys, formattedPubKeys, err := interact(cliCtx, &stdin, validatingPublicKeys)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, rawPubKeys)
|
|
|
|
require.NotNil(t, formattedPubKeys)
|
2020-08-20 17:53:09 +00:00
|
|
|
|
2021-04-16 07:46:38 +00:00
|
|
|
cfg := PerformExitCfg{
|
2020-09-09 18:29:17 +00:00
|
|
|
mockValidatorClient,
|
|
|
|
mockNodeClient,
|
|
|
|
keymanager,
|
|
|
|
rawPubKeys,
|
|
|
|
formattedPubKeys,
|
|
|
|
}
|
2021-04-16 07:46:38 +00:00
|
|
|
rawExitedKeys, formattedExitedKeys, err := PerformVoluntaryExit(cliCtx.Context, cfg)
|
2020-09-09 18:29:17 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-01 19:00:06 +00:00
|
|
|
require.Equal(t, 1, len(rawExitedKeys))
|
|
|
|
assert.DeepEqual(t, rawPubKeys[0], rawExitedKeys[0])
|
|
|
|
require.Equal(t, 1, len(formattedExitedKeys))
|
|
|
|
assert.Equal(t, "0x"+keystore.Pubkey[:12], formattedExitedKeys[0])
|
2020-08-20 17:53:09 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 16:50:16 +00:00
|
|
|
func TestExitAccountsCli_OK_AllPublicKeys(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
mockValidatorClient := mock.NewMockBeaconNodeValidatorClient(ctrl)
|
|
|
|
mockNodeClient := mock.NewMockNodeClient(ctrl)
|
|
|
|
|
|
|
|
mockValidatorClient.EXPECT().
|
|
|
|
ValidatorIndex(gomock.Any(), gomock.Any()).
|
|
|
|
Return(ðpb.ValidatorIndexResponse{Index: 0}, nil)
|
|
|
|
|
|
|
|
mockValidatorClient.EXPECT().
|
|
|
|
ValidatorIndex(gomock.Any(), gomock.Any()).
|
|
|
|
Return(ðpb.ValidatorIndexResponse{Index: 1}, nil)
|
|
|
|
|
|
|
|
// Any time in the past will suffice
|
2021-05-17 18:32:04 +00:00
|
|
|
genesisTime := ×tamppb.Timestamp{
|
2021-02-11 16:50:16 +00:00
|
|
|
Seconds: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Unix(),
|
|
|
|
}
|
|
|
|
|
|
|
|
mockNodeClient.EXPECT().
|
|
|
|
GetGenesis(gomock.Any(), gomock.Any()).
|
|
|
|
Times(2).
|
|
|
|
Return(ðpb.Genesis{GenesisTime: genesisTime}, nil)
|
|
|
|
|
|
|
|
mockValidatorClient.EXPECT().
|
|
|
|
DomainData(gomock.Any(), gomock.Any()).
|
|
|
|
Times(2).
|
|
|
|
Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil)
|
|
|
|
|
|
|
|
mockValidatorClient.EXPECT().
|
|
|
|
ProposeExit(gomock.Any(), gomock.AssignableToTypeOf(ðpb.SignedVoluntaryExit{})).
|
|
|
|
Times(2).
|
|
|
|
Return(ðpb.ProposeExitResponse{}, nil)
|
|
|
|
|
|
|
|
walletDir, _, passwordFilePath := setupWalletAndPasswordsDir(t)
|
|
|
|
// Write a directory where we will import keys from.
|
|
|
|
keysDir := filepath.Join(t.TempDir(), "keysDir")
|
|
|
|
require.NoError(t, os.MkdirAll(keysDir, os.ModePerm))
|
|
|
|
|
|
|
|
// Create keystore file in the keys directory we can then import from in our wallet.
|
|
|
|
keystore1, _ := createKeystore(t, keysDir)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
keystore2, _ := createKeystore(t, keysDir)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
// We initialize a wallet with a imported keymanager.
|
|
|
|
cliCtx := setupWalletCtx(t, &testWalletConfig{
|
|
|
|
// Wallet configuration flags.
|
|
|
|
walletDir: walletDir,
|
|
|
|
keymanagerKind: keymanager.Imported,
|
|
|
|
walletPasswordFile: passwordFilePath,
|
|
|
|
accountPasswordFile: passwordFilePath,
|
|
|
|
// Flag required for ImportAccounts to work.
|
|
|
|
keysDir: keysDir,
|
|
|
|
// Exit all public keys.
|
|
|
|
exitAll: true,
|
|
|
|
})
|
|
|
|
_, err := CreateWalletWithKeymanager(cliCtx.Context, &CreateWalletConfig{
|
|
|
|
WalletCfg: &wallet.Config{
|
|
|
|
WalletDir: walletDir,
|
|
|
|
KeymanagerKind: keymanager.Imported,
|
|
|
|
WalletPassword: password,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, ImportAccountsCli(cliCtx))
|
|
|
|
|
|
|
|
validatingPublicKeys, keymanager, err := prepareWallet(cliCtx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, validatingPublicKeys)
|
|
|
|
require.NotNil(t, keymanager)
|
|
|
|
|
|
|
|
// Prepare user input for final confirmation step
|
|
|
|
var stdin bytes.Buffer
|
|
|
|
stdin.Write([]byte(exitPassphrase))
|
|
|
|
rawPubKeys, formattedPubKeys, err := interact(cliCtx, &stdin, validatingPublicKeys)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, rawPubKeys)
|
|
|
|
require.NotNil(t, formattedPubKeys)
|
|
|
|
|
2021-04-16 07:46:38 +00:00
|
|
|
cfg := PerformExitCfg{
|
2021-02-11 16:50:16 +00:00
|
|
|
mockValidatorClient,
|
|
|
|
mockNodeClient,
|
|
|
|
keymanager,
|
|
|
|
rawPubKeys,
|
|
|
|
formattedPubKeys,
|
|
|
|
}
|
2021-04-16 07:46:38 +00:00
|
|
|
rawExitedKeys, formattedExitedKeys, err := PerformVoluntaryExit(cliCtx.Context, cfg)
|
2021-02-11 16:50:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, len(rawExitedKeys))
|
|
|
|
assert.DeepEqual(t, rawPubKeys, rawExitedKeys)
|
|
|
|
require.Equal(t, 2, len(formattedExitedKeys))
|
|
|
|
wantedFormatted := []string{
|
|
|
|
"0x" + keystore1.Pubkey[:12],
|
|
|
|
"0x" + keystore2.Pubkey[:12],
|
|
|
|
}
|
|
|
|
sort.Strings(wantedFormatted)
|
|
|
|
sort.Strings(formattedExitedKeys)
|
|
|
|
require.DeepEqual(t, wantedFormatted, formattedExitedKeys)
|
|
|
|
}
|
|
|
|
|
2020-09-09 18:29:17 +00:00
|
|
|
func TestPrepareWallet_EmptyWalletReturnsError(t *testing.T) {
|
2020-10-16 18:45:14 +00:00
|
|
|
imported.ResetCaches()
|
2020-08-20 17:53:09 +00:00
|
|
|
walletDir, _, passwordFilePath := setupWalletAndPasswordsDir(t)
|
|
|
|
cliCtx := setupWalletCtx(t, &testWalletConfig{
|
|
|
|
walletDir: walletDir,
|
2020-10-16 18:45:14 +00:00
|
|
|
keymanagerKind: keymanager.Imported,
|
2020-08-20 17:53:09 +00:00
|
|
|
walletPasswordFile: passwordFilePath,
|
|
|
|
accountPasswordFile: passwordFilePath,
|
|
|
|
})
|
2020-08-31 19:46:45 +00:00
|
|
|
_, err := CreateWalletWithKeymanager(cliCtx.Context, &CreateWalletConfig{
|
2020-09-17 01:34:42 +00:00
|
|
|
WalletCfg: &wallet.Config{
|
2020-08-31 19:46:45 +00:00
|
|
|
WalletDir: walletDir,
|
2020-10-16 18:45:14 +00:00
|
|
|
KeymanagerKind: keymanager.Imported,
|
2021-01-12 16:52:01 +00:00
|
|
|
WalletPassword: password,
|
2020-08-31 19:46:45 +00:00
|
|
|
},
|
|
|
|
})
|
2020-08-20 17:53:09 +00:00
|
|
|
require.NoError(t, err)
|
2020-09-09 18:29:17 +00:00
|
|
|
_, _, err = prepareWallet(cliCtx)
|
2020-10-20 02:26:31 +00:00
|
|
|
assert.ErrorContains(t, "wallet is empty", err)
|
2020-08-20 17:53:09 +00:00
|
|
|
}
|
2021-01-27 13:12:48 +00:00
|
|
|
|
|
|
|
func TestPrepareClients_AddsGRPCHeaders(t *testing.T) {
|
|
|
|
imported.ResetCaches()
|
|
|
|
walletDir, _, passwordFilePath := setupWalletAndPasswordsDir(t)
|
|
|
|
cliCtx := setupWalletCtx(t, &testWalletConfig{
|
|
|
|
walletDir: walletDir,
|
|
|
|
keymanagerKind: keymanager.Imported,
|
|
|
|
walletPasswordFile: passwordFilePath,
|
|
|
|
accountPasswordFile: passwordFilePath,
|
|
|
|
grpcHeaders: "Authorization=Basic some-token,Some-Other-Header=some-value",
|
|
|
|
})
|
|
|
|
_, err := CreateWalletWithKeymanager(cliCtx.Context, &CreateWalletConfig{
|
|
|
|
WalletCfg: &wallet.Config{
|
|
|
|
WalletDir: walletDir,
|
|
|
|
KeymanagerKind: keymanager.Imported,
|
|
|
|
WalletPassword: password,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, _, err = prepareClients(cliCtx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
md, _ := metadata.FromOutgoingContext(cliCtx.Context)
|
|
|
|
assert.Equal(t, "Basic some-token", md.Get("Authorization")[0])
|
|
|
|
assert.Equal(t, "some-value", md.Get("Some-Other-Header")[0])
|
|
|
|
}
|
2021-02-01 19:00:06 +00:00
|
|
|
|
|
|
|
func TestDisplayExitInfo(t *testing.T) {
|
|
|
|
logHook := test.NewGlobal()
|
|
|
|
key := []byte("0x123456")
|
|
|
|
displayExitInfo([][]byte{key}, []string{string(key)})
|
|
|
|
assert.LogsContain(t, logHook, "https://beaconcha.in/validator/3078313233343536")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDisplayExitInfo_NoKeys(t *testing.T) {
|
|
|
|
logHook := test.NewGlobal()
|
|
|
|
displayExitInfo([][]byte{}, []string{})
|
|
|
|
assert.LogsContain(t, logHook, "No successful voluntary exits")
|
|
|
|
}
|
2021-05-18 17:17:42 +00:00
|
|
|
|
|
|
|
func TestPrepareAllKeys(t *testing.T) {
|
|
|
|
key1 := bytesutil.ToBytes48([]byte("key1"))
|
|
|
|
key2 := bytesutil.ToBytes48([]byte("key2"))
|
|
|
|
raw, formatted := prepareAllKeys([][48]byte{key1, key2})
|
|
|
|
require.Equal(t, 2, len(raw))
|
|
|
|
require.Equal(t, 2, len(formatted))
|
|
|
|
assert.DeepEqual(t, bytesutil.ToBytes48([]byte{107, 101, 121, 49}), bytesutil.ToBytes48(raw[0]))
|
|
|
|
assert.DeepEqual(t, bytesutil.ToBytes48([]byte{107, 101, 121, 50}), bytesutil.ToBytes48(raw[1]))
|
|
|
|
assert.Equal(t, "0x6b6579310000", formatted[0])
|
|
|
|
assert.Equal(t, "0x6b6579320000", formatted[1])
|
|
|
|
}
|