Address PR comments
This commit is contained in:
parent
7383c99026
commit
c7b680a545
|
@ -14,6 +14,7 @@ import (
|
||||||
"github.com/cosmos/cosmos-sdk/client/core"
|
"github.com/cosmos/cosmos-sdk/client/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// NewCoreContextFromViper - return a new context with parameters from the command line
|
||||||
func NewCoreContextFromViper() core.CoreContext {
|
func NewCoreContextFromViper() core.CoreContext {
|
||||||
nodeURI := viper.GetString(client.FlagNode)
|
nodeURI := viper.GetString(client.FlagNode)
|
||||||
var rpc rpcclient.Client
|
var rpc rpcclient.Client
|
||||||
|
@ -21,19 +22,11 @@ func NewCoreContextFromViper() core.CoreContext {
|
||||||
rpc = rpcclient.NewHTTP(nodeURI, "/websocket")
|
rpc = rpcclient.NewHTTP(nodeURI, "/websocket")
|
||||||
}
|
}
|
||||||
chainID := viper.GetString(client.FlagChainID)
|
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 == "" {
|
if chainID == "" {
|
||||||
cfg, err := tcmd.ParseConfig()
|
def, err := defaultChainID()
|
||||||
if err == nil {
|
if err != nil {
|
||||||
genesisFile := cfg.GenesisFile()
|
chainID = def
|
||||||
bz, err := ioutil.ReadFile(genesisFile)
|
|
||||||
if err == nil {
|
|
||||||
var doc tmtypes.GenesisDoc
|
|
||||||
err = json.Unmarshal(bz, &doc)
|
|
||||||
if err == nil {
|
|
||||||
chainID = doc.ChainID
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return core.CoreContext{
|
return core.CoreContext{
|
||||||
|
@ -49,19 +42,39 @@ func NewCoreContextFromViper() core.CoreContext {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Automatically set sequence number
|
// read chain ID from genesis file, if present
|
||||||
func AutoSequence(ctx core.CoreContext) (core.CoreContext, error) {
|
func defaultChainID() (string, error) {
|
||||||
if !viper.IsSet(client.FlagSequence) {
|
cfg, err := tcmd.ParseConfig()
|
||||||
from, err := ctx.GetFromAddress()
|
if err != nil {
|
||||||
if err != nil {
|
return "", err
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
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
|
return ctx, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,46 +18,55 @@ type CoreContext struct {
|
||||||
AccountStore string
|
AccountStore string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithChainID - return a copy of the context with an updated chainID
|
||||||
func (c CoreContext) WithChainID(chainID string) CoreContext {
|
func (c CoreContext) WithChainID(chainID string) CoreContext {
|
||||||
c.ChainID = chainID
|
c.ChainID = chainID
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithHeight - eturn a copy of the context with an updated height
|
||||||
func (c CoreContext) WithHeight(height int64) CoreContext {
|
func (c CoreContext) WithHeight(height int64) CoreContext {
|
||||||
c.Height = height
|
c.Height = height
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithTrustNode - return a copy of the context with an updated TrustNode flag
|
||||||
func (c CoreContext) WithTrustNode(trustNode bool) CoreContext {
|
func (c CoreContext) WithTrustNode(trustNode bool) CoreContext {
|
||||||
c.TrustNode = trustNode
|
c.TrustNode = trustNode
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithNodeURI - return a copy of the context with an updated node URI
|
||||||
func (c CoreContext) WithNodeURI(nodeURI string) CoreContext {
|
func (c CoreContext) WithNodeURI(nodeURI string) CoreContext {
|
||||||
c.NodeURI = nodeURI
|
c.NodeURI = nodeURI
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithFromAddressName - return a copy of the context with an updated from address
|
||||||
func (c CoreContext) WithFromAddressName(fromAddressName string) CoreContext {
|
func (c CoreContext) WithFromAddressName(fromAddressName string) CoreContext {
|
||||||
c.FromAddressName = fromAddressName
|
c.FromAddressName = fromAddressName
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithSequence - return a copy of the context with an updated sequence number
|
||||||
func (c CoreContext) WithSequence(sequence int64) CoreContext {
|
func (c CoreContext) WithSequence(sequence int64) CoreContext {
|
||||||
c.Sequence = sequence
|
c.Sequence = sequence
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithClient - return a copy of the context with an updated RPC client instance
|
||||||
func (c CoreContext) WithClient(client rpcclient.Client) CoreContext {
|
func (c CoreContext) WithClient(client rpcclient.Client) CoreContext {
|
||||||
c.Client = client
|
c.Client = client
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithDecoder - return a copy of the context with an updated Decoder
|
||||||
func (c CoreContext) WithDecoder(decoder sdk.AccountDecoder) CoreContext {
|
func (c CoreContext) WithDecoder(decoder sdk.AccountDecoder) CoreContext {
|
||||||
c.Decoder = decoder
|
c.Decoder = decoder
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithAccountStore - return a copy of the context with an updated AccountStore
|
||||||
func (c CoreContext) WithAccountStore(accountStore string) CoreContext {
|
func (c CoreContext) WithAccountStore(accountStore string) CoreContext {
|
||||||
c.AccountStore = accountStore
|
c.AccountStore = accountStore
|
||||||
return c
|
return c
|
||||||
|
|
|
@ -40,7 +40,7 @@ func QuizTxCmd(cdc *wire.Codec) *cobra.Command {
|
||||||
name := viper.GetString(client.FlagName)
|
name := viper.GetString(client.FlagName)
|
||||||
|
|
||||||
// default to next sequence number if none provided
|
// default to next sequence number if none provided
|
||||||
ctx, err = context.AutoSequence(ctx)
|
ctx, err = context.EnsureSequence(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ func SetTrendTxCmd(cdc *wire.Codec) *cobra.Command {
|
||||||
name := viper.GetString(client.FlagName)
|
name := viper.GetString(client.FlagName)
|
||||||
|
|
||||||
// default to next sequence number if none provided
|
// default to next sequence number if none provided
|
||||||
ctx, err = context.AutoSequence(ctx)
|
ctx, err = context.EnsureSequence(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,7 +55,7 @@ func MineCmd(cdc *wire.Codec) *cobra.Command {
|
||||||
name := ctx.FromAddressName
|
name := ctx.FromAddressName
|
||||||
|
|
||||||
// default to next sequence number if none provided
|
// default to next sequence number if none provided
|
||||||
ctx, err = context.AutoSequence(ctx)
|
ctx, err = context.EnsureSequence(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,7 +64,7 @@ func (c Commander) sendTxCmd(cmd *cobra.Command, args []string) error {
|
||||||
msg := BuildMsg(from, to, coins)
|
msg := BuildMsg(from, to, coins)
|
||||||
|
|
||||||
// default to next sequence number if none provided
|
// default to next sequence number if none provided
|
||||||
ctx, err = context.AutoSequence(ctx)
|
ctx, err = context.EnsureSequence(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,7 +55,7 @@ func (c sendCommander) sendIBCTransfer(cmd *cobra.Command, args []string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// default to next sequence number if none provided
|
// default to next sequence number if none provided
|
||||||
ctx, err = context.AutoSequence(ctx)
|
ctx, err = context.EnsureSequence(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,7 +98,7 @@ func (co commander) sendMsg(msg sdk.Msg) error {
|
||||||
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(co.cdc))
|
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(co.cdc))
|
||||||
|
|
||||||
// default to next sequence number if none provided
|
// default to next sequence number if none provided
|
||||||
ctx, err := context.AutoSequence(ctx)
|
ctx, err := context.EnsureSequence(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,7 +96,7 @@ func GetCmdDeclareCandidacy(cdc *wire.Codec) *cobra.Command {
|
||||||
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
|
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
|
||||||
|
|
||||||
// default to next sequence number if none provided
|
// default to next sequence number if none provided
|
||||||
ctx, err = context.AutoSequence(ctx)
|
ctx, err = context.EnsureSequence(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -140,7 +140,7 @@ func GetCmdEditCandidacy(cdc *wire.Codec) *cobra.Command {
|
||||||
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
|
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
|
||||||
|
|
||||||
// default to next sequence number if none provided
|
// default to next sequence number if none provided
|
||||||
ctx, err = context.AutoSequence(ctx)
|
ctx, err = context.EnsureSequence(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -183,7 +183,7 @@ func GetCmdDelegate(cdc *wire.Codec) *cobra.Command {
|
||||||
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
|
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
|
||||||
|
|
||||||
// default to next sequence number if none provided
|
// default to next sequence number if none provided
|
||||||
ctx, err = context.AutoSequence(ctx)
|
ctx, err = context.EnsureSequence(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -237,7 +237,7 @@ func GetCmdUnbond(cdc *wire.Codec) *cobra.Command {
|
||||||
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
|
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
|
||||||
|
|
||||||
// default to next sequence number if none provided
|
// default to next sequence number if none provided
|
||||||
ctx, err = context.AutoSequence(ctx)
|
ctx, err = context.EnsureSequence(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue