diff --git a/CHANGELOG.md b/CHANGELOG.md index a5d4ab03257d..30f60c32002c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -77,6 +77,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/crisis) [#12208](https://github.com/cosmos/cosmos-sdk/pull/12208) Fix progress index of crisis invariant assertion logs. * (types) [#12229](https://github.com/cosmos/cosmos-sdk/pull/12229) Increase sdk.Dec maxApproxRootIterations to 300 * (x/staking) [#12303](https://github.com/cosmos/cosmos-sdk/pull/12303) Use bytes instead of string comparison in delete validator queue +* (testutil/sims) [#12374](https://github.com/cosmos/cosmos-sdk/pull/12374) fix the non-determinstic behavior in simulations caused by `GenSignedMockTx` and check +empty coins slice before it is used to create `banktype.MsgSend`. ## [v0.46.0-rc1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0-rc1) - 2022-05-23 diff --git a/simapp/test_helpers.go b/simapp/test_helpers.go index 4cdc47f3abad..84ab354dd1a7 100644 --- a/simapp/test_helpers.go +++ b/simapp/test_helpers.go @@ -3,7 +3,9 @@ package simapp import ( "context" "encoding/json" + "math/rand" "testing" + "time" "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" @@ -257,6 +259,7 @@ func SignCheckDeliver( chainID string, accNums, accSeqs []uint64, expSimPass, expPass bool, priv ...cryptotypes.PrivKey, ) (sdk.GasInfo, *sdk.Result, error) { tx, err := simtestutil.GenSignedMockTx( + rand.New(rand.NewSource(time.Now().UnixNano())), txCfg, msgs, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)}, @@ -307,6 +310,7 @@ func GenSequenceOfTxs(txGen client.TxConfig, msgs []sdk.Msg, accNums []uint64, i var err error for i := 0; i < numToGenerate; i++ { txs[i], err = simtestutil.GenSignedMockTx( + rand.New(rand.NewSource(time.Now().UnixNano())), txGen, msgs, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)}, diff --git a/testutil/sims/tx_helpers.go b/testutil/sims/tx_helpers.go index ef79b9e393b4..0586e85f53e9 100644 --- a/testutil/sims/tx_helpers.go +++ b/testutil/sims/tx_helpers.go @@ -2,7 +2,6 @@ package sims import ( "math/rand" - "time" "github.com/cosmos/cosmos-sdk/client" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" @@ -13,12 +12,10 @@ import ( ) // GenSignedMockTx generates a signed mock transaction. -func GenSignedMockTx(txConfig client.TxConfig, msgs []sdk.Msg, feeAmt sdk.Coins, gas uint64, chainID string, accNums, accSeqs []uint64, priv ...cryptotypes.PrivKey) (sdk.Tx, error) { +func GenSignedMockTx(r *rand.Rand, txConfig client.TxConfig, msgs []sdk.Msg, feeAmt sdk.Coins, gas uint64, chainID string, accNums, accSeqs []uint64, priv ...cryptotypes.PrivKey) (sdk.Tx, error) { sigs := make([]signing.SignatureV2, len(priv)) // create a random length memo - r := rand.New(rand.NewSource(time.Now().UnixNano())) - memo := simulation.RandStringOfLength(r, simulation.RandIntBetween(r, 0, 100)) signMode := txConfig.SignModeHandler().DefaultMode() diff --git a/x/authz/simulation/operations.go b/x/authz/simulation/operations.go index d78744a1bd49..fdea8f349e3c 100644 --- a/x/authz/simulation/operations.go +++ b/x/authz/simulation/operations.go @@ -124,6 +124,7 @@ func SimulateMsgGrant(cdc *codec.ProtoCodec, ak authz.AccountKeeper, bk authz.Ba } txCfg := tx.NewTxConfig(cdc, tx.DefaultSignModes) tx, err := simtestutil.GenSignedMockTx( + r, txCfg, []sdk.Msg{msg}, fees, @@ -194,6 +195,7 @@ func SimulateMsgRevoke(cdc *codec.ProtoCodec, ak authz.AccountKeeper, bk authz.B txCfg := tx.NewTxConfig(cdc, tx.DefaultSignModes) account := ak.GetAccount(ctx, granterAddr) tx, err := simtestutil.GenSignedMockTx( + r, txCfg, []sdk.Msg{&msg}, fees, @@ -256,6 +258,10 @@ func SimulateMsgExec(cdc *codec.ProtoCodec, ak authz.AccountKeeper, bk authz.Ban granterspendableCoins := bk.SpendableCoins(ctx, granterAddr) coins := simtypes.RandSubsetCoins(r, granterspendableCoins) + // if coins slice is empty, we can not create valid banktype.MsgSend + if len(coins) == 0 { + return simtypes.NoOpMsg(authz.ModuleName, TypeMsgExec, "empty coins slice"), nil, nil + } // Check send_enabled status of each sent coin denom if err := bk.IsSendEnabledCoins(ctx, coins...); err != nil { @@ -283,6 +289,7 @@ func SimulateMsgExec(cdc *codec.ProtoCodec, ak authz.AccountKeeper, bk authz.Ban txCfg := tx.NewTxConfig(cdc, tx.DefaultSignModes) granteeAcc := ak.GetAccount(ctx, granteeAddr) tx, err := simtestutil.GenSignedMockTx( + r, txCfg, []sdk.Msg{&msgExec}, fees, diff --git a/x/bank/simulation/operations.go b/x/bank/simulation/operations.go index 3873bfa3264d..b1456b7c6d90 100644 --- a/x/bank/simulation/operations.go +++ b/x/bank/simulation/operations.go @@ -61,6 +61,11 @@ func SimulateMsgSend(ak types.AccountKeeper, bk keeper.Keeper) simtypes.Operatio ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { from, to, coins, skip := randomSendFields(r, ctx, accs, bk, ak) + // if coins slice is empty, we can not create valid types.MsgSend + if len(coins) == 0 { + return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSend, "empty coins slice"), nil, nil + } + // Check send_enabled status of each coin denom if err := bk.IsSendEnabledCoins(ctx, coins...); err != nil { return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSend, err.Error()), nil, nil @@ -94,6 +99,10 @@ func SimulateMsgSendToModuleAccount(ak types.AccountKeeper, bk keeper.Keeper, mo spendable := bk.SpendableCoins(ctx, from.Address) coins := simtypes.RandSubsetCoins(r, spendable) + // if coins slice is empty, we can not create valid types.MsgSend + if len(coins) == 0 { + return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSend, "empty coins slice"), nil, nil + } // Check send_enabled status of each coin denom if err := bk.IsSendEnabledCoins(ctx, coins...); err != nil { @@ -138,6 +147,7 @@ func sendMsgSend( } txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := simtestutil.GenSignedMockTx( + r, txGen, []sdk.Msg{msg}, fees, @@ -351,6 +361,7 @@ func sendMsgMultiSend( txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := simtestutil.GenSignedMockTx( + r, txGen, []sdk.Msg{msg}, fees, diff --git a/x/distribution/simulation/operations.go b/x/distribution/simulation/operations.go index 2bbd52b6a814..b93e8cf3533f 100644 --- a/x/distribution/simulation/operations.go +++ b/x/distribution/simulation/operations.go @@ -236,6 +236,7 @@ func SimulateMsgFundCommunityPool(txConfig client.TxConfig, ak types.AccountKeep msg := types.NewMsgFundCommunityPool(fundAmount, funder.Address) txCtx := simulation.OperationInput{ + R: r, App: app, TxGen: txConfig, Cdc: nil, diff --git a/x/genutil/gentx_test.go b/x/genutil/gentx_test.go index ae9c7ed365bc..9b79895df0d9 100644 --- a/x/genutil/gentx_test.go +++ b/x/genutil/gentx_test.go @@ -3,7 +3,9 @@ package genutil_test import ( "encoding/json" "fmt" + "math/rand" "testing" + "time" "github.com/stretchr/testify/suite" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -231,8 +233,10 @@ func (suite *GenTxTestSuite) TestDeliverGenTxs() { _ = suite.setAccountBalance(addr1, 50) _ = suite.setAccountBalance(addr2, 1) + r := rand.New(rand.NewSource(time.Now().UnixNano())) msg := banktypes.NewMsgSend(addr1, addr2, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 1)}) tx, err := simtestutil.GenSignedMockTx( + r, suite.encodingConfig.TxConfig, []sdk.Msg{msg}, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 10)}, diff --git a/x/gov/simulation/operations.go b/x/gov/simulation/operations.go index 591eb47abc00..84f404145eed 100644 --- a/x/gov/simulation/operations.go +++ b/x/gov/simulation/operations.go @@ -171,6 +171,7 @@ func SimulateMsgSubmitProposal( txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := simtestutil.GenSignedMockTx( + r, txGen, []sdk.Msg{msg}, fees, @@ -257,6 +258,7 @@ func SimulateMsgDeposit(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Ke } txCtx := simulation.OperationInput{ + R: r, App: app, TxGen: simappparams.MakeTestEncodingConfig().TxConfig, Cdc: nil, diff --git a/x/group/simulation/operations.go b/x/group/simulation/operations.go index 44382dd216c4..20bd2691050d 100644 --- a/x/group/simulation/operations.go +++ b/x/group/simulation/operations.go @@ -263,6 +263,7 @@ func SimulateMsgCreateGroup(cdc *codec.ProtoCodec, ak group.AccountKeeper, bk gr txGen := tx.NewTxConfig(cdc, tx.DefaultSignModes) tx, err := simtestutil.GenSignedMockTx( + r, txGen, []sdk.Msg{msg}, fees, @@ -322,6 +323,7 @@ func SimulateMsgCreateGroupWithPolicy(cdc *codec.ProtoCodec, ak group.AccountKee txGen := tx.NewTxConfig(cdc, tx.DefaultSignModes) tx, err := simtestutil.GenSignedMockTx( + r, txGen, []sdk.Msg{msg}, fees, @@ -381,6 +383,7 @@ func SimulateMsgCreateGroupPolicy(cdc *codec.ProtoCodec, ak group.AccountKeeper, txGen := tx.NewTxConfig(cdc, tx.DefaultSignModes) tx, err := simtestutil.GenSignedMockTx( + r, txGen, []sdk.Msg{msg}, fees, @@ -456,6 +459,7 @@ func SimulateMsgSubmitProposal(cdc *codec.ProtoCodec, ak group.AccountKeeper, bk txGen := tx.NewTxConfig(cdc, tx.DefaultSignModes) tx, err := simtestutil.GenSignedMockTx( + r, txGen, []sdk.Msg{&msg}, fees, @@ -515,6 +519,7 @@ func SimulateMsgUpdateGroupAdmin(cdc *codec.ProtoCodec, ak group.AccountKeeper, txGen := tx.NewTxConfig(cdc, tx.DefaultSignModes) tx, err := simtestutil.GenSignedMockTx( + r, txGen, []sdk.Msg{&msg}, fees, @@ -565,6 +570,7 @@ func SimulateMsgUpdateGroupMetadata(cdc *codec.ProtoCodec, ak group.AccountKeepe txGen := tx.NewTxConfig(cdc, tx.DefaultSignModes) tx, err := simtestutil.GenSignedMockTx( + r, txGen, []sdk.Msg{&msg}, fees, @@ -644,6 +650,7 @@ func SimulateMsgUpdateGroupMembers(cdc *codec.ProtoCodec, ak group.AccountKeeper txGen := tx.NewTxConfig(cdc, tx.DefaultSignModes) tx, err := simtestutil.GenSignedMockTx( + r, txGen, []sdk.Msg{&msg}, fees, @@ -703,6 +710,7 @@ func SimulateMsgUpdateGroupPolicyAdmin(cdc *codec.ProtoCodec, ak group.AccountKe txGen := tx.NewTxConfig(cdc, tx.DefaultSignModes) tx, err := simtestutil.GenSignedMockTx( + r, txGen, []sdk.Msg{&msg}, fees, @@ -764,6 +772,7 @@ func SimulateMsgUpdateGroupPolicyDecisionPolicy(cdc *codec.ProtoCodec, ak group. txGen := tx.NewTxConfig(cdc, tx.DefaultSignModes) tx, err := simtestutil.GenSignedMockTx( + r, txGen, []sdk.Msg{msg}, fees, @@ -815,6 +824,7 @@ func SimulateMsgUpdateGroupPolicyMetadata(cdc *codec.ProtoCodec, ak group.Accoun txGen := tx.NewTxConfig(cdc, tx.DefaultSignModes) tx, err := simtestutil.GenSignedMockTx( + r, txGen, []sdk.Msg{&msg}, fees, @@ -917,6 +927,7 @@ func SimulateMsgWithdrawProposal(cdc *codec.ProtoCodec, ak group.AccountKeeper, txGen := tx.NewTxConfig(cdc, tx.DefaultSignModes) tx, err := simtestutil.GenSignedMockTx( + r, txGen, []sdk.Msg{&msg}, fees, @@ -1022,6 +1033,7 @@ func SimulateMsgVote(cdc *codec.ProtoCodec, ak group.AccountKeeper, } txGen := tx.NewTxConfig(cdc, tx.DefaultSignModes) tx, err := simtestutil.GenSignedMockTx( + r, txGen, []sdk.Msg{&msg}, fees, @@ -1100,6 +1112,7 @@ func SimulateMsgExec(cdc *codec.ProtoCodec, ak group.AccountKeeper, } txGen := tx.NewTxConfig(cdc, tx.DefaultSignModes) tx, err := simtestutil.GenSignedMockTx( + r, txGen, []sdk.Msg{&msg}, fees, @@ -1162,6 +1175,7 @@ func SimulateMsgLeaveGroup(cdc *codec.ProtoCodec, k keeper.Keeper, ak group.Acco txGen := tx.NewTxConfig(cdc, tx.DefaultSignModes) tx, err := simtestutil.GenSignedMockTx( + r, txGen, []sdk.Msg{msg}, fees, diff --git a/x/nft/simulation/operations.go b/x/nft/simulation/operations.go index b7f9b5848039..b05f1c25faba 100644 --- a/x/nft/simulation/operations.go +++ b/x/nft/simulation/operations.go @@ -97,6 +97,7 @@ func SimulateMsgSend( txCfg := tx.NewTxConfig(cdc, tx.DefaultSignModes) tx, err := simtestutil.GenSignedMockTx( + r, txCfg, []sdk.Msg{msg}, fees, diff --git a/x/simulation/util.go b/x/simulation/util.go index cde482813574..534b984566b0 100644 --- a/x/simulation/util.go +++ b/x/simulation/util.go @@ -101,6 +101,7 @@ func GenAndDeliverTxWithRandFees(txCtx OperationInput) (simtypes.OperationMsg, [ func GenAndDeliverTx(txCtx OperationInput, fees sdk.Coins) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { account := txCtx.AccountKeeper.GetAccount(txCtx.Context, txCtx.SimAccount.Address) tx, err := simtestutil.GenSignedMockTx( + txCtx.R, txCtx.TxGen, []sdk.Msg{txCtx.Msg}, fees, diff --git a/x/slashing/simulation/operations.go b/x/slashing/simulation/operations.go index d135ce9eebe1..e7e5b8289d49 100644 --- a/x/slashing/simulation/operations.go +++ b/x/slashing/simulation/operations.go @@ -91,6 +91,7 @@ func SimulateMsgUnjail(cdc *codec.ProtoCodec, ak types.AccountKeeper, bk types.B txGen := tx.NewTxConfig(cdc, tx.DefaultSignModes) tx, err := simtestutil.GenSignedMockTx( + r, txGen, []sdk.Msg{msg}, fees, diff --git a/x/staking/simulation/operations.go b/x/staking/simulation/operations.go index 263761877d9e..0eab097d1831 100644 --- a/x/staking/simulation/operations.go +++ b/x/staking/simulation/operations.go @@ -165,6 +165,7 @@ func SimulateMsgCreateValidator(ak types.AccountKeeper, bk types.BankKeeper, k * } txCtx := simulation.OperationInput{ + R: r, App: app, TxGen: simappparams.MakeTestEncodingConfig().TxConfig, Cdc: nil, @@ -289,6 +290,7 @@ func SimulateMsgDelegate(ak types.AccountKeeper, bk types.BankKeeper, k *keeper. msg := types.NewMsgDelegate(simAccount.Address, val.GetOperator(), bondAmt) txCtx := simulation.OperationInput{ + R: r, App: app, TxGen: simappparams.MakeTestEncodingConfig().TxConfig, Cdc: nil,