From 6a742c877363f4961caeb58fd385f417972ca820 Mon Sep 17 00:00:00 2001 From: ValarDragon Date: Tue, 17 Jul 2018 21:39:32 -0700 Subject: [PATCH] client: On any tx that broadcasts a tx, check that acct exists This subverts the really hard to decipher ABCI error message, with a nice user friendly error message. --- CHANGELOG.md | 3 +++ client/context/helpers.go | 5 +++++ client/context/viper.go | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f143fcaf47..2967a649db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ BREAKING CHANGES * [x/stake] Fixed the period check for the inflation calculation +IMPROVEMENTS +* [cli] Improve error messages for all txs when the account doesn't exist + ## 0.22.0 *July 16th, 2018* diff --git a/client/context/helpers.go b/client/context/helpers.go index 00ff8a81c7..7742dfe03a 100644 --- a/client/context/helpers.go +++ b/client/context/helpers.go @@ -186,6 +186,11 @@ func (ctx CoreContext) SignAndBuild(name, passphrase string, msgs []sdk.Msg, cdc // sign and build the transaction from the msg func (ctx CoreContext) ensureSignBuild(name string, msgs []sdk.Msg, cdc *wire.Codec) (tyBytes []byte, err error) { + err = EnsureAccountExists(ctx, name) + if err != nil { + return nil, err + } + ctx, err = EnsureAccountNumber(ctx) if err != nil { return nil, err diff --git a/client/context/viper.go b/client/context/viper.go index 611ad1b92f..6c7646079e 100644 --- a/client/context/viper.go +++ b/client/context/viper.go @@ -3,6 +3,7 @@ package context import ( "fmt" + "github.com/pkg/errors" "github.com/spf13/viper" tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" @@ -10,6 +11,9 @@ import ( tmtypes "github.com/tendermint/tendermint/types" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/keys" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" ) // NewCoreContextFromViper - return a new context with parameters from the command line @@ -68,6 +72,36 @@ func defaultChainID() (string, error) { return doc.ChainID, nil } +// EnsureAccountExists - Make sure account exists +func EnsureAccountExists(ctx CoreContext, name string) error { + keybase, err := keys.GetKeyBase() + if err != nil { + return err + } + + if name == "" { + return errors.Errorf("must provide a from address name") + } + + info, err := keybase.Get(name) + if err != nil { + return errors.Errorf("no key for: %s", name) + } + + accAddr := sdk.AccAddress(info.GetPubKey().Address()) + + Acc, err := ctx.QueryStore(auth.AddressStoreKey(accAddr), ctx.AccountStore) + if err != nil { + return err + } + + // Check if account was found + if Acc == nil { + return errors.Errorf("No account with address %s was found in the state.\nAre you sure there has been a transaction involving it?", accAddr) + } + return nil +} + // EnsureAccount - automatically set account number if none provided func EnsureAccountNumber(ctx CoreContext) (CoreContext, error) { // Should be viper.IsSet, but this does not work - https://github.com/spf13/viper/pull/331