2019-10-23 02:14:45 -07:00
|
|
|
package simulation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
2019-12-05 01:29:54 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2019-10-23 02:14:45 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp/helpers"
|
2019-12-05 01:29:54 -08:00
|
|
|
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
|
2019-10-23 02:14:45 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-03-23 04:55:44 -07:00
|
|
|
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
2019-10-23 02:14:45 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/gov/keeper"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/simulation"
|
|
|
|
)
|
|
|
|
|
|
|
|
var initialProposalID = uint64(100000000000000)
|
|
|
|
|
2019-12-05 01:29:54 -08:00
|
|
|
// Simulation operation weights constants
|
|
|
|
const (
|
2020-11-03 03:48:53 -08:00
|
|
|
OpWeightMsgDeposit = "op_weight_msg_deposit"
|
|
|
|
OpWeightMsgVote = "op_weight_msg_vote"
|
2021-01-24 17:53:45 -08:00
|
|
|
OpWeightMsgVoteWeighted = "op_weight_msg_weighted_vote"
|
2019-12-05 01:29:54 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// WeightedOperations returns all the operations from the module with their respective weights
|
2020-01-30 13:31:16 -08:00
|
|
|
func WeightedOperations(
|
2021-04-29 03:46:22 -07:00
|
|
|
appParams simtypes.AppParams, cdc codec.JSONCodec, ak types.AccountKeeper,
|
2020-03-23 04:55:44 -07:00
|
|
|
bk types.BankKeeper, k keeper.Keeper, wContents []simtypes.WeightedProposalContent,
|
2020-01-30 13:31:16 -08:00
|
|
|
) simulation.WeightedOperations {
|
2019-12-05 01:29:54 -08:00
|
|
|
|
|
|
|
var (
|
2020-11-03 03:48:53 -08:00
|
|
|
weightMsgDeposit int
|
|
|
|
weightMsgVote int
|
2021-01-24 17:53:45 -08:00
|
|
|
weightMsgVoteWeighted int
|
2019-12-05 01:29:54 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
appParams.GetOrGenerate(cdc, OpWeightMsgDeposit, &weightMsgDeposit, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
weightMsgDeposit = simappparams.DefaultWeightMsgDeposit
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
appParams.GetOrGenerate(cdc, OpWeightMsgVote, &weightMsgVote, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
weightMsgVote = simappparams.DefaultWeightMsgVote
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-01-24 17:53:45 -08:00
|
|
|
appParams.GetOrGenerate(cdc, OpWeightMsgVoteWeighted, &weightMsgVoteWeighted, nil,
|
2020-11-03 03:48:53 -08:00
|
|
|
func(_ *rand.Rand) {
|
2021-01-24 17:53:45 -08:00
|
|
|
weightMsgVoteWeighted = simappparams.DefaultWeightMsgVoteWeighted
|
2020-11-03 03:48:53 -08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2019-12-05 01:29:54 -08:00
|
|
|
// generate the weighted operations for the proposal contents
|
|
|
|
var wProposalOps simulation.WeightedOperations
|
|
|
|
|
|
|
|
for _, wContent := range wContents {
|
|
|
|
wContent := wContent // pin variable
|
|
|
|
var weight int
|
2020-03-23 04:55:44 -07:00
|
|
|
appParams.GetOrGenerate(cdc, wContent.AppParamsKey(), &weight, nil,
|
|
|
|
func(_ *rand.Rand) { weight = wContent.DefaultWeight() })
|
2019-12-05 01:29:54 -08:00
|
|
|
|
|
|
|
wProposalOps = append(
|
|
|
|
wProposalOps,
|
|
|
|
simulation.NewWeightedOperation(
|
|
|
|
weight,
|
2020-07-15 02:44:53 -07:00
|
|
|
SimulateMsgSubmitProposal(ak, bk, k, wContent.ContentSimulatorFn()),
|
2019-12-05 01:29:54 -08:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
wGovOps := simulation.WeightedOperations{
|
|
|
|
simulation.NewWeightedOperation(
|
|
|
|
weightMsgDeposit,
|
2020-01-30 13:31:16 -08:00
|
|
|
SimulateMsgDeposit(ak, bk, k),
|
2019-12-05 01:29:54 -08:00
|
|
|
),
|
|
|
|
simulation.NewWeightedOperation(
|
|
|
|
weightMsgVote,
|
2020-01-30 13:31:16 -08:00
|
|
|
SimulateMsgVote(ak, bk, k),
|
2019-12-05 01:29:54 -08:00
|
|
|
),
|
2020-11-03 03:48:53 -08:00
|
|
|
simulation.NewWeightedOperation(
|
2021-01-24 17:53:45 -08:00
|
|
|
weightMsgVoteWeighted,
|
|
|
|
SimulateMsgVoteWeighted(ak, bk, k),
|
2020-11-03 03:48:53 -08:00
|
|
|
),
|
2019-12-05 01:29:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return append(wProposalOps, wGovOps...)
|
|
|
|
}
|
2019-10-23 02:14:45 -07:00
|
|
|
|
2020-11-03 03:48:53 -08:00
|
|
|
// SimulateMsgSubmitProposal simulates creating a msg Submit Proposal
|
2019-10-23 02:14:45 -07:00
|
|
|
// voting on the proposal, and subsequently slashing the proposal. It is implemented using
|
|
|
|
// future operations.
|
2020-07-15 02:44:53 -07:00
|
|
|
func SimulateMsgSubmitProposal(
|
2020-03-23 04:55:44 -07:00
|
|
|
ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper, contentSim simtypes.ContentSimulatorFn,
|
|
|
|
) simtypes.Operation {
|
2019-10-23 02:14:45 -07:00
|
|
|
// The states are:
|
|
|
|
// column 1: All validators vote
|
|
|
|
// column 2: 90% vote
|
|
|
|
// column 3: 75% vote
|
|
|
|
// column 4: 40% vote
|
|
|
|
// column 5: 15% vote
|
|
|
|
// column 6: noone votes
|
|
|
|
// All columns sum to 100 for simplicity, values chosen by @valardragon semi-arbitrarily,
|
|
|
|
// feel free to change.
|
|
|
|
numVotesTransitionMatrix, _ := simulation.CreateTransitionMatrix([][]int{
|
|
|
|
{20, 10, 0, 0, 0, 0},
|
|
|
|
{55, 50, 20, 10, 0, 0},
|
|
|
|
{25, 25, 30, 25, 30, 15},
|
|
|
|
{0, 15, 30, 25, 30, 30},
|
|
|
|
{0, 0, 20, 30, 30, 30},
|
|
|
|
{0, 0, 0, 10, 10, 25},
|
|
|
|
})
|
|
|
|
|
|
|
|
statePercentageArray := []float64{1, .9, .75, .4, .15, 0}
|
|
|
|
curNumVotesState := 1
|
|
|
|
|
|
|
|
return func(
|
|
|
|
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
|
2020-03-23 04:55:44 -07:00
|
|
|
accs []simtypes.Account, chainID string,
|
|
|
|
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
|
2019-10-23 02:14:45 -07:00
|
|
|
// 1) submit proposal now
|
|
|
|
content := contentSim(r, ctx, accs)
|
|
|
|
if content == nil {
|
2020-05-26 09:52:23 -07:00
|
|
|
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSubmitProposal, "content is nil"), nil, nil
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
|
2020-03-23 04:55:44 -07:00
|
|
|
simAccount, _ := simtypes.RandomAcc(r, accs)
|
2020-01-30 13:31:16 -08:00
|
|
|
deposit, skip, err := randomDeposit(r, ctx, ak, bk, k, simAccount.Address)
|
2019-10-23 02:14:45 -07:00
|
|
|
switch {
|
|
|
|
case skip:
|
2020-05-26 09:52:23 -07:00
|
|
|
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSubmitProposal, "skip deposit"), nil, nil
|
2019-10-23 02:14:45 -07:00
|
|
|
case err != nil:
|
2020-05-26 09:52:23 -07:00
|
|
|
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSubmitProposal, "unable to generate deposit"), nil, err
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
|
2020-05-19 13:17:29 -07:00
|
|
|
msg, err := types.NewMsgSubmitProposal(content, deposit, simAccount.Address)
|
2020-04-14 12:05:14 -07:00
|
|
|
if err != nil {
|
2020-05-26 09:52:23 -07:00
|
|
|
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate a submit proposal msg"), nil, err
|
2020-04-14 12:05:14 -07:00
|
|
|
}
|
2019-10-23 02:14:45 -07:00
|
|
|
|
|
|
|
account := ak.GetAccount(ctx, simAccount.Address)
|
2020-01-30 13:31:16 -08:00
|
|
|
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
2019-10-23 02:14:45 -07:00
|
|
|
|
|
|
|
var fees sdk.Coins
|
2020-01-30 13:31:16 -08:00
|
|
|
coins, hasNeg := spendable.SafeSub(deposit)
|
2019-10-23 02:14:45 -07:00
|
|
|
if !hasNeg {
|
2020-03-23 04:55:44 -07:00
|
|
|
fees, err = simtypes.RandomFees(r, ctx, coins)
|
2019-10-23 02:14:45 -07:00
|
|
|
if err != nil {
|
2020-05-26 09:52:23 -07:00
|
|
|
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate fees"), nil, err
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 04:33:48 -07:00
|
|
|
txGen := simappparams.MakeTestEncodingConfig().TxConfig
|
2020-07-14 10:33:31 -07:00
|
|
|
tx, err := helpers.GenTx(
|
|
|
|
txGen,
|
2019-10-23 02:14:45 -07:00
|
|
|
[]sdk.Msg{msg},
|
|
|
|
fees,
|
2019-12-18 06:48:22 -08:00
|
|
|
helpers.DefaultGenTxGas,
|
2019-10-23 02:14:45 -07:00
|
|
|
chainID,
|
|
|
|
[]uint64{account.GetAccountNumber()},
|
|
|
|
[]uint64{account.GetSequence()},
|
|
|
|
simAccount.PrivKey,
|
|
|
|
)
|
2020-07-14 10:33:31 -07:00
|
|
|
if err != nil {
|
|
|
|
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
|
|
|
|
}
|
2019-10-23 02:14:45 -07:00
|
|
|
|
refactor!: BaseApp {Check,Deliver}Tx with middleware design (#9920)
<!--
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
ref: #9585
This PR if the 1st step (out of 2) in the #9585 refactor. It transforms baseapp's ABCI {Check,Deliver}Tx into middleware stacks. A middleware is defined by the following interfaces:
```go
// types/tx package
type Handler interface {
CheckTx(ctx context.Context, tx sdk.Tx, req abci.RequestCheckTx) (abci.ResponseCheckTx, error)
DeliverTx(ctx context.Context, tx sdk.Tx, req abci.RequestDeliverTx) (abci.ResponseDeliverTx, error)
SimulateTx(ctx context.Context, tx sdk.Tx, req RequestSimulateTx) (ResponseSimulateTx, error)
}
type Middleware func(Handler) Handler
```
This Pr doesn't migrate antehandlers, but only baseapp's runTx and runMsgs as middlewares. It bundles antehandlers into one single middleware (for now).
More specifically, it introduces the 5 following middlewares:
| Middleware | Description |
| ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| RunMsgsTxMiddleware | This middleware replaces the old baseapp's `runMsgs`. |
| LegacyAnteMiddleware | This middleware is temporary, it bundles all antehandlers into one middleware. It's only created for the purpose of breaking the refactor into 2 pieces. **It will be removed in Part 2**, and each antehandler will be replaced by its own middleware. |
| IndexEventsTxMiddleware | This is a simple middleware that chooses which events to index in Tendermint. Replaces `baseapp.indexEvents` (which unfortunately still exists in baseapp too, because it's used to index Begin/EndBlock events) |
| RecoveryTxMiddleware | This index recovers from panics. It replaces baseapp.runTx's panic recovery. |
| GasTxMiddleware | This replaces the [`Setup`](https://github.com/cosmos/cosmos-sdk/blob/master/x/auth/ante/setup.go) Antehandler. It sets a GasMeter on sdk.Context. Note that before, GasMeter was set on sdk.Context inside the antehandlers, and there was some mess around the fact that antehandlers had their own panic recovery system so that the GasMeter could be read by baseapp's recovery system. Now, this mess is all removed: one middleware sets GasMeter, another one handles recovery. |
---
### 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
- [x] 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/master/CONTRIBUTING.md#pr-targeting))
- [x] 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)
- [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] 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)
2021-08-25 07:40:33 -07:00
|
|
|
_, _, err = app.SimDeliver(txGen.TxEncoder(), tx)
|
2019-12-27 09:57:54 -08:00
|
|
|
if err != nil {
|
2020-05-26 09:52:23 -07:00
|
|
|
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
|
2021-01-25 08:41:30 -08:00
|
|
|
opMsg := simtypes.NewOperationMsg(msg, true, "", nil)
|
2019-10-23 02:14:45 -07:00
|
|
|
|
|
|
|
// get the submitted proposal ID
|
|
|
|
proposalID, err := k.GetProposalID(ctx)
|
|
|
|
if err != nil {
|
2020-05-26 09:52:23 -07:00
|
|
|
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate proposalID"), nil, err
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2) Schedule operations for votes
|
|
|
|
// 2.1) first pick a number of people to vote.
|
|
|
|
curNumVotesState = numVotesTransitionMatrix.NextState(r, curNumVotesState)
|
|
|
|
numVotes := int(math.Ceil(float64(len(accs)) * statePercentageArray[curNumVotesState]))
|
|
|
|
|
|
|
|
// 2.2) select who votes and when
|
|
|
|
whoVotes := r.Perm(len(accs))
|
|
|
|
|
|
|
|
// didntVote := whoVotes[numVotes:]
|
|
|
|
whoVotes = whoVotes[:numVotes]
|
|
|
|
votingPeriod := k.GetVotingParams(ctx).VotingPeriod
|
|
|
|
|
2020-03-23 04:55:44 -07:00
|
|
|
fops := make([]simtypes.FutureOperation, numVotes+1)
|
2019-10-23 02:14:45 -07:00
|
|
|
for i := 0; i < numVotes; i++ {
|
|
|
|
whenVote := ctx.BlockHeader().Time.Add(time.Duration(r.Int63n(int64(votingPeriod.Seconds()))) * time.Second)
|
2020-03-23 04:55:44 -07:00
|
|
|
fops[i] = simtypes.FutureOperation{
|
2019-10-23 02:14:45 -07:00
|
|
|
BlockTime: whenVote,
|
2020-01-30 13:31:16 -08:00
|
|
|
Op: operationSimulateMsgVote(ak, bk, k, accs[whoVotes[i]], int64(proposalID)),
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return opMsg, fops, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SimulateMsgDeposit generates a MsgDeposit with random values.
|
2020-03-23 04:55:44 -07:00
|
|
|
func SimulateMsgDeposit(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) simtypes.Operation {
|
2019-10-23 02:14:45 -07:00
|
|
|
return func(
|
|
|
|
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
|
2020-03-23 04:55:44 -07:00
|
|
|
accs []simtypes.Account, chainID string,
|
|
|
|
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
|
|
|
|
simAccount, _ := simtypes.RandomAcc(r, accs)
|
2019-10-23 02:14:45 -07:00
|
|
|
proposalID, ok := randomProposalID(r, k, ctx, types.StatusDepositPeriod)
|
|
|
|
if !ok {
|
2020-05-26 09:52:23 -07:00
|
|
|
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgDeposit, "unable to generate proposalID"), nil, nil
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
deposit, skip, err := randomDeposit(r, ctx, ak, bk, k, simAccount.Address)
|
2019-10-23 02:14:45 -07:00
|
|
|
switch {
|
|
|
|
case skip:
|
2020-05-26 09:52:23 -07:00
|
|
|
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgDeposit, "skip deposit"), nil, nil
|
2019-10-23 02:14:45 -07:00
|
|
|
case err != nil:
|
2020-05-26 09:52:23 -07:00
|
|
|
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgDeposit, "unable to generate deposit"), nil, err
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := types.NewMsgDeposit(simAccount.Address, proposalID, deposit)
|
|
|
|
|
|
|
|
account := ak.GetAccount(ctx, simAccount.Address)
|
2020-01-30 13:31:16 -08:00
|
|
|
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
2019-10-23 02:14:45 -07:00
|
|
|
|
|
|
|
var fees sdk.Coins
|
2020-01-30 13:31:16 -08:00
|
|
|
coins, hasNeg := spendable.SafeSub(deposit)
|
2019-10-23 02:14:45 -07:00
|
|
|
if !hasNeg {
|
2020-03-23 04:55:44 -07:00
|
|
|
fees, err = simtypes.RandomFees(r, ctx, coins)
|
2019-10-23 02:14:45 -07:00
|
|
|
if err != nil {
|
2020-05-26 09:52:23 -07:00
|
|
|
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate fees"), nil, err
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 02:03:24 -07:00
|
|
|
txCtx := simulation.OperationInput{
|
|
|
|
App: app,
|
|
|
|
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
|
|
|
|
Cdc: nil,
|
|
|
|
Msg: msg,
|
|
|
|
MsgType: msg.Type(),
|
|
|
|
Context: ctx,
|
|
|
|
SimAccount: simAccount,
|
|
|
|
AccountKeeper: ak,
|
|
|
|
ModuleName: types.ModuleName,
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
|
2021-06-07 02:03:24 -07:00
|
|
|
return simulation.GenAndDeliverTx(txCtx, fees)
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SimulateMsgVote generates a MsgVote with random values.
|
2020-03-23 04:55:44 -07:00
|
|
|
func SimulateMsgVote(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) simtypes.Operation {
|
|
|
|
return operationSimulateMsgVote(ak, bk, k, simtypes.Account{}, -1)
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
func operationSimulateMsgVote(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper,
|
2020-03-23 04:55:44 -07:00
|
|
|
simAccount simtypes.Account, proposalIDInt int64) simtypes.Operation {
|
2019-10-23 02:14:45 -07:00
|
|
|
return func(
|
|
|
|
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
|
2020-03-23 04:55:44 -07:00
|
|
|
accs []simtypes.Account, chainID string,
|
|
|
|
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
|
|
|
|
if simAccount.Equals(simtypes.Account{}) {
|
|
|
|
simAccount, _ = simtypes.RandomAcc(r, accs)
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var proposalID uint64
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case proposalIDInt < 0:
|
|
|
|
var ok bool
|
|
|
|
proposalID, ok = randomProposalID(r, k, ctx, types.StatusVotingPeriod)
|
|
|
|
if !ok {
|
2020-05-26 09:52:23 -07:00
|
|
|
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgVote, "unable to generate proposalID"), nil, nil
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
proposalID = uint64(proposalIDInt)
|
|
|
|
}
|
|
|
|
|
|
|
|
option := randomVotingOption(r)
|
2020-11-03 03:48:53 -08:00
|
|
|
msg := types.NewMsgVote(simAccount.Address, proposalID, option)
|
|
|
|
|
|
|
|
account := ak.GetAccount(ctx, simAccount.Address)
|
|
|
|
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
|
|
|
|
2021-06-07 02:03:24 -07:00
|
|
|
txCtx := simulation.OperationInput{
|
|
|
|
R: r,
|
|
|
|
App: app,
|
|
|
|
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
|
|
|
|
Cdc: nil,
|
|
|
|
Msg: msg,
|
|
|
|
MsgType: msg.Type(),
|
|
|
|
Context: ctx,
|
|
|
|
SimAccount: simAccount,
|
|
|
|
AccountKeeper: ak,
|
|
|
|
Bankkeeper: bk,
|
|
|
|
ModuleName: types.ModuleName,
|
|
|
|
CoinsSpentInMsg: spendable,
|
2020-11-03 03:48:53 -08:00
|
|
|
}
|
|
|
|
|
2021-06-07 02:03:24 -07:00
|
|
|
return simulation.GenAndDeliverTxWithRandFees(txCtx)
|
2020-11-03 03:48:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 17:53:45 -08:00
|
|
|
// SimulateMsgVoteWeighted generates a MsgVoteWeighted with random values.
|
|
|
|
func SimulateMsgVoteWeighted(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) simtypes.Operation {
|
|
|
|
return operationSimulateMsgVoteWeighted(ak, bk, k, simtypes.Account{}, -1)
|
2020-11-03 03:48:53 -08:00
|
|
|
}
|
|
|
|
|
2021-01-24 17:53:45 -08:00
|
|
|
func operationSimulateMsgVoteWeighted(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper,
|
2020-11-03 03:48:53 -08:00
|
|
|
simAccount simtypes.Account, proposalIDInt int64) simtypes.Operation {
|
|
|
|
return func(
|
|
|
|
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
|
|
|
|
accs []simtypes.Account, chainID string,
|
|
|
|
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
|
|
|
|
if simAccount.Equals(simtypes.Account{}) {
|
|
|
|
simAccount, _ = simtypes.RandomAcc(r, accs)
|
|
|
|
}
|
|
|
|
|
|
|
|
var proposalID uint64
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case proposalIDInt < 0:
|
|
|
|
var ok bool
|
|
|
|
proposalID, ok = randomProposalID(r, k, ctx, types.StatusVotingPeriod)
|
|
|
|
if !ok {
|
2021-01-24 17:53:45 -08:00
|
|
|
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgVoteWeighted, "unable to generate proposalID"), nil, nil
|
2020-11-03 03:48:53 -08:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
proposalID = uint64(proposalIDInt)
|
|
|
|
}
|
|
|
|
|
|
|
|
options := randomWeightedVotingOptions(r)
|
2021-01-24 17:53:45 -08:00
|
|
|
msg := types.NewMsgVoteWeighted(simAccount.Address, proposalID, options)
|
2019-10-23 02:14:45 -07:00
|
|
|
|
|
|
|
account := ak.GetAccount(ctx, simAccount.Address)
|
2020-01-30 13:31:16 -08:00
|
|
|
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
|
|
|
|
2021-06-07 02:03:24 -07:00
|
|
|
txCtx := simulation.OperationInput{
|
|
|
|
R: r,
|
|
|
|
App: app,
|
|
|
|
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
|
|
|
|
Cdc: nil,
|
|
|
|
Msg: msg,
|
|
|
|
MsgType: msg.Type(),
|
|
|
|
Context: ctx,
|
|
|
|
SimAccount: simAccount,
|
|
|
|
AccountKeeper: ak,
|
|
|
|
Bankkeeper: bk,
|
|
|
|
ModuleName: types.ModuleName,
|
|
|
|
CoinsSpentInMsg: spendable,
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
|
2021-06-07 02:03:24 -07:00
|
|
|
return simulation.GenAndDeliverTxWithRandFees(txCtx)
|
2019-10-23 02:14:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pick a random deposit with a random denomination with a
|
|
|
|
// deposit amount between (0, min(balance, minDepositAmount))
|
|
|
|
// This is to simulate multiple users depositing to get the
|
|
|
|
// proposal above the minimum deposit amount
|
|
|
|
func randomDeposit(r *rand.Rand, ctx sdk.Context,
|
2020-01-30 13:31:16 -08:00
|
|
|
ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper, addr sdk.AccAddress,
|
2019-10-23 02:14:45 -07:00
|
|
|
) (deposit sdk.Coins, skip bool, err error) {
|
|
|
|
account := ak.GetAccount(ctx, addr)
|
2020-01-30 13:31:16 -08:00
|
|
|
spendable := bk.SpendableCoins(ctx, account.GetAddress())
|
|
|
|
|
|
|
|
if spendable.Empty() {
|
2019-10-23 02:14:45 -07:00
|
|
|
return nil, true, nil // skip
|
|
|
|
}
|
|
|
|
|
|
|
|
minDeposit := k.GetDepositParams(ctx).MinDeposit
|
|
|
|
denomIndex := r.Intn(len(minDeposit))
|
|
|
|
denom := minDeposit[denomIndex].Denom
|
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
depositCoins := spendable.AmountOf(denom)
|
2019-10-23 02:14:45 -07:00
|
|
|
if depositCoins.IsZero() {
|
|
|
|
return nil, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
maxAmt := depositCoins
|
|
|
|
if maxAmt.GT(minDeposit[denomIndex].Amount) {
|
|
|
|
maxAmt = minDeposit[denomIndex].Amount
|
|
|
|
}
|
|
|
|
|
2020-03-23 04:55:44 -07:00
|
|
|
amount, err := simtypes.RandPositiveInt(r, maxAmt)
|
2019-10-23 02:14:45 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sdk.Coins{sdk.NewCoin(denom, amount)}, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pick a random proposal ID between the initial proposal ID
|
|
|
|
// (defined in gov GenesisState) and the latest proposal ID
|
|
|
|
// that matches a given Status.
|
|
|
|
// It does not provide a default ID.
|
|
|
|
func randomProposalID(r *rand.Rand, k keeper.Keeper,
|
|
|
|
ctx sdk.Context, status types.ProposalStatus) (proposalID uint64, found bool) {
|
|
|
|
proposalID, _ = k.GetProposalID(ctx)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case proposalID > initialProposalID:
|
|
|
|
// select a random ID between [initialProposalID, proposalID]
|
2020-03-23 04:55:44 -07:00
|
|
|
proposalID = uint64(simtypes.RandIntBetween(r, int(initialProposalID), int(proposalID)))
|
2019-10-23 02:14:45 -07:00
|
|
|
|
|
|
|
default:
|
|
|
|
// This is called on the first call to this funcion
|
|
|
|
// in order to update the global variable
|
|
|
|
initialProposalID = proposalID
|
|
|
|
}
|
|
|
|
|
|
|
|
proposal, ok := k.GetProposal(ctx, proposalID)
|
|
|
|
if !ok || proposal.Status != status {
|
|
|
|
return proposalID, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return proposalID, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pick a random voting option
|
|
|
|
func randomVotingOption(r *rand.Rand) types.VoteOption {
|
|
|
|
switch r.Intn(4) {
|
|
|
|
case 0:
|
|
|
|
return types.OptionYes
|
|
|
|
case 1:
|
|
|
|
return types.OptionAbstain
|
|
|
|
case 2:
|
|
|
|
return types.OptionNo
|
|
|
|
case 3:
|
|
|
|
return types.OptionNoWithVeto
|
|
|
|
default:
|
|
|
|
panic("invalid vote option")
|
|
|
|
}
|
|
|
|
}
|
2020-11-03 03:48:53 -08:00
|
|
|
|
|
|
|
// Pick a random weighted voting options
|
|
|
|
func randomWeightedVotingOptions(r *rand.Rand) types.WeightedVoteOptions {
|
|
|
|
w1 := r.Intn(100 + 1)
|
|
|
|
w2 := r.Intn(100 - w1 + 1)
|
|
|
|
w3 := r.Intn(100 - w1 - w2 + 1)
|
|
|
|
w4 := 100 - w1 - w2 - w3
|
|
|
|
weightedVoteOptions := types.WeightedVoteOptions{}
|
|
|
|
if w1 > 0 {
|
|
|
|
weightedVoteOptions = append(weightedVoteOptions, types.WeightedVoteOption{
|
|
|
|
Option: types.OptionYes,
|
|
|
|
Weight: sdk.NewDecWithPrec(int64(w1), 2),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if w2 > 0 {
|
|
|
|
weightedVoteOptions = append(weightedVoteOptions, types.WeightedVoteOption{
|
|
|
|
Option: types.OptionAbstain,
|
|
|
|
Weight: sdk.NewDecWithPrec(int64(w2), 2),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if w3 > 0 {
|
|
|
|
weightedVoteOptions = append(weightedVoteOptions, types.WeightedVoteOption{
|
|
|
|
Option: types.OptionNo,
|
|
|
|
Weight: sdk.NewDecWithPrec(int64(w3), 2),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if w4 > 0 {
|
|
|
|
weightedVoteOptions = append(weightedVoteOptions, types.WeightedVoteOption{
|
|
|
|
Option: types.OptionNoWithVeto,
|
|
|
|
Weight: sdk.NewDecWithPrec(int64(w4), 2),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return weightedVoteOptions
|
|
|
|
}
|