diff --git a/CHANGELOG.md b/CHANGELOG.md index 87995357afe3..d02f1e3c667c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +* (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`. * [#12448](https://github.com/cosmos/cosmos-sdk/pull/12448) Start telemetry independently from the API server. * [#12509](https://github.com/cosmos/cosmos-sdk/pull/12509) Fix `Register{Tx,Tendermint}Service` not being called, resulting in some endpoints like the Simulate endpoint not working. diff --git a/simapp/helpers/test_helpers.go b/simapp/helpers/test_helpers.go index 0dc88f9b4623..a0888c09019e 100644 --- a/simapp/helpers/test_helpers.go +++ b/simapp/helpers/test_helpers.go @@ -2,7 +2,6 @@ package helpers import ( "math/rand" - "time" "github.com/cosmos/cosmos-sdk/client" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" @@ -19,15 +18,13 @@ const ( ) // GenSignedMockTx generates a signed mock transaction. -func GenSignedMockTx(gen 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 := gen.SignModeHandler().DefaultMode() + signMode := txConfig.SignModeHandler().DefaultMode() // 1st round: set SignatureV2 with empty signatures, to set correct // signer infos. @@ -41,7 +38,7 @@ func GenSignedMockTx(gen client.TxConfig, msgs []sdk.Msg, feeAmt sdk.Coins, gas } } - tx := gen.NewTxBuilder() + tx := txConfig.NewTxBuilder() err := tx.SetMsgs(msgs...) if err != nil { return nil, err @@ -63,7 +60,7 @@ func GenSignedMockTx(gen client.TxConfig, msgs []sdk.Msg, feeAmt sdk.Coins, gas Sequence: accSeqs[i], PubKey: p.PubKey(), } - signBytes, err := gen.SignModeHandler().GetSignBytes(signMode, signerData, tx.GetTx()) + signBytes, err := txConfig.SignModeHandler().GetSignBytes(signMode, signerData, tx.GetTx()) if err != nil { panic(err) } diff --git a/simapp/test_helpers.go b/simapp/test_helpers.go index dd3bda28f5b9..fc1ac3195260 100644 --- a/simapp/test_helpers.go +++ b/simapp/test_helpers.go @@ -5,6 +5,7 @@ import ( "encoding/hex" "encoding/json" "fmt" + "math/rand" "strconv" "testing" "time" @@ -412,6 +413,7 @@ func SignCheckDeliver( chainID string, accNums, accSeqs []uint64, expSimPass, expPass bool, priv ...cryptotypes.PrivKey, ) (sdk.GasInfo, *sdk.Result, error) { tx, err := helpers.GenSignedMockTx( + rand.New(rand.NewSource(time.Now().UnixNano())), txCfg, msgs, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)}, @@ -462,6 +464,7 @@ func GenSequenceOfTxs(txGen client.TxConfig, msgs []sdk.Msg, accNums []uint64, i var err error for i := 0; i < numToGenerate; i++ { txs[i], err = helpers.GenSignedMockTx( + rand.New(rand.NewSource(time.Now().UnixNano())), txGen, msgs, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)}, diff --git a/x/authz/simulation/operations.go b/x/authz/simulation/operations.go index 956a7b4c325d..5a047bbf6448 100644 --- a/x/authz/simulation/operations.go +++ b/x/authz/simulation/operations.go @@ -116,8 +116,10 @@ func SimulateMsgGrant(ak authz.AccountKeeper, bk authz.BankKeeper, _ keeper.Keep if err != nil { return simtypes.NoOpMsg(authz.ModuleName, TypeMsgGrant, err.Error()), nil, err } + txCfg := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenSignedMockTx( + r, txCfg, []sdk.Msg{msg}, fees, @@ -188,6 +190,7 @@ func SimulateMsgRevoke(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Kee txCfg := simappparams.MakeTestEncodingConfig().TxConfig account := ak.GetAccount(ctx, granterAddr) tx, err := helpers.GenSignedMockTx( + r, txCfg, []sdk.Msg{&msg}, fees, @@ -250,6 +253,10 @@ func SimulateMsgExec(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keepe 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 { @@ -277,6 +284,7 @@ func SimulateMsgExec(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keepe txCfg := simappparams.MakeTestEncodingConfig().TxConfig granteeAcc := ak.GetAccount(ctx, granteeAddr) tx, err := helpers.GenSignedMockTx( + r, txCfg, []sdk.Msg{&msgExec}, fees, diff --git a/x/bank/simulation/operations.go b/x/bank/simulation/operations.go index dae8471b0edf..8f6561a0585c 100644 --- a/x/bank/simulation/operations.go +++ b/x/bank/simulation/operations.go @@ -60,6 +60,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 @@ -93,6 +98,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 { @@ -137,6 +146,7 @@ func sendMsgSend( } txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenSignedMockTx( + r, txGen, []sdk.Msg{msg}, fees, @@ -350,6 +360,7 @@ func sendMsgMultiSend( txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenSignedMockTx( + r, txGen, []sdk.Msg{msg}, fees, diff --git a/x/distribution/simulation/operations.go b/x/distribution/simulation/operations.go index c76318fe82cd..edd0aef1ff12 100644 --- a/x/distribution/simulation/operations.go +++ b/x/distribution/simulation/operations.go @@ -229,6 +229,7 @@ func SimulateMsgFundCommunityPool(ak types.AccountKeeper, bk types.BankKeeper, k msg := types.NewMsgFundCommunityPool(fundAmount, funder.Address) txCtx := simulation.OperationInput{ + R: r, App: app, TxGen: simappparams.MakeTestEncodingConfig().TxConfig, Cdc: nil, diff --git a/x/genutil/gentx_test.go b/x/genutil/gentx_test.go index a83b310c46d1..be286051321d 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 := helpers.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 289c9413d6cd..91ed7f525350 100644 --- a/x/gov/simulation/operations.go +++ b/x/gov/simulation/operations.go @@ -170,6 +170,7 @@ func SimulateMsgSubmitProposal( txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenSignedMockTx( + r, txGen, []sdk.Msg{msg}, fees, @@ -256,6 +257,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 f541393771a3..36875b55cc8b 100644 --- a/x/group/simulation/operations.go +++ b/x/group/simulation/operations.go @@ -261,6 +261,7 @@ func SimulateMsgCreateGroup(ak group.AccountKeeper, bk group.BankKeeper) simtype txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenSignedMockTx( + r, txGen, []sdk.Msg{msg}, fees, @@ -320,6 +321,7 @@ func SimulateMsgCreateGroupWithPolicy(ak group.AccountKeeper, bk group.BankKeepe txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenSignedMockTx( + r, txGen, []sdk.Msg{msg}, fees, @@ -379,6 +381,7 @@ func SimulateMsgCreateGroupPolicy(ak group.AccountKeeper, bk group.BankKeeper, k txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenSignedMockTx( + r, txGen, []sdk.Msg{msg}, fees, @@ -454,6 +457,7 @@ func SimulateMsgSubmitProposal(ak group.AccountKeeper, bk group.BankKeeper, k ke txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenSignedMockTx( + r, txGen, []sdk.Msg{&msg}, fees, @@ -513,6 +517,7 @@ func SimulateMsgUpdateGroupAdmin(ak group.AccountKeeper, bk group.BankKeeper, k txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenSignedMockTx( + r, txGen, []sdk.Msg{&msg}, fees, @@ -563,6 +568,7 @@ func SimulateMsgUpdateGroupMetadata(ak group.AccountKeeper, bk group.BankKeeper, txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenSignedMockTx( + r, txGen, []sdk.Msg{&msg}, fees, @@ -642,6 +648,7 @@ func SimulateMsgUpdateGroupMembers(ak group.AccountKeeper, txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenSignedMockTx( + r, txGen, []sdk.Msg{&msg}, fees, @@ -701,6 +708,7 @@ func SimulateMsgUpdateGroupPolicyAdmin(ak group.AccountKeeper, bk group.BankKeep txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenSignedMockTx( + r, txGen, []sdk.Msg{&msg}, fees, @@ -762,6 +770,7 @@ func SimulateMsgUpdateGroupPolicyDecisionPolicy(ak group.AccountKeeper, txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenSignedMockTx( + r, txGen, []sdk.Msg{msg}, fees, @@ -813,6 +822,7 @@ func SimulateMsgUpdateGroupPolicyMetadata(ak group.AccountKeeper, txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenSignedMockTx( + r, txGen, []sdk.Msg{&msg}, fees, @@ -915,6 +925,7 @@ func SimulateMsgWithdrawProposal(ak group.AccountKeeper, txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenSignedMockTx( + r, txGen, []sdk.Msg{&msg}, fees, @@ -1020,6 +1031,7 @@ func SimulateMsgVote(ak group.AccountKeeper, } txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenSignedMockTx( + r, txGen, []sdk.Msg{&msg}, fees, @@ -1098,6 +1110,7 @@ func SimulateMsgExec(ak group.AccountKeeper, } txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenSignedMockTx( + r, txGen, []sdk.Msg{&msg}, fees, @@ -1160,6 +1173,7 @@ func SimulateMsgLeaveGroup(k keeper.Keeper, ak group.AccountKeeper, bk group.Ban txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenSignedMockTx( + r, txGen, []sdk.Msg{msg}, fees, diff --git a/x/nft/simulation/operations.go b/x/nft/simulation/operations.go index c71ab17c3175..c4e9f501ac72 100644 --- a/x/nft/simulation/operations.go +++ b/x/nft/simulation/operations.go @@ -97,6 +97,7 @@ func SimulateMsgSend( txCfg := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenSignedMockTx( + r, txCfg, []sdk.Msg{msg}, fees, diff --git a/x/simulation/util.go b/x/simulation/util.go index 3c72f67538cb..09278e6aeb8e 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 := helpers.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 eabe6486b7bc..23be89b1b21f 100644 --- a/x/slashing/simulation/operations.go +++ b/x/slashing/simulation/operations.go @@ -86,9 +86,10 @@ func SimulateMsgUnjail(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Kee msg := types.NewMsgUnjail(validator.GetOperator()) - txGen := simappparams.MakeTestEncodingConfig().TxConfig + txCfg := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenSignedMockTx( - txGen, + r, + txCfg, []sdk.Msg{msg}, fees, helpers.DefaultGenTxGas, @@ -101,7 +102,7 @@ func SimulateMsgUnjail(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Kee return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err } - _, res, err := app.SimDeliver(txGen.TxEncoder(), tx) + _, res, err := app.SimDeliver(txCfg.TxEncoder(), tx) // result should fail if: // - validator cannot be unjailed due to tombstone diff --git a/x/staking/simulation/operations.go b/x/staking/simulation/operations.go index 7cff6459db18..c28b489d6a51 100644 --- a/x/staking/simulation/operations.go +++ b/x/staking/simulation/operations.go @@ -164,6 +164,7 @@ func SimulateMsgCreateValidator(ak types.AccountKeeper, bk types.BankKeeper, k k } txCtx := simulation.OperationInput{ + R: r, App: app, TxGen: simappparams.MakeTestEncodingConfig().TxConfig, Cdc: nil, @@ -288,6 +289,7 @@ func SimulateMsgDelegate(ak types.AccountKeeper, bk types.BankKeeper, k keeper.K msg := types.NewMsgDelegate(simAccount.Address, val.GetOperator(), bondAmt) txCtx := simulation.OperationInput{ + R: r, App: app, TxGen: simappparams.MakeTestEncodingConfig().TxConfig, Cdc: nil,