Skip to content

Commit

Permalink
Upgrade test helper signature
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Aug 6, 2020
1 parent 053b325 commit 51a4745
Showing 1 changed file with 54 additions and 17 deletions.
71 changes: 54 additions & 17 deletions app/integration/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ This file is full of test helper functions, taken from simapp

import (
"fmt"
"math/rand"
"os"
"testing"
"time"

wasmd "github.com/CosmWasm/wasmd/app"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authsign "github.com/cosmos/cosmos-sdk/x/auth/signing"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/stretchr/testify/require"

abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/libs/log"
Expand Down Expand Up @@ -93,8 +98,9 @@ func SignAndDeliver(
t *testing.T, app *wasmd.WasmApp, msgs []sdk.Msg,
accNums, seq []uint64, expPass bool, priv ...crypto.PrivKey,
) (sdk.GasInfo, *sdk.Result, error) {

tx := GenTx(
t.Helper()
tx, err := GenTx(
wasmd.MakeEncodingConfig().TxConfig,
msgs,
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)},
DefaultGenTxGas,
Expand All @@ -103,7 +109,7 @@ func SignAndDeliver(
seq,
priv...,
)

require.NoError(t, err)
// Simulate a sending a transaction and committing a block
app.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{Height: app.LastBlockHeight() + 1, ChainID: SimAppChainID}})

Expand All @@ -123,27 +129,58 @@ func SignAndDeliver(
}

// 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{
Amount: feeAmt,
Gas: gas,
}
func GenTx(gen client.TxConfig, 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))

sigs := make([]authtypes.StdSignature, len(priv))
signMode := gen.SignModeHandler().DefaultMode()

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

memo := "Test tx"
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{
PubKey: p.PubKey(),
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
}

0 comments on commit 51a4745

Please sign in to comment.