Automatically determine chainID, throw error if not provided (closes #810)

This commit is contained in:
Christopher Goes 2018-04-06 18:08:29 +02:00
parent e588ebfa9a
commit 0be655b122
No known key found for this signature in database
GPG Key ID: E828D98232D328D3
2 changed files with 24 additions and 1 deletions

View File

@ -1,9 +1,13 @@
package context
import (
"encoding/json"
"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"
@ -15,8 +19,24 @@ func NewCoreContextFromViper() core.CoreContext {
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 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
}
}
}
}
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),

View File

@ -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,