From c7b680a545937ba8b614eb24f09c77f0a33aa06f Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Tue, 10 Apr 2018 11:15:34 +0200 Subject: [PATCH] Address PR comments --- client/context/viper.go | 63 +++++++++++++++---------- client/core/context.go | 9 ++++ examples/democoin/x/cool/commands/tx.go | 4 +- examples/democoin/x/pow/commands/tx.go | 2 +- x/bank/commands/sendtx.go | 2 +- x/ibc/commands/ibctx.go | 2 +- x/simplestake/commands/commands.go | 2 +- x/stake/commands/tx.go | 8 ++-- 8 files changed, 57 insertions(+), 35 deletions(-) diff --git a/client/context/viper.go b/client/context/viper.go index acc6b391c7..193fe9baa6 100644 --- a/client/context/viper.go +++ b/client/context/viper.go @@ -14,6 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/client/core" ) +// NewCoreContextFromViper - return a new context with parameters from the command line func NewCoreContextFromViper() core.CoreContext { nodeURI := viper.GetString(client.FlagNode) var rpc rpcclient.Client @@ -21,19 +22,11 @@ func NewCoreContextFromViper() core.CoreContext { rpc = rpcclient.NewHTTP(nodeURI, "/websocket") } chainID := viper.GetString(client.FlagChainID) - // if chain ID is not specified manually, read chain ID from genesis file if present + // if chain ID is not specified manually, read default chain ID if chainID == "" { - cfg, err := tcmd.ParseConfig() - if err == nil { - genesisFile := cfg.GenesisFile() - bz, err := ioutil.ReadFile(genesisFile) - if err == nil { - var doc tmtypes.GenesisDoc - err = json.Unmarshal(bz, &doc) - if err == nil { - chainID = doc.ChainID - } - } + def, err := defaultChainID() + if err != nil { + chainID = def } } return core.CoreContext{ @@ -49,19 +42,39 @@ func NewCoreContextFromViper() core.CoreContext { } } -// Automatically set sequence number -func AutoSequence(ctx core.CoreContext) (core.CoreContext, error) { - if !viper.IsSet(client.FlagSequence) { - from, err := ctx.GetFromAddress() - if err != nil { - return ctx, err - } - seq, err := ctx.NextSequence(from) - if err != nil { - return ctx, err - } - fmt.Printf("Defaulting to next sequence number: %d\n", seq) - ctx = ctx.WithSequence(seq) +// read chain ID from genesis file, if present +func defaultChainID() (string, error) { + cfg, err := tcmd.ParseConfig() + if err != nil { + return "", err } + genesisFile := cfg.GenesisFile() + bz, err := ioutil.ReadFile(genesisFile) + if err != nil { + return "", err + } + var doc tmtypes.GenesisDoc + err = json.Unmarshal(bz, &doc) + if err != nil { + return "", err + } + return doc.ChainID, nil +} + +// EnsureSequence - automatically set sequence number if none provided +func EnsureSequence(ctx core.CoreContext) (core.CoreContext, error) { + if viper.IsSet(client.FlagSequence) { + return ctx, nil + } + from, err := ctx.GetFromAddress() + if err != nil { + return ctx, err + } + seq, err := ctx.NextSequence(from) + if err != nil { + return ctx, err + } + fmt.Printf("Defaulting to next sequence number: %d\n", seq) + ctx = ctx.WithSequence(seq) return ctx, nil } diff --git a/client/core/context.go b/client/core/context.go index d56af5a816..34376ece4f 100644 --- a/client/core/context.go +++ b/client/core/context.go @@ -18,46 +18,55 @@ type CoreContext struct { AccountStore string } +// WithChainID - return a copy of the context with an updated chainID func (c CoreContext) WithChainID(chainID string) CoreContext { c.ChainID = chainID return c } +// WithHeight - eturn a copy of the context with an updated height func (c CoreContext) WithHeight(height int64) CoreContext { c.Height = height return c } +// WithTrustNode - return a copy of the context with an updated TrustNode flag func (c CoreContext) WithTrustNode(trustNode bool) CoreContext { c.TrustNode = trustNode return c } +// WithNodeURI - return a copy of the context with an updated node URI func (c CoreContext) WithNodeURI(nodeURI string) CoreContext { c.NodeURI = nodeURI return c } +// WithFromAddressName - return a copy of the context with an updated from address func (c CoreContext) WithFromAddressName(fromAddressName string) CoreContext { c.FromAddressName = fromAddressName return c } +// WithSequence - return a copy of the context with an updated sequence number func (c CoreContext) WithSequence(sequence int64) CoreContext { c.Sequence = sequence return c } +// WithClient - return a copy of the context with an updated RPC client instance func (c CoreContext) WithClient(client rpcclient.Client) CoreContext { c.Client = client return c } +// WithDecoder - return a copy of the context with an updated Decoder func (c CoreContext) WithDecoder(decoder sdk.AccountDecoder) CoreContext { c.Decoder = decoder return c } +// WithAccountStore - return a copy of the context with an updated AccountStore func (c CoreContext) WithAccountStore(accountStore string) CoreContext { c.AccountStore = accountStore return c diff --git a/examples/democoin/x/cool/commands/tx.go b/examples/democoin/x/cool/commands/tx.go index 8ce009f0ff..88c5e4e68a 100644 --- a/examples/democoin/x/cool/commands/tx.go +++ b/examples/democoin/x/cool/commands/tx.go @@ -40,7 +40,7 @@ func QuizTxCmd(cdc *wire.Codec) *cobra.Command { name := viper.GetString(client.FlagName) // default to next sequence number if none provided - ctx, err = context.AutoSequence(ctx) + ctx, err = context.EnsureSequence(ctx) if err != nil { return err } @@ -79,7 +79,7 @@ func SetTrendTxCmd(cdc *wire.Codec) *cobra.Command { name := viper.GetString(client.FlagName) // default to next sequence number if none provided - ctx, err = context.AutoSequence(ctx) + ctx, err = context.EnsureSequence(ctx) if err != nil { return err } diff --git a/examples/democoin/x/pow/commands/tx.go b/examples/democoin/x/pow/commands/tx.go index abd5d27ebf..5fa11a4766 100644 --- a/examples/democoin/x/pow/commands/tx.go +++ b/examples/democoin/x/pow/commands/tx.go @@ -55,7 +55,7 @@ func MineCmd(cdc *wire.Codec) *cobra.Command { name := ctx.FromAddressName // default to next sequence number if none provided - ctx, err = context.AutoSequence(ctx) + ctx, err = context.EnsureSequence(ctx) if err != nil { return err } diff --git a/x/bank/commands/sendtx.go b/x/bank/commands/sendtx.go index a37e969a63..56048262d7 100644 --- a/x/bank/commands/sendtx.go +++ b/x/bank/commands/sendtx.go @@ -64,7 +64,7 @@ func (c Commander) sendTxCmd(cmd *cobra.Command, args []string) error { msg := BuildMsg(from, to, coins) // default to next sequence number if none provided - ctx, err = context.AutoSequence(ctx) + ctx, err = context.EnsureSequence(ctx) if err != nil { return err } diff --git a/x/ibc/commands/ibctx.go b/x/ibc/commands/ibctx.go index ee0ac02eea..d17b40b21e 100644 --- a/x/ibc/commands/ibctx.go +++ b/x/ibc/commands/ibctx.go @@ -55,7 +55,7 @@ func (c sendCommander) sendIBCTransfer(cmd *cobra.Command, args []string) error } // default to next sequence number if none provided - ctx, err = context.AutoSequence(ctx) + ctx, err = context.EnsureSequence(ctx) if err != nil { return err } diff --git a/x/simplestake/commands/commands.go b/x/simplestake/commands/commands.go index 0e8a7a9bb4..ba66020283 100644 --- a/x/simplestake/commands/commands.go +++ b/x/simplestake/commands/commands.go @@ -98,7 +98,7 @@ func (co commander) sendMsg(msg sdk.Msg) error { ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(co.cdc)) // default to next sequence number if none provided - ctx, err := context.AutoSequence(ctx) + ctx, err := context.EnsureSequence(ctx) if err != nil { return err } diff --git a/x/stake/commands/tx.go b/x/stake/commands/tx.go index 1b9063a65b..5cc9747b61 100644 --- a/x/stake/commands/tx.go +++ b/x/stake/commands/tx.go @@ -96,7 +96,7 @@ func GetCmdDeclareCandidacy(cdc *wire.Codec) *cobra.Command { ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc)) // default to next sequence number if none provided - ctx, err = context.AutoSequence(ctx) + ctx, err = context.EnsureSequence(ctx) if err != nil { return err } @@ -140,7 +140,7 @@ func GetCmdEditCandidacy(cdc *wire.Codec) *cobra.Command { ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc)) // default to next sequence number if none provided - ctx, err = context.AutoSequence(ctx) + ctx, err = context.EnsureSequence(ctx) if err != nil { return err } @@ -183,7 +183,7 @@ func GetCmdDelegate(cdc *wire.Codec) *cobra.Command { ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc)) // default to next sequence number if none provided - ctx, err = context.AutoSequence(ctx) + ctx, err = context.EnsureSequence(ctx) if err != nil { return err } @@ -237,7 +237,7 @@ func GetCmdUnbond(cdc *wire.Codec) *cobra.Command { ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc)) // default to next sequence number if none provided - ctx, err = context.AutoSequence(ctx) + ctx, err = context.EnsureSequence(ctx) if err != nil { return err }