Skip to content

Commit 5aad86b

Browse files
adu-web3tomtau
authored andcommitted
fix: Simulation is not deterministic due to GenTx (backport cosmos#12374) (cosmos#12437)
1 parent 2ec2cd4 commit 5aad86b

File tree

10 files changed

+31
-5
lines changed

10 files changed

+31
-5
lines changed

simapp/helpers/test_helpers.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package helpers
22

33
import (
44
"math/rand"
5-
"time"
65

76
"github.com/cosmos/cosmos-sdk/client"
87
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
@@ -19,12 +18,10 @@ const (
1918
)
2019

2120
// GenTx generates a signed mock transaction.
22-
func GenTx(gen client.TxConfig, msgs []sdk.Msg, feeAmt sdk.Coins, gas uint64, chainID string, accNums, accSeqs []uint64, priv ...cryptotypes.PrivKey) (sdk.Tx, error) {
21+
func GenTx(r *rand.Rand, gen client.TxConfig, msgs []sdk.Msg, feeAmt sdk.Coins, gas uint64, chainID string, accNums, accSeqs []uint64, priv ...cryptotypes.PrivKey) (sdk.Tx, error) {
2322
sigs := make([]signing.SignatureV2, len(priv))
2423

2524
// create a random length memo
26-
r := rand.New(rand.NewSource(time.Now().UnixNano()))
27-
2825
memo := simulation.RandStringOfLength(r, simulation.RandIntBetween(r, 0, 100))
2926

3027
signMode := gen.SignModeHandler().DefaultMode()

simapp/test_helpers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/hex"
66
"encoding/json"
77
"fmt"
8+
"math/rand"
89
"strconv"
910
"testing"
1011
"time"
@@ -327,6 +328,7 @@ func SignCheckDeliver(
327328
) (sdk.GasInfo, *sdk.Result, error) {
328329

329330
tx, err := helpers.GenTx(
331+
rand.New(rand.NewSource(time.Now().UnixNano())),
330332
txCfg,
331333
msgs,
332334
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)},
@@ -377,6 +379,7 @@ func GenSequenceOfTxs(txGen client.TxConfig, msgs []sdk.Msg, accNums []uint64, i
377379
var err error
378380
for i := 0; i < numToGenerate; i++ {
379381
txs[i], err = helpers.GenTx(
382+
rand.New(rand.NewSource(time.Now().UnixNano())),
380383
txGen,
381384
msgs,
382385
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)},

x/authz/simulation/operations.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func SimulateMsgGrant(ak authz.AccountKeeper, bk authz.BankKeeper, _ keeper.Keep
115115
}
116116
txCfg := simappparams.MakeTestEncodingConfig().TxConfig
117117
tx, err := helpers.GenTx(
118+
r,
118119
txCfg,
119120
[]sdk.Msg{msg},
120121
fees,
@@ -181,6 +182,7 @@ func SimulateMsgRevoke(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Kee
181182
txCfg := simappparams.MakeTestEncodingConfig().TxConfig
182183
account := ak.GetAccount(ctx, granterAddr)
183184
tx, err := helpers.GenTx(
185+
r,
184186
txCfg,
185187
[]sdk.Msg{&msg},
186188
fees,
@@ -239,7 +241,10 @@ func SimulateMsgExec(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keepe
239241

240242
granterspendableCoins := bk.SpendableCoins(ctx, granterAddr)
241243
coins := simtypes.RandSubsetCoins(r, granterspendableCoins)
242-
244+
// if coins slice is empty, we can not create valid banktype.MsgSend
245+
if len(coins) == 0 {
246+
return simtypes.NoOpMsg(authz.ModuleName, TypeMsgExec, "empty coins slice"), nil, nil
247+
}
243248
// Check send_enabled status of each sent coin denom
244249
if err := bk.IsSendEnabledCoins(ctx, coins...); err != nil {
245250
return simtypes.NoOpMsg(authz.ModuleName, TypeMsgExec, err.Error()), nil, nil
@@ -272,6 +277,7 @@ func SimulateMsgExec(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keepe
272277
txCfg := simappparams.MakeTestEncodingConfig().TxConfig
273278
granteeAcc := ak.GetAccount(ctx, granteeAddr)
274279
tx, err := helpers.GenTx(
280+
r,
275281
txCfg,
276282
[]sdk.Msg{&msgExec},
277283
fees,

x/bank/simulation/operations.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ func SimulateMsgSend(ak types.AccountKeeper, bk keeper.Keeper) simtypes.Operatio
6060
accs []simtypes.Account, chainID string,
6161
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
6262
from, to, coins, skip := randomSendFields(r, ctx, accs, bk, ak)
63+
// if coins slice is empty, we can not create valid types.MsgSend
64+
if len(coins) == 0 {
65+
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSend, "empty coins slice"), nil, nil
66+
}
6367

6468
// Check send_enabled status of each coin denom
6569
if err := bk.IsSendEnabledCoins(ctx, coins...); err != nil {
@@ -94,6 +98,10 @@ func SimulateMsgSendToModuleAccount(ak types.AccountKeeper, bk keeper.Keeper, mo
9498

9599
spendable := bk.SpendableCoins(ctx, from.Address)
96100
coins := simtypes.RandSubsetCoins(r, spendable)
101+
// if coins slice is empty, we can not create valid types.MsgSend
102+
if len(coins) == 0 {
103+
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSend, "empty coins slice"), nil, nil
104+
}
97105

98106
// Check send_enabled status of each coin denom
99107
if err := bk.IsSendEnabledCoins(ctx, coins...); err != nil {
@@ -139,6 +147,7 @@ func sendMsgSend(
139147
}
140148
txGen := simappparams.MakeTestEncodingConfig().TxConfig
141149
tx, err := helpers.GenTx(
150+
r,
142151
txGen,
143152
[]sdk.Msg{msg},
144153
fees,
@@ -361,6 +370,7 @@ func sendMsgMultiSend(
361370

362371
txGen := simappparams.MakeTestEncodingConfig().TxConfig
363372
tx, err := helpers.GenTx(
373+
r,
364374
txGen,
365375
[]sdk.Msg{msg},
366376
fees,

x/distribution/simulation/operations.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ func SimulateMsgFundCommunityPool(ak types.AccountKeeper, bk types.BankKeeper, k
233233
msg := types.NewMsgFundCommunityPool(fundAmount, funder.Address)
234234

235235
txCtx := simulation.OperationInput{
236+
R: r,
236237
App: app,
237238
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
238239
Cdc: nil,

x/genutil/gentx_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package genutil_test
33
import (
44
"encoding/json"
55
"fmt"
6+
"math/rand"
67
"testing"
8+
"time"
79

810
"github.com/stretchr/testify/suite"
911
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
@@ -234,6 +236,7 @@ func (suite *GenTxTestSuite) TestDeliverGenTxs() {
234236

235237
msg := banktypes.NewMsgSend(addr1, addr2, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 1)})
236238
tx, err := helpers.GenTx(
239+
rand.New(rand.NewSource(time.Now().UnixNano())),
237240
suite.encodingConfig.TxConfig,
238241
[]sdk.Msg{msg},
239242
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 10)},

x/gov/simulation/operations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ func SimulateMsgSubmitProposal(
156156

157157
txGen := simappparams.MakeTestEncodingConfig().TxConfig
158158
tx, err := helpers.GenTx(
159+
r,
159160
txGen,
160161
[]sdk.Msg{msg},
161162
fees,
@@ -242,6 +243,7 @@ func SimulateMsgDeposit(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Ke
242243
}
243244

244245
txCtx := simulation.OperationInput{
246+
R: r,
245247
App: app,
246248
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
247249
Cdc: nil,

x/simulation/util.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func GenAndDeliverTxWithRandFees(txCtx OperationInput) (simtypes.OperationMsg, [
101101
func GenAndDeliverTx(txCtx OperationInput, fees sdk.Coins) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
102102
account := txCtx.AccountKeeper.GetAccount(txCtx.Context, txCtx.SimAccount.Address)
103103
tx, err := helpers.GenTx(
104+
txCtx.R,
104105
txCtx.TxGen,
105106
[]sdk.Msg{txCtx.Msg},
106107
fees,

x/slashing/simulation/operations.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func SimulateMsgUnjail(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Kee
9090

9191
txGen := simappparams.MakeTestEncodingConfig().TxConfig
9292
tx, err := helpers.GenTx(
93+
r,
9394
txGen,
9495
[]sdk.Msg{msg},
9596
fees,

x/staking/simulation/operations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ func SimulateMsgCreateValidator(ak types.AccountKeeper, bk types.BankKeeper, k k
152152
}
153153

154154
txCtx := simulation.OperationInput{
155+
R: r,
155156
App: app,
156157
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
157158
Cdc: nil,
@@ -276,6 +277,7 @@ func SimulateMsgDelegate(ak types.AccountKeeper, bk types.BankKeeper, k keeper.K
276277
msg := types.NewMsgDelegate(simAccount.Address, val.GetOperator(), bondAmt)
277278

278279
txCtx := simulation.OperationInput{
280+
R: r,
279281
App: app,
280282
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
281283
Cdc: nil,

0 commit comments

Comments
 (0)