2020-07-30 08:36:36 -07:00
|
|
|
package client_test
|
2018-08-22 04:38:55 -07:00
|
|
|
|
|
|
|
import (
|
2020-07-30 08:36:36 -07:00
|
|
|
"fmt"
|
2020-06-08 08:19:29 -07:00
|
|
|
"strings"
|
2018-12-19 16:26:33 -08:00
|
|
|
"testing"
|
|
|
|
|
2020-07-30 08:36:36 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
2020-07-09 05:21:20 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/testutil"
|
2020-07-20 05:30:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
2020-07-30 08:36:36 -07:00
|
|
|
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
|
2020-06-18 13:29:41 -07:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
|
2018-12-12 13:29:42 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/tendermint/tendermint/crypto/ed25519"
|
2018-12-10 06:27:25 -08:00
|
|
|
|
2019-02-26 03:34:27 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-06-04 03:38:24 -07:00
|
|
|
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
|
2018-12-10 06:27:25 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-06-05 16:26:17 -07:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2018-08-22 04:38:55 -07:00
|
|
|
)
|
|
|
|
|
2018-12-12 13:29:42 -08:00
|
|
|
var (
|
|
|
|
priv = ed25519.GenPrivKey()
|
|
|
|
addr = sdk.AccAddress(priv.PubKey().Address())
|
|
|
|
)
|
|
|
|
|
2018-08-22 04:38:55 -07:00
|
|
|
func TestParseQueryResponse(t *testing.T) {
|
2020-04-03 07:59:46 -07:00
|
|
|
simRes := &sdk.SimulationResponse{
|
2020-03-19 18:40:07 -07:00
|
|
|
GasInfo: sdk.GasInfo{GasUsed: 10, GasWanted: 20},
|
2020-03-19 18:48:20 -07:00
|
|
|
Result: &sdk.Result{Data: []byte("tx data"), Log: "log"},
|
2020-03-19 18:40:07 -07:00
|
|
|
}
|
|
|
|
|
2020-04-03 07:59:46 -07:00
|
|
|
bz, err := codec.ProtoMarshalJSON(simRes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-07-30 08:36:36 -07:00
|
|
|
res, err := authclient.ParseQueryResponse(bz)
|
2020-03-19 18:40:07 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 10, int(res.GasInfo.GasUsed))
|
2020-03-19 18:48:20 -07:00
|
|
|
require.NotNil(t, res.Result)
|
2020-03-19 18:40:07 -07:00
|
|
|
|
2020-07-30 08:36:36 -07:00
|
|
|
res, err = authclient.ParseQueryResponse([]byte("fuzzy"))
|
2020-03-19 18:40:07 -07:00
|
|
|
require.Error(t, err)
|
2018-08-22 04:38:55 -07:00
|
|
|
}
|
2018-08-25 12:12:14 -07:00
|
|
|
|
2020-07-30 08:36:36 -07:00
|
|
|
// TODO: remove this and authclient.GetTxEncoder after the proto tx migration is complete
|
2018-12-12 13:29:42 -08:00
|
|
|
func TestDefaultTxEncoder(t *testing.T) {
|
2019-05-28 01:44:04 -07:00
|
|
|
cdc := makeCodec()
|
2018-12-12 13:29:42 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
defaultEncoder := authtypes.DefaultTxEncoder(cdc)
|
2020-07-30 08:36:36 -07:00
|
|
|
encoder := authclient.GetTxEncoder(cdc)
|
2018-12-12 13:29:42 -08:00
|
|
|
|
|
|
|
compareEncoders(t, defaultEncoder, encoder)
|
|
|
|
}
|
|
|
|
|
2020-07-30 08:36:36 -07:00
|
|
|
func TestReadTxFromFile(t *testing.T) {
|
2020-06-08 08:19:29 -07:00
|
|
|
t.Parallel()
|
2020-07-30 08:36:36 -07:00
|
|
|
encodingConfig := simapp.MakeEncodingConfig()
|
2020-06-18 13:29:41 -07:00
|
|
|
|
2020-07-30 08:36:36 -07:00
|
|
|
txCfg := encodingConfig.TxConfig
|
2020-06-18 13:29:41 -07:00
|
|
|
clientCtx := client.Context{}
|
2020-07-30 08:36:36 -07:00
|
|
|
clientCtx = clientCtx.WithInterfaceRegistry(encodingConfig.InterfaceRegistry)
|
|
|
|
clientCtx = clientCtx.WithTxConfig(txCfg)
|
|
|
|
|
|
|
|
feeAmount := sdk.Coins{sdk.NewInt64Coin("atom", 150)}
|
|
|
|
gasLimit := uint64(50000)
|
|
|
|
memo := "foomemo"
|
2019-02-26 03:34:27 -08:00
|
|
|
|
2020-07-30 08:36:36 -07:00
|
|
|
txBuilder := txCfg.NewTxBuilder()
|
|
|
|
txBuilder.SetFeeAmount(feeAmount)
|
|
|
|
txBuilder.SetGasLimit(gasLimit)
|
|
|
|
txBuilder.SetMemo(memo)
|
2019-02-26 03:34:27 -08:00
|
|
|
|
|
|
|
// Write it to the file
|
2020-07-30 08:36:36 -07:00
|
|
|
encodedTx, err := txCfg.TxJSONEncoder()(txBuilder.GetTx())
|
2020-06-18 13:29:41 -07:00
|
|
|
require.NoError(t, err)
|
2020-07-09 05:21:20 -07:00
|
|
|
jsonTxFile, cleanup := testutil.WriteToNewTempFile(t, string(encodedTx))
|
|
|
|
t.Cleanup(cleanup)
|
2019-02-26 03:34:27 -08:00
|
|
|
|
|
|
|
// Read it back
|
2020-07-30 08:36:36 -07:00
|
|
|
decodedTx, err := authclient.ReadTxFromFile(clientCtx, jsonTxFile.Name())
|
2019-05-01 08:34:43 -07:00
|
|
|
require.NoError(t, err)
|
2020-07-30 08:36:36 -07:00
|
|
|
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())
|
2019-02-26 03:34:27 -08:00
|
|
|
}
|
|
|
|
|
2020-06-08 08:19:29 -07:00
|
|
|
func TestBatchScanner_Scan(t *testing.T) {
|
|
|
|
t.Parallel()
|
2020-07-30 08:36:36 -07:00
|
|
|
encodingConfig := simapp.MakeEncodingConfig()
|
2020-07-29 15:33:42 -07:00
|
|
|
|
|
|
|
txGen := encodingConfig.TxConfig
|
|
|
|
clientCtx := client.Context{}
|
|
|
|
clientCtx = clientCtx.WithTxConfig(txGen)
|
2020-06-08 08:19:29 -07:00
|
|
|
|
2020-07-30 08:36:36 -07:00
|
|
|
// 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)
|
|
|
|
|
2020-06-08 08:19:29 -07:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
batch string
|
|
|
|
wantScannerError bool
|
|
|
|
wantUnmarshalError bool
|
|
|
|
numTxs int
|
|
|
|
}{
|
2020-07-30 08:36:36 -07:00
|
|
|
{"good batch", goodBatchOf3Txs, false, false, 3},
|
|
|
|
{"malformed", malformedBatch, false, true, 1},
|
|
|
|
{"missing trailing newline", batchOf2TxsWithNoNewline, false, false, 2},
|
|
|
|
{"empty line", batchWithEmptyLine, false, true, 1},
|
2020-06-08 08:19:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-07-30 08:36:36 -07:00
|
|
|
scanner, i := authclient.NewBatchScanner(clientCtx.TxConfig, strings.NewReader(tt.batch)), 0
|
2020-06-08 08:19:29 -07:00
|
|
|
for scanner.Scan() {
|
2020-07-29 15:33:42 -07:00
|
|
|
_ = scanner.Tx()
|
2020-06-08 08:19:29 -07:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
require.Equal(t, tt.wantScannerError, scanner.Err() != nil)
|
|
|
|
require.Equal(t, tt.wantUnmarshalError, scanner.UnmarshalErr() != nil)
|
|
|
|
require.Equal(t, tt.numTxs, i)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 08:34:43 -07:00
|
|
|
func compareEncoders(t *testing.T, expected sdk.TxEncoder, actual sdk.TxEncoder) {
|
2020-07-06 10:03:45 -07:00
|
|
|
msgs := []sdk.Msg{testdata.NewTestMsg(addr)}
|
2019-06-08 03:04:52 -07:00
|
|
|
tx := authtypes.NewStdTx(msgs, authtypes.StdFee{}, []authtypes.StdSignature{}, "")
|
2019-05-01 08:34:43 -07:00
|
|
|
|
|
|
|
defaultEncoderBytes, err := expected(tx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
encoderBytes, err := actual(tx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, defaultEncoderBytes, encoderBytes)
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:41:21 -07:00
|
|
|
func makeCodec() *codec.LegacyAmino {
|
2019-05-01 08:34:43 -07:00
|
|
|
var cdc = codec.New()
|
|
|
|
sdk.RegisterCodec(cdc)
|
2020-06-04 03:38:24 -07:00
|
|
|
cryptocodec.RegisterCrypto(cdc)
|
2019-06-05 16:26:17 -07:00
|
|
|
authtypes.RegisterCodec(cdc)
|
2020-07-06 10:03:45 -07:00
|
|
|
cdc.RegisterConcrete(testdata.TestMsg{}, "cosmos-sdk/Test", nil)
|
2019-05-01 08:34:43 -07:00
|
|
|
return cdc
|
|
|
|
}
|