Skip to content

Commit

Permalink
fix: fix sims (#14827)
Browse files Browse the repository at this point in the history
(cherry picked from commit 5f08a5c)

# Conflicts:
#	x/gov/simulation/operations.go
  • Loading branch information
julienrbrt authored and mergify[bot] committed Jan 28, 2023
1 parent a9c18cd commit 25d1c21
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 42 deletions.
70 changes: 28 additions & 42 deletions x/gov/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ var (
TypeMsgVote = sdk.MsgTypeURL(&v1.MsgVote{})
TypeMsgVoteWeighted = sdk.MsgTypeURL(&v1.MsgVoteWeighted{})
TypeMsgSubmitProposal = sdk.MsgTypeURL(&v1.MsgSubmitProposal{})
<<<<<<< HEAD
=======
TypeMsgCancelProposal = sdk.MsgTypeURL(&v1.MsgCancelProposal{})
>>>>>>> 5f08a5c9b (fix: fix sims (#14827))
)

// Simulation operation weights constants
Expand Down Expand Up @@ -73,7 +77,8 @@ func WeightedOperations(appParams simtypes.AppParams, cdc codec.JSONCodec, ak ty
wMsg := wMsg // pin variable
var weight int
appParams.GetOrGenerate(cdc, wMsg.AppParamsKey(), &weight, nil,
func(_ *rand.Rand) { weight = wMsg.DefaultWeight() })
func(_ *rand.Rand) { weight = wMsg.DefaultWeight() },
)

wProposalOps = append(
wProposalOps,
Expand All @@ -90,7 +95,8 @@ func WeightedOperations(appParams simtypes.AppParams, cdc codec.JSONCodec, ak ty
wContent := wContent // pin variable
var weight int
appParams.GetOrGenerate(cdc, wContent.AppParamsKey(), &weight, nil,
func(_ *rand.Rand) { weight = wContent.DefaultWeight() })
func(_ *rand.Rand) { weight = wContent.DefaultWeight() },
)

wLegacyProposalOps = append(
wLegacyProposalOps,
Expand Down Expand Up @@ -123,73 +129,41 @@ func WeightedOperations(appParams simtypes.AppParams, cdc codec.JSONCodec, ak ty
// voting on the proposal, and subsequently slashing the proposal. It is implemented using
// future operations.
func SimulateMsgSubmitProposal(ak types.AccountKeeper, bk types.BankKeeper, k *keeper.Keeper, msgSim simtypes.MsgSimulatorFn) simtypes.Operation {
return func(
r *rand.Rand,
app *baseapp.BaseApp,
ctx sdk.Context,
accs []simtypes.Account,
chainID string,
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
simAccount, _ := simtypes.RandomAcc(r, accs)
deposit, skip, err := randomDeposit(r, ctx, ak, bk, k, simAccount.Address, true)
switch {
case skip:
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSubmitProposal, "skip deposit"), nil, nil
case err != nil:
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSubmitProposal, "unable to generate deposit"), nil, err
}

msgs := []sdk.Msg{}
proposalMsg := msgSim(r, ctx, accs)
if proposalMsg != nil {
msgs = append(msgs, proposalMsg)
}

return simulateMsgSubmitProposal(ak, bk, k, msgs, simAccount, deposit)(r, app, ctx, accs, chainID)
return simulateMsgSubmitProposal(ak, bk, k, msgs)(r, app, ctx, accs, chainID)
}
}

// SimulateMsgSubmitLegacyProposal simulates creating a msg Submit Proposal
// voting on the proposal, and subsequently slashing the proposal. It is implemented using
// future operations.
func SimulateMsgSubmitLegacyProposal(ak types.AccountKeeper, bk types.BankKeeper, k *keeper.Keeper, contentSim simtypes.ContentSimulatorFn) simtypes.Operation { //nolint:staticcheck
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simtypes.Account, chainID string,
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
// 1) submit proposal now
content := contentSim(r, ctx, accs)
if content == nil {
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSubmitProposal, "content is nil"), nil, nil
}

simAccount, _ := simtypes.RandomAcc(r, accs)
deposit, skip, err := randomDeposit(r, ctx, ak, bk, k, simAccount.Address, true)
switch {
case skip:
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSubmitProposal, "skip deposit"), nil, nil
case err != nil:
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSubmitProposal, "unable to generate deposit"), nil, err
}

macc := k.GetGovernanceAccount(ctx)
contentMsg, err := v1.NewLegacyContent(content, macc.GetAddress().String())
govacc := k.GetGovernanceAccount(ctx)
contentMsg, err := v1.NewLegacyContent(content, govacc.GetAddress().String())
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSubmitProposal, "error converting legacy content into proposal message"), nil, err
}

return simulateMsgSubmitProposal(ak, bk, k, []sdk.Msg{contentMsg}, simAccount, deposit)(r, app, ctx, accs, chainID)
return simulateMsgSubmitProposal(ak, bk, k, []sdk.Msg{contentMsg})(r, app, ctx, accs, chainID)
}
}

func simulateMsgSubmitProposal(
ak types.AccountKeeper,
bk types.BankKeeper,
k *keeper.Keeper,
proposalMsgs []sdk.Msg,
simAccount simtypes.Account,
deposit sdk.Coins,
) simtypes.Operation {
func simulateMsgSubmitProposal(ak types.AccountKeeper, bk types.BankKeeper, k *keeper.Keeper, proposalMsgs []sdk.Msg) simtypes.Operation {
// The states are:
// column 1: All validators vote
// column 2: 90% vote
Expand Down Expand Up @@ -218,6 +192,15 @@ func simulateMsgSubmitProposal(
accs []simtypes.Account,
chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
simAccount, _ := simtypes.RandomAcc(r, accs)
deposit, skip, err := randomDeposit(r, ctx, ak, bk, k, simAccount.Address, true)
switch {
case skip:
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSubmitProposal, "skip deposit"), nil, nil
case err != nil:
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSubmitProposal, "unable to generate deposit"), nil, err
}

msg, err := v1.NewMsgSubmitProposal(
proposalMsgs,
deposit,
Expand All @@ -231,6 +214,7 @@ func simulateMsgSubmitProposal(
}

account := ak.GetAccount(ctx, simAccount.Address)
<<<<<<< HEAD
spendable := bk.SpendableCoins(ctx, account.GetAddress())

var fees sdk.Coins
Expand All @@ -242,12 +226,14 @@ func simulateMsgSubmitProposal(
}
}

=======
>>>>>>> 5f08a5c9b (fix: fix sims (#14827))
txGen := moduletestutil.MakeTestEncodingConfig().TxConfig
tx, err := simtestutil.GenSignedMockTx(
r,
txGen,
[]sdk.Msg{msg},
fees,
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)},
simtestutil.DefaultGenTxGas,
chainID,
[]uint64{account.GetAccountNumber()},
Expand Down
1 change: 1 addition & 0 deletions x/gov/simulation/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func TestWeightedOperations(t *testing.T) {
{0, types.ModuleName, simulation.TypeMsgSubmitProposal},
}

require.Equal(t, len(weightesOps), len(expected), "number of operations should be the same")
for i, w := range weightesOps {
operationMsg, _, err := w.Op()(r, app.BaseApp, ctx, accs, ctx.ChainID())
require.NoError(t, err)
Expand Down

0 comments on commit 25d1c21

Please sign in to comment.