prysm-pulse/validator/accounts/accounts_exit.go
james-prysm aed6e13498
Web3Signer: CLI implementation (#10056)
* initial commit for cli integration of web3signer

* resolving conflicts and execution

* remove aggregation slot from proto

* rem aggregation slot

* define a sync message block root struct

* fix sync message name

* sync message block root struct

* amend where sync committee block root is used

* altered switch statement to return correct json request by type

* fixing fork data import, types, and unit tests

* reverting unwanted changes

* reverting more unwanted changes

* fixing deepsource issues

* fixing formatting

* more fixes for deepsource and code clean up

* only want to fetch once for fetch validating public keys

* adding more comments

* new unit tests for requests and fixing a mapper issue

* Update validator/client/validator.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update validator/accounts/wallet/wallet.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* adjusting comment

* adjusting comment

* fixing import organization

* including more unit tests

* adding new cli edit

* adding in checks for wallet initialize

* adding web3signer flags to main.go

* some how resolved files did not save correctly

* adding in check to make sure web flag only works with types imported and derived

* Update validator/client/sync_committee.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update validator/client/aggregate.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update validator/accounts/wallet/wallet.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update cmd/validator/wallet/wallet.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update cmd/validator/wallet/wallet.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update cmd/validator/main.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update cmd/validator/flags/flags.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update cmd/validator/flags/flags.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update cmd/validator/wallet/wallet.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update cmd/validator/wallet/wallet.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* reverting changes that accidently got checked in

* reverting

* reverting

* continuing to revert unintenteded changes

* reverting

* removing more unneeded changes

* addressing review comment

* initial refactor

* adding in more clarifying comments

* fixing mock

* resolving desource issues

* addressing gosec scan for helper go file

* addressing gosec

* trying to fix bazel build

* removal of interface to fix build

* fixing maligned struct

* addressing deepsource

* fixing deepsource

* addressing efficiency of type checking

* fixing bazel test failure

* fixing go linter errors

* gaz

* web changes

* add w3signer

* new kind

* proper use

* align

* adding prysm validator flags to help flags list

* addressing root comment

* ci lint

* fixing standardapi tests

* fixing accounts_test after removal of keymanager from rpc server

* fixing more unit tests

* Update cmd/validator/flags/flags.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update cmd/validator/flags/flags.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update validator/client/service.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update validator/client/service.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* addressing missed err checks

* fixing mock tests

* fixing gofmt

* unskipping minimal e2e test and removing related TODOs

* Update testing/endtoend/components/validator.go

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>

* Update testing/endtoend/components/validator.go

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>

* adding some error wrapers to clarify failure point

* fixing bazel build with new error checks

* taking preston's advice to make test fail faster to understand what's going on with the test

* checking if genesis validators root is not zero hash

* adding check for genesis validators root giving zero hash

* fixing missing dependency

* adding check for wallet

* log all

* fixing errors for http responses

* switching marshal to pretty print

* adding pretty sign request test

* fixing base url setting

* adding in check for web3signer and temporary wallet instead of having to open the wallet

* refactoring web3signer to not require wallet

* bazel build fix

* fixing gazelle build

* adding content type of request

* fixing more bazel

* removing unused code

* removing unused comments

* adding skip test back in

* addressing a validation and error message

* fix parse

* body

* fixing logic for datadir

* improving error handling

* show resp

* fix

* sign resp as str

* point of pointer remove

* sign resp

* unmarshal sig resp

* read body as str

* adding more verbose logging

* removing unused result

* fixing unit test

* reconfiguring files to properly nest code and mocks

* fix build issue

* using context when using client function calls

* fixing based on suggestion

* addressing comments

* gaz

* removing defined max timeout

* reverting json print pretty

* Update validator/accounts/wallet_edit.go

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>

* removing unneeded code restrictions

* should not introduce new code that may impact existing key manager types

* adjusting comments

* adding in json validation

* running go mod tidy

* some logging

* more logs

* fixing typo

* remove logs

* testing without byte trim

* fixing order or properties

* gaz

* tidy

* reverting some logs

* removing the confusing comments

* Update validator/client/aggregate.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update validator/client/aggregate.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* addressing pr comments

* editing bytes test

* Run gazelle update-repos

* run gazelle

* improving unit test coverage

* fixing text

* fixing a potential escaped error

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
2022-01-31 10:44:17 -06:00

303 lines
10 KiB
Go

package accounts
import (
"bytes"
"context"
"fmt"
"io"
"strings"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"
grpcutil "github.com/prysmaticlabs/prysm/api/grpc"
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/cmd"
"github.com/prysmaticlabs/prysm/cmd/validator/flags"
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
"github.com/prysmaticlabs/prysm/config/params"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/io/prompt"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/validator/accounts/iface"
"github.com/prysmaticlabs/prysm/validator/accounts/userprompt"
"github.com/prysmaticlabs/prysm/validator/accounts/wallet"
"github.com/prysmaticlabs/prysm/validator/client"
"github.com/prysmaticlabs/prysm/validator/keymanager"
"github.com/urfave/cli/v2"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"
)
// PerformExitCfg for account voluntary exits.
type PerformExitCfg struct {
ValidatorClient ethpb.BeaconNodeValidatorClient
NodeClient ethpb.NodeClient
Keymanager keymanager.IKeymanager
RawPubKeys [][]byte
FormattedPubKeys []string
}
const exitPassphrase = "Exit my validator"
// ExitAccountsCli performs a voluntary exit on one or more accounts.
func ExitAccountsCli(cliCtx *cli.Context, r io.Reader) error {
validatingPublicKeys, kManager, err := prepareWallet(cliCtx)
if err != nil {
return err
}
rawPubKeys, trimmedPubKeys, err := interact(cliCtx, r, validatingPublicKeys)
if err != nil {
return err
}
// User decided to cancel the voluntary exit.
if rawPubKeys == nil && trimmedPubKeys == nil {
return nil
}
validatorClient, nodeClient, err := prepareClients(cliCtx)
if err != nil {
return err
}
if nodeClient == nil {
return errors.New("could not prepare beacon node client")
}
syncStatus, err := (*nodeClient).GetSyncStatus(cliCtx.Context, &emptypb.Empty{})
if err != nil {
return err
}
if syncStatus == nil {
return errors.New("could not get sync status")
}
if (*syncStatus).Syncing {
return errors.New("could not perform exit: beacon node is syncing.")
}
cfg := PerformExitCfg{
*validatorClient,
*nodeClient,
kManager,
rawPubKeys,
trimmedPubKeys,
}
rawExitedKeys, trimmedExitedKeys, err := PerformVoluntaryExit(cliCtx.Context, cfg)
if err != nil {
return err
}
displayExitInfo(rawExitedKeys, trimmedExitedKeys)
return nil
}
// PerformVoluntaryExit uses gRPC clients to submit a voluntary exit message to a beacon node.
func PerformVoluntaryExit(
ctx context.Context, cfg PerformExitCfg,
) (rawExitedKeys [][]byte, formattedExitedKeys []string, err error) {
var rawNotExitedKeys [][]byte
for i, key := range cfg.RawPubKeys {
if err := client.ProposeExit(ctx, cfg.ValidatorClient, cfg.NodeClient, cfg.Keymanager.Sign, key); err != nil {
rawNotExitedKeys = append(rawNotExitedKeys, key)
msg := err.Error()
if strings.Contains(msg, blocks.ValidatorAlreadyExitedMsg) ||
strings.Contains(msg, blocks.ValidatorCannotExitYetMsg) {
log.Warningf("Could not perform voluntary exit for account %s: %s", cfg.FormattedPubKeys[i], msg)
} else {
log.WithError(err).Errorf("voluntary exit failed for account %s", cfg.FormattedPubKeys[i])
}
}
}
rawExitedKeys = make([][]byte, 0)
formattedExitedKeys = make([]string, 0)
for i, key := range cfg.RawPubKeys {
found := false
for _, notExited := range rawNotExitedKeys {
if bytes.Equal(notExited, key) {
found = true
break
}
}
if !found {
rawExitedKeys = append(rawExitedKeys, key)
formattedExitedKeys = append(formattedExitedKeys, cfg.FormattedPubKeys[i])
}
}
return rawExitedKeys, formattedExitedKeys, nil
}
func prepareWallet(cliCtx *cli.Context) (validatingPublicKeys [][fieldparams.BLSPubkeyLength]byte, km keymanager.IKeymanager, err error) {
w, err := wallet.OpenWalletOrElseCli(cliCtx, func(cliCtx *cli.Context) (*wallet.Wallet, error) {
return nil, wallet.ErrNoWalletFound
})
if err != nil {
return nil, nil, errors.Wrap(err, "could not open wallet")
}
// TODO(#9883) - Remove this when we have a better way to handle this.
if w.KeymanagerKind() == keymanager.Web3Signer {
return nil, nil, errors.New(
"web3signer wallets cannot exit accounts through cli command yet. please perform this on the remote signer node",
)
}
km, err = w.InitializeKeymanager(cliCtx.Context, iface.InitKeymanagerConfig{ListenForChanges: false})
if err != nil {
return nil, nil, errors.Wrap(err, ErrCouldNotInitializeKeymanager)
}
validatingPublicKeys, err = km.FetchValidatingPublicKeys(cliCtx.Context)
if err != nil {
return nil, nil, err
}
if len(validatingPublicKeys) == 0 {
return nil, nil, errors.New("wallet is empty, no accounts to perform voluntary exit")
}
return validatingPublicKeys, km, nil
}
func interact(
cliCtx *cli.Context,
r io.Reader,
validatingPublicKeys [][fieldparams.BLSPubkeyLength]byte,
) (rawPubKeys [][]byte, formattedPubKeys []string, err error) {
if !cliCtx.IsSet(flags.ExitAllFlag.Name) {
// Allow the user to interactively select the accounts to exit or optionally
// provide them via cli flags as a string of comma-separated, hex strings.
filteredPubKeys, err := filterPublicKeysFromUserInput(
cliCtx,
flags.VoluntaryExitPublicKeysFlag,
validatingPublicKeys,
userprompt.SelectAccountsVoluntaryExitPromptText,
)
if err != nil {
return nil, nil, errors.Wrap(err, "could not filter public keys for voluntary exit")
}
rawPubKeys = make([][]byte, len(filteredPubKeys))
formattedPubKeys = make([]string, len(filteredPubKeys))
for i, pk := range filteredPubKeys {
pubKeyBytes := pk.Marshal()
rawPubKeys[i] = pubKeyBytes
formattedPubKeys[i] = fmt.Sprintf("%#x", bytesutil.Trunc(pubKeyBytes))
}
allAccountStr := strings.Join(formattedPubKeys, ", ")
if !cliCtx.IsSet(flags.VoluntaryExitPublicKeysFlag.Name) {
if len(filteredPubKeys) == 1 {
promptText := "Are you sure you want to perform a voluntary exit on 1 account? (%s) Y/N"
resp, err := prompt.ValidatePrompt(
r, fmt.Sprintf(promptText, au.BrightGreen(formattedPubKeys[0])), prompt.ValidateYesOrNo,
)
if err != nil {
return nil, nil, err
}
if strings.EqualFold(resp, "n") {
return nil, nil, nil
}
} else {
promptText := "Are you sure you want to perform a voluntary exit on %d accounts? (%s) Y/N"
if len(filteredPubKeys) == len(validatingPublicKeys) {
promptText = fmt.Sprintf(
"Are you sure you want to perform a voluntary exit on all accounts? Y/N (%s)",
au.BrightGreen(allAccountStr))
} else {
promptText = fmt.Sprintf(promptText, len(filteredPubKeys), au.BrightGreen(allAccountStr))
}
resp, err := prompt.ValidatePrompt(r, promptText, prompt.ValidateYesOrNo)
if err != nil {
return nil, nil, err
}
if strings.EqualFold(resp, "n") {
return nil, nil, nil
}
}
}
} else {
rawPubKeys, formattedPubKeys = prepareAllKeys(validatingPublicKeys)
fmt.Printf("About to perform a voluntary exit of %d accounts\n", len(rawPubKeys))
}
promptHeader := au.Red("===============IMPORTANT===============")
promptDescription := "Withdrawing funds is not possible in Phase 0 of the system. " +
"Please navigate to the following website and make sure you understand the current implications " +
"of a voluntary exit before making the final decision:"
promptURL := au.Blue("https://docs.prylabs.network/docs/wallet/exiting-a-validator/#withdrawal-delay-warning")
promptQuestion := "If you still want to continue with the voluntary exit, please input a phrase found at the end " +
"of the page from the above URL"
promptText := fmt.Sprintf("%s\n%s\n%s\n%s", promptHeader, promptDescription, promptURL, promptQuestion)
resp, err := prompt.ValidatePrompt(r, promptText, func(input string) error {
return prompt.ValidatePhrase(input, exitPassphrase)
})
if err != nil {
return nil, nil, err
}
if strings.EqualFold(resp, "n") {
return nil, nil, nil
}
return rawPubKeys, formattedPubKeys, nil
}
func prepareAllKeys(validatingKeys [][fieldparams.BLSPubkeyLength]byte) (raw [][]byte, formatted []string) {
raw = make([][]byte, len(validatingKeys))
formatted = make([]string, len(validatingKeys))
for i, pk := range validatingKeys {
raw[i] = make([]byte, len(pk))
copy(raw[i], pk[:])
formatted[i] = fmt.Sprintf("%#x", bytesutil.Trunc(pk[:]))
}
return
}
func prepareClients(cliCtx *cli.Context) (*ethpb.BeaconNodeValidatorClient, *ethpb.NodeClient, error) {
dialOpts := client.ConstructDialOptions(
cliCtx.Int(cmd.GrpcMaxCallRecvMsgSizeFlag.Name),
cliCtx.String(flags.CertFlag.Name),
cliCtx.Uint(flags.GrpcRetriesFlag.Name),
cliCtx.Duration(flags.GrpcRetryDelayFlag.Name),
)
if dialOpts == nil {
return nil, nil, errors.New("failed to construct dial options")
}
grpcHeaders := strings.Split(cliCtx.String(flags.GrpcHeadersFlag.Name), ",")
cliCtx.Context = grpcutil.AppendHeaders(cliCtx.Context, grpcHeaders)
conn, err := grpc.DialContext(cliCtx.Context, cliCtx.String(flags.BeaconRPCProviderFlag.Name), dialOpts...)
if err != nil {
return nil, nil, errors.Wrapf(err, "could not dial endpoint %s", flags.BeaconRPCProviderFlag.Name)
}
validatorClient := ethpb.NewBeaconNodeValidatorClient(conn)
nodeClient := ethpb.NewNodeClient(conn)
return &validatorClient, &nodeClient, nil
}
func displayExitInfo(rawExitedKeys [][]byte, trimmedExitedKeys []string) {
if len(rawExitedKeys) > 0 {
urlFormattedPubKeys := make([]string, len(rawExitedKeys))
for i, key := range rawExitedKeys {
var baseUrl string
if params.BeaconConfig().ConfigName == params.ConfigNames[params.Pyrmont] {
baseUrl = "https://pyrmont.beaconcha.in/validator/"
} else if params.BeaconConfig().ConfigName == params.ConfigNames[params.Prater] {
baseUrl = "https://prater.beaconcha.in/validator/"
} else {
baseUrl = "https://beaconcha.in/validator/"
}
// Remove '0x' prefix
urlFormattedPubKeys[i] = baseUrl + hexutil.Encode(key)[2:]
}
ifaceKeys := make([]interface{}, len(urlFormattedPubKeys))
for i, k := range urlFormattedPubKeys {
ifaceKeys[i] = k
}
info := fmt.Sprintf("Voluntary exit was successful for the accounts listed. "+
"URLs where you can track each validator's exit:\n"+strings.Repeat("%s\n", len(ifaceKeys)), ifaceKeys...)
log.WithField("publicKeys", strings.Join(trimmedExitedKeys, ", ")).Info(info)
} else {
log.Info("No successful voluntary exits")
}
}