Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cwgoes committed Apr 10, 2018
1 parent 7383c99 commit c7b680a
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 35 deletions.
63 changes: 38 additions & 25 deletions client/context/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,19 @@ 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
if nodeURI != "" {
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{
Expand All @@ -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
}
9 changes: 9 additions & 0 deletions client/core/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions examples/democoin/x/cool/commands/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion examples/democoin/x/pow/commands/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion x/bank/commands/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion x/ibc/commands/ibctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion x/simplestake/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions x/stake/commands/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit c7b680a

Please sign in to comment.