Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update simapp helpers to use Tx generator #6655

Merged
merged 24 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8bb2f14
update helpers to use tx generator
sahith-narahari Jul 9, 2020
06de08e
update modules to use tx generator
sahith-narahari Jul 9, 2020
e8e34b6
add todo
sahith-narahari Jul 9, 2020
5c65e59
update ibc to use txG
sahith-narahari Jul 9, 2020
7493dc3
fix lint
sahith-narahari Jul 9, 2020
eebce25
Merge branch 'master' into sahith/update-simapp-gentx
sahith-narahari Jul 9, 2020
666fb64
Eliminate panics from Gentx
sahith-narahari Jul 10, 2020
ba9d70c
Merge branch 'sahith/update-simapp-gentx' of github.com:cosmos/cosmos…
sahith-narahari Jul 10, 2020
2531b7b
update module ops
sahith-narahari Jul 10, 2020
3d5383c
fix lgtm alert
sahith-narahari Jul 10, 2020
64e68c7
Merge branch 'master' into sahith/update-simapp-gentx
sahith-narahari Jul 10, 2020
1a81a59
Merge branch 'master' into sahith/update-simapp-gentx
sahith-narahari Jul 11, 2020
b7cfa60
Merge branch 'master' into sahith/update-simapp-gentx
sahith-narahari Jul 12, 2020
a238cba
update ibc with txG
sahith-narahari Jul 12, 2020
b432be9
Remove todo
sahith-narahari Jul 13, 2020
6ca3b47
Merge branch 'master' into sahith/update-simapp-gentx
sahith-narahari Jul 13, 2020
643ed20
move simapp/helpers to test_helpers
sahith-narahari Jul 14, 2020
10f2575
Merge branch 'master' into sahith/update-simapp-gentx
sahith-narahari Jul 14, 2020
2d70c54
revert simapp/helpers to test_helpers
sahith-narahari Jul 14, 2020
13c7ef6
revert renaming
sahith-narahari Jul 14, 2020
9f18e3b
address suggestion
sahith-narahari Jul 14, 2020
4db31b2
Merge branch 'master' into sahith/update-simapp-gentx
fedekunze Jul 14, 2020
6393ec6
fix tests
sahith-narahari Jul 14, 2020
88de829
Merge branch 'master' into sahith/update-simapp-gentx
mergify[bot] Jul 14, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
sahith-narahari marked this conversation as resolved.
Show resolved Hide resolved
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 {
panic(err)
}
err = tx.SetSignatures(sigs...)
if err != nil {
panic(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()
}
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
11 changes: 7 additions & 4 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(
sahith-narahari marked this conversation as resolved.
Show resolved Hide resolved
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(
txGen,
msgs,
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)},
helpers.DefaultGenTxGas,
Expand All @@ -326,7 +328,7 @@ func SignCheckDeliver(
priv...,
)

txBytes, err := cdc.MarshalBinaryBare(tx)
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,11 @@ 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(msgs []sdk.Msg, txGen client.TxGenerator, accNums []uint64, initSeqNums []uint64, numToGenerate int, priv ...crypto.PrivKey) []sdk.Tx {
txs := make([]sdk.Tx, numToGenerate)
for i := 0; i < numToGenerate; i++ {
txs[i] = helpers.GenTx(
txGen,
msgs,
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)},
helpers.DefaultGenTxGas,
Expand Down
19 changes: 13 additions & 6 deletions x/bank/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/stretchr/testify/require"

simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"

abci "github.com/tendermint/tendermint/abci/types"
Expand Down Expand Up @@ -110,7 +111,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 := simappparams.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 +179,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 := simappparams.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 +250,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 := simappparams.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 +302,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 := simappparams.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 +357,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 := simappparams.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 +410,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 := simappparams.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
5 changes: 4 additions & 1 deletion 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
}
}

txGen := simappparams.MakeEncodingConfig().TxGenerator
tx := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
Expand Down Expand Up @@ -247,7 +248,9 @@ func sendMsgMultiSend(
}
}

txGen := simappparams.MakeEncodingConfig().TxGenerator
tx := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
Expand Down
8 changes: 8 additions & 0 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)

txGen := simappparams.MakeEncodingConfig().TxGenerator
tx := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
Expand Down Expand Up @@ -147,7 +149,9 @@ func SimulateMsgWithdrawDelegatorReward(ak types.AccountKeeper, bk types.BankKee

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

txGen := simappparams.MakeEncodingConfig().TxGenerator
tx := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
Expand Down Expand Up @@ -197,7 +201,9 @@ func SimulateMsgWithdrawValidatorCommission(ak types.AccountKeeper, bk types.Ban

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

txGen := simappparams.MakeEncodingConfig().TxGenerator
tx := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
Expand Down Expand Up @@ -247,7 +253,9 @@ func SimulateMsgFundCommunityPool(ak types.AccountKeeper, bk types.BankKeeper, k
}

msg := types.NewMsgFundCommunityPool(fundAmount, funder.Address)
txGen := simappparams.MakeEncodingConfig().TxGenerator
tx := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
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 @@ -142,7 +142,9 @@ func SimulateSubmitProposal(
}
}

txGen := simappparams.MakeEncodingConfig().TxGenerator
tx := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
Expand Down Expand Up @@ -224,7 +226,9 @@ func SimulateMsgDeposit(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Ke
}
}

txGen := simappparams.MakeEncodingConfig().TxGenerator
tx := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
Expand Down Expand Up @@ -282,7 +286,9 @@ func operationSimulateMsgVote(ak types.AccountKeeper, bk types.BankKeeper, k kee
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate fees"), nil, err
}

txGen := simappparams.MakeEncodingConfig().TxGenerator
tx := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
Expand Down
1 change: 1 addition & 0 deletions x/ibc/testing/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ func (chain *TestChain) NextBlock() {

// SendMsg delivers a transaction through the application. It updates the senders sequence
// number and updates the TestChain's headers.
// TODO: update SignCheckDeliver to use tx generator
sahith-narahari marked this conversation as resolved.
Show resolved Hide resolved
func (chain *TestChain) SendMsg(msg sdk.Msg) error {
_, _, err := simapp.SignCheckDeliver(
chain.t,
Expand Down
2 changes: 2 additions & 0 deletions x/slashing/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ func SimulateMsgUnjail(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Kee

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

txGen := simappparams.MakeEncodingConfig().TxGenerator
tx := helpers.GenTx(
txGen,
[]sdk.Msg{msg},
fees,
helpers.DefaultGenTxGas,
Expand Down
10 changes: 6 additions & 4 deletions x/staking/app_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"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
Expand Down Expand Up @@ -69,7 +70,8 @@ func TestStakingMsgs(t *testing.T) {
)

header := abci.Header{Height: app.LastBlockHeight() + 1}
_, _, err := simapp.SignCheckDeliver(t, app.Codec(), app.BaseApp, header, []sdk.Msg{createValidatorMsg}, []uint64{0}, []uint64{0}, true, true, priv1)
txGen := simappparams.MakeEncodingConfig().TxGenerator
_, _, err := simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, []sdk.Msg{createValidatorMsg}, []uint64{0}, []uint64{0}, true, true, priv1)
require.NoError(t, err)
simapp.CheckBalance(t, app, addr1, sdk.Coins{genCoin.Sub(bondCoin)})

Expand All @@ -89,7 +91,7 @@ func TestStakingMsgs(t *testing.T) {
editValidatorMsg := types.NewMsgEditValidator(sdk.ValAddress(addr1), description, nil, nil)

header = abci.Header{Height: app.LastBlockHeight() + 1}
_, _, err = simapp.SignCheckDeliver(t, app.Codec(), app.BaseApp, header, []sdk.Msg{editValidatorMsg}, []uint64{0}, []uint64{1}, true, true, priv1)
_, _, err = simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, []sdk.Msg{editValidatorMsg}, []uint64{0}, []uint64{1}, true, true, priv1)
require.NoError(t, err)

validator = checkValidator(t, app, sdk.ValAddress(addr1), true)
Expand All @@ -100,7 +102,7 @@ func TestStakingMsgs(t *testing.T) {
delegateMsg := types.NewMsgDelegate(addr2, sdk.ValAddress(addr1), bondCoin)

header = abci.Header{Height: app.LastBlockHeight() + 1}
_, _, err = simapp.SignCheckDeliver(t, app.Codec(), app.BaseApp, header, []sdk.Msg{delegateMsg}, []uint64{1}, []uint64{0}, true, true, priv2)
_, _, err = simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, []sdk.Msg{delegateMsg}, []uint64{1}, []uint64{0}, true, true, priv2)
require.NoError(t, err)

simapp.CheckBalance(t, app, addr2, sdk.Coins{genCoin.Sub(bondCoin)})
Expand All @@ -109,7 +111,7 @@ func TestStakingMsgs(t *testing.T) {
// begin unbonding
beginUnbondingMsg := types.NewMsgUndelegate(addr2, sdk.ValAddress(addr1), bondCoin)
header = abci.Header{Height: app.LastBlockHeight() + 1}
_, _, err = simapp.SignCheckDeliver(t, app.Codec(), app.BaseApp, header, []sdk.Msg{beginUnbondingMsg}, []uint64{1}, []uint64{1}, true, true, priv2)
_, _, err = simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, []sdk.Msg{beginUnbondingMsg}, []uint64{1}, []uint64{1}, true, true, priv2)
require.NoError(t, err)

// delegation should exist anymore
Expand Down
Loading