Skip to content

Commit

Permalink
fix: Simulation is not deterministic due to GenSignedMockTx (#12374)
Browse files Browse the repository at this point in the history
(cherry picked from commit 17dc431)

# Conflicts:
#	CHANGELOG.md
#	simapp/helpers/test_helpers.go
#	simapp/test_helpers.go
#	x/authz/simulation/operations.go
#	x/bank/simulation/operations.go
#	x/genutil/gentx_test.go
#	x/gov/simulation/operations.go
#	x/group/simulation/operations.go
#	x/nft/simulation/operations.go
#	x/simulation/util.go
#	x/slashing/simulation/operations.go
  • Loading branch information
adu-web3 authored and mergify[bot] committed Jul 14, 2022
1 parent 373e88c commit 0295da5
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (types) [#12154](https://github.com/cosmos/cosmos-sdk/pull/12154) Add `baseAccountGetter` to avoid invalid account error when create vesting account.
* (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
<<<<<<< HEAD
=======
* (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`.
>>>>>>> 17dc43166 (fix: Simulation is not deterministic due to GenSignedMockTx (#12374))
## [v0.46.0-rc1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0-rc1) - 2022-05-23

Expand Down
7 changes: 4 additions & 3 deletions simapp/helpers/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package helpers

import (
"math/rand"
"time"

"github.com/cosmos/cosmos-sdk/client"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
Expand All @@ -19,12 +18,14 @@ const (
)

// GenSignedMockTx generates a signed mock transaction.
<<<<<<< HEAD:simapp/helpers/test_helpers.go
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) {
>>>>>>> 17dc43166 (fix: Simulation is not deterministic due to GenSignedMockTx (#12374)):testutil/sims/tx_helpers.go
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()
Expand Down
14 changes: 14 additions & 0 deletions simapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import (
"bytes"
"encoding/hex"
"encoding/json"
<<<<<<< HEAD
"fmt"
"strconv"
=======
"math/rand"
>>>>>>> 17dc43166 (fix: Simulation is not deterministic due to GenSignedMockTx (#12374))
"testing"
"time"

Expand Down Expand Up @@ -411,7 +415,12 @@ 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) {
<<<<<<< HEAD
tx, err := helpers.GenSignedMockTx(
=======
tx, err := simtestutil.GenSignedMockTx(
rand.New(rand.NewSource(time.Now().UnixNano())),
>>>>>>> 17dc43166 (fix: Simulation is not deterministic due to GenSignedMockTx (#12374))
txCfg,
msgs,
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)},
Expand Down Expand Up @@ -461,7 +470,12 @@ func GenSequenceOfTxs(txGen client.TxConfig, msgs []sdk.Msg, accNums []uint64, i
txs := make([]sdk.Tx, numToGenerate)
var err error
for i := 0; i < numToGenerate; i++ {
<<<<<<< HEAD
txs[i], err = helpers.GenSignedMockTx(
=======
txs[i], err = simtestutil.GenSignedMockTx(
rand.New(rand.NewSource(time.Now().UnixNano())),
>>>>>>> 17dc43166 (fix: Simulation is not deterministic due to GenSignedMockTx (#12374))
txGen,
msgs,
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)},
Expand Down
20 changes: 20 additions & 0 deletions x/authz/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,14 @@ func SimulateMsgGrant(ak authz.AccountKeeper, bk authz.BankKeeper, _ keeper.Keep
if err != nil {
return simtypes.NoOpMsg(authz.ModuleName, TypeMsgGrant, err.Error()), nil, err
}
<<<<<<< HEAD
txCfg := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenSignedMockTx(
=======
txCfg := tx.NewTxConfig(cdc, tx.DefaultSignModes)
tx, err := simtestutil.GenSignedMockTx(
r,
>>>>>>> 17dc43166 (fix: Simulation is not deterministic due to GenSignedMockTx (#12374))
txCfg,
[]sdk.Msg{msg},
fees,
Expand Down Expand Up @@ -187,7 +193,12 @@ func SimulateMsgRevoke(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Kee
msg := authz.NewMsgRevoke(granterAddr, granteeAddr, a.MsgTypeURL())
txCfg := simappparams.MakeTestEncodingConfig().TxConfig
account := ak.GetAccount(ctx, granterAddr)
<<<<<<< HEAD
tx, err := helpers.GenSignedMockTx(
=======
tx, err := simtestutil.GenSignedMockTx(
r,
>>>>>>> 17dc43166 (fix: Simulation is not deterministic due to GenSignedMockTx (#12374))
txCfg,
[]sdk.Msg{&msg},
fees,
Expand Down Expand Up @@ -250,6 +261,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 {
Expand All @@ -276,7 +291,12 @@ func SimulateMsgExec(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keepe

txCfg := simappparams.MakeTestEncodingConfig().TxConfig
granteeAcc := ak.GetAccount(ctx, granteeAddr)
<<<<<<< HEAD
tx, err := helpers.GenSignedMockTx(
=======
tx, err := simtestutil.GenSignedMockTx(
r,
>>>>>>> 17dc43166 (fix: Simulation is not deterministic due to GenSignedMockTx (#12374))
txCfg,
[]sdk.Msg{&msgExec},
fees,
Expand Down
19 changes: 19 additions & 0 deletions x/bank/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -136,7 +145,12 @@ func sendMsgSend(
}
}
txGen := simappparams.MakeTestEncodingConfig().TxConfig
<<<<<<< HEAD
tx, err := helpers.GenSignedMockTx(
=======
tx, err := simtestutil.GenSignedMockTx(
r,
>>>>>>> 17dc43166 (fix: Simulation is not deterministic due to GenSignedMockTx (#12374))
txGen,
[]sdk.Msg{msg},
fees,
Expand Down Expand Up @@ -349,7 +363,12 @@ func sendMsgMultiSend(
}

txGen := simappparams.MakeTestEncodingConfig().TxConfig
<<<<<<< HEAD
tx, err := helpers.GenSignedMockTx(
=======
tx, err := simtestutil.GenSignedMockTx(
r,
>>>>>>> 17dc43166 (fix: Simulation is not deterministic due to GenSignedMockTx (#12374))
txGen,
[]sdk.Msg{msg},
fees,
Expand Down
1 change: 1 addition & 0 deletions x/distribution/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions x/genutil/gentx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -231,8 +233,14 @@ 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)})
<<<<<<< HEAD
tx, err := helpers.GenSignedMockTx(
=======
tx, err := simtestutil.GenSignedMockTx(
r,
>>>>>>> 17dc43166 (fix: Simulation is not deterministic due to GenSignedMockTx (#12374))
suite.encodingConfig.TxConfig,
[]sdk.Msg{msg},
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 10)},
Expand Down
6 changes: 6 additions & 0 deletions x/gov/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ func SimulateMsgSubmitProposal(
}

txGen := simappparams.MakeTestEncodingConfig().TxConfig
<<<<<<< HEAD
tx, err := helpers.GenSignedMockTx(
=======
tx, err := simtestutil.GenSignedMockTx(
r,
>>>>>>> 17dc43166 (fix: Simulation is not deterministic due to GenSignedMockTx (#12374))
txGen,
[]sdk.Msg{msg},
fees,
Expand Down Expand Up @@ -256,6 +261,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,
Expand Down
Loading

0 comments on commit 0295da5

Please sign in to comment.