diff --git a/helpers/test_helpers.go b/helpers/test_helpers.go index fa7b6d648bd0..5f71e24a8cc1 100644 --- a/helpers/test_helpers.go +++ b/helpers/test_helpers.go @@ -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 @@ -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 } diff --git a/simd/cmd/genaccounts.go b/simd/cmd/genaccounts.go index d5b4ce67dcbe..5dddc2ee7858 100644 --- a/simd/cmd/genaccounts.go +++ b/simd/cmd/genaccounts.go @@ -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 diff --git a/test_helpers.go b/test_helpers.go index 6ba55f418a93..758b84d4b616 100644 --- a/test_helpers.go +++ b/test_helpers.go @@ -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" @@ -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, @@ -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 @@ -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, @@ -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) {