Skip to content

Commit

Permalink
Update simapp helpers to use Tx generator (#6655)
Browse files Browse the repository at this point in the history
* update helpers to use tx generator

* update modules to use tx generator

* add todo

* update ibc to use txG

* fix lint

* Eliminate panics from Gentx

* update module ops

* fix lgtm alert

* update ibc with txG

* Remove todo

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* move simapp/helpers to test_helpers

* revert simapp/helpers to test_helpers

* revert renaming

* address suggestion

Co-authored-by: Aaron Craelius <aaron@regen.network>

* fix tests

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: Aaron Craelius <aaron@regen.network>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Jul 14, 2020
1 parent 6bd36c8 commit b9f86dd
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 56 deletions.
57 changes: 43 additions & 14 deletions simapp/helpers/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (

"github.com/tendermint/tendermint/crypto"

"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/simulation"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authsign "github.com/cosmos/cosmos-sdk/x/auth/signing"
)

// SimAppChainID hardcoded chainID for simulation
Expand All @@ -18,31 +20,58 @@ const (
)

// GenTx generates a signed mock transaction.
func GenTx(msgs []sdk.Msg, feeAmt sdk.Coins, gas uint64, chainID string, accnums []uint64, seq []uint64, priv ...crypto.PrivKey) authtypes.StdTx {
fee := authtypes.StdFee{ //nolint:staticcheck // SA1019: authtypes.StdFee is deprecated
Amount: feeAmt,
Gas: gas,
}

sigs := make([]authtypes.StdSignature, len(priv)) //nolint:staticcheck // SA1019: authtypes.StdSignature is deprecated
func GenTx(gen client.TxGenerator, msgs []sdk.Msg, feeAmt sdk.Coins, gas uint64, chainID string, accnums []uint64, seq []uint64, priv ...crypto.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()

for i, p := range priv {
sigs[i] = signing.SignatureV2{
PubKey: p.PubKey(),
Data: &signing.SingleSignatureData{
SignMode: signMode,
},
}
}

tx := gen.NewTxBuilder()
err := tx.SetMsgs(msgs...)
if err != nil {
return nil, err
}
err = tx.SetSignatures(sigs...)
if err != nil {
return nil, err
}
tx.SetMemo(memo)
tx.SetFeeAmount(feeAmt)
tx.SetGasLimit(gas)
for i, p := range priv {
// use a empty chainID for ease of testing
sig, err := p.Sign(authtypes.StdSignBytes(chainID, accnums[i], seq[i], fee, msgs, memo))
signerData := authsign.SignerData{
ChainID: chainID,
AccountNumber: accnums[i],
AccountSequence: seq[i],
}
signBytes, err := gen.SignModeHandler().GetSignBytes(signMode, signerData, tx.GetTx())
if err != nil {
panic(err)
}

sigs[i] = authtypes.StdSignature{ //nolint:staticcheck // SA1019: authtypes.StdSignature is deprecated
PubKey: p.PubKey().Bytes(),
Signature: sig,
sig, err := p.Sign(signBytes)
if err != nil {
panic(err)
}
sigs[i].Data.(*signing.SingleSignatureData).Signature = sig
err = tx.SetSignatures(sigs...)
if err != nil {
panic(err)
}
}

return authtypes.NewStdTx(msgs, fee, sigs, memo)
return tx.GetTx(), nil
}
4 changes: 2 additions & 2 deletions simapp/simd/cmd/genaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
depCdc := clientCtx.Codec
cdc := clientCtx.JSONMarshaler.(codec.Marshaler)
depCdc := clientCtx.JSONMarshaler
cdc := depCdc.(codec.Marshaler)

serverCtx := server.GetServerContextFromCmd(cmd)
config := serverCtx.Config
Expand Down
23 changes: 15 additions & 8 deletions simapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
dbm "github.com/tendermint/tm-db"

bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -312,11 +313,12 @@ func CheckBalance(t *testing.T, app *SimApp, addr sdk.AccAddress, balances sdk.C
// the parameter 'expPass' against the result. A corresponding result is
// returned.
func SignCheckDeliver(
t *testing.T, cdc *codec.Codec, app *bam.BaseApp, header abci.Header, msgs []sdk.Msg,
t *testing.T, txGen client.TxGenerator, app *bam.BaseApp, header abci.Header, msgs []sdk.Msg,
accNums, seq []uint64, expSimPass, expPass bool, priv ...crypto.PrivKey,
) (sdk.GasInfo, *sdk.Result, error) {

tx := helpers.GenTx(
tx, err := helpers.GenTx(
txGen,
msgs,
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)},
helpers.DefaultGenTxGas,
Expand All @@ -325,8 +327,8 @@ func SignCheckDeliver(
seq,
priv...,
)

txBytes, err := cdc.MarshalBinaryBare(tx)
require.NoError(t, err)
txBytes, err := txGen.TxEncoder()(tx)
require.Nil(t, err)

// Must simulate now as CheckTx doesn't run Msgs anymore
Expand Down Expand Up @@ -361,10 +363,12 @@ func SignCheckDeliver(
// GenSequenceOfTxs generates a set of signed transactions of messages, such
// that they differ only by having the sequence numbers incremented between
// every transaction.
func GenSequenceOfTxs(msgs []sdk.Msg, accNums []uint64, initSeqNums []uint64, numToGenerate int, priv ...crypto.PrivKey) []authtypes.StdTx {
txs := make([]authtypes.StdTx, numToGenerate)
func GenSequenceOfTxs(txGen client.TxGenerator, msgs []sdk.Msg, accNums []uint64, initSeqNums []uint64, numToGenerate int, priv ...crypto.PrivKey) ([]sdk.Tx, error) {
txs := make([]sdk.Tx, numToGenerate)
var err error
for i := 0; i < numToGenerate; i++ {
txs[i] = helpers.GenTx(
txs[i], err = helpers.GenTx(
txGen,
msgs,
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)},
helpers.DefaultGenTxGas,
Expand All @@ -373,10 +377,13 @@ func GenSequenceOfTxs(msgs []sdk.Msg, accNums []uint64, initSeqNums []uint64, nu
initSeqNums,
priv...,
)
if err != nil {
break
}
incrementAllSequenceNumbers(initSeqNums)
}

return txs
return txs, err
}

func incrementAllSequenceNumbers(initSeqNums []uint64) {
Expand Down
18 changes: 12 additions & 6 deletions x/bank/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ func TestSendNotEnoughBalance(t *testing.T) {

sendMsg := types.NewMsgSend(addr1, addr2, sdk.Coins{sdk.NewInt64Coin("foocoin", 100)})
header := abci.Header{Height: app.LastBlockHeight() + 1}
_, _, err = simapp.SignCheckDeliver(t, app.Codec(), app.BaseApp, header, []sdk.Msg{sendMsg}, []uint64{origAccNum}, []uint64{origSeq}, false, false, priv1)
txGen := simapp.MakeEncodingConfig().TxGenerator
_, _, err = simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, []sdk.Msg{sendMsg}, []uint64{origAccNum}, []uint64{origSeq}, false, false, priv1)
require.Error(t, err)

simapp.CheckBalance(t, app, addr1, sdk.Coins{sdk.NewInt64Coin("foocoin", 67)})
Expand Down Expand Up @@ -177,7 +178,8 @@ func TestSendToModuleAcc(t *testing.T) {
origSeq := res1.GetSequence()

header := abci.Header{Height: app.LastBlockHeight() + 1}
_, _, err = simapp.SignCheckDeliver(t, app.Codec(), app.BaseApp, header, []sdk.Msg{test.msg}, []uint64{origAccNum}, []uint64{origSeq}, test.expSimPass, test.expPass, priv1)
txGen := simapp.MakeEncodingConfig().TxGenerator
_, _, err = simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, []sdk.Msg{test.msg}, []uint64{origAccNum}, []uint64{origSeq}, test.expSimPass, test.expPass, priv1)
if test.expPass {
require.NoError(t, err)
} else {
Expand Down Expand Up @@ -247,7 +249,8 @@ func TestMsgMultiSendWithAccounts(t *testing.T) {

for _, tc := range testCases {
header := abci.Header{Height: app.LastBlockHeight() + 1}
_, _, err := simapp.SignCheckDeliver(t, app.Codec(), app.BaseApp, header, tc.msgs, tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...)
txGen := simapp.MakeEncodingConfig().TxGenerator
_, _, err := simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, tc.msgs, tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...)
if tc.expPass {
require.NoError(t, err)
} else {
Expand Down Expand Up @@ -298,7 +301,8 @@ func TestMsgMultiSendMultipleOut(t *testing.T) {

for _, tc := range testCases {
header := abci.Header{Height: app.LastBlockHeight() + 1}
_, _, err := simapp.SignCheckDeliver(t, app.Codec(), app.BaseApp, header, tc.msgs, tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...)
txGen := simapp.MakeEncodingConfig().TxGenerator
_, _, err := simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, tc.msgs, tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...)
require.NoError(t, err)

for _, eb := range tc.expectedBalances {
Expand Down Expand Up @@ -352,7 +356,8 @@ func TestMsgMultiSendMultipleInOut(t *testing.T) {

for _, tc := range testCases {
header := abci.Header{Height: app.LastBlockHeight() + 1}
_, _, err := simapp.SignCheckDeliver(t, app.Codec(), app.BaseApp, header, tc.msgs, tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...)
txGen := simapp.MakeEncodingConfig().TxGenerator
_, _, err := simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, tc.msgs, tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...)
require.NoError(t, err)

for _, eb := range tc.expectedBalances {
Expand Down Expand Up @@ -404,7 +409,8 @@ func TestMsgMultiSendDependent(t *testing.T) {

for _, tc := range testCases {
header := abci.Header{Height: app.LastBlockHeight() + 1}
_, _, err := simapp.SignCheckDeliver(t, app.Codec(), app.BaseApp, header, tc.msgs, tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...)
txGen := simapp.MakeEncodingConfig().TxGenerator
_, _, err := simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, tc.msgs, tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...)
require.NoError(t, err)

for _, eb := range tc.expectedBalances {
Expand Down
9 changes: 7 additions & 2 deletions x/bank/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/simapp"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand All @@ -31,9 +32,11 @@ func BenchmarkOneBankSendTxPerBlock(b *testing.B) {
require.NoError(b, err)

benchmarkApp.Commit()
txGen := simappparams.MakeEncodingConfig().TxGenerator

// Precompute all txs
txs := simapp.GenSequenceOfTxs([]sdk.Msg{sendMsg1}, []uint64{0}, []uint64{uint64(0)}, b.N, priv1)
txs, err := simapp.GenSequenceOfTxs(txGen, []sdk.Msg{sendMsg1}, []uint64{0}, []uint64{uint64(0)}, b.N, priv1)
require.NoError(b, err)
b.ResetTimer()

height := int64(3)
Expand Down Expand Up @@ -71,9 +74,11 @@ func BenchmarkOneBankMultiSendTxPerBlock(b *testing.B) {
require.NoError(b, err)

benchmarkApp.Commit()
txGen := simappparams.MakeEncodingConfig().TxGenerator

// Precompute all txs
txs := simapp.GenSequenceOfTxs([]sdk.Msg{multiSendMsg1}, []uint64{0}, []uint64{uint64(0)}, b.N, priv1)
txs, err := simapp.GenSequenceOfTxs(txGen, []sdk.Msg{multiSendMsg1}, []uint64{0}, []uint64{uint64(0)}, b.N, priv1)
require.NoError(b, err)
b.ResetTimer()

height := int64(3)
Expand Down
15 changes: 12 additions & 3 deletions x/bank/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ func sendMsgSend(
return err
}
}

tx := helpers.GenTx(
txGen := simappparams.MakeEncodingConfig().TxGenerator
tx, err := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
Expand All @@ -113,6 +114,9 @@ func sendMsgSend(
[]uint64{account.GetSequence()},
privkeys...,
)
if err != nil {
return err
}

_, _, err = app.Deliver(tx)
if err != nil {
Expand Down Expand Up @@ -247,7 +251,9 @@ func sendMsgMultiSend(
}
}

tx := helpers.GenTx(
txGen := simappparams.MakeEncodingConfig().TxGenerator
tx, err := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
Expand All @@ -256,6 +262,9 @@ func sendMsgMultiSend(
sequenceNumbers,
privkeys...,
)
if err != nil {
return err
}

_, _, err = app.Deliver(tx)
if err != nil {
Expand Down
28 changes: 24 additions & 4 deletions x/distribution/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ func SimulateMsgSetWithdrawAddress(ak types.AccountKeeper, bk types.BankKeeper,

msg := types.NewMsgSetWithdrawAddress(simAccount.Address, simToAccount.Address)

tx := helpers.GenTx(
txGen := simappparams.MakeEncodingConfig().TxGenerator
tx, err := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
Expand All @@ -109,6 +111,9 @@ func SimulateMsgSetWithdrawAddress(ak types.AccountKeeper, bk types.BankKeeper,
[]uint64{account.GetSequence()},
simAccount.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
}

_, _, err = app.Deliver(tx)
if err != nil {
Expand Down Expand Up @@ -147,7 +152,9 @@ func SimulateMsgWithdrawDelegatorReward(ak types.AccountKeeper, bk types.BankKee

msg := types.NewMsgWithdrawDelegatorReward(simAccount.Address, validator.GetOperator())

tx := helpers.GenTx(
txGen := simappparams.MakeEncodingConfig().TxGenerator
tx, err := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
Expand All @@ -156,6 +163,9 @@ func SimulateMsgWithdrawDelegatorReward(ak types.AccountKeeper, bk types.BankKee
[]uint64{account.GetSequence()},
simAccount.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
}

_, _, err = app.Deliver(tx)
if err != nil {
Expand Down Expand Up @@ -197,7 +207,9 @@ func SimulateMsgWithdrawValidatorCommission(ak types.AccountKeeper, bk types.Ban

msg := types.NewMsgWithdrawValidatorCommission(validator.GetOperator())

tx := helpers.GenTx(
txGen := simappparams.MakeEncodingConfig().TxGenerator
tx, err := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
Expand All @@ -206,6 +218,9 @@ func SimulateMsgWithdrawValidatorCommission(ak types.AccountKeeper, bk types.Ban
[]uint64{account.GetSequence()},
simAccount.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
}

_, _, err = app.Deliver(tx)
if err != nil {
Expand Down Expand Up @@ -247,7 +262,9 @@ func SimulateMsgFundCommunityPool(ak types.AccountKeeper, bk types.BankKeeper, k
}

msg := types.NewMsgFundCommunityPool(fundAmount, funder.Address)
tx := helpers.GenTx(
txGen := simappparams.MakeEncodingConfig().TxGenerator
tx, err := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
Expand All @@ -256,6 +273,9 @@ func SimulateMsgFundCommunityPool(ak types.AccountKeeper, bk types.BankKeeper, k
[]uint64{account.GetSequence()},
funder.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
}

_, _, err = app.Deliver(tx)
if err != nil {
Expand Down
Loading

0 comments on commit b9f86dd

Please sign in to comment.