cosmos-sdk/x/mint/simulation/genesis.go
Rafael Tenfen 91a916ec1a
refactor: add option to set bondDenom on simulations (#14529)
## 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)
2023-01-08 18:21:51 +00:00

95 lines
2.7 KiB
Go

package simulation
import (
"encoding/json"
"fmt"
"math/rand"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/mint/types"
)
// Simulation parameter constants
const (
Inflation = "inflation"
InflationRateChange = "inflation_rate_change"
InflationMax = "inflation_max"
InflationMin = "inflation_min"
GoalBonded = "goal_bonded"
)
// GenInflation randomized Inflation
func GenInflation(r *rand.Rand) math.LegacyDec {
return sdk.NewDecWithPrec(int64(r.Intn(99)), 2)
}
// GenInflationRateChange randomized InflationRateChange
func GenInflationRateChange(r *rand.Rand) math.LegacyDec {
return sdk.NewDecWithPrec(int64(r.Intn(99)), 2)
}
// GenInflationMax randomized InflationMax
func GenInflationMax(r *rand.Rand) math.LegacyDec {
return sdk.NewDecWithPrec(20, 2)
}
// GenInflationMin randomized InflationMin
func GenInflationMin(r *rand.Rand) math.LegacyDec {
return sdk.NewDecWithPrec(7, 2)
}
// GenGoalBonded randomized GoalBonded
func GenGoalBonded(r *rand.Rand) math.LegacyDec {
return sdk.NewDecWithPrec(67, 2)
}
// RandomizedGenState generates a random GenesisState for mint
func RandomizedGenState(simState *module.SimulationState) {
// minter
var inflation sdk.Dec
simState.AppParams.GetOrGenerate(
simState.Cdc, Inflation, &inflation, simState.Rand,
func(r *rand.Rand) { inflation = GenInflation(r) },
)
// params
var inflationRateChange sdk.Dec
simState.AppParams.GetOrGenerate(
simState.Cdc, InflationRateChange, &inflationRateChange, simState.Rand,
func(r *rand.Rand) { inflationRateChange = GenInflationRateChange(r) },
)
var inflationMax sdk.Dec
simState.AppParams.GetOrGenerate(
simState.Cdc, InflationMax, &inflationMax, simState.Rand,
func(r *rand.Rand) { inflationMax = GenInflationMax(r) },
)
var inflationMin sdk.Dec
simState.AppParams.GetOrGenerate(
simState.Cdc, InflationMin, &inflationMin, simState.Rand,
func(r *rand.Rand) { inflationMin = GenInflationMin(r) },
)
var goalBonded sdk.Dec
simState.AppParams.GetOrGenerate(
simState.Cdc, GoalBonded, &goalBonded, simState.Rand,
func(r *rand.Rand) { goalBonded = GenGoalBonded(r) },
)
mintDenom := simState.BondDenom
blocksPerYear := uint64(60 * 60 * 8766 / 5)
params := types.NewParams(mintDenom, inflationRateChange, inflationMax, inflationMin, goalBonded, blocksPerYear)
mintGenesis := types.NewGenesisState(types.InitialMinter(inflation), params)
bz, err := json.MarshalIndent(&mintGenesis, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("Selected randomly generated minting parameters:\n%s\n", bz)
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(mintGenesis)
}