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
(cherry picked from commit 148451b)

# Conflicts:
#	CHANGELOG.md
#	x/genutil/client/cli/init.go
#	x/genutil/client/cli/init_test.go
  • Loading branch information
alexanderbez authored and mergify-bot committed Sep 15, 2021
1 parent 8f49499 commit 096675a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 7 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,23 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Deprecated

<<<<<<< HEAD
* (x/upgrade) [\#9906](https://github.com/cosmos/cosmos-sdk/pull/9906) Deprecate `UpgradeConsensusState` gRPC query since this functionality is only used for IBC, which now has its own [IBC replacement](https://github.com/cosmos/ibc-go/blob/2c880a22e9f9cc75f62b527ca94aa75ce1106001/proto/ibc/core/client/v1/query.proto#L54)
=======
* (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`
* (x/genutil) [\#9574](https://github.com/cosmos/cosmos-sdk/pull/9575) Actually use the `gentx` client tx flags (like `--keyring-dir`)
* (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
* [\#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
* [\#9854](https://github.com/cosmos/cosmos-sdk/pull/9854) Fixed the `make proto-gen` to get dynamic container name based on project name for the cosmos based sdks.
* [\#9829](https://github.com/cosmos/cosmos-sdk/pull/9829) Fixed Coin denom sorting not being checked during `Balance.Validate` check. Refactored the Validation logic to use `Coins.Validate` for `Balance.Coins`.
+ [\#9965](https://github.com/cosmos/cosmos-sdk/pull/9965) Fixed `simd version` command output to report the right release tag.
+ [\#9980](https://github.com/cosmos/cosmos-sdk/pull/9980) Returning the error when the invalid argument is passed to bank query total supply cli.
* (server) [#10016](https://github.com/cosmos/cosmos-sdk/issues/10016) Fix marshaling of index-events into server config file.
>>>>>>> 148451b51 (fix: Allow --home to propagate to init command (#10104))
### Bug Fixes

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
35 changes: 35 additions & 0 deletions x/genutil/client/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/genutil"
<<<<<<< HEAD
=======
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
>>>>>>> 148451b51 (fix: Allow --home to propagate to init command (#10104))
)

const (
Expand Down Expand Up @@ -77,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 @@ -109,11 +114,40 @@ func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {

genFile := config.GenesisFile()
overwrite, _ := cmd.Flags().GetBool(FlagOverwrite)
<<<<<<< HEAD
=======
stakingBondDenom, _ := cmd.Flags().GetString(FlagStakingBondDenom)
>>>>>>> 148451b51 (fix: Allow --home to propagate to init command (#10104))

if !overwrite && tmos.FileExists(genFile) {
return fmt.Errorf("genesis.json file already exists: %v", genFile)
}
<<<<<<< HEAD
appState, err := json.MarshalIndent(mbm.DefaultGenesis(cdc), "", " ")
=======

appGenState := mbm.DefaultGenesis(cdc)

if stakingBondDenom != "" {
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
}

appState, err := json.MarshalIndent(appGenState, "", " ")
>>>>>>> 148451b51 (fix: Allow --home to propagate to init command (#10104))
if err != nil {
return errors.Wrap(err, "Failed to marshall default genesis state")
}
Expand All @@ -133,6 +167,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
37 changes: 36 additions & 1 deletion 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 @@ -117,6 +121,37 @@ func TestInitRecover(t *testing.T) {
require.NoError(t, cmd.ExecuteContext(ctx))
}

<<<<<<< HEAD
=======
func TestInitStakingBondDenom(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()).
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{
"appnode-test",
fmt.Sprintf("--%s=%s", cli.HomeFlag, home),
fmt.Sprintf("--%s=testtoken", genutilcli.FlagStakingBondDenom),
})
require.NoError(t, cmd.ExecuteContext(ctx))
}

>>>>>>> 148451b51 (fix: Allow --home to propagate to init command (#10104))
func TestEmptyState(t *testing.T) {
home := t.TempDir()
logger := log.NewNopLogger()
Expand Down

0 comments on commit 096675a

Please sign in to comment.