Skip to content

Commit

Permalink
Don't fallback to any default values for chain id
Browse files Browse the repository at this point in the history
- Remove DefaultChainID(). User needs to suplly chain ID
  via either config or flag.
- Mark --chain-id as required by all tx commands.
- Fix gaiacli config values containing underscores.
  Underscore '_' character is not automagically translated
  into hyphen '-'. Viper values wouldn't be affected.
- Refresh gaiacli config tests

Closes: #810
  • Loading branch information
Alessio Treglia committed Dec 16, 2018
1 parent ff6c2f2 commit 4abbcb6
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 36 deletions.
3 changes: 3 additions & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ BREAKING CHANGES
* [gaia-lite] [\#3056](https://github.com/cosmos/cosmos-sdk/pull/3056) `generate_only` and `simulate` have moved from query arguments to POST requests body.

* Gaia CLI (`gaiacli`)
* [\#810](https://github.com/cosmos/cosmos-sdk/issues/810) Don't fallback to any default values for chain ID.
- Users need to supply chain ID either via config file or the `--chain-id` flag.
- Change `chain_id` and `trust_node` in `gaiacli` configuration to `chain-id` and `trust-node` respectively.

* Gaia

Expand Down
8 changes: 4 additions & 4 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var configDefaults map[string]string

func init() {
configDefaults = map[string]string{
"chain_id": "",
"chain-id": "",
"output": "text",
"node": "tcp://localhost:26657",
}
Expand Down Expand Up @@ -78,7 +78,7 @@ func runConfigCmd(cmd *cobra.Command, args []string) error {
// Get value action
if getAction {
switch key {
case "trace", "trust_node":
case "trace", "trust-node":
fmt.Println(tree.GetDefault(key, false).(bool))
default:
if defaultValue, ok := configDefaults[key]; ok {
Expand All @@ -93,9 +93,9 @@ func runConfigCmd(cmd *cobra.Command, args []string) error {
// Set value action
value := args[1]
switch key {
case "chain_id", "output", "node":
case "chain-id", "output", "node":
tree.Set(key, value)
case "trace", "trust_node":
case "trace", "trust-node":
boolVal, err := strconv.ParseBool(value)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions client/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ func PostCommands(cmds ...*cobra.Command) []*cobra.Command {
viper.BindPFlag(FlagUseLedger, c.Flags().Lookup(FlagUseLedger))
viper.BindPFlag(FlagChainID, c.Flags().Lookup(FlagChainID))
viper.BindPFlag(FlagNode, c.Flags().Lookup(FlagNode))

c.MarkFlagRequired(FlagChainID)
}
return cmds
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/gaia/cli_test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,16 +632,16 @@ func TestGaiaCLIConfig(t *testing.T) {
node := fmt.Sprintf("%s:%s", servAddr, port)
executeWrite(t, fmt.Sprintf(`gaiacli --home=%s config node %s`, gaiacliHome, node))
executeWrite(t, fmt.Sprintf(`gaiacli --home=%s config output text`, gaiacliHome))
executeWrite(t, fmt.Sprintf(`gaiacli --home=%s config trust_node true`, gaiacliHome))
executeWrite(t, fmt.Sprintf(`gaiacli --home=%s config chain_id %s`, gaiacliHome, chainID))
executeWrite(t, fmt.Sprintf(`gaiacli --home=%s config trust-node true`, gaiacliHome))
executeWrite(t, fmt.Sprintf(`gaiacli --home=%s config chain-id %s`, gaiacliHome, chainID))
executeWrite(t, fmt.Sprintf(`gaiacli --home=%s config trace false`, gaiacliHome))
config, err := ioutil.ReadFile(path.Join(gaiacliHome, "config", "config.toml"))
require.NoError(t, err)
expectedConfig := fmt.Sprintf(`chain_id = "%s"
expectedConfig := fmt.Sprintf(`chain-id = "%s"
node = "%s"
output = "text"
trace = false
trust_node = true
trust-node = true
`, chainID, node)
require.Equal(t, expectedConfig, string(config))
cleanupDirs(gaiadHome, gaiacliHome)
Expand Down
22 changes: 0 additions & 22 deletions types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import (
"encoding/binary"
"encoding/json"
"time"

tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
tmtypes "github.com/tendermint/tendermint/types"
)

// SortedJSON takes any JSON and returns it sorted by keys. Also, all white-spaces
Expand Down Expand Up @@ -61,22 +58,3 @@ func ParseTimeBytes(bz []byte) (time.Time, error) {
}
return t.UTC().Round(0), nil
}

// DefaultChainID returns the chain ID from the genesis file if present. An
// error is returned if the file cannot be read or parsed.
//
// TODO: This should be removed and the chainID should always be provided by
// the end user.
func DefaultChainID() (string, error) {
cfg, err := tcmd.ParseConfig()
if err != nil {
return "", err
}

doc, err := tmtypes.GenesisDocFromFile(cfg.GenesisFile())
if err != nil {
return "", err
}

return doc.ChainID, nil
}
6 changes: 0 additions & 6 deletions x/auth/client/txbuilder/txbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ type TxBuilder struct {
func NewTxBuilderFromCLI() TxBuilder {
// if chain ID is not specified manually, read default chain ID
chainID := viper.GetString(client.FlagChainID)
if chainID == "" {
defaultChainID, err := sdk.DefaultChainID()
if err != nil {
chainID = defaultChainID
}
}

return TxBuilder{
ChainID: chainID,
Expand Down

0 comments on commit 4abbcb6

Please sign in to comment.