<!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Added the ability to set a minimum commission rate that all validators cannot set their commission rate below. replaces https://github.com/cosmos/cosmos-sdk/pull/10422 --- ### 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... - [ ] 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 - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] 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)
111 lines
3.3 KiB
Go
111 lines
3.3 KiB
Go
package simulation
|
|
|
|
// DONTCOVER
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"math/rand"
|
|
"time"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
|
"github.com/cosmos/cosmos-sdk/types/simulation"
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
)
|
|
|
|
// Simulation parameter constants
|
|
const (
|
|
unbondingTime = "unbonding_time"
|
|
maxValidators = "max_validators"
|
|
historicalEntries = "historical_entries"
|
|
)
|
|
|
|
// genUnbondingTime returns randomized UnbondingTime
|
|
func genUnbondingTime(r *rand.Rand) (ubdTime time.Duration) {
|
|
return time.Duration(simulation.RandIntBetween(r, 60, 60*60*24*3*2)) * time.Second
|
|
}
|
|
|
|
// genMaxValidators returns randomized MaxValidators
|
|
func genMaxValidators(r *rand.Rand) (maxValidators uint32) {
|
|
return uint32(r.Intn(250) + 1)
|
|
}
|
|
|
|
// getHistEntries returns randomized HistoricalEntries between 0-100.
|
|
func getHistEntries(r *rand.Rand) uint32 {
|
|
return uint32(r.Intn(int(types.DefaultHistoricalEntries + 1)))
|
|
}
|
|
|
|
// RandomizedGenState generates a random GenesisState for staking
|
|
func RandomizedGenState(simState *module.SimulationState) {
|
|
// params
|
|
var (
|
|
unbondTime time.Duration
|
|
maxVals uint32
|
|
histEntries uint32
|
|
minCommissionRate sdk.Dec
|
|
)
|
|
|
|
simState.AppParams.GetOrGenerate(
|
|
simState.Cdc, unbondingTime, &unbondTime, simState.Rand,
|
|
func(r *rand.Rand) { unbondTime = genUnbondingTime(r) },
|
|
)
|
|
|
|
simState.AppParams.GetOrGenerate(
|
|
simState.Cdc, maxValidators, &maxVals, simState.Rand,
|
|
func(r *rand.Rand) { maxVals = genMaxValidators(r) },
|
|
)
|
|
|
|
simState.AppParams.GetOrGenerate(
|
|
simState.Cdc, historicalEntries, &histEntries, simState.Rand,
|
|
func(r *rand.Rand) { histEntries = getHistEntries(r) },
|
|
)
|
|
|
|
// 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)
|
|
|
|
// validators & delegations
|
|
var (
|
|
validators []types.Validator
|
|
delegations []types.Delegation
|
|
)
|
|
|
|
valAddrs := make([]sdk.ValAddress, simState.NumBonded)
|
|
|
|
for i := 0; i < int(simState.NumBonded); i++ {
|
|
valAddr := sdk.ValAddress(simState.Accounts[i].Address)
|
|
valAddrs[i] = valAddr
|
|
|
|
maxCommission := sdk.NewDecWithPrec(int64(simulation.RandIntBetween(simState.Rand, 1, 100)), 2)
|
|
commission := types.NewCommission(
|
|
simulation.RandomDecAmount(simState.Rand, maxCommission),
|
|
maxCommission,
|
|
simulation.RandomDecAmount(simState.Rand, maxCommission),
|
|
)
|
|
|
|
validator, err := types.NewValidator(valAddr, simState.Accounts[i].ConsKey.PubKey(), types.Description{})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
validator.Tokens = sdk.NewInt(simState.InitialStake)
|
|
validator.DelegatorShares = sdk.NewDec(simState.InitialStake)
|
|
validator.Commission = commission
|
|
|
|
delegation := types.NewDelegation(simState.Accounts[i].Address, valAddr, sdk.NewDec(simState.InitialStake))
|
|
|
|
validators = append(validators, validator)
|
|
delegations = append(delegations, delegation)
|
|
}
|
|
|
|
stakingGenesis := types.NewGenesisState(params, validators, delegations)
|
|
|
|
bz, err := json.MarshalIndent(&stakingGenesis.Params, "", " ")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Printf("Selected randomly generated staking parameters:\n%s\n", bz)
|
|
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(stakingGenesis)
|
|
}
|