Skip to content

Commit

Permalink
fix: Simulation is not deterministic due to GenSignedMockTx (backport #…
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Jul 14, 2022
1 parent 373e88c commit 93af9e8
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
11 changes: 4 additions & 7 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,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.
Expand All @@ -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
Expand All @@ -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)
}
Expand Down
3 changes: 3 additions & 0 deletions simapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"math/rand"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -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)},
Expand Down Expand Up @@ -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)},
Expand Down
8 changes: 8 additions & 0 deletions x/authz/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 11 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 @@ -137,6 +146,7 @@ func sendMsgSend(
}
txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenSignedMockTx(
r,
txGen,
[]sdk.Msg{msg},
fees,
Expand Down Expand Up @@ -350,6 +360,7 @@ func sendMsgMultiSend(

txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenSignedMockTx(
r,
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
4 changes: 4 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,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)},
Expand Down
2 changes: 2 additions & 0 deletions x/gov/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func SimulateMsgSubmitProposal(

txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenSignedMockTx(
r,
txGen,
[]sdk.Msg{msg},
fees,
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions x/group/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -642,6 +648,7 @@ func SimulateMsgUpdateGroupMembers(ak group.AccountKeeper,

txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenSignedMockTx(
r,
txGen,
[]sdk.Msg{&msg},
fees,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -762,6 +770,7 @@ func SimulateMsgUpdateGroupPolicyDecisionPolicy(ak group.AccountKeeper,

txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenSignedMockTx(
r,
txGen,
[]sdk.Msg{msg},
fees,
Expand Down Expand Up @@ -813,6 +822,7 @@ func SimulateMsgUpdateGroupPolicyMetadata(ak group.AccountKeeper,

txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenSignedMockTx(
r,
txGen,
[]sdk.Msg{&msg},
fees,
Expand Down Expand Up @@ -915,6 +925,7 @@ func SimulateMsgWithdrawProposal(ak group.AccountKeeper,

txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenSignedMockTx(
r,
txGen,
[]sdk.Msg{&msg},
fees,
Expand Down Expand Up @@ -1020,6 +1031,7 @@ func SimulateMsgVote(ak group.AccountKeeper,
}
txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenSignedMockTx(
r,
txGen,
[]sdk.Msg{&msg},
fees,
Expand Down Expand Up @@ -1098,6 +1110,7 @@ func SimulateMsgExec(ak group.AccountKeeper,
}
txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenSignedMockTx(
r,
txGen,
[]sdk.Msg{&msg},
fees,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions x/nft/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func SimulateMsgSend(

txCfg := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := helpers.GenSignedMockTx(
r,
txCfg,
[]sdk.Msg{msg},
fees,
Expand Down
1 change: 1 addition & 0 deletions x/simulation/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions x/slashing/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions x/staking/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 93af9e8

Please sign in to comment.