2020-09-04 00:58:36 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-01 03:37:38 +00:00
|
|
|
"fmt"
|
2020-10-10 02:07:28 +00:00
|
|
|
"path/filepath"
|
2020-09-04 00:58:36 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-10-02 21:35:28 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2020-09-04 00:58:36 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/validator/accounts/v2"
|
2020-10-02 21:35:28 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-09-04 00:58:36 +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"
|
2020-10-27 20:51:29 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts/iface"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts/wallet"
|
2020-10-10 02:07:28 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/flags"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/derived"
|
2020-09-04 00:58:36 +00:00
|
|
|
)
|
|
|
|
|
2020-10-10 02:07:28 +00:00
|
|
|
var defaultWalletPath = filepath.Join(flags.DefaultValidatorDir(), flags.WalletDefaultDirName)
|
2020-10-02 21:35:28 +00:00
|
|
|
|
|
|
|
type mockAccountCreator struct {
|
|
|
|
data *ethpb.Deposit_Data
|
|
|
|
pubKey []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockAccountCreator) CreateAccount(ctx context.Context) ([]byte, *ethpb.Deposit_Data, error) {
|
|
|
|
return m.pubKey, m.data, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 00:58:36 +00:00
|
|
|
func TestServer_CreateAccount(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
localWalletDir := setupWalletDir(t)
|
|
|
|
defaultWalletPath = localWalletDir
|
|
|
|
strongPass := "29384283xasjasd32%%&*@*#*"
|
|
|
|
// We attempt to create the wallet.
|
2020-10-15 22:31:52 +00:00
|
|
|
w, err := accounts.CreateWalletWithKeymanager(ctx, &accounts.CreateWalletConfig{
|
2020-09-17 01:34:42 +00:00
|
|
|
WalletCfg: &wallet.Config{
|
2020-09-04 00:58:36 +00:00
|
|
|
WalletDir: defaultWalletPath,
|
2020-10-15 22:31:52 +00:00
|
|
|
KeymanagerKind: keymanager.Derived,
|
2020-09-04 00:58:36 +00:00
|
|
|
WalletPassword: strongPass,
|
|
|
|
},
|
|
|
|
SkipMnemonicConfirm: true,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2020-10-27 20:51:29 +00:00
|
|
|
km, err := w.InitializeKeymanager(ctx, &iface.InitializeKeymanagerConfig{
|
|
|
|
SkipMnemonicConfirm: true,
|
|
|
|
})
|
2020-09-04 00:58:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
s := &Server{
|
|
|
|
keymanager: km,
|
|
|
|
walletInitialized: true,
|
2020-09-17 01:34:42 +00:00
|
|
|
wallet: w,
|
2020-09-04 00:58:36 +00:00
|
|
|
}
|
2020-09-29 01:58:14 +00:00
|
|
|
_, err = s.CreateAccount(ctx, &pb.CreateAccountRequest{})
|
2020-09-04 00:58:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_ListAccounts(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
localWalletDir := setupWalletDir(t)
|
|
|
|
defaultWalletPath = localWalletDir
|
|
|
|
strongPass := "29384283xasjasd32%%&*@*#*"
|
|
|
|
// We attempt to create the wallet.
|
2020-10-15 22:31:52 +00:00
|
|
|
w, err := accounts.CreateWalletWithKeymanager(ctx, &accounts.CreateWalletConfig{
|
2020-09-17 01:34:42 +00:00
|
|
|
WalletCfg: &wallet.Config{
|
2020-09-04 00:58:36 +00:00
|
|
|
WalletDir: defaultWalletPath,
|
2020-10-15 22:31:52 +00:00
|
|
|
KeymanagerKind: keymanager.Derived,
|
2020-09-04 00:58:36 +00:00
|
|
|
WalletPassword: strongPass,
|
|
|
|
},
|
|
|
|
SkipMnemonicConfirm: true,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2020-10-27 20:51:29 +00:00
|
|
|
km, err := w.InitializeKeymanager(ctx, &iface.InitializeKeymanagerConfig{
|
|
|
|
SkipMnemonicConfirm: true,
|
|
|
|
})
|
2020-09-04 00:58:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
s := &Server{
|
|
|
|
keymanager: km,
|
|
|
|
walletInitialized: true,
|
2020-09-17 01:34:42 +00:00
|
|
|
wallet: w,
|
2020-09-04 00:58:36 +00:00
|
|
|
}
|
2020-10-03 22:26:39 +00:00
|
|
|
numAccounts := 50
|
2020-09-04 00:58:36 +00:00
|
|
|
keys := make([][]byte, numAccounts)
|
|
|
|
for i := 0; i < numAccounts; i++ {
|
2020-10-02 21:35:28 +00:00
|
|
|
key, _, err := km.(*derived.Keymanager).CreateAccount(ctx)
|
2020-09-04 00:58:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
keys[i] = key
|
|
|
|
}
|
2020-10-03 22:26:39 +00:00
|
|
|
resp, err := s.ListAccounts(ctx, &pb.ListAccountsRequest{
|
|
|
|
PageSize: int32(numAccounts),
|
|
|
|
})
|
2020-09-04 00:58:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(resp.Accounts), numAccounts)
|
|
|
|
for i := 0; i < numAccounts; i++ {
|
|
|
|
assert.DeepEqual(t, resp.Accounts[i].ValidatingPublicKey, keys[i])
|
|
|
|
}
|
2020-10-03 22:26:39 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2020-09-04 00:58:36 +00:00
|
|
|
}
|
2020-10-01 03:37:38 +00:00
|
|
|
|
2020-10-02 21:35:28 +00:00
|
|
|
func Test_createAccountWithDepositData(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
pubKey := bls.RandKey().PublicKey().Marshal()
|
|
|
|
m := &mockAccountCreator{
|
|
|
|
data: ðpb.Deposit_Data{
|
|
|
|
PublicKey: pubKey,
|
|
|
|
WithdrawalCredentials: make([]byte, 32),
|
|
|
|
Amount: params.BeaconConfig().MaxEffectiveBalance,
|
|
|
|
Signature: make([]byte, 96),
|
|
|
|
},
|
|
|
|
pubKey: pubKey,
|
|
|
|
}
|
|
|
|
rawResp, err := createAccountWithDepositData(ctx, m)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.DeepEqual(
|
|
|
|
t, rawResp.Data["pubkey"], fmt.Sprintf("%x", pubKey),
|
|
|
|
)
|
|
|
|
assert.DeepEqual(
|
|
|
|
t, rawResp.Data["withdrawal_credentials"], fmt.Sprintf("%x", make([]byte, 32)),
|
|
|
|
)
|
|
|
|
assert.DeepEqual(
|
|
|
|
t, rawResp.Data["amount"], fmt.Sprintf("%d", params.BeaconConfig().MaxEffectiveBalance),
|
|
|
|
)
|
|
|
|
assert.DeepEqual(
|
|
|
|
t, rawResp.Data["signature"], fmt.Sprintf("%x", make([]byte, 96)),
|
|
|
|
)
|
|
|
|
assert.DeepEqual(
|
|
|
|
t, rawResp.Data["fork_version"], fmt.Sprintf("%x", params.BeaconConfig().GenesisForkVersion),
|
|
|
|
)
|
|
|
|
}
|