From a2afd37a97998d717678421a570af896557a4629 Mon Sep 17 00:00:00 2001 From: Jim McDonald Date: Tue, 31 Jan 2023 15:25:40 +0000 Subject: [PATCH] Provide better error messages when offline preparation file cannot be read. --- CHANGELOG.md | 1 + cmd/validator/credentials/set/chaininfo.go | 18 +++++++++--------- cmd/validator/credentials/set/process.go | 6 +++--- cmd/validator/exit/chaininfo.go | 18 +++++++++--------- util/account.go | 4 ++-- 5 files changed, 24 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e70ebc1..610fb16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ dev: - support additional mnemonic word list languages - increase minimum timeout for commands that fetch all validators to 2 minutes + - provide better error messages when offline preparation file cannot be read 1.27.1: - fix issue with voluntary exits using incorrect domain (thanks to @0xTylerHolmes) diff --git a/cmd/validator/credentials/set/chaininfo.go b/cmd/validator/credentials/set/chaininfo.go index b539912..345a86a 100644 --- a/cmd/validator/credentials/set/chaininfo.go +++ b/cmd/validator/credentials/set/chaininfo.go @@ -19,22 +19,22 @@ import ( "fmt" "os" - "github.com/pkg/errors" "github.com/wealdtech/ethdo/beacon" ) // obtainChainInfo obtains the chain information required to create a withdrawal credentials change operation. func (c *command) obtainChainInfo(ctx context.Context) error { + var err error // Use the offline preparation file if present (and we haven't been asked to recreate it). if !c.prepareOffline { - err := c.obtainChainInfoFromFile(ctx) - if err == nil { + if err = c.obtainChainInfoFromFile(ctx); err == nil { return nil } } if c.offline { - return fmt.Errorf("%s is unavailable or outdated; this is required to have been previously generated using --offline-preparation on an online machine and be readable in the directory in which this command is being run", offlinePreparationFilename) + // If we are here it means that we are offline without chain information, and cannot continue. + return fmt.Errorf("failed to obtain offline preparation file: %w", err) } if err := c.obtainChainInfoFromNode(ctx); err != nil { @@ -51,7 +51,7 @@ func (c *command) obtainChainInfoFromFile(_ context.Context) error { if c.debug { fmt.Fprintf(os.Stderr, "Failed to read offline preparation file: %v\n", err) } - return errors.Wrap(err, fmt.Sprintf("cannot find %s", offlinePreparationFilename)) + return err } if c.debug { @@ -60,16 +60,16 @@ func (c *command) obtainChainInfoFromFile(_ context.Context) error { data, err := os.ReadFile(offlinePreparationFilename) if err != nil { if c.debug { - fmt.Fprintf(os.Stderr, "failed to load chain state: %v\n", err) + fmt.Fprintf(os.Stderr, "failed to load offline preparation file: %v\n", err) } - return errors.Wrap(err, "failed to read offline preparation file") + return err } c.chainInfo = &beacon.ChainInfo{} if err := json.Unmarshal(data, c.chainInfo); err != nil { if c.debug { - fmt.Fprintf(os.Stderr, "chain state invalid: %v\n", err) + fmt.Fprintf(os.Stderr, "offline preparation file invalid: %v\n", err) } - return errors.Wrap(err, "failed to parse offline preparation file") + return err } return nil diff --git a/cmd/validator/credentials/set/process.go b/cmd/validator/credentials/set/process.go index e702fc5..c849241 100644 --- a/cmd/validator/credentials/set/process.go +++ b/cmd/validator/credentials/set/process.go @@ -250,14 +250,14 @@ func (c *command) generateOperationsFromMnemonic(ctx context.Context) error { } func (c *command) generateOperationsFromAccountAndWithdrawalAccount(ctx context.Context) error { - validatorAccount, err := util.ParseAccount(ctx, c.account, nil, true) + validatorAccount, err := util.ParseAccount(ctx, c.account, nil, false) if err != nil { - return err + return errors.Wrap(err, "failed to obtain validator account") } withdrawalAccount, err := util.ParseAccount(ctx, c.withdrawalAccount, c.passphrases, true) if err != nil { - return err + return errors.Wrap(err, "failed to obtain withdrawal account") } validatorPubkey, err := util.BestPublicKey(validatorAccount) diff --git a/cmd/validator/exit/chaininfo.go b/cmd/validator/exit/chaininfo.go index 29b8cb7..a9230f6 100644 --- a/cmd/validator/exit/chaininfo.go +++ b/cmd/validator/exit/chaininfo.go @@ -19,22 +19,22 @@ import ( "fmt" "os" - "github.com/pkg/errors" "github.com/wealdtech/ethdo/beacon" ) // obtainChainInfo obtains the chain information required to create an exit operation. func (c *command) obtainChainInfo(ctx context.Context) error { + var err error // Use the offline preparation file if present (and we haven't been asked to recreate it). if !c.prepareOffline { - err := c.obtainChainInfoFromFile(ctx) - if err == nil { + if err = c.obtainChainInfoFromFile(ctx); err == nil { return nil } } if c.offline { - return fmt.Errorf("%s is unavailable or outdated; this is required to have been previously generated using --offline-preparation on an online machine and be readable in the directory in which this command is being run", offlinePreparationFilename) + // If we are here it means that we are offline without chain information, and cannot continue. + return fmt.Errorf("failed to obtain offline preparation file: %w", err) } if err := c.obtainChainInfoFromNode(ctx); err != nil { @@ -51,7 +51,7 @@ func (c *command) obtainChainInfoFromFile(_ context.Context) error { if c.debug { fmt.Fprintf(os.Stderr, "Failed to read offline preparation file: %v\n", err) } - return errors.Wrap(err, fmt.Sprintf("cannot find %s", offlinePreparationFilename)) + return err } if c.debug { @@ -60,16 +60,16 @@ func (c *command) obtainChainInfoFromFile(_ context.Context) error { data, err := os.ReadFile(offlinePreparationFilename) if err != nil { if c.debug { - fmt.Fprintf(os.Stderr, "failed to load chain state: %v\n", err) + fmt.Fprintf(os.Stderr, "failed to load offline preparation file: %v\n", err) } - return errors.Wrap(err, "failed to read offline preparation file") + return err } c.chainInfo = &beacon.ChainInfo{} if err := json.Unmarshal(data, c.chainInfo); err != nil { if c.debug { - fmt.Fprintf(os.Stderr, "chain state invalid: %v\n", err) + fmt.Fprintf(os.Stderr, "offline preparation file invalid: %v\n", err) } - return errors.Wrap(err, "failed to parse offline preparation file") + return err } return nil diff --git a/util/account.go b/util/account.go index 6b84e0b..7d49d95 100644 --- a/util/account.go +++ b/util/account.go @@ -66,7 +66,7 @@ func ParseAccount(ctx context.Context, if unlock { _, err = UnlockAccount(ctx, account, nil) if err != nil { - return nil, errors.Wrap(err, "failed to unlock account") + return nil, err } } default: @@ -82,7 +82,7 @@ func ParseAccount(ctx context.Context, // Supplementary will be the unlock passphrase(s). _, err = UnlockAccount(ctx, account, supplementary) if err != nil { - return nil, errors.Wrap(err, "failed to unlock account") + return nil, err } } case strings.Contains(accountStr, " "):