Accounts V2: Change fatal logs to return errors (#6672)

* Change logging out errors to returns
* Merge branch 'master' of github.com:prysmaticlabs/prysm into change-logs-to-return
* Lint
* Merge branch 'master' into change-logs-to-return
This commit is contained in:
Ivan Martinez 2020-07-21 22:04:08 -04:00 committed by GitHub
parent d1a828ad87
commit 8a65af168d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 84 additions and 54 deletions

View File

@ -23,7 +23,12 @@ this command outputs a deposit data string which is required to become a validat
flags.PasswordFileFlag,
flags.SkipMnemonicConfirmFlag,
},
Action: NewAccount,
Action: func(cliCtx *cli.Context) error {
if err := NewAccount(cliCtx); err != nil {
log.WithError(err).Fatal("Could not create new account")
}
return nil
},
},
{
Name: "list",
@ -33,7 +38,12 @@ this command outputs a deposit data string which is required to become a validat
flags.WalletPasswordsDirFlag,
flags.ShowDepositDataFlag,
},
Action: ListAccounts,
Action: func(cliCtx *cli.Context) error {
if err := ListAccounts(cliCtx); err != nil {
log.WithError(err).Fatal("Could not list accounts")
}
return nil
},
},
{
Name: "export",
@ -44,7 +54,12 @@ this command outputs a deposit data string which is required to become a validat
flags.BackupPathFlag,
flags.AccountsFlag,
},
Action: ExportAccount,
Action: func(cliCtx *cli.Context) error {
if err := ExportAccount(cliCtx); err != nil {
log.WithError(err).Fatal("Could not export accounts")
}
return nil
},
},
{
Name: "import",
@ -55,7 +70,12 @@ this command outputs a deposit data string which is required to become a validat
flags.BackupPathFlag,
flags.PasswordFileFlag,
},
Action: ImportAccount,
Action: func(cliCtx *cli.Context) error {
if err := ImportAccount(cliCtx); err != nil {
log.WithError(err).Fatal("Could not import accounts")
}
return nil
},
},
},
}

View File

@ -24,7 +24,12 @@ var WalletCommands = &cli.Command{
flags.RemoteSignerKeyPathFlag,
flags.RemoteSignerCACertPathFlag,
},
Action: CreateWallet,
Action: func(cliCtx *cli.Context) error {
if err := CreateWallet(cliCtx); err != nil {
log.WithError(err).Fatal("Could not create a wallet")
}
return nil
},
},
{
Name: "edit-config",
@ -36,7 +41,12 @@ var WalletCommands = &cli.Command{
flags.RemoteSignerKeyPathFlag,
flags.RemoteSignerCACertPathFlag,
},
Action: EditWalletConfiguration,
Action: func(cliCtx *cli.Context) error {
if err := EditWalletConfiguration(cliCtx); err != nil {
log.WithError(err).Fatal("Could not edit wallet configuration")
}
return nil
},
},
},
}

View File

@ -26,14 +26,14 @@ func ExportAccount(cliCtx *cli.Context) error {
// Read a wallet's directory from user input.
walletDir, err := inputWalletDir(cliCtx)
if errors.Is(err, ErrNoWalletFound) {
log.Fatal("no wallet found, create a new one with ./prysm.sh validator wallet-v2 create")
return errors.New("no wallet found, create a new one with ./prysm.sh validator wallet-v2 create")
} else if err != nil {
log.WithError(err).Fatal("Could not parse wallet directory")
return errors.Wrap(err, "could not parse wallet directory")
}
outputDir, err := inputExportDir(cliCtx)
if err != nil {
log.WithError(err).Fatal("Could not parse output directory")
return errors.Wrap(err, "could not parse output directory")
}
wallet, err := OpenWallet(context.Background(), &WalletConfig{
@ -41,16 +41,16 @@ func ExportAccount(cliCtx *cli.Context) error {
WalletDir: walletDir,
})
if err != nil {
log.WithError(err).Fatal("Could not open wallet")
return errors.Wrap(err, "could not open wallet")
}
allAccounts, err := wallet.AccountNames()
if err != nil {
log.WithError(err).Fatal("Could not get account names")
return errors.Wrap(err, "could not get account names")
}
accounts, err := selectAccounts(cliCtx, allAccounts)
if err != nil {
log.WithError(err).Fatal("Could not select accounts")
return errors.Wrap(err, "could not select accounts")
}
if len(accounts) == 0 {
return errors.New("no accounts to export")
@ -61,7 +61,7 @@ func ExportAccount(cliCtx *cli.Context) error {
}
if err := logAccountsExported(wallet, accounts); err != nil {
log.WithError(err).Fatal("Could not log out exported accounts")
return errors.Wrap(err, "could not log out exported accounts")
}
return nil

View File

@ -29,12 +29,12 @@ func ImportAccount(cliCtx *cli.Context) error {
backupDir, err := inputImportDir(cliCtx)
if err != nil {
log.Fatalf("Could not parse output directory: %v", err)
return errors.Wrap(err, "could not parse output directory")
}
accountsImported, err := unzipArchiveToTarget(backupDir, walletDir)
if err != nil {
log.WithError(err).Fatal("Could not unzip archive")
return errors.Wrap(err, "could not unzip archive")
}
au := aurora.NewAurora(true)
@ -53,16 +53,16 @@ func ImportAccount(cliCtx *cli.Context) error {
WalletDir: walletDir,
})
if err != nil {
log.Fatalf("Could not open wallet: %v", err)
return errors.Wrap(err, "could not open wallet")
}
for _, accountName := range accountsImported {
if err := wallet.enterPasswordForAccount(cliCtx, accountName); err != nil {
log.WithError(err).Fatal("Could not set account password")
return errors.Wrap(err, "could not set account password")
}
}
if err := logAccountsImported(wallet, accountsImported); err != nil {
log.WithError(err).Fatal("Could not log accounts imported")
return errors.Wrap(err, "could not log accounts imported")
}
return nil

View File

@ -32,32 +32,32 @@ func ListAccounts(cliCtx *cli.Context) error {
CanUnlockAccounts: false,
})
if err != nil {
log.Fatalf("Could not read wallet at specified path %s: %v", walletDir, err)
return errors.Wrapf(err, "could not read wallet at specified path %s", walletDir)
}
keymanager, err := wallet.InitializeKeymanager(ctx, false /* skipMnemonicConfirm */)
if err != nil {
log.Fatalf("Could not initialize keymanager: %v", err)
return errors.Wrap(err, "could not initialize keymanager")
}
showDepositData := cliCtx.Bool(flags.ShowDepositDataFlag.Name)
switch wallet.KeymanagerKind() {
case v2keymanager.Direct:
km, ok := keymanager.(*direct.Keymanager)
if !ok {
log.Fatal("Could not assert keymanager interface to concrete type")
return errors.New("could not assert keymanager interface to concrete type")
}
if err := listDirectKeymanagerAccounts(showDepositData, wallet, km); err != nil {
log.Fatalf("Could not list validator accounts with direct keymanager: %v", err)
return errors.Wrap(err, "could not list validator accounts with direct keymanager")
}
case v2keymanager.Derived:
km, ok := keymanager.(*derived.Keymanager)
if !ok {
log.Fatal("Could not assert keymanager interface to concrete type")
return errors.New("could not assert keymanager interface to concrete type")
}
if err := listDerivedKeymanagerAccounts(showDepositData, wallet, km); err != nil {
log.Fatalf("Could not list validator accounts with derived keymanager: %v", err)
return errors.Wrap(err, "could not list validator accounts with derived keymanager")
}
default:
log.Fatalf("Keymanager kind %s not yet supported", wallet.KeymanagerKind().String())
return fmt.Errorf("keymanager kind %s not yet supported", wallet.KeymanagerKind().String())
}
return nil
}

View File

@ -37,19 +37,19 @@ func NewAccount(cliCtx *cli.Context) error {
// Read a wallet's directory from user input.
walletDir, err := inputWalletDir(cliCtx)
if errors.Is(err, ErrNoWalletFound) {
log.Fatal("No wallet found, create a new one with ./prysm.sh validator wallet-v2 create")
return errors.New("no wallet found, create a new one with ./prysm.sh validator wallet-v2 create")
} else if err != nil {
log.Fatal(err)
return errors.Wrap(err, "could not get wallet directory")
}
ctx := context.Background()
keymanagerKind, err := readKeymanagerKindFromWalletPath(walletDir)
if err != nil {
log.Fatal(err)
return errors.Wrap(err, "could not get keymanager kind")
}
// Only direct keymanagers can create accounts for now.
if keymanagerKind == v2keymanager.Remote {
log.Fatal("Cannot create a new account for a remote keymanager")
return errors.New("cannot create a new account for a remote keymanager")
}
// Read the directory for password storage from user input.
passwordsDirPath := inputPasswordsDirectory(cliCtx)
@ -60,25 +60,25 @@ func NewAccount(cliCtx *cli.Context) error {
KeymanagerKind: keymanagerKind,
})
if err != nil {
log.Fatalf("Could not open wallet: %v", err)
return errors.Wrap(err, "could not open wallet")
}
// We initialize a new keymanager depending on the wallet's keymanager kind.
skipMnemonicConfirm := cliCtx.Bool(flags.SkipMnemonicConfirmFlag.Name)
keymanager, err := wallet.InitializeKeymanager(ctx, skipMnemonicConfirm)
if err != nil {
log.Fatalf("Could not initialize keymanager: %v", err)
return errors.Wrap(err, "could not initialize keymanager")
}
// Read the new account's password from user input.
password, err := inputNewAccountPassword(cliCtx)
if err != nil {
log.Fatalf("Could not read password: %v", err)
return errors.Wrap(err, "could not read password")
}
// Create a new validator account using the specified keymanager.
if _, err := keymanager.CreateAccount(ctx, password); err != nil {
log.Fatalf("Could not create account in wallet: %v", err)
return errors.Wrap(err, "could not create account in wallet")
}
return nil
}
@ -186,7 +186,7 @@ func inputNewAccountPassword(cliCtx *cli.Context) (string, error) {
}
enteredPassword := string(data)
if err := validatePasswordInput(enteredPassword); err != nil {
log.WithError(err).Fatal("Password did not pass validation")
return "", errors.Wrap(err, "password did not pass validation")
}
return enteredPassword, nil
}

View File

@ -22,17 +22,17 @@ func CreateWallet(cliCtx *cli.Context) error {
// Read a wallet's directory from user input.
walletDir, err := inputWalletDir(cliCtx)
if err != nil && !errors.Is(err, ErrNoWalletFound) {
log.Fatalf("Could not parse wallet directory: %v", err)
return errors.Wrap(err, "could not parse wallet directory")
}
// Check if the user has a wallet at the specified path.
// If a user does not have a wallet, we instantiate one
// based on specified options.
walletExists, err := hasDir(walletDir)
if err != nil {
log.Fatal(err)
return errors.Wrap(err, "could not check if wallet directory exists")
}
if walletExists {
log.Fatal(
return errors.New(
"You already have a wallet at the specified path. You can " +
"edit your wallet configuration by running ./prysm.sh validator wallet-v2 edit",
)
@ -40,12 +40,12 @@ func CreateWallet(cliCtx *cli.Context) error {
// Determine the desired keymanager kind for the wallet from user input.
keymanagerKind, err := inputKeymanagerKind(cliCtx)
if err != nil {
log.Fatalf("Could not select keymanager kind: %v", err)
return errors.Wrap(err, "could not select keymanager kind")
}
switch keymanagerKind {
case v2keymanager.Direct:
if err = createDirectWallet(cliCtx, walletDir); err != nil {
log.Fatalf("Could not initialize wallet with direct keymanager: %v", err)
return errors.Wrap(err, "could not initialize wallet with direct keymanager")
}
log.WithField("wallet-path", walletDir).Infof(
"Successfully created wallet with on-disk keymanager configuration. " +
@ -53,7 +53,7 @@ func CreateWallet(cliCtx *cli.Context) error {
)
case v2keymanager.Derived:
if err = createDerivedWallet(cliCtx, walletDir); err != nil {
log.Fatalf("Could not initialize wallet with derived keymanager: %v", err)
return errors.Wrap(err, "could not initialize wallet with derived keymanager")
}
log.WithField("wallet-path", walletDir).Infof(
"Successfully created HD wallet and saved configuration to disk. " +
@ -61,13 +61,13 @@ func CreateWallet(cliCtx *cli.Context) error {
)
case v2keymanager.Remote:
if err = createRemoteWallet(cliCtx, walletDir); err != nil {
log.Fatalf("Could not initialize wallet with remote keymanager: %v", err)
return errors.Wrap(err, "could not initialize wallet with remote keymanager")
}
log.WithField("wallet-path", walletDir).Infof(
"Successfully created wallet with remote keymanager configuration",
)
default:
log.Fatalf("Keymanager type %s is not supported", keymanagerKind)
return errors.Wrap(err, "keymanager type %s is not supported")
}
return nil
}

View File

@ -17,14 +17,14 @@ func EditWalletConfiguration(cliCtx *cli.Context) error {
// Read a wallet's directory from user input.
walletDir, err := inputWalletDir(cliCtx)
if errors.Is(err, ErrNoWalletFound) {
log.Fatal("No wallet found, create a new one with ./prysm.sh validator wallet-v2 create")
return errors.New("no wallet found, create a new one with ./prysm.sh validator wallet-v2 create")
} else if err != nil {
log.Fatal("Could not parse wallet directory")
return errors.Wrap(err, "could not parse wallet directory")
}
// Determine the keymanager kind for the wallet.
keymanagerKind, err := readKeymanagerKindFromWalletPath(walletDir)
if err != nil {
log.Fatalf("Could not select keymanager kind: %v", err)
return errors.Wrap(err, "could not select keymanager kind")
}
ctx := context.Background()
wallet, err := OpenWallet(ctx, &WalletConfig{
@ -33,37 +33,37 @@ func EditWalletConfiguration(cliCtx *cli.Context) error {
KeymanagerKind: keymanagerKind,
})
if err != nil {
log.Fatalf("Could not open wallet: %v", err)
return errors.Wrap(err, "could not open wallet")
}
switch keymanagerKind {
case v2keymanager.Direct:
log.Fatal("No configuration options available to edit for direct keymanager")
return errors.New("no configuration options available to edit for direct keymanager")
case v2keymanager.Derived:
log.Fatal("Derived keymanager is not yet supported")
return errors.New("derived keymanager is not yet supported")
case v2keymanager.Remote:
enc, err := wallet.ReadKeymanagerConfigFromDisk(ctx)
if err != nil {
log.Fatalf("Could not read: %v", err)
return errors.Wrap(err, "could not read config")
}
cfg, err := remote.UnmarshalConfigFile(enc)
if err != nil {
log.Fatalf("Could not unmarshal: %v", err)
return errors.Wrap(err, "could not unmarshal config")
}
log.Infof("Current configuration")
fmt.Printf("%s\n", cfg)
newCfg, err := inputRemoteKeymanagerConfig(cliCtx)
if err != nil {
log.Fatal(err)
return errors.Wrap(err, "could not get keymanager config")
}
encodedCfg, err := remote.MarshalConfigFile(ctx, newCfg)
if err != nil {
log.Fatal(err)
return errors.Wrap(err, "could not marshal config file")
}
if err := wallet.WriteKeymanagerConfigToDisk(ctx, encodedCfg); err != nil {
log.Fatal(err)
return errors.Wrap(err, "could not write config to disk")
}
default:
log.Fatalf("Keymanager type %s is not supported", keymanagerKind)
return fmt.Errorf("keymanager type %s is not supported", keymanagerKind)
}
return nil
}