From 156370ea9cef39f6d84255fa2b6201425916b53f Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Mon, 12 Nov 2018 08:15:08 -0500 Subject: [PATCH 1/6] Generate random moniker when missing --- PENDING.md | 1 + cmd/gaia/init/init.go | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/PENDING.md b/PENDING.md index 1ff6e3ee1947..dd092f5ce173 100644 --- a/PENDING.md +++ b/PENDING.md @@ -34,6 +34,7 @@ IMPROVEMENTS * [\#2749](https://github.com/cosmos/cosmos-sdk/pull/2749) Add --chain-id flag to gaiad testnet * Gaia + * Generate random moniker on `gaiad init` if one was not provided. * SDK - [x/mock/simulation] [\#2720] major cleanup, introduction of helper objects, reorganization diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index a297caee2300..5536a4fa9c7f 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -51,25 +51,32 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob RunE: func(_ *cobra.Command, _ []string) error { config := ctx.Config config.SetRoot(viper.GetString(cli.HomeFlag)) + chainID := viper.GetString(client.FlagChainID) if chainID == "" { chainID = fmt.Sprintf("test-chain-%v", common.RandStr(6)) } + nodeID, _, err := InitializeNodeValidatorFiles(config) if err != nil { return err } - if viper.GetString(flagMoniker) != "" { - config.Moniker = viper.GetString(flagMoniker) + moniker := viper.GetString(flagMoniker) + if moniker == "" { + moniker = fmt.Sprintf("node-%v", common.RandStr(6)) } + config.Moniker = moniker + var appState json.RawMessage genFile := config.GenesisFile() + if appState, err = initializeEmptyGenesis(cdc, genFile, chainID, viper.GetBool(flagOverwrite)); err != nil { return err } + if err = ExportGenesisFile(genFile, chainID, nil, appState); err != nil { return err } @@ -91,5 +98,6 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob cmd.Flags().BoolP(flagOverwrite, "o", false, "overwrite the genesis.json file") cmd.Flags().String(client.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created") cmd.Flags().String(flagMoniker, "", "set the validator's moniker") + return cmd } From 29bec833f315956bee38f649d99f4956ed562e14 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Mon, 12 Nov 2018 08:16:47 -0500 Subject: [PATCH 2/6] Update moniker prefix --- PENDING.md | 2 +- cmd/gaia/init/init.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/PENDING.md b/PENDING.md index dd092f5ce173..73c832c017a0 100644 --- a/PENDING.md +++ b/PENDING.md @@ -34,7 +34,7 @@ IMPROVEMENTS * [\#2749](https://github.com/cosmos/cosmos-sdk/pull/2749) Add --chain-id flag to gaiad testnet * Gaia - * Generate random moniker on `gaiad init` if one was not provided. + * Generate a random moniker on `gaiad init` if one was not provided. * SDK - [x/mock/simulation] [\#2720] major cleanup, introduction of helper objects, reorganization diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index 5536a4fa9c7f..ece9efb9a656 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -64,7 +64,7 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob moniker := viper.GetString(flagMoniker) if moniker == "" { - moniker = fmt.Sprintf("node-%v", common.RandStr(6)) + moniker = fmt.Sprintf("gaianode-%v", common.RandStr(6)) } config.Moniker = moniker From 08a16672467dc6aaa9c951860434c05f3c98a455 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Mon, 12 Nov 2018 08:27:26 -0500 Subject: [PATCH 3/6] Require moniker instead of generating a random one --- PENDING.md | 2 +- cmd/gaia/init/init.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/PENDING.md b/PENDING.md index 73c832c017a0..bb757f7c0e2d 100644 --- a/PENDING.md +++ b/PENDING.md @@ -34,7 +34,7 @@ IMPROVEMENTS * [\#2749](https://github.com/cosmos/cosmos-sdk/pull/2749) Add --chain-id flag to gaiad testnet * Gaia - * Generate a random moniker on `gaiad init` if one was not provided. + * Require moniker to be provided on `gaiad init`. * SDK - [x/mock/simulation] [\#2720] major cleanup, introduction of helper objects, reorganization diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index ece9efb9a656..852ecc3f892b 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -64,7 +64,7 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob moniker := viper.GetString(flagMoniker) if moniker == "" { - moniker = fmt.Sprintf("gaianode-%v", common.RandStr(6)) + return fmt.Errorf("required flag \"%s\" has not been set", flagMoniker) } config.Moniker = moniker From 8c14b1d77ff756db2d34b197018bf351628863e9 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Mon, 12 Nov 2018 08:45:05 -0500 Subject: [PATCH 4/6] Fix test coverage --- cmd/gaia/init/init.go | 8 ++------ cmd/gaia/init/init_test.go | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index 852ecc3f892b..bb56de1bd635 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -62,12 +62,7 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob return err } - moniker := viper.GetString(flagMoniker) - if moniker == "" { - return fmt.Errorf("required flag \"%s\" has not been set", flagMoniker) - } - - config.Moniker = moniker + config.Moniker = viper.GetString(flagMoniker) var appState json.RawMessage genFile := config.GenesisFile() @@ -94,6 +89,7 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob }, } + cmd.MarkFlagRequired(flagMoniker) cmd.Flags().String(cli.HomeFlag, app.DefaultNodeHome, "node's home directory") cmd.Flags().BoolP(flagOverwrite, "o", false, "overwrite the genesis.json file") cmd.Flags().String(client.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created") diff --git a/cmd/gaia/init/init_test.go b/cmd/gaia/init/init_test.go index bd5274c26113..d1d9b9ce2d6a 100644 --- a/cmd/gaia/init/init_test.go +++ b/cmd/gaia/init/init_test.go @@ -2,15 +2,16 @@ package init import ( "bytes" - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/cmd/gaia/app" - "github.com/tendermint/tendermint/libs/cli" "io" "io/ioutil" "os" "testing" "time" + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/cmd/gaia/app" + "github.com/tendermint/tendermint/libs/cli" + "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/server/mock" "github.com/stretchr/testify/require" @@ -28,12 +29,16 @@ func TestInitCmd(t *testing.T) { logger := log.NewNopLogger() cfg, err := tcmd.ParseConfig() require.Nil(t, err) + ctx := server.NewContext(cfg, logger) cdc := app.MakeCodec() appInit := server.AppInit{ AppGenState: mock.AppGenState, } cmd := InitCmd(ctx, cdc, appInit) + + viper.Set(flagMoniker, "gaianode-test") + err = cmd.RunE(nil, nil) require.NoError(t, err) } @@ -53,14 +58,19 @@ func setupClientHome(t *testing.T) func() { func TestEmptyState(t *testing.T) { defer server.SetupViper(t)() defer setupClientHome(t)() + logger := log.NewNopLogger() cfg, err := tcmd.ParseConfig() require.Nil(t, err) + ctx := server.NewContext(cfg, logger) cdc := app.MakeCodec() appInit := server.AppInit{ AppGenState: mock.AppGenStateEmpty, } + + viper.Set(flagMoniker, "gaianode-test") + cmd := InitCmd(ctx, cdc, appInit) err = cmd.RunE(nil, nil) require.NoError(t, err) @@ -69,6 +79,7 @@ func TestEmptyState(t *testing.T) { r, w, _ := os.Pipe() os.Stdout = w cmd = server.ExportCmd(ctx, cdc, nil) + err = cmd.RunE(nil, nil) require.NoError(t, err) From 964123356b3f2bed4f61f68cdc34d01f1b156507 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Mon, 12 Nov 2018 15:44:59 -0500 Subject: [PATCH 5/6] Fix required flag --- cmd/gaia/init/init.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/gaia/init/init.go b/cmd/gaia/init/init.go index bb56de1bd635..1f12da6bde7c 100644 --- a/cmd/gaia/init/init.go +++ b/cmd/gaia/init/init.go @@ -89,11 +89,11 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec, appInit server.AppInit) *cob }, } - cmd.MarkFlagRequired(flagMoniker) cmd.Flags().String(cli.HomeFlag, app.DefaultNodeHome, "node's home directory") cmd.Flags().BoolP(flagOverwrite, "o", false, "overwrite the genesis.json file") cmd.Flags().String(client.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created") cmd.Flags().String(flagMoniker, "", "set the validator's moniker") + cmd.MarkFlagRequired(flagMoniker) return cmd } From e76a18dcecd8e59333b8be7ab8f89e31c52f7d45 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Tue, 13 Nov 2018 13:46:38 +0100 Subject: [PATCH 6/6] Link to issue --- PENDING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PENDING.md b/PENDING.md index a9115c5fdbcc..71e7c3734d68 100644 --- a/PENDING.md +++ b/PENDING.md @@ -37,7 +37,7 @@ IMPROVEMENTS * [\#2749](https://github.com/cosmos/cosmos-sdk/pull/2749) Add --chain-id flag to gaiad testnet * Gaia - * Require moniker to be provided on `gaiad init`. + - #2773 Require moniker to be provided on `gaiad init`. - #2672 [Makefile] Updated for better Windows compatibility and ledger support logic, get_tools was rewritten as a cross-compatible Makefile. * SDK