Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
/ cosmos-sdk Public archive
forked from cosmos/cosmos-sdk

Commit

Permalink
fix: fix sims (cosmos#14827)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored and tsenart committed Apr 12, 2023
1 parent 12d12c0 commit 74d8abb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 62 deletions.
75 changes: 21 additions & 54 deletions x/gov/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ var (
TypeMsgVoteWeighted = sdk.MsgTypeURL(&v1.MsgVoteWeighted{})
TypeMsgSubmitProposal = sdk.MsgTypeURL(&v1.MsgSubmitProposal{})
TypeMsgCancelProposal = sdk.MsgTypeURL(&v1.MsgCancelProposal{})
TypeMsgUpdateParams = sdk.MsgTypeURL(&v1.MsgUpdateParams{})
)

// Simulation operation weights constants
Expand Down Expand Up @@ -84,7 +83,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 @@ -101,7 +101,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 @@ -138,73 +139,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 @@ -233,6 +202,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 @@ -246,23 +224,12 @@ func simulateMsgSubmitProposal(
}

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

var fees sdk.Coins
coins, hasNeg := spendable.SafeSub(deposit...)
if !hasNeg {
fees, err = simtypes.RandomFees(r, ctx, coins)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to generate fees"), nil, err
}
}

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
4 changes: 2 additions & 2 deletions x/gov/simulation/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"
"time"

storetypes "cosmossdk.io/store/types"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
Expand Down Expand Up @@ -115,8 +114,9 @@ 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.WithBlockGasMeter(storetypes.NewInfiniteGasMeter()), accs, ctx.ChainID())
operationMsg, _, err := w.Op()(r, app.BaseApp, ctx, accs, ctx.ChainID())
require.NoError(t, err)

// the following checks are very much dependent from the ordering of the output given
Expand Down
6 changes: 0 additions & 6 deletions x/nft/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,6 @@ func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
simulation.RandomizedGenState(simState)
}

// ProposalContents returns all the nft content functions used to
// simulate governance proposals.
func (am AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent {
return nil
}

// RegisterStoreDecoder registers a decoder for nft module's types
func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
sdr[keeper.StoreKey] = simulation.NewDecodeStore(am.cdc)
Expand Down

0 comments on commit 74d8abb

Please sign in to comment.