Address PR comments

This commit is contained in:
Christopher Goes
2018-04-10 11:16:30 +02:00
parent 7383c99026
commit c7b680a545
8 changed files with 57 additions and 35 deletions
+38 -25
View File
@@ -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
}
+9
View File
@@ -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