Skip to content

Commit

Permalink
feat: overwrite default denom in init cmd (#13535)
Browse files Browse the repository at this point in the history
* feat: overwrite default denom in `init` cmd

* add changelog

* wording

* `make format`

* update comment
  • Loading branch information
julienrbrt committed Oct 13, 2022
1 parent 77e00d1 commit 89c65f4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -154,6 +154,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### CLI Breaking Changes

* (x/genutil) [#13535](https://github.com/cosmos/cosmos-sdk/pull/13535) Replace in `simd init`, the `--staking-bond-denom` flag with `--default-denom` which is used for all default denomination in the genesis, instead of only staking.
* (tx) [#12659](https://github.com/cosmos/cosmos-sdk/pull/12659) Remove broadcast mode `block`.

### Bug Fixes
Expand Down
31 changes: 15 additions & 16 deletions types/staking.go
@@ -1,26 +1,25 @@
package types

// staking constants
const (
// Delay, in blocks, between when validator updates are returned to the
// consensus-engine and when they are applied. For example, if
// ValidatorUpdateDelay is set to X, and if a validator set update is
// returned with new validators at the end of block 10, then the new
// validators are expected to sign blocks beginning at block 11+X.
//
// This value is constant as this should not change without a hard fork.
// For Tendermint this should be set to 1 block, for more details see:
// https://tendermint.com/docs/spec/abci/apps.html#endblock
const ValidatorUpdateDelay int64 = 1

// default bond denomination
var (
// DefaultBondDenom is the default bondable coin denomination (defaults to stake)
// Overwriting this value has the side effect of changing the default denomination in genesis
DefaultBondDenom = "stake"

// Delay, in blocks, between when validator updates are returned to the
// consensus-engine and when they are applied. For example, if
// ValidatorUpdateDelay is set to X, and if a validator set update is
// returned with new validators at the end of block 10, then the new
// validators are expected to sign blocks beginning at block 11+X.
//
// This value is constant as this should not change without a hard fork.
// For Tendermint this should be set to 1 block, for more details see:
// https://tendermint.com/docs/spec/abci/apps.html#endblock
ValidatorUpdateDelay int64 = 1
// DefaultPowerReduction is the default amount of staking tokens required for 1 unit of consensus-engine power
DefaultPowerReduction = NewIntFromUint64(1000000)
)

// DefaultPowerReduction is the default amount of staking tokens required for 1 unit of consensus-engine power
var DefaultPowerReduction = NewIntFromUint64(1000000)

// TokensToConsensusPower - convert input tokens to potential consensus-engine power
func TokensToConsensusPower(tokens Int, powerReduction Int) int64 {
return (tokens.Quo(powerReduction)).Int64()
Expand Down
31 changes: 8 additions & 23 deletions x/genutil/client/cli/init.go
Expand Up @@ -22,7 +22,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/genutil"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

const (
Expand All @@ -32,8 +31,8 @@ const (
// FlagSeed defines a flag to initialize the private validator key from a specific seed.
FlagRecover = "recover"

// FlagStakingBondDenom defines a flag to specify the staking token in the genesis file.
FlagStakingBondDenom = "staking-bond-denom"
// FlagDefaultBondDenom defines the default denom to use in the genesis file.
FlagDefaultBondDenom = "default-denom"
)

type printInfo struct {
Expand Down Expand Up @@ -115,33 +114,19 @@ func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {

genFile := config.GenesisFile()
overwrite, _ := cmd.Flags().GetBool(FlagOverwrite)
stakingBondDenom, _ := cmd.Flags().GetString(FlagStakingBondDenom)
defaultDenom, _ := cmd.Flags().GetString(FlagDefaultBondDenom)

// use os.Stat to check if the file exists
_, err = os.Stat(genFile)
if !overwrite && !os.IsNotExist(err) {
return fmt.Errorf("genesis.json file already exists: %v", genFile)
}

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
// Overwrites the SDK default denom for side-effects
if defaultDenom != "" {
sdk.DefaultBondDenom = defaultDenom
}
appGenState := mbm.DefaultGenesis(cdc)

appState, err := json.MarshalIndent(appGenState, "", " ")
if err != nil {
Expand Down Expand Up @@ -179,7 +164,7 @@ func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {
cmd.Flags().BoolP(FlagOverwrite, "o", false, "overwrite the genesis.json file")
cmd.Flags().Bool(FlagRecover, false, "provide seed phrase to recover existing key instead of creating")
cmd.Flags().String(flags.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created")
cmd.Flags().String(FlagStakingBondDenom, "", "genesis file staking bond denomination, if left blank default value is 'stake'")
cmd.Flags().String(FlagDefaultBondDenom, "", "genesis file default denomination, if left blank default value is 'stake'")

return cmd
}
4 changes: 2 additions & 2 deletions x/genutil/client/cli/init_test.go
Expand Up @@ -120,7 +120,7 @@ func TestInitRecover(t *testing.T) {
require.NoError(t, cmd.ExecuteContext(ctx))
}

func TestInitStakingBondDenom(t *testing.T) {
func TestInitDefaultBondDenom(t *testing.T) {
home := t.TempDir()
logger := log.NewNopLogger()
cfg, err := genutiltest.CreateDefaultTendermintConfig(home)
Expand All @@ -143,7 +143,7 @@ func TestInitStakingBondDenom(t *testing.T) {
cmd.SetArgs([]string{
"appnode-test",
fmt.Sprintf("--%s=%s", cli.HomeFlag, home),
fmt.Sprintf("--%s=testtoken", genutilcli.FlagStakingBondDenom),
fmt.Sprintf("--%s=testtoken", genutilcli.FlagDefaultBondDenom),
})
require.NoError(t, cmd.ExecuteContext(ctx))
}
Expand Down

0 comments on commit 89c65f4

Please sign in to comment.