Skip to content

Commit

Permalink
use Simstate.Rand in GenSignedMockTx
Browse files Browse the repository at this point in the history
  • Loading branch information
adu-web3 committed Jun 28, 2022
1 parent abf3b18 commit d218ae6
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 4 deletions.
6 changes: 6 additions & 0 deletions simapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)},
Expand Down Expand Up @@ -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)},
Expand Down
5 changes: 1 addition & 4 deletions testutil/sims/tx_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package sims

import (
"math/rand"
"time"

"github.com/cosmos/cosmos-sdk/client"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
Expand All @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions x/authz/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions x/bank/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func sendMsgSend(
}
txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := simtestutil.GenSignedMockTx(
r,
txGen,
[]sdk.Msg{msg},
fees,
Expand Down Expand Up @@ -351,6 +352,7 @@ func sendMsgMultiSend(

txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := simtestutil.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 @@ -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,
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 := simtestutil.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 @@ -171,6 +171,7 @@ func SimulateMsgSubmitProposal(

txGen := simappparams.MakeTestEncodingConfig().TxConfig
tx, err := simtestutil.GenSignedMockTx(
r,
txGen,
[]sdk.Msg{msg},
fees,
Expand Down Expand Up @@ -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,
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 @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
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 := tx.NewTxConfig(cdc, tx.DefaultSignModes)
tx, err := simtestutil.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 := simtestutil.GenSignedMockTx(
txCtx.R,
txCtx.TxGen,
[]sdk.Msg{txCtx.Msg},
fees,
Expand Down
1 change: 1 addition & 0 deletions x/slashing/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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 @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit d218ae6

Please sign in to comment.