cosmos-sdk/x/gov/simulation/genesis.go
Callum Waters eb5b11be8a
feat: implement new gov msg & query servers (#10868)
## Description

Ref: #9438

This PR performs the major work of swapping out the v1beta1 msg server and query server for the new one which can process a proposal as an array of messages. This PR still retains the legacy servers which simply wrap around the new ones, providing the same interface as before.

In order to keep backwards compatibility, a new msg, `MsgExecLegacyContent` has been created which allows `Content` to become a `Msg` type and still be used as part of the new implementation. 


---

### 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)
2022-01-21 11:14:00 +00:00

112 lines
3.7 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/gov/types"
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta2"
)
// Simulation parameter constants
const (
DepositParamsMinDeposit = "deposit_params_min_deposit"
DepositParamsDepositPeriod = "deposit_params_deposit_period"
VotingParamsVotingPeriod = "voting_params_voting_period"
TallyParamsQuorum = "tally_params_quorum"
TallyParamsThreshold = "tally_params_threshold"
TallyParamsVeto = "tally_params_veto"
)
// GenDepositParamsDepositPeriod randomized DepositParamsDepositPeriod
func GenDepositParamsDepositPeriod(r *rand.Rand) time.Duration {
return time.Duration(simulation.RandIntBetween(r, 1, 2*60*60*24*2)) * time.Second
}
// GenDepositParamsMinDeposit randomized DepositParamsMinDeposit
func GenDepositParamsMinDeposit(r *rand.Rand) sdk.Coins {
return sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, int64(simulation.RandIntBetween(r, 1, 1e3))))
}
// GenVotingParamsVotingPeriod randomized VotingParamsVotingPeriod
func GenVotingParamsVotingPeriod(r *rand.Rand) time.Duration {
return time.Duration(simulation.RandIntBetween(r, 1, 2*60*60*24*2)) * time.Second
}
// GenTallyParamsQuorum randomized TallyParamsQuorum
func GenTallyParamsQuorum(r *rand.Rand) sdk.Dec {
return sdk.NewDecWithPrec(int64(simulation.RandIntBetween(r, 334, 500)), 3)
}
// GenTallyParamsThreshold randomized TallyParamsThreshold
func GenTallyParamsThreshold(r *rand.Rand) sdk.Dec {
return sdk.NewDecWithPrec(int64(simulation.RandIntBetween(r, 450, 550)), 3)
}
// GenTallyParamsVeto randomized TallyParamsVeto
func GenTallyParamsVeto(r *rand.Rand) sdk.Dec {
return sdk.NewDecWithPrec(int64(simulation.RandIntBetween(r, 250, 334)), 3)
}
// RandomizedGenState generates a random GenesisState for gov
func RandomizedGenState(simState *module.SimulationState) {
startingProposalID := uint64(simState.Rand.Intn(100))
var minDeposit sdk.Coins
simState.AppParams.GetOrGenerate(
simState.Cdc, DepositParamsMinDeposit, &minDeposit, simState.Rand,
func(r *rand.Rand) { minDeposit = GenDepositParamsMinDeposit(r) },
)
var depositPeriod time.Duration
simState.AppParams.GetOrGenerate(
simState.Cdc, DepositParamsDepositPeriod, &depositPeriod, simState.Rand,
func(r *rand.Rand) { depositPeriod = GenDepositParamsDepositPeriod(r) },
)
var votingPeriod time.Duration
simState.AppParams.GetOrGenerate(
simState.Cdc, VotingParamsVotingPeriod, &votingPeriod, simState.Rand,
func(r *rand.Rand) { votingPeriod = GenVotingParamsVotingPeriod(r) },
)
var quorum sdk.Dec
simState.AppParams.GetOrGenerate(
simState.Cdc, TallyParamsQuorum, &quorum, simState.Rand,
func(r *rand.Rand) { quorum = GenTallyParamsQuorum(r) },
)
var threshold sdk.Dec
simState.AppParams.GetOrGenerate(
simState.Cdc, TallyParamsThreshold, &threshold, simState.Rand,
func(r *rand.Rand) { threshold = GenTallyParamsThreshold(r) },
)
var veto sdk.Dec
simState.AppParams.GetOrGenerate(
simState.Cdc, TallyParamsVeto, &veto, simState.Rand,
func(r *rand.Rand) { veto = GenTallyParamsVeto(r) },
)
govGenesis := v1beta2.NewGenesisState(
startingProposalID,
v1beta2.NewDepositParams(minDeposit, depositPeriod),
v1beta2.NewVotingParams(votingPeriod),
v1beta2.NewTallyParams(quorum, threshold, veto),
)
bz, err := json.MarshalIndent(&govGenesis, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("Selected randomly generated governance parameters:\n%s\n", bz)
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(govGenesis)
}