Skip to content

Commit

Permalink
Merge pull request #812 from cosmos/cwgoes/misc-cli-fixes
Browse files Browse the repository at this point in the history
Misc CLI fixes
  • Loading branch information
rigelrozanski authored Apr 11, 2018
2 parents 885b6dc + c741be1 commit 5212ac0
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ FEATURES:

* Add CacheContext

FEATURES:

* Add auto sequencing to client

## 0.14.1 (April 9, 2018)

BUG FIXES
Expand Down
55 changes: 54 additions & 1 deletion client/context/viper.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,80 @@
package context

import (
"encoding/json"
"fmt"
"github.com/spf13/viper"
"io/ioutil"

tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
rpcclient "github.com/tendermint/tendermint/rpc/client"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/cosmos/cosmos-sdk/client"
"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
if nodeURI != "" {
rpc = rpcclient.NewHTTP(nodeURI, "/websocket")
}
chainID := viper.GetString(client.FlagChainID)
// if chain ID is not specified manually, read default chain ID
if chainID == "" {
def, err := defaultChainID()
if err != nil {
chainID = def
}
}
return core.CoreContext{
ChainID: viper.GetString(client.FlagChainID),
ChainID: chainID,
Height: viper.GetInt64(client.FlagHeight),
TrustNode: viper.GetBool(client.FlagTrustNode),
FromAddressName: viper.GetString(client.FlagName),
NodeURI: nodeURI,
Sequence: viper.GetInt64(client.FlagSequence),
Client: rpc,
Decoder: nil,
AccountStore: "main",
}
}

// 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
}
23 changes: 23 additions & 0 deletions client/core/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package core

import (
rpcclient "github.com/tendermint/tendermint/rpc/client"

sdk "github.com/cosmos/cosmos-sdk/types"
)

type CoreContext struct {
Expand All @@ -12,40 +14,61 @@ type CoreContext struct {
FromAddressName string
Sequence int64
Client rpcclient.Client
Decoder sdk.AccountDecoder
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
c.Client = rpcclient.NewHTTP(nodeURI, "/websocket")
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
}
22 changes: 22 additions & 0 deletions client/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ func (ctx CoreContext) SignAndBuild(name, passphrase string, msg sdk.Msg, cdc *w

// build the Sign Messsage from the Standard Message
chainID := ctx.ChainID
if chainID == "" {
return nil, errors.Errorf("Chain ID required but not specified")
}
sequence := ctx.Sequence
signMsg := sdk.StdSignMsg{
ChainID: chainID,
Expand Down Expand Up @@ -137,6 +140,25 @@ func (ctx CoreContext) SignBuildBroadcast(name string, msg sdk.Msg, cdc *wire.Co
return ctx.BroadcastTx(txBytes)
}

// get the next sequence for the account address
func (c CoreContext) NextSequence(address []byte) (int64, error) {
if c.Decoder == nil {
return 0, errors.New("AccountDecoder required but not provided")
}

res, err := c.Query(address, c.AccountStore)
if err != nil {
return 0, err
}

account, err := c.Decoder(res)
if err != nil {
panic(err)
}

return account.GetSequence(), nil
}

// get passphrase from std input
func (ctx CoreContext) GetPassphraseFromStdin(name string) (pass string, err error) {
buf := client.BufferStdin()
Expand Down
17 changes: 15 additions & 2 deletions examples/democoin/x/cool/commands/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/wire"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands"

"github.com/cosmos/cosmos-sdk/examples/democoin/x/cool"
)
Expand All @@ -24,7 +25,7 @@ func QuizTxCmd(cdc *wire.Codec) *cobra.Command {
return errors.New("You must provide an answer")
}

ctx := context.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))

// get the from address from the name flag
from, err := ctx.GetFromAddress()
Expand All @@ -38,6 +39,12 @@ func QuizTxCmd(cdc *wire.Codec) *cobra.Command {
// get account name
name := viper.GetString(client.FlagName)

// default to next sequence number if none provided
ctx, err = context.EnsureSequence(ctx)
if err != nil {
return err
}

// build and sign the transaction, then broadcast to Tendermint
res, err := ctx.SignBuildBroadcast(name, msg, cdc)
if err != nil {
Expand All @@ -60,7 +67,7 @@ func SetTrendTxCmd(cdc *wire.Codec) *cobra.Command {
return errors.New("You must provide an answer")
}

ctx := context.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))

// get the from address from the name flag
from, err := ctx.GetFromAddress()
Expand All @@ -71,6 +78,12 @@ func SetTrendTxCmd(cdc *wire.Codec) *cobra.Command {
// get account name
name := viper.GetString(client.FlagName)

// default to next sequence number if none provided
ctx, err = context.EnsureSequence(ctx)
if err != nil {
return err
}

// create the message
msg := cool.NewSetTrendMsg(from, args[0])

Expand Down
9 changes: 8 additions & 1 deletion examples/democoin/x/pow/commands/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/cosmos/cosmos-sdk/examples/democoin/x/pow"
"github.com/cosmos/cosmos-sdk/wire"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands"
)

func MineCmd(cdc *wire.Codec) *cobra.Command {
Expand All @@ -24,7 +25,7 @@ func MineCmd(cdc *wire.Codec) *cobra.Command {

// get from address and parse arguments

ctx := context.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))

from, err := ctx.GetFromAddress()
if err != nil {
Expand Down Expand Up @@ -53,6 +54,12 @@ func MineCmd(cdc *wire.Codec) *cobra.Command {
// get account name
name := ctx.FromAddressName

// default to next sequence number if none provided
ctx, err = context.EnsureSequence(ctx)
if err != nil {
return err
}

// build and sign the transaction, then broadcast to Tendermint
res, err := ctx.SignBuildBroadcast(name, msg, cdc)
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion x/bank/commands/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands"
"github.com/cosmos/cosmos-sdk/x/bank"
)

Expand All @@ -36,7 +37,7 @@ type Commander struct {
}

func (c Commander) sendTxCmd(cmd *cobra.Command, args []string) error {
ctx := context.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(c.Cdc))

// get the from address
from, err := ctx.GetFromAddress()
Expand All @@ -62,6 +63,12 @@ func (c Commander) sendTxCmd(cmd *cobra.Command, args []string) error {
// build message
msg := BuildMsg(from, to, coins)

// default to next sequence number if none provided
ctx, err = context.EnsureSequence(ctx)
if err != nil {
return err
}

// build and sign the transaction, then broadcast to Tendermint
res, err := ctx.SignBuildBroadcast(ctx.FromAddressName, msg, c.Cdc)
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion x/ibc/commands/ibctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
wire "github.com/cosmos/cosmos-sdk/wire"

authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands"
"github.com/cosmos/cosmos-sdk/x/ibc"
)

Expand All @@ -39,7 +40,7 @@ type sendCommander struct {
}

func (c sendCommander) sendIBCTransfer(cmd *cobra.Command, args []string) error {
ctx := context.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(c.cdc))

// get the from address
from, err := ctx.GetFromAddress()
Expand All @@ -53,6 +54,12 @@ func (c sendCommander) sendIBCTransfer(cmd *cobra.Command, args []string) error
return err
}

// default to next sequence number if none provided
ctx, err = context.EnsureSequence(ctx)
if err != nil {
return err
}

// get password
res, err := ctx.SignBuildBroadcast(ctx.FromAddressName, msg, c.cdc)
if err != nil {
Expand Down
10 changes: 9 additions & 1 deletion x/simplestake/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands"
"github.com/cosmos/cosmos-sdk/x/simplestake"
)

Expand Down Expand Up @@ -94,7 +95,14 @@ func (co commander) unbondTxCmd(cmd *cobra.Command, args []string) error {
}

func (co commander) sendMsg(msg sdk.Msg) error {
ctx := context.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(co.cdc))

// default to next sequence number if none provided
ctx, err := context.EnsureSequence(ctx)
if err != nil {
return err
}

res, err := ctx.SignBuildBroadcast(ctx.FromAddressName, msg, co.cdc)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 5212ac0

Please sign in to comment.