Skip to content

Commit

Permalink
Add --list-validator-indices flag (#8520)
Browse files Browse the repository at this point in the history
* Add --list-validator-indices flag

* Fix style issues

* Print pubkeys alongside the validator indexes

* Improve list-indices output

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
Co-authored-by: terence tsao <terence@prysmaticlabs.com>
  • Loading branch information
3 people authored Feb 26, 2021
1 parent e39ce36 commit 878bc15
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions validator/accounts/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ go_test(
"@com_github_golang_mock//gomock:go_default_library",
"@com_github_google_uuid//:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prysmaticlabs_eth2_types//:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_sirupsen_logrus//hooks/test:go_default_library",
Expand Down
35 changes: 35 additions & 0 deletions validator/accounts/accounts_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package accounts
import (
"context"
"fmt"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"math"
"path/filepath"
"strings"

Expand Down Expand Up @@ -37,6 +39,16 @@ func ListAccountsCli(cliCtx *cli.Context) error {
}
showDepositData := cliCtx.Bool(flags.ShowDepositDataFlag.Name)
showPrivateKeys := cliCtx.Bool(flags.ShowPrivateKeysFlag.Name)
listIndices := cliCtx.Bool(flags.ListValidatorIndices.Name)

if listIndices {
client, _, err := prepareClients(cliCtx)
if err != nil {
return err
}
return listValidatorIndices(cliCtx.Context, km, *client)
}

switch w.KeymanagerKind() {
case keymanager.Imported:
km, ok := km.(*imported.Keymanager)
Expand Down Expand Up @@ -221,3 +233,26 @@ func listRemoteKeymanagerAccounts(
}
return nil
}

func listValidatorIndices(ctx context.Context, km keymanager.IKeymanager, client ethpb.BeaconNodeValidatorClient) error {
pubKeys, err := km.FetchValidatingPublicKeys(ctx)
if err != nil {
return errors.Wrap(err, "could not get validating public keys")
}
var pks [][]byte
for i := range pubKeys {
pks = append(pks, pubKeys[i][:])
}
req := &ethpb.MultipleValidatorStatusRequest{PublicKeys: pks}
resp, err := client.MultipleValidatorStatus(ctx, req)
if err != nil {
return errors.Wrap(err, "could not request validator indices")
}
fmt.Println(au.BrightGreen("Validator indices:").Bold())
for i, idx := range resp.Indices {
if idx != math.MaxUint64 {
fmt.Printf("%#x: %d\n", pubKeys[i][0:4], idx)
}
}
return nil
}
57 changes: 57 additions & 0 deletions validator/accounts/accounts_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ package accounts
import (
"context"
"fmt"
"github.com/golang/mock/gomock"
types "github.com/prysmaticlabs/eth2-types"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/mock"
"io/ioutil"
"math"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -472,3 +477,55 @@ func TestListAccounts_RemoteKeymanager(t *testing.T) {
assert.Equal(t, true, keyFound, "Public Key %s not found on line number %d", keyString, lineNumber)
}
}

func TestListAccounts_ListValidatorIndices(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

numAccounts := 3
pubKeys := make([][48]byte, numAccounts)
pks := make([][]byte, numAccounts)

for i := 0; i < numAccounts; i++ {
key := make([]byte, 48)
copy(key, strconv.Itoa(i))
pubKeys[i] = bytesutil.ToBytes48(key)
pks[i] = key
}

km := &mockRemoteKeymanager{
publicKeys: pubKeys,
}

rescueStdout := os.Stdout
r, writer, err := os.Pipe()
require.NoError(t, err)
os.Stdout = writer

m := mock.NewMockBeaconNodeValidatorClient(ctrl)

req := &ethpb.MultipleValidatorStatusRequest{PublicKeys: pks}
resp := &ethpb.MultipleValidatorStatusResponse{Indices: []types.ValidatorIndex{1, math.MaxUint64, 2}}

m.
EXPECT().
MultipleValidatorStatus(gomock.Eq(context.Background()), gomock.Eq(req)).
Return(resp, nil)

require.NoError(
t,
listValidatorIndices(
context.Background(),
km,
m,
),
)

require.NoError(t, writer.Close())
out, err := ioutil.ReadAll(r)
require.NoError(t, err)
os.Stdout = rescueStdout

expectedStdout := au.BrightGreen("Validator indices:").Bold().String() + "\n0x30000000: 1\n0x32000000: 2\n"
require.Equal(t, expectedStdout, string(out))
}
7 changes: 7 additions & 0 deletions validator/accounts/cmd_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ var AccountCommands = &cli.Command{
flags.WalletPasswordFileFlag,
flags.ShowDepositDataFlag,
flags.ShowPrivateKeysFlag,
flags.ListValidatorIndices,
flags.BeaconRPCProviderFlag,
cmd.GrpcMaxCallRecvMsgSizeFlag,
flags.CertFlag,
flags.GrpcHeadersFlag,
flags.GrpcRetriesFlag,
flags.GrpcRetryDelayFlag,
featureconfig.Mainnet,
featureconfig.PyrmontTestnet,
featureconfig.ToledoTestnet,
Expand Down
6 changes: 6 additions & 0 deletions validator/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ var (
Usage: "Display the private keys for validator accounts",
Value: false,
}
// ListValidatorIndices for accounts.
ListValidatorIndices = &cli.BoolFlag{
Name: "list-validator-indices",
Usage: "List validator indices",
Value: false,
}
// NumAccountsFlag defines the amount of accounts to generate for derived wallets.
NumAccountsFlag = &cli.IntFlag{
Name: "num-accounts",
Expand Down

0 comments on commit 878bc15

Please sign in to comment.