Skip to content

Commit

Permalink
refactor: add option to set bondDenom on simulations (#14529)
Browse files Browse the repository at this point in the history
## Description




- Add new property `BondDenom` to `SimulationState` struct in `types/module/simulation.go`
- Changed the `x/modules/simulation/genesis.go` mostly to use the new property

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
RafilxTenfen authored Jan 8, 2023
1 parent 3729243 commit 91a916e
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 21 deletions.
19 changes: 10 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* [#14529](https://github.com/cosmos/cosmos-sdk/pull/14529) Add new property `BondDenom` to `SimulationState` struct.
* (x/group, x/gov) [#14483](https://github.com/cosmos/cosmos-sdk/pull/14483) Add support for `[]string` and `[]int` in `draft-proposal` prompt.
* (protobuf) [#14476](https://github.com/cosmos/cosmos-sdk/pull/14476) Clean up protobuf annotations `{accepts,implements}_interface`.
* (module) [#14415](https://github.com/cosmos/cosmos-sdk/pull/14415) Loosen assertions in SetOrderBeginBlockers() and SetOrderEndBlockers()
Expand Down Expand Up @@ -116,10 +117,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [#13881](https://github.com/cosmos/cosmos-sdk/pull/13881) Optimize iteration on nested cached KV stores and other operations in general.
* (x/gov) [#14347](https://github.com/cosmos/cosmos-sdk/pull/14347) Support `v1.Proposal` message in `v1beta1.Proposal.Content`.
* (x/gov) [#14390](https://github.com/cosmos/cosmos-sdk/pull/14390) Add title, proposer and summary to proposal struct
* (baseapp) [#14417](https://github.com/cosmos/cosmos-sdk/pull/14417) `SetStreamingService` accepts appOptions, AppCodec and Storekeys needed to set streamers.
* Store pacakge no longer has a dependency on baseapp.
* (store) [#14438](https://github.com/cosmos/cosmos-sdk/pull/14438) Pass logger from baseapp to store.
* (store) [#14439](https://github.com/cosmos/cosmos-sdk/pull/14439) Remove global metric gatherer from store.
* (baseapp) [#14417](https://github.com/cosmos/cosmos-sdk/pull/14417) `SetStreamingService` accepts appOptions, AppCodec and Storekeys needed to set streamers.
* Store pacakge no longer has a dependency on baseapp.
* (store) [#14438](https://github.com/cosmos/cosmos-sdk/pull/14438) Pass logger from baseapp to store.
* (store) [#14439](https://github.com/cosmos/cosmos-sdk/pull/14439) Remove global metric gatherer from store.
* By default store has a no op metric gatherer, the application developer must set another metric gatherer or us the provided one in `store/metrics`.

### State Machine Breaking
Expand Down Expand Up @@ -199,13 +200,13 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (types) [#13430](https://github.com/cosmos/cosmos-sdk/pull/13430) Remove unused code `ResponseCheckTx` and `ResponseDeliverTx`
* (store) [#13529](https://github.com/cosmos/cosmos-sdk/pull/13529) Add method `LatestVersion` to `MultiStore` interface, add method `SetQueryMultiStore` to baesapp to support alternative `MultiStore` implementation for query service.
* (pruning) [#13609](https://github.com/cosmos/cosmos-sdk/pull/13609) Move pruning package to be under store package
* [#13794](https://github.com/cosmos/cosmos-sdk/pull/13794) Most methods on `types/module.AppModule` have been moved to
extension interfaces. `module.Manager.Modules` is now of type `map[string]interface{}` to support in parallel the new
* [#13794](https://github.com/cosmos/cosmos-sdk/pull/13794) Most methods on `types/module.AppModule` have been moved to
extension interfaces. `module.Manager.Modules` is now of type `map[string]interface{}` to support in parallel the new
`cosmossdk.io/core/appmodule.AppModule` API.
* (signing) [#13701](https://github.com/cosmos/cosmos-sdk/pull/) Add `context.Context` as an argument `x/auth/signing.VerifySignature`.
* (x/group) [#13876](https://github.com/cosmos/cosmos-sdk/pull/13876) Add `GetMinExecutionPeriod` method on DecisionPolicy interface.
* (x/auth)[#13780](https://github.com/cosmos/cosmos-sdk/pull/13780) Querying with `id` (type of int64) in `AccountAddressByID` grpc query now throws error, use account-id(type of uint64) instead.
* (snapshots) [14048](https://github.com/cosmos/cosmos-sdk/pull/14048) Move the Snapshot package to the store package. This is done in an effort group all storage related logic under one package.
* (snapshots) [14048](https://github.com/cosmos/cosmos-sdk/pull/14048) Move the Snapshot package to the store package. This is done in an effort group all storage related logic under one package.
* (baseapp) [#14050](https://github.com/cosmos/cosmos-sdk/pull/14050) refactor `ABCIListener` interface to accept go contexts

### CLI Breaking Changes
Expand All @@ -220,7 +221,7 @@ extension interfaces. `module.Manager.Modules` is now of type `map[string]interf
* (server) [#14441](https://github.com/cosmos/cosmos-sdk/pull/14441) Fix `--log_format` flag not working.
* (x/upgrade) [#13936](https://github.com/cosmos/cosmos-sdk/pull/13936) Make downgrade verification work again
* (x/group) [#13742](https://github.com/cosmos/cosmos-sdk/pull/13742) Fix `validate-genesis` when group policy accounts exist.
* (x/auth) [#13838](https://github.com/cosmos/cosmos-sdk/pull/13838) Fix calling `String()` when pubkey is set on a `BaseAccount`.
* (x/auth) [#13838](https://github.com/cosmos/cosmos-sdk/pull/13838) Fix calling `String()` when pubkey is set on a `BaseAccount`.
* (rosetta) [#13583](https://github.com/cosmos/cosmos-sdk/pull/13583) Misc fixes for cosmos-rosetta.
* (x/evidence) [#13740](https://github.com/cosmos/cosmos-sdk/pull/13740) Fix evidence query API to decode the hash properly.
* (bank) [#13691](https://github.com/cosmos/cosmos-sdk/issues/13691) Fix unhandled error for vesting account transfers, when total vesting amount exceeds total balance.
Expand Down Expand Up @@ -260,7 +261,7 @@ extension interfaces. `module.Manager.Modules` is now of type `map[string]interf

* (deps) Bump Tendermint version to [v0.34.24](https://github.com/tendermint/tendermint/releases/tag/v0.34.24).
* [#13651](https://github.com/cosmos/cosmos-sdk/pull/13651) Update `server/config/config.GetConfig` function.
* [#14175](https://github.com/cosmos/cosmos-sdk/pull/14175) Add `server.DefaultBaseappOptions(appopts)` function to reduce boiler plate in root.go.
* [#14175](https://github.com/cosmos/cosmos-sdk/pull/14175) Add `server.DefaultBaseappOptions(appopts)` function to reduce boiler plate in root.go.

### State Machine Breaking

Expand Down
1 change: 1 addition & 0 deletions simapp/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ func AppStateRandomizedFn(
Accounts: accs,
InitialStake: initialStake,
NumBonded: numInitiallyBonded,
BondDenom: sdk.DefaultBondDenom,
GenTimestamp: genesisTimestamp,
}

Expand Down
1 change: 1 addition & 0 deletions testutil/sims/simulation_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func SimulationOperations(app runtime.AppI, cdc codec.JSONCodec, config simtypes
simState := module.SimulationState{
AppParams: make(simtypes.AppParams),
Cdc: cdc,
BondDenom: sdk.DefaultBondDenom,
}

if config.ParamsFile != "" {
Expand Down
1 change: 1 addition & 0 deletions types/module/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ type SimulationState struct {
Accounts []simulation.Account // simulation accounts
InitialStake sdkmath.Int // initial coins per account
NumBonded int64 // number of initially bonded accounts
BondDenom string // denom to be used as default
GenTimestamp time.Time // genesis timestamp
UnbondTime time.Duration // staking unbond time stored to use it as the slashing maximum evidence duration
ParamChanges []simulation.ParamChange // simulated parameter changes from modules
Expand Down
2 changes: 1 addition & 1 deletion x/auth/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func RandomGenesisAccounts(simState *module.SimulationState) types.GenesisAccoun
continue
}

initialVesting := sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, simState.Rand.Int63n(simState.InitialStake.Int64())))
initialVesting := sdk.NewCoins(sdk.NewInt64Coin(simState.BondDenom, simState.Rand.Int63n(simState.InitialStake.Int64())))
var endTime int64

startTime := simState.GenTimestamp.Unix()
Expand Down
12 changes: 6 additions & 6 deletions x/bank/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ func RandomGenesisDefaultSendEnabledParam(r *rand.Rand) bool {
}

// RandomGenesisSendEnabled creates randomized values for the SendEnabled slice.
func RandomGenesisSendEnabled(r *rand.Rand) []types.SendEnabled {
func RandomGenesisSendEnabled(r *rand.Rand, bondDenom string) []types.SendEnabled {
rv := make([]types.SendEnabled, 0, 2)
// 60% of the time, add a denom specific record.
if r.Int63n(100) < 60 {
// 75% of the those times, set send enabled to true.
bondEnabled := r.Int63n(100) < 75
rv = append(rv, types.SendEnabled{Denom: sdk.DefaultBondDenom, Enabled: bondEnabled})
rv = append(rv, types.SendEnabled{Denom: bondDenom, Enabled: bondEnabled})
}
// Probabilities:
// P(a) = 60.0% = There's SendEnable entry for the bond denom = .600
Expand Down Expand Up @@ -53,14 +53,14 @@ func RandomGenesisSendEnabled(r *rand.Rand) []types.SendEnabled {
}

// RandomGenesisBalances returns a slice of account balances. Each account has
// a balance of simState.InitialStake for sdk.DefaultBondDenom.
// a balance of simState.InitialStake for simState.BondDenom.
func RandomGenesisBalances(simState *module.SimulationState) []types.Balance {
genesisBalances := []types.Balance{}

for _, acc := range simState.Accounts {
genesisBalances = append(genesisBalances, types.Balance{
Address: acc.Address.String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, simState.InitialStake)),
Coins: sdk.NewCoins(sdk.NewCoin(simState.BondDenom, simState.InitialStake)),
})
}

Expand All @@ -75,11 +75,11 @@ func RandomizedGenState(simState *module.SimulationState) {
func(r *rand.Rand) { defaultSendEnabledParam = RandomGenesisDefaultSendEnabledParam(r) },
)

sendEnabled := RandomGenesisSendEnabled(simState.Rand)
sendEnabled := RandomGenesisSendEnabled(simState.Rand, simState.BondDenom)

numAccs := int64(len(simState.Accounts))
totalSupply := simState.InitialStake.Mul(sdk.NewInt((numAccs + simState.NumBonded)))
supply := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, totalSupply))
supply := sdk.NewCoins(sdk.NewCoin(simState.BondDenom, totalSupply))

bankGenesis := types.GenesisState{
Params: types.NewParams(defaultSendEnabledParam),
Expand Down
2 changes: 2 additions & 0 deletions x/bank/simulation/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/bank/simulation"
Expand All @@ -30,6 +31,7 @@ func TestRandomizedGenState(t *testing.T) {
Cdc: cdc,
Rand: r,
NumBonded: 3,
BondDenom: sdk.DefaultBondDenom,
Accounts: simtypes.RandomAccounts(r, 3),
InitialStake: sdkmath.NewInt(1000),
GenState: make(map[string]json.RawMessage),
Expand Down
6 changes: 3 additions & 3 deletions x/gov/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func GenDepositParamsDepositPeriod(r *rand.Rand) time.Duration {
}

// GenDepositParamsMinDeposit returns randomized DepositParamsMinDeposit
func GenDepositParamsMinDeposit(r *rand.Rand) sdk.Coins {
return sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, int64(simulation.RandIntBetween(r, 1, 1e3))))
func GenDepositParamsMinDeposit(r *rand.Rand, bondDenom string) sdk.Coins {
return sdk.NewCoins(sdk.NewInt64Coin(bondDenom, int64(simulation.RandIntBetween(r, 1, 1e3))))
}

// GenDepositMinInitialRatio returns randomized DepositMinInitialRatio
Expand Down Expand Up @@ -68,7 +68,7 @@ func RandomizedGenState(simState *module.SimulationState) {
var minDeposit sdk.Coins
simState.AppParams.GetOrGenerate(
simState.Cdc, DepositParamsMinDeposit, &minDeposit, simState.Rand,
func(r *rand.Rand) { minDeposit = GenDepositParamsMinDeposit(r) },
func(r *rand.Rand) { minDeposit = GenDepositParamsMinDeposit(r, simState.BondDenom) },
)

var depositPeriod time.Duration
Expand Down
2 changes: 2 additions & 0 deletions x/gov/simulation/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/gov/simulation"
Expand All @@ -31,6 +32,7 @@ func TestRandomizedGenState(t *testing.T) {
Cdc: cdc,
Rand: r,
NumBonded: 3,
BondDenom: sdk.DefaultBondDenom,
Accounts: simtypes.RandomAccounts(r, 3),
InitialStake: sdkmath.NewInt(1000),
GenState: make(map[string]json.RawMessage),
Expand Down
2 changes: 1 addition & 1 deletion x/mint/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func RandomizedGenState(simState *module.SimulationState) {
func(r *rand.Rand) { goalBonded = GenGoalBonded(r) },
)

mintDenom := sdk.DefaultBondDenom
mintDenom := simState.BondDenom
blocksPerYear := uint64(60 * 60 * 8766 / 5)
params := types.NewParams(mintDenom, inflationRateChange, inflationMax, inflationMin, goalBonded, blocksPerYear)

Expand Down
1 change: 1 addition & 0 deletions x/mint/simulation/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestRandomizedGenState(t *testing.T) {
Cdc: encCfg.Codec,
Rand: r,
NumBonded: 3,
BondDenom: sdk.DefaultBondDenom,
Accounts: simtypes.RandomAccounts(r, 3),
InitialStake: math.NewInt(1000),
GenState: make(map[string]json.RawMessage),
Expand Down
2 changes: 1 addition & 1 deletion x/staking/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func RandomizedGenState(simState *module.SimulationState) {
// NOTE: the slashing module need to be defined after the staking module on the
// NewSimulationManager constructor for this to work
simState.UnbondTime = unbondTime
params := types.NewParams(simState.UnbondTime, maxVals, 7, histEntries, sdk.DefaultBondDenom, minCommissionRate)
params := types.NewParams(simState.UnbondTime, maxVals, 7, histEntries, simState.BondDenom, minCommissionRate)

// validators & delegations
var (
Expand Down
2 changes: 2 additions & 0 deletions x/staking/simulation/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/staking/simulation"
Expand All @@ -32,6 +33,7 @@ func TestRandomizedGenState(t *testing.T) {
Cdc: cdc,
Rand: r,
NumBonded: 3,
BondDenom: sdk.DefaultBondDenom,
Accounts: simtypes.RandomAccounts(r, 3),
InitialStake: sdkmath.NewInt(1000),
GenState: make(map[string]json.RawMessage),
Expand Down

0 comments on commit 91a916e

Please sign in to comment.