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

Migrate remaining x/auth tx tests to proto #6898

Merged
merged 7 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions x/auth/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func CalculateGas(
return sdk.SimulationResponse{}, 0, err
}

simRes, err := parseQueryResponse(rawRes)
simRes, err := ParseQueryResponse(rawRes)
if err != nil {
return sdk.SimulationResponse{}, 0, err
}
Expand Down Expand Up @@ -211,7 +211,7 @@ func adjustGasEstimate(estimate uint64, adjustment float64) uint64 {
return uint64(adjustment * float64(estimate))
}

func parseQueryResponse(bz []byte) (sdk.SimulationResponse, error) {
func ParseQueryResponse(bz []byte) (sdk.SimulationResponse, error) {
var simRes sdk.SimulationResponse
if err := jsonpb.Unmarshal(strings.NewReader(string(bz)), &simRes); err != nil {
return sdk.SimulationResponse{}, err
Expand Down
108 changes: 50 additions & 58 deletions x/auth/client/tx_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package client
package client_test

import (
"encoding/json"
"errors"
"fmt"
"strings"
"testing"

"github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata"

simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"

"github.com/cosmos/cosmos-sdk/client"

Expand All @@ -37,12 +36,12 @@ func TestParseQueryResponse(t *testing.T) {
bz, err := codec.ProtoMarshalJSON(simRes)
require.NoError(t, err)

res, err := parseQueryResponse(bz)
res, err := authclient.ParseQueryResponse(bz)
require.NoError(t, err)
require.Equal(t, 10, int(res.GasInfo.GasUsed))
require.NotNil(t, res.Result)

res, err = parseQueryResponse([]byte("fuzzy"))
res, err = authclient.ParseQueryResponse([]byte("fuzzy"))
require.Error(t, err)
}

Expand Down Expand Up @@ -84,7 +83,7 @@ func TestCalculateGas(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
queryFunc := makeQueryFunc(tt.args.queryFuncGasUsed, tt.args.queryFuncWantErr)
simRes, gotAdjusted, err := CalculateGas(queryFunc, cdc, []byte(""), tt.args.adjustment)
simRes, gotAdjusted, err := authclient.CalculateGas(queryFunc, cdc, []byte(""), tt.args.adjustment)
if tt.expPass {
require.NoError(t, err)
require.Equal(t, simRes.GasInfo.GasUsed, tt.wantEstimate)
Expand All @@ -98,101 +97,94 @@ func TestCalculateGas(t *testing.T) {
}
}

// TODO: remove this and authclient.GetTxEncoder after the proto tx migration is complete
func TestDefaultTxEncoder(t *testing.T) {
cdc := makeCodec()

defaultEncoder := authtypes.DefaultTxEncoder(cdc)
encoder := GetTxEncoder(cdc)
encoder := authclient.GetTxEncoder(cdc)

compareEncoders(t, defaultEncoder, encoder)
}

func TestConfiguredTxEncoder(t *testing.T) {
cdc := makeCodec()

customEncoder := func(tx sdk.Tx) ([]byte, error) {
return json.Marshal(tx)
}

config := sdk.GetConfig()
config.SetTxEncoder(customEncoder)

encoder := GetTxEncoder(cdc)

compareEncoders(t, customEncoder, encoder)
}

func TestReadStdTxFromFile(t *testing.T) {
func TestReadTxFromFile(t *testing.T) {
t.Parallel()
encodingConfig := simapp.MakeEncodingConfig()

encodingConfig := simappparams.MakeEncodingConfig()
sdk.RegisterCodec(encodingConfig.Amino)

txGen := encodingConfig.TxConfig
txCfg := encodingConfig.TxConfig
clientCtx := client.Context{}
clientCtx = clientCtx.WithTxConfig(txGen)
clientCtx = clientCtx.WithInterfaceRegistry(encodingConfig.InterfaceRegistry)
clientCtx = clientCtx.WithTxConfig(txCfg)

feeAmount := sdk.Coins{sdk.NewInt64Coin("atom", 150)}
gasLimit := uint64(50000)
memo := "foomemo"

// Build a test transaction
fee := authtypes.NewStdFee(50000, sdk.Coins{sdk.NewInt64Coin("atom", 150)})
stdTx := authtypes.NewStdTx([]sdk.Msg{}, fee, []authtypes.StdSignature{}, "foomemo")
txBuilder := txCfg.NewTxBuilder()
txBuilder.SetFeeAmount(feeAmount)
txBuilder.SetGasLimit(gasLimit)
txBuilder.SetMemo(memo)

// Write it to the file
encodedTx, err := txGen.TxJSONEncoder()(stdTx)
encodedTx, err := txCfg.TxJSONEncoder()(txBuilder.GetTx())
require.NoError(t, err)
jsonTxFile, cleanup := testutil.WriteToNewTempFile(t, string(encodedTx))
t.Cleanup(cleanup)

// Read it back
decodedTx, err := ReadTxFromFile(clientCtx, jsonTxFile.Name())
decodedTx, err := authclient.ReadTxFromFile(clientCtx, jsonTxFile.Name())
require.NoError(t, err)
require.Equal(t, decodedTx.(authtypes.StdTx).Memo, "foomemo")
txBldr, err := txCfg.WrapTxBuilder(decodedTx)
require.NoError(t, err)
t.Log(txBuilder.GetTx())
t.Log(txBldr.GetTx())
require.Equal(t, txBuilder.GetTx().GetMemo(), txBldr.GetTx().GetMemo())
require.Equal(t, txBuilder.GetTx().GetFee(), txBldr.GetTx().GetFee())
}

func TestBatchScanner_Scan(t *testing.T) {
t.Parallel()
encodingConfig := simappparams.MakeEncodingConfig()
std.RegisterCodec(encodingConfig.Amino)
encodingConfig := simapp.MakeEncodingConfig()

txGen := encodingConfig.TxConfig
clientCtx := client.Context{}
clientCtx = clientCtx.WithTxConfig(txGen)

batch1 := `{"msg":[],"fee":{"amount":[{"denom":"atom","amount":"150"}],"gas":"50000"},"signatures":[],"memo":"foomemo"}
{"msg":[],"fee":{"amount":[{"denom":"atom","amount":"150"}],"gas":"10000"},"signatures":[],"memo":"foomemo"}
{"msg":[],"fee":{"amount":[{"denom":"atom","amount":"1"}],"gas":"10000"},"signatures":[],"memo":"foomemo"}
`
batch2 := `{"msg":[],"fee":{"amount":[{"denom":"atom","amount":"150"}],"gas":"50000"},"signatures":[],"memo":"foomemo"}
malformed
{"msg":[],"fee":{"amount":[{"denom":"atom","amount":"1"}],"gas":"10000"},"signatures":[],"memo":"foomemo"}
`
batch3 := `{"msg":[],"fee":{"amount":[{"denom":"atom","amount":"150"}],"gas":"50000"},"signatures":[],"memo":"foomemo"}
{"msg":[],"fee":{"amount":[{"denom":"atom","amount":"1"}],"gas":"10000"},"signatures":[],"memo":"foomemo"}`
batch4 := `{"msg":[],"fee":{"amount":[{"denom":"atom","amount":"150"}],"gas":"50000"},"signatures":[],"memo":"foomemo"}

{"msg":[],"fee":{"amount":[{"denom":"atom","amount":"1"}],"gas":"10000"},"signatures":[],"memo":"foomemo"}
`
// generate some tx JSON
bldr := txGen.NewTxBuilder()
bldr.SetGasLimit(50000)
bldr.SetFeeAmount(sdk.NewCoins(sdk.NewInt64Coin("atom", 150)))
bldr.SetMemo("foomemo")
txJson, err := txGen.TxJSONEncoder()(bldr.GetTx())
require.NoError(t, err)

// use the tx JSON to generate some tx batches (it doesn't matter that we use the same JSON because we don't care about the actual context)
goodBatchOf3Txs := fmt.Sprintf("%s\n%s\n%s\n", txJson, txJson, txJson)
malformedBatch := fmt.Sprintf("%s\nmalformed\n%s\n", txJson, txJson)
batchOf2TxsWithNoNewline := fmt.Sprintf("%s\n%s", txJson, txJson)
batchWithEmptyLine := fmt.Sprintf("%s\n\n%s", txJson, txJson)

tests := []struct {
name string
batch string
wantScannerError bool
wantUnmarshalError bool
numTxs int
}{
{"good batch", batch1, false, false, 3},
{"malformed", batch2, false, true, 1},
{"missing trailing newline", batch3, false, false, 2},
{"empty line", batch4, false, true, 1},
{"good batch", goodBatchOf3Txs, false, false, 3},
{"malformed", malformedBatch, false, true, 1},
{"missing trailing newline", batchOf2TxsWithNoNewline, false, false, 2},
{"empty line", batchWithEmptyLine, false, true, 1},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
scanner, i := NewBatchScanner(clientCtx.TxConfig, strings.NewReader(tt.batch)), 0
scanner, i := authclient.NewBatchScanner(clientCtx.TxConfig, strings.NewReader(tt.batch)), 0
for scanner.Scan() {
_ = scanner.Tx()
i++
}
t.Log(scanner.theTx)
require.Equal(t, tt.wantScannerError, scanner.Err() != nil)
require.Equal(t, tt.wantUnmarshalError, scanner.UnmarshalErr() != nil)
require.Equal(t, tt.numTxs, i)
Expand Down