2020-10-15 22:31:52 +00:00
|
|
|
package accounts
|
2020-08-20 17:53:09 +00:00
|
|
|
|
|
|
|
import (
|
2020-09-09 18:29:17 +00:00
|
|
|
"bytes"
|
2020-08-20 17:53:09 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-09-09 18:29:17 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2020-10-22 15:01:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
|
2020-08-20 17:53:09 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2020-09-09 18:29:17 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/cmd"
|
2020-08-20 17:53:09 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/promptutil"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts/prompt"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts/wallet"
|
2020-09-09 18:29:17 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/client"
|
2020-08-20 17:53:09 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/flags"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager"
|
2020-08-20 17:53:09 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-09-09 18:29:17 +00:00
|
|
|
"google.golang.org/grpc"
|
2020-08-20 17:53:09 +00:00
|
|
|
)
|
|
|
|
|
2020-09-09 18:29:17 +00:00
|
|
|
type performExitCfg struct {
|
|
|
|
validatorClient ethpb.BeaconNodeValidatorClient
|
|
|
|
nodeClient ethpb.NodeClient
|
2020-10-15 22:31:52 +00:00
|
|
|
keymanager keymanager.IKeymanager
|
2020-09-09 18:29:17 +00:00
|
|
|
rawPubKeys [][]byte
|
|
|
|
formattedPubKeys []string
|
|
|
|
}
|
|
|
|
|
2020-09-24 11:00:44 +00:00
|
|
|
const exitPassphrase = "Exit my validator"
|
|
|
|
|
2020-08-31 19:46:45 +00:00
|
|
|
// ExitAccountsCli performs a voluntary exit on one or more accounts.
|
|
|
|
func ExitAccountsCli(cliCtx *cli.Context, r io.Reader) error {
|
2020-09-09 18:29:17 +00:00
|
|
|
validatingPublicKeys, keymanager, err := prepareWallet(cliCtx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawPubKeys, formattedPubKeys, err := interact(cliCtx, r, validatingPublicKeys)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// User decided to cancel the voluntary exit.
|
|
|
|
if rawPubKeys == nil && formattedPubKeys == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
validatorClient, nodeClient, err := prepareClients(cliCtx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cfg := performExitCfg{
|
|
|
|
*validatorClient,
|
|
|
|
*nodeClient,
|
|
|
|
keymanager,
|
|
|
|
rawPubKeys,
|
|
|
|
formattedPubKeys,
|
|
|
|
}
|
|
|
|
|
|
|
|
formattedExitedKeys, err := performExit(cliCtx, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(formattedExitedKeys) > 0 {
|
|
|
|
log.WithField("publicKeys", strings.Join(formattedExitedKeys, ", ")).
|
|
|
|
Info("Voluntary exit was successful for the accounts listed")
|
|
|
|
} else {
|
|
|
|
log.Info("No successful voluntary exits")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 22:31:52 +00:00
|
|
|
func prepareWallet(cliCtx *cli.Context) ([][48]byte, keymanager.IKeymanager, error) {
|
2020-09-17 01:34:42 +00:00
|
|
|
w, err := wallet.OpenWalletOrElseCli(cliCtx, func(cliCtx *cli.Context) (*wallet.Wallet, error) {
|
2020-10-22 17:31:03 +00:00
|
|
|
return nil, wallet.ErrNoWalletFound
|
2020-08-31 19:46:45 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-09-09 18:29:17 +00:00
|
|
|
return nil, nil, errors.Wrap(err, "could not open wallet")
|
2020-08-20 17:53:09 +00:00
|
|
|
}
|
2020-08-20 19:14:03 +00:00
|
|
|
|
2020-11-16 22:26:04 +00:00
|
|
|
keymanager, err := w.InitializeKeymanager(cliCtx.Context)
|
2020-08-20 17:53:09 +00:00
|
|
|
if err != nil {
|
2020-09-09 18:29:17 +00:00
|
|
|
return nil, nil, errors.Wrap(err, "could not initialize keymanager")
|
2020-08-20 17:53:09 +00:00
|
|
|
}
|
2020-08-31 19:46:45 +00:00
|
|
|
validatingPublicKeys, err := keymanager.FetchValidatingPublicKeys(cliCtx.Context)
|
2020-08-20 17:53:09 +00:00
|
|
|
if err != nil {
|
2020-09-09 18:29:17 +00:00
|
|
|
return nil, nil, err
|
2020-08-20 17:53:09 +00:00
|
|
|
}
|
|
|
|
if len(validatingPublicKeys) == 0 {
|
2020-09-09 18:29:17 +00:00
|
|
|
return nil, nil, errors.New("wallet is empty, no accounts to perform voluntary exit")
|
2020-08-20 17:53:09 +00:00
|
|
|
}
|
2020-09-09 18:29:17 +00:00
|
|
|
|
|
|
|
return validatingPublicKeys, keymanager, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func interact(cliCtx *cli.Context, r io.Reader, validatingPublicKeys [][48]byte) ([][]byte, []string, error) {
|
2020-08-20 17:53:09 +00:00
|
|
|
// 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,
|
2020-09-17 01:34:42 +00:00
|
|
|
prompt.SelectAccountsVoluntaryExitPromptText,
|
2020-08-20 17:53:09 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2020-09-09 18:29:17 +00:00
|
|
|
return nil, nil, errors.Wrap(err, "could not filter public keys for voluntary exit")
|
2020-08-20 17:53:09 +00:00
|
|
|
}
|
2020-09-09 18:29:17 +00:00
|
|
|
rawPubKeys := make([][]byte, len(filteredPubKeys))
|
2020-08-20 17:53:09 +00:00
|
|
|
formattedPubKeys := make([]string, len(filteredPubKeys))
|
|
|
|
for i, pk := range filteredPubKeys {
|
|
|
|
pubKeyBytes := pk.Marshal()
|
2020-09-09 18:29:17 +00:00
|
|
|
rawPubKeys[i] = pubKeyBytes
|
2020-08-20 17:53:09 +00:00
|
|
|
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 := promptutil.ValidatePrompt(
|
|
|
|
r, fmt.Sprintf(promptText, au.BrightGreen(formattedPubKeys[0])), promptutil.ValidateYesOrNo,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2020-09-09 18:29:17 +00:00
|
|
|
return nil, nil, err
|
2020-08-20 17:53:09 +00:00
|
|
|
}
|
|
|
|
if strings.ToLower(resp) == "n" {
|
2020-09-09 18:29:17 +00:00
|
|
|
return nil, nil, nil
|
2020-08-20 17:53:09 +00:00
|
|
|
}
|
|
|
|
} 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 := promptutil.ValidatePrompt(r, promptText, promptutil.ValidateYesOrNo)
|
|
|
|
if err != nil {
|
2020-09-09 18:29:17 +00:00
|
|
|
return nil, nil, err
|
2020-08-20 17:53:09 +00:00
|
|
|
}
|
|
|
|
if strings.ToLower(resp) == "n" {
|
2020-09-09 18:29:17 +00:00
|
|
|
return nil, nil, nil
|
2020-08-20 17:53:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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:"
|
2020-09-27 22:12:28 +00:00
|
|
|
promptURL := au.Blue("https://docs.prylabs.network/docs/wallet/exiting-a-validator/#withdrawal-delay-warning")
|
2020-10-22 19:45:31 +00:00
|
|
|
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"
|
2020-08-20 17:53:09 +00:00
|
|
|
promptText := fmt.Sprintf("%s\n%s\n%s\n%s", promptHeader, promptDescription, promptURL, promptQuestion)
|
2020-09-24 11:00:44 +00:00
|
|
|
resp, err := promptutil.ValidatePrompt(r, promptText, func(input string) error {
|
|
|
|
return promptutil.ValidatePhrase(input, exitPassphrase)
|
|
|
|
})
|
2020-08-20 17:53:09 +00:00
|
|
|
if err != nil {
|
2020-09-09 18:29:17 +00:00
|
|
|
return nil, nil, err
|
2020-08-20 17:53:09 +00:00
|
|
|
}
|
|
|
|
if strings.ToLower(resp) == "n" {
|
2020-09-09 18:29:17 +00:00
|
|
|
return nil, nil, nil
|
2020-08-20 17:53:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 18:29:17 +00:00
|
|
|
return rawPubKeys, formattedPubKeys, nil
|
2020-08-20 17:53:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 18:29:17 +00:00
|
|
|
func prepareClients(cliCtx *cli.Context) (*ethpb.BeaconNodeValidatorClient, *ethpb.NodeClient, error) {
|
|
|
|
dialOpts := client.ConstructDialOptions(
|
2020-10-16 14:27:01 +00:00
|
|
|
cliCtx.Int(cmd.GrpcMaxCallRecvMsgSizeFlag.Name),
|
|
|
|
cliCtx.String(flags.CertFlag.Name),
|
|
|
|
strings.Split(cliCtx.String(flags.GrpcHeadersFlag.Name), ","),
|
|
|
|
cliCtx.Uint(flags.GrpcRetriesFlag.Name),
|
|
|
|
cliCtx.Duration(flags.GrpcRetryDelayFlag.Name),
|
2020-09-09 18:29:17 +00:00
|
|
|
)
|
|
|
|
if dialOpts == nil {
|
|
|
|
return nil, nil, errors.New("failed to construct dial options")
|
|
|
|
}
|
|
|
|
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 performExit(cliCtx *cli.Context, cfg performExitCfg) ([]string, error) {
|
|
|
|
var rawNotExitedKeys [][]byte
|
|
|
|
for i, key := range cfg.rawPubKeys {
|
|
|
|
if err := client.ProposeExit(cliCtx.Context, cfg.validatorClient, cfg.nodeClient, cfg.keymanager.Sign, key); err != nil {
|
|
|
|
rawNotExitedKeys = append(rawNotExitedKeys, key)
|
2020-10-22 15:01:11 +00:00
|
|
|
|
|
|
|
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])
|
|
|
|
}
|
2020-09-09 18:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var formattedExitedKeys []string
|
|
|
|
for i, key := range cfg.rawPubKeys {
|
|
|
|
found := false
|
|
|
|
for _, notExited := range rawNotExitedKeys {
|
|
|
|
if bytes.Equal(notExited, key) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
formattedExitedKeys = append(formattedExitedKeys, cfg.formattedPubKeys[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return formattedExitedKeys, nil
|
2020-08-20 17:53:09 +00:00
|
|
|
}
|