prysm-pulse/validator/rpc/accounts_test.go
Ivan Martinez afc3b3168a
Add Back Accounts Backup Validator RPC Functionality (#8367)
* Add Back Accounts Backup Validator RPC Functionality

This reverts commit a39db494eb.

* Fix
2021-01-29 18:30:44 -06:00

164 lines
4.7 KiB
Go

package rpc
import (
"archive/zip"
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"testing"
pb "github.com/prysmaticlabs/prysm/proto/validator/accounts/v2"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
"github.com/prysmaticlabs/prysm/validator/accounts"
"github.com/prysmaticlabs/prysm/validator/accounts/wallet"
"github.com/prysmaticlabs/prysm/validator/flags"
"github.com/prysmaticlabs/prysm/validator/keymanager"
"github.com/prysmaticlabs/prysm/validator/keymanager/derived"
)
var (
defaultWalletPath = filepath.Join(flags.DefaultValidatorDir(), flags.WalletDefaultDirName)
testMnemonic = "tumble turn jewel sudden social great water general cabin jacket bounce dry flip monster advance problem social half flee inform century chicken hard reason"
)
func TestServer_ListAccounts(t *testing.T) {
ctx := context.Background()
localWalletDir := setupWalletDir(t)
defaultWalletPath = localWalletDir
strongPass := "29384283xasjasd32%%&*@*#*"
// We attempt to create the wallet.
w, err := accounts.CreateWalletWithKeymanager(ctx, &accounts.CreateWalletConfig{
WalletCfg: &wallet.Config{
WalletDir: defaultWalletPath,
KeymanagerKind: keymanager.Derived,
WalletPassword: strongPass,
},
SkipMnemonicConfirm: true,
})
require.NoError(t, err)
km, err := w.InitializeKeymanager(ctx)
require.NoError(t, err)
s := &Server{
keymanager: km,
walletInitialized: true,
wallet: w,
}
numAccounts := 50
dr, ok := km.(*derived.Keymanager)
require.Equal(t, true, ok)
err = dr.RecoverAccountsFromMnemonic(ctx, testMnemonic, "", numAccounts)
require.NoError(t, err)
resp, err := s.ListAccounts(ctx, &pb.ListAccountsRequest{
PageSize: int32(numAccounts),
})
require.NoError(t, err)
require.Equal(t, len(resp.Accounts), numAccounts)
tests := []struct {
req *pb.ListAccountsRequest
res *pb.ListAccountsResponse
}{
{
req: &pb.ListAccountsRequest{
PageSize: 5,
},
res: &pb.ListAccountsResponse{
Accounts: resp.Accounts[0:5],
NextPageToken: "1",
TotalSize: int32(numAccounts),
},
},
{
req: &pb.ListAccountsRequest{
PageSize: 5,
PageToken: "1",
},
res: &pb.ListAccountsResponse{
Accounts: resp.Accounts[5:10],
NextPageToken: "2",
TotalSize: int32(numAccounts),
},
},
}
for _, test := range tests {
res, err := s.ListAccounts(context.Background(), test.req)
require.NoError(t, err)
assert.DeepEqual(t, res, test.res)
}
}
func TestServer_BackupAccounts(t *testing.T) {
ctx := context.Background()
localWalletDir := setupWalletDir(t)
defaultWalletPath = localWalletDir
strongPass := "29384283xasjasd32%%&*@*#*"
// We attempt to create the wallet.
w, err := accounts.CreateWalletWithKeymanager(ctx, &accounts.CreateWalletConfig{
WalletCfg: &wallet.Config{
WalletDir: defaultWalletPath,
KeymanagerKind: keymanager.Derived,
WalletPassword: strongPass,
},
SkipMnemonicConfirm: true,
})
require.NoError(t, err)
km, err := w.InitializeKeymanager(ctx)
require.NoError(t, err)
s := &Server{
keymanager: km,
walletInitialized: true,
wallet: w,
}
numAccounts := 50
dr, ok := km.(*derived.Keymanager)
require.Equal(t, true, ok)
err = dr.RecoverAccountsFromMnemonic(ctx, testMnemonic, "", numAccounts)
require.NoError(t, err)
resp, err := s.ListAccounts(ctx, &pb.ListAccountsRequest{
PageSize: int32(numAccounts),
})
require.NoError(t, err)
require.Equal(t, len(resp.Accounts), numAccounts)
pubKeys := make([][]byte, numAccounts)
for i, aa := range resp.Accounts {
pubKeys[i] = aa.ValidatingPublicKey
}
// We now attempt to backup all public keys from the wallet.
res, err := s.BackupAccounts(context.Background(), &pb.BackupAccountsRequest{
PublicKeys: pubKeys,
BackupPassword: s.wallet.Password(),
})
require.NoError(t, err)
require.NotNil(t, res.ZipFile)
// Open a zip archive for reading.
buf := bytes.NewReader(res.ZipFile)
r, err := zip.NewReader(buf, int64(len(res.ZipFile)))
require.NoError(t, err)
require.Equal(t, len(pubKeys), len(r.File))
// Iterate through the files in the archive, checking they
// match the keystores we wanted to backup.
for i, f := range r.File {
keystoreFile, err := f.Open()
require.NoError(t, err)
encoded, err := ioutil.ReadAll(keystoreFile)
if err != nil {
require.NoError(t, keystoreFile.Close())
t.Fatal(err)
}
keystore := &keymanager.Keystore{}
if err := json.Unmarshal(encoded, &keystore); err != nil {
require.NoError(t, keystoreFile.Close())
t.Fatal(err)
}
assert.Equal(t, keystore.Pubkey, fmt.Sprintf("%x", pubKeys[i]))
require.NoError(t, keystoreFile.Close())
}
}