Migrate remaining x/auth tx tests to proto (#6898)

* Fix ReadTx

* Migrate batch scanner tests

* add TODO

Co-authored-by: Aaron Craelius <aaronc@users.noreply.github.com>
Co-authored-by: Aaron Craelius <aaron@regen.network>
Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
This commit is contained in:
SaReN 2020-07-30 21:06:36 +05:30 committed by GitHub
parent 05c513ee59
commit e15d0322dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 59 deletions

View File

@ -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
}
@ -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

View File

@ -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"
@ -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)
}
@ -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)
@ -98,79 +97,73 @@ 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)
// Build a test transaction
fee := authtypes.NewStdFee(50000, sdk.Coins{sdk.NewInt64Coin("atom", 150)})
stdTx := authtypes.NewStdTx([]sdk.Msg{}, fee, []authtypes.StdSignature{}, "foomemo")
feeAmount := sdk.Coins{sdk.NewInt64Coin("atom", 150)}
gasLimit := uint64(50000)
memo := "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"}
// 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)
{"msg":[],"fee":{"amount":[{"denom":"atom","amount":"1"}],"gas":"10000"},"signatures":[],"memo":"foomemo"}
`
tests := []struct {
name string
batch string
@ -178,21 +171,20 @@ malformed
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)