Skip to content

Commit

Permalink
Merge PR #2773: Require moniker on gaiad init
Browse files Browse the repository at this point in the history
* Generate random moniker when missing
* Update moniker prefix
* Require moniker instead of generating a random one
* Fix test coverage
* Fix required flag
* Link to issue
  • Loading branch information
cwgoes authored Nov 13, 2018
2 parents 4177f30 + e76a18d commit a4e5d8e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ IMPROVEMENTS
* [\#2749](https://github.com/cosmos/cosmos-sdk/pull/2749) Add --chain-id flag to gaiad testnet

* Gaia
- #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
Expand Down
10 changes: 7 additions & 3 deletions cmd/gaia/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,27 @@ 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)
}
config.Moniker = viper.GetString(flagMoniker)

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
}
Expand All @@ -91,5 +93,7 @@ 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")
cmd.MarkFlagRequired(flagMoniker)

return cmd
}
17 changes: 14 additions & 3 deletions cmd/gaia/init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)

Expand Down

0 comments on commit a4e5d8e

Please sign in to comment.