Skip to content

Commit

Permalink
fix: init with config chain-id (#9762)
Browse files Browse the repository at this point in the history
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->

## Description

Closes: #9604, Ref: #9644

<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->

The `init` command uses the `chain-id` from the client config if `--chain-id` is not provided.

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) -n/a
- [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc) -n/a
- [x] updated the relevant documentation or specification -n/a
- [x] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
ryanchristo authored Jul 26, 2021
1 parent b34ceec commit f4d0682
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/distribution) [\#9599](https://github.com/cosmos/cosmos-sdk/pull/9599) Withdraw rewards event now includes a value attribute even if there are 0 rewards (due to situations like 100% commission).
* (x/genutil) [\#9638](https://github.com/cosmos/cosmos-sdk/pull/9638) Added missing validator key save when recovering from mnemonic
* (server) [#9704](https://github.com/cosmos/cosmos-sdk/pull/9704) Start GRPCWebServer in goroutine, avoid blocking other services from starting.
* [\#9762](https://github.com/cosmos/cosmos-sdk/pull/9762) The init command uses the chain-id from the client config if --chain-id is not provided

### State Machine Breaking

Expand Down
6 changes: 5 additions & 1 deletion x/genutil/client/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {
config.SetRoot(clientCtx.HomeDir)

chainID, _ := cmd.Flags().GetString(flags.FlagChainID)
if chainID == "" {
switch {
case chainID != "":
case clientCtx.ChainID != "":
chainID = clientCtx.ChainID
default:
chainID = fmt.Sprintf("test-chain-%v", tmrand.Str(6))
}

Expand Down
49 changes: 48 additions & 1 deletion x/genutil/client/cli/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,60 @@ func TestStartStandAlone(t *testing.T) {
func TestInitNodeValidatorFiles(t *testing.T) {
home := t.TempDir()
cfg, err := genutiltest.CreateDefaultTendermintConfig(home)
require.NoError(t, err)

nodeID, valPubKey, err := genutil.InitializeNodeValidatorFiles(cfg)
require.NoError(t, err)

require.Nil(t, err)
require.NotEqual(t, "", nodeID)
require.NotEqual(t, 0, len(valPubKey.Bytes()))
}

func TestInitConfig(t *testing.T) {
home := t.TempDir()
logger := log.NewNopLogger()
cfg, err := genutiltest.CreateDefaultTendermintConfig(home)
require.NoError(t, err)

serverCtx := server.NewContext(viper.New(), cfg, logger)
interfaceRegistry := types.NewInterfaceRegistry()
marshaler := codec.NewProtoCodec(interfaceRegistry)
clientCtx := client.Context{}.
WithCodec(marshaler).
WithLegacyAmino(makeCodec()).
WithChainID("foo"). // add chain-id to clientCtx
WithHomeDir(home)

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx)

cmd := genutilcli.InitCmd(testMbm, home)
cmd.SetArgs([]string{"testnode"})

require.NoError(t, cmd.ExecuteContext(ctx))

old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

cmd = server.ExportCmd(nil, home)
require.NoError(t, cmd.ExecuteContext(ctx))

outC := make(chan string)
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()

w.Close()
os.Stdout = old
out := <-outC

require.Contains(t, out, "\"chain_id\": \"foo\"")
}

// custom tx codec
func makeCodec() *codec.LegacyAmino {
var cdc = codec.NewLegacyAmino()
Expand Down

0 comments on commit f4d0682

Please sign in to comment.