From d218ae6e27a23bfdede1ae8fe9a0af89a04d7ab8 Mon Sep 17 00:00:00 2001 From: Adu Date: Tue, 28 Jun 2022 18:36:39 +0800 Subject: [PATCH 1/5] use Simstate.Rand in GenSignedMockTx --- simapp/test_helpers.go | 6 ++++++ testutil/sims/tx_helpers.go | 5 +---- x/authz/simulation/operations.go | 3 +++ x/bank/simulation/operations.go | 2 ++ x/distribution/simulation/operations.go | 1 + x/genutil/gentx_test.go | 4 ++++ x/gov/simulation/operations.go | 2 ++ x/group/simulation/operations.go | 14 ++++++++++++++ x/nft/simulation/operations.go | 1 + x/simulation/util.go | 1 + x/slashing/simulation/operations.go | 1 + x/staking/simulation/operations.go | 2 ++ 12 files changed, 38 insertions(+), 4 deletions(-) diff --git a/simapp/test_helpers.go b/simapp/test_helpers.go index 4cdc47f3abad..9ca9a9d1109d 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" @@ -256,7 +258,9 @@ func SignCheckDeliver( t *testing.T, txCfg client.TxConfig, app *bam.BaseApp, header tmproto.Header, msgs []sdk.Msg, chainID string, accNums, accSeqs []uint64, expSimPass, expPass bool, priv ...cryptotypes.PrivKey, ) (sdk.GasInfo, *sdk.Result, error) { + r := rand.New(rand.NewSource(time.Now().UnixNano())) tx, err := simtestutil.GenSignedMockTx( + r, txCfg, msgs, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)}, @@ -304,9 +308,11 @@ func SignCheckDeliver( // every transaction. func GenSequenceOfTxs(txGen client.TxConfig, msgs []sdk.Msg, accNums []uint64, initSeqNums []uint64, numToGenerate int, priv ...cryptotypes.PrivKey) ([]sdk.Tx, error) { txs := make([]sdk.Tx, numToGenerate) + r := rand.New(rand.NewSource(time.Now().UnixNano())) var err error for i := 0; i < numToGenerate; i++ { txs[i], err = simtestutil.GenSignedMockTx( + r, 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..41cbced650a4 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, @@ -283,6 +285,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..531db2a9bdca 100644 --- a/x/bank/simulation/operations.go +++ b/x/bank/simulation/operations.go @@ -138,6 +138,7 @@ func sendMsgSend( } txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := simtestutil.GenSignedMockTx( + r, txGen, []sdk.Msg{msg}, fees, @@ -351,6 +352,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 e85410066eb1..775079ff82bd 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, From d6c8c47978b6a03e97e4c6f49fc6ee80297bc6d5 Mon Sep 17 00:00:00 2001 From: Adu Date: Wed, 29 Jun 2022 10:08:13 +0800 Subject: [PATCH 2/5] directly pass rand to functions as an argument --- simapp/test_helpers.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/simapp/test_helpers.go b/simapp/test_helpers.go index 9ca9a9d1109d..84ab354dd1a7 100644 --- a/simapp/test_helpers.go +++ b/simapp/test_helpers.go @@ -258,9 +258,8 @@ func SignCheckDeliver( t *testing.T, txCfg client.TxConfig, app *bam.BaseApp, header tmproto.Header, msgs []sdk.Msg, chainID string, accNums, accSeqs []uint64, expSimPass, expPass bool, priv ...cryptotypes.PrivKey, ) (sdk.GasInfo, *sdk.Result, error) { - r := rand.New(rand.NewSource(time.Now().UnixNano())) tx, err := simtestutil.GenSignedMockTx( - r, + rand.New(rand.NewSource(time.Now().UnixNano())), txCfg, msgs, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)}, @@ -308,11 +307,10 @@ func SignCheckDeliver( // every transaction. func GenSequenceOfTxs(txGen client.TxConfig, msgs []sdk.Msg, accNums []uint64, initSeqNums []uint64, numToGenerate int, priv ...cryptotypes.PrivKey) ([]sdk.Tx, error) { txs := make([]sdk.Tx, numToGenerate) - r := rand.New(rand.NewSource(time.Now().UnixNano())) var err error for i := 0; i < numToGenerate; i++ { txs[i], err = simtestutil.GenSignedMockTx( - r, + rand.New(rand.NewSource(time.Now().UnixNano())), txGen, msgs, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)}, From 454a7a71c814ac8848a80c58eec2719d5ef5bbd7 Mon Sep 17 00:00:00 2001 From: Adu Date: Wed, 29 Jun 2022 10:12:45 +0800 Subject: [PATCH 3/5] add changelog item --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 301c6e4deb53..c9bba59fefdc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -76,6 +76,7 @@ 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` ## [v0.46.0-rc1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0-rc1) - 2022-05-23 From 1f9cf20470ccd9e17edddf520793708947afcbf7 Mon Sep 17 00:00:00 2001 From: Adu Date: Thu, 30 Jun 2022 14:50:56 +0800 Subject: [PATCH 4/5] empty coins slice could not be used to create valid banktype.MsgSend --- x/authz/simulation/operations.go | 4 ++++ x/bank/simulation/operations.go | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/x/authz/simulation/operations.go b/x/authz/simulation/operations.go index 41cbced650a4..fdea8f349e3c 100644 --- a/x/authz/simulation/operations.go +++ b/x/authz/simulation/operations.go @@ -258,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 { diff --git a/x/bank/simulation/operations.go b/x/bank/simulation/operations.go index 531db2a9bdca..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 { From 92f1587e8ca2aee30c7aec0dd146e8ace88ece19 Mon Sep 17 00:00:00 2001 From: Adu Date: Thu, 30 Jun 2022 15:19:31 +0800 Subject: [PATCH 5/5] modify appended changelog item --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9bba59fefdc..4307e7980190 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -76,7 +76,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` +* (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