Skip to content

Commit

Permalink
fix: Allow --home to propagate to init command (#10104)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored Sep 9, 2021
1 parent 1820647 commit 148451b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Bug Fixes

* (x/genutil) [#10104](https://github.com/cosmos/cosmos-sdk/pull/10104) Ensure the `init` command reads the `--home` flag value correctly.
* [\#9651](https://github.com/cosmos/cosmos-sdk/pull/9651) Change inconsistent limit of `0` to `MaxUint64` on InfiniteGasMeter and add GasRemaining func to GasMeter.
* [\#9639](https://github.com/cosmos/cosmos-sdk/pull/9639) Check store keys length before accessing them by making sure that `key` is of length `m+1` (for `key[n:m]`)
* (types) [\#9627](https://github.com/cosmos/cosmos-sdk/pull/9627) Fix nil pointer panic on `NewBigIntFromInt`
Expand Down
8 changes: 2 additions & 6 deletions client/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,8 @@ func readTxCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Context, err
return clientCtx, nil
}

// ReadHomeFlag checks if home flag is changed.
// If this is a case, we update HomeDir field of Client Context
/* Discovered a bug with Cory
./build/simd init andrei --home ./test
cd test/config there is no client.toml configuration file
*/
// ReadHomeFlag checks if home flag is changed. If this is a case, we update
// HomeDir field of Client Context.
func ReadHomeFlag(clientCtx Context, cmd *cobra.Command) Context {
if cmd.Flags().Changed(flags.FlagHome) {
rootDir, _ := cmd.Flags().GetString(flags.FlagHome)
Expand Down
10 changes: 7 additions & 3 deletions x/genutil/client/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"

"github.com/cosmos/go-bip39"
"github.com/pkg/errors"
"github.com/spf13/cobra"
cfg "github.com/tendermint/tendermint/config"
Expand All @@ -23,7 +24,6 @@ import (
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/genutil"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/go-bip39"
)

const (
Expand Down Expand Up @@ -81,6 +81,7 @@ func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {
serverCtx := server.GetServerContextFromCmd(cmd)
config := serverCtx.Config

clientCtx = client.ReadHomeFlag(clientCtx, cmd)
config.SetRoot(clientCtx.HomeDir)

chainID, _ := cmd.Flags().GetString(flags.FlagChainID)
Expand Down Expand Up @@ -117,7 +118,6 @@ func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {

genFile := config.GenesisFile()
overwrite, _ := cmd.Flags().GetBool(FlagOverwrite)

stakingBondDenom, _ := cmd.Flags().GetString(FlagStakingBondDenom)

if !overwrite && tmos.FileExists(genFile) {
Expand All @@ -127,17 +127,20 @@ func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {
appGenState := mbm.DefaultGenesis(cdc)

if stakingBondDenom != "" {
stakingRaw := appGenState[stakingtypes.ModuleName]
var stakingGenesis stakingtypes.GenesisState

stakingRaw := appGenState[stakingtypes.ModuleName]
err := clientCtx.Codec.UnmarshalJSON(stakingRaw, &stakingGenesis)
if err != nil {
return err
}

stakingGenesis.Params.BondDenom = stakingBondDenom
modifiedStakingStr, err := clientCtx.Codec.MarshalJSON(&stakingGenesis)
if err != nil {
return err
}

appGenState[stakingtypes.ModuleName] = modifiedStakingStr
}

Expand All @@ -161,6 +164,7 @@ func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {
genDoc.ChainID = chainID
genDoc.Validators = nil
genDoc.AppState = appState

if err = genutil.ExportGenesisFile(genDoc, genFile); err != nil {
return errors.Wrap(err, "Failed to export gensis file")
}
Expand Down
12 changes: 10 additions & 2 deletions x/genutil/client/cli/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ import (
"github.com/cosmos/cosmos-sdk/x/genutil"
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
genutiltest "github.com/cosmos/cosmos-sdk/x/genutil/client/testutil"
"github.com/cosmos/cosmos-sdk/x/staking"
)

var testMbm = module.NewBasicManager(genutil.AppModuleBasic{})
var testMbm = module.NewBasicManager(
staking.AppModuleBasic{},
genutil.AppModuleBasic{},
)

func TestInitCmd(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -137,7 +141,11 @@ func TestInitStakingBondDenom(t *testing.T) {

cmd := genutilcli.InitCmd(testMbm, home)

cmd.SetArgs([]string{"appnode-test", fmt.Sprintf("--%s=%s --%s=testtoken", cli.HomeFlag, home, genutilcli.FlagStakingBondDenom)})
cmd.SetArgs([]string{
"appnode-test",
fmt.Sprintf("--%s=%s", cli.HomeFlag, home),
fmt.Sprintf("--%s=testtoken", genutilcli.FlagStakingBondDenom),
})
require.NoError(t, cmd.ExecuteContext(ctx))
}

Expand Down

0 comments on commit 148451b

Please sign in to comment.