Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: init with config chain-id #9762

Merged
merged 4 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

## [v0.43.0-rc0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.43.0-rc0) - 2021-06-25

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 @@ -81,7 +81,11 @@ func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {

chainID, _ := cmd.Flags().GetString(flags.FlagChainID)
if chainID == "" {
chainID = fmt.Sprintf("test-chain-%v", tmrand.Str(6))
if clientCtx.ChainID == "" {
chainID = fmt.Sprintf("test-chain-%v", tmrand.Str(6))
} else {
chainID = clientCtx.ChainID
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this could be cleaned up with just a single switch clause.

switch {
  case chainID != "":
  case clientCtx.ChainID != ""
  default:
}

}

// Get bip39 mnemonic
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