Skip to content

Commit

Permalink
amino test
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuvubk committed Oct 21, 2024
1 parent 4d3a46e commit 9cba934
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 5 deletions.
6 changes: 6 additions & 0 deletions tests/systemtests/testdata/tx_amino1.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
((�
o��a�
-cosmos1hzw8v2kk4csg8xr3fhy8ykyymykknm2tdgvf7k-cosmos1hzw8v2kk4csg8xr3fhy8ykyymykknm2tdgvf7k
stake10

stake10�� "foobar
32 changes: 32 additions & 0 deletions tests/systemtests/testdata/tx_amino1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"type": "cosmos-sdk/StdTx",
"value": {
"msg": [
{
"type": "cosmos-sdk/MsgSend",
"value": {
"from_address": "cosmos1hzw8v2kk4csg8xr3fhy8ykyymykknm2tdgvf7k",
"to_address": "cosmos1hzw8v2kk4csg8xr3fhy8ykyymykknm2tdgvf7k",
"amount": [
{
"denom": "stake",
"amount": "10"
}
]
}
}
],
"fee": {
"amount": [
{
"denom": "stake",
"amount": "10"
}
],
"gas": "200000"
},
"signatures": [],
"memo": "foobar",
"timeout_height": "0"
}
}
218 changes: 213 additions & 5 deletions tests/systemtests/tx_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//go:build system_test
//go:build !system_test

package systemtests

import (
"context"
"encoding/base64"
"encoding/json"
"os"

"fmt"

Expand All @@ -16,9 +17,14 @@ import (
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/legacy"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/std"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

var bankMsgSendEventAction = "message.action='/cosmos.bank.v1beta1.MsgSend'"
Expand Down Expand Up @@ -171,6 +177,8 @@ func TestSimulateTx_GRPCGateway(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
reqBz, err := json.Marshal(tc.req)
require.NoError(t, err)

res, err := testutil.PostRequest(fmt.Sprintf("%s/cosmos/tx/v1beta1/simulate", baseURL), "application/json", reqBz)
require.NoError(t, err)
if tc.expErr {
Expand Down Expand Up @@ -306,7 +314,6 @@ func TestGetTxEvents_GRPC(t *testing.T) {
}
})
}

}

func TestGetTxEvents_GRPCGateway(t *testing.T) {
Expand Down Expand Up @@ -405,7 +412,6 @@ func TestGetTxEvents_GRPCGateway(t *testing.T) {
}
})
}

}

func TestGetTx_GRPC(t *testing.T) {
Expand Down Expand Up @@ -722,6 +728,7 @@ func TestTxEncode_GRPCGateway(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
reqBz, err := json.Marshal(tc.req)
require.NoError(t, err)

res, err := testutil.PostRequest(fmt.Sprintf("%s/cosmos/tx/v1beta1/encode", baseUrl), "application/json", reqBz)
require.NoError(t, err)
Expand Down Expand Up @@ -847,6 +854,190 @@ func TestTxDecode_GRPCGateway(t *testing.T) {
}
}

func TestTxEncodeAmino_GRPC(t *testing.T) {
sut.ResetChain(t)
sut.StartChain(t)

legacyAmino := codec.NewLegacyAmino()
std.RegisterLegacyAminoCodec(legacyAmino)
legacytx.RegisterLegacyAminoCodec(legacyAmino)
legacy.RegisterAminoMsg(legacyAmino, &banktypes.MsgSend{}, "cosmos-sdk/MsgSend")

qc := tx.NewServiceClient(sut.RPCClient(t))
txJSONBytes, stdTx := readTestAminoTxJSON(t, legacyAmino)

testCases := []struct {
name string
req *tx.TxEncodeAminoRequest
expErr bool
expErrMsg string
}{
{"nil request", nil, true, "request cannot be nil"},
{"empty request", &tx.TxEncodeAminoRequest{}, true, "invalid empty tx json"},
{"invalid request", &tx.TxEncodeAminoRequest{AminoJson: "invalid tx json"}, true, "invalid request"},
{"valid request with amino-json", &tx.TxEncodeAminoRequest{AminoJson: string(txJSONBytes)}, false, ""},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res, err := qc.TxEncodeAmino(context.Background(), tc.req)
if tc.expErr {
require.Error(t, err)
require.Contains(t, err.Error(), tc.expErrMsg)
require.Empty(t, res)
} else {
require.NoError(t, err)
require.NotEmpty(t, res.GetAminoBinary())

var decodedTx legacytx.StdTx
err = legacyAmino.Unmarshal(res.AminoBinary, &decodedTx)
require.NoError(t, err)
require.Equal(t, decodedTx.GetMsgs(), stdTx.GetMsgs())
}
})
}
}

func TestTxEncodeAmino_GRPCGateway(t *testing.T) {
sut.ResetChain(t)
sut.StartChain(t)

legacyAmino := codec.NewLegacyAmino()
std.RegisterLegacyAminoCodec(legacyAmino)
legacytx.RegisterLegacyAminoCodec(legacyAmino)
legacy.RegisterAminoMsg(legacyAmino, &banktypes.MsgSend{}, "cosmos-sdk/MsgSend")

baseUrl := sut.APIAddress()
txJSONBytes, stdTx := readTestAminoTxJSON(t, legacyAmino)

testCases := []struct {
name string
req *tx.TxEncodeAminoRequest
expErr bool
expErrMsg string
}{
{"empty request", &tx.TxEncodeAminoRequest{}, true, "invalid empty tx json"},
{"invalid request", &tx.TxEncodeAminoRequest{AminoJson: "invalid tx json"}, true, "cannot parse disfix JSON wrapper"},
{"valid request with amino-json", &tx.TxEncodeAminoRequest{AminoJson: string(txJSONBytes)}, false, ""},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
reqBz, err := json.Marshal(tc.req)
require.NoError(t, err)

res, err := testutil.PostRequest(fmt.Sprintf("%s/cosmos/tx/v1beta1/encode/amino", baseUrl), "application/json", reqBz)
require.NoError(t, err)
if tc.expErr {
require.Contains(t, string(res), tc.expErrMsg)
} else {
var result tx.TxEncodeAminoResponse
err := json.Unmarshal(res, &result)
require.NoError(t, err)

var decodedTx legacytx.StdTx
err = legacyAmino.Unmarshal(result.AminoBinary, &decodedTx)
require.NoError(t, err)
require.Equal(t, decodedTx.GetMsgs(), stdTx.GetMsgs())
}
})
}
}

func TestTxDecodeAmino_GRPC(t *testing.T) {
sut.ResetChain(t)
sut.StartChain(t)

legacyAmino := codec.NewLegacyAmino()
std.RegisterLegacyAminoCodec(legacyAmino)
legacytx.RegisterLegacyAminoCodec(legacyAmino)
legacy.RegisterAminoMsg(legacyAmino, &banktypes.MsgSend{}, "cosmos-sdk/MsgSend")

qc := tx.NewServiceClient(sut.RPCClient(t))
encodedTx, stdTx := readTestAminoTxBinary(t, legacyAmino)

invalidTxBytes := append(encodedTx, byte(0o00))

testCases := []struct {
name string
req *tx.TxDecodeAminoRequest
expErr bool
expErrMsg string
}{
{"nil request", nil, true, "request cannot be nil"},
{"empty request", &tx.TxDecodeAminoRequest{}, true, "invalid empty tx bytes"},
{"invalid tx bytes", &tx.TxDecodeAminoRequest{AminoBinary: invalidTxBytes}, true, "invalid request"},
{"valid request with tx bytes", &tx.TxDecodeAminoRequest{AminoBinary: encodedTx}, false, ""},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res, err := qc.TxDecodeAmino(context.Background(), tc.req)
if tc.expErr {
require.Error(t, err)
require.Contains(t, err.Error(), tc.expErrMsg)
require.Empty(t, res)
} else {
require.NoError(t, err)
require.NotEmpty(t, res.GetAminoJson())

var decodedTx legacytx.StdTx
err = legacyAmino.UnmarshalJSON([]byte(res.GetAminoJson()), &decodedTx)
require.NoError(t, err)
require.Equal(t, stdTx.GetMsgs(), decodedTx.GetMsgs())
}
})
}
}

func TestTxDecodeAmino_GRPCGateway(t *testing.T) {
sut.ResetChain(t)
sut.StartChain(t)

legacyAmino := codec.NewLegacyAmino()
std.RegisterLegacyAminoCodec(legacyAmino)
legacytx.RegisterLegacyAminoCodec(legacyAmino)
legacy.RegisterAminoMsg(legacyAmino, &banktypes.MsgSend{}, "cosmos-sdk/MsgSend")

baseUrl := sut.APIAddress()
encodedTx, stdTx := readTestAminoTxBinary(t, legacyAmino)

invalidTxBytes := append(encodedTx, byte(0o00))

testCases := []struct {
name string
req *tx.TxDecodeAminoRequest
expErr bool
expErrMsg string
}{
{"empty request", &tx.TxDecodeAminoRequest{}, true, "invalid empty tx bytes"},
{"invalid tx bytes", &tx.TxDecodeAminoRequest{AminoBinary: invalidTxBytes}, true, "unmarshal to legacytx.StdTx failed"},
{"valid request with tx bytes", &tx.TxDecodeAminoRequest{AminoBinary: encodedTx}, false, ""},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
reqBz, err := json.Marshal(tc.req)
require.NoError(t, err)

res, err := testutil.PostRequest(fmt.Sprintf("%s/cosmos/tx/v1beta1/decode/amino", baseUrl), "application/json", reqBz)
require.NoError(t, err)
if tc.expErr {
require.Contains(t, string(res), tc.expErrMsg)
} else {
var result tx.TxDecodeAminoResponse
err := json.Unmarshal(res, &result)
require.NoError(t, err)

var decodedTx legacytx.StdTx
err = legacyAmino.UnmarshalJSON([]byte(result.AminoJson), &decodedTx)
require.NoError(t, err)
require.Equal(t, stdTx.GetMsgs(), decodedTx.GetMsgs())
}
})
}
}

func TestSimMultiSigTx(t *testing.T) {
sut.ResetChain(t)

Expand All @@ -864,7 +1055,7 @@ func TestSimMultiSigTx(t *testing.T) {
sut.StartChain(t)

multiSigName := "multisig"
res := cli.RunCommandWithArgs("keys", "add", multiSigName, "--multisig=account1,account2", "--multisig-threshold=2", "--keyring-backend=test", "--home=./testnet")
cli.RunCommandWithArgs("keys", "add", multiSigName, "--multisig=account1,account2", "--multisig-threshold=2", "--keyring-backend=test", "--home=./testnet")
multiSigAddr := cli.GetKeyAddr(multiSigName)

// Send from validator to multisig addr
Expand All @@ -880,7 +1071,7 @@ func TestSimMultiSigTx(t *testing.T) {
// create unsign tx
var newTransferAmount int64 = 100
bankSendCmdArgs := []string{"tx", "bank", "send", multiSigAddr, valAddr, fmt.Sprintf("%d%s", newTransferAmount, denom), "--fees=10stake", "--sign-mode=direct", "--generate-only"}
res = cli.RunCommandWithArgs(bankSendCmdArgs...)
res := cli.RunCommandWithArgs(bankSendCmdArgs...)
txFile := StoreTempFile(t, []byte(res))

res = cli.RunCommandWithArgs("tx", "sign", txFile.Name(), fmt.Sprintf("--from=%s", "account1"), fmt.Sprintf("--multisig=%s", multiSigAddr), fmt.Sprintf("--chain-id=%s", sut.chainID), "--keyring-backend=test", "--home=./testnet")
Expand All @@ -896,5 +1087,22 @@ func TestSimMultiSigTx(t *testing.T) {

multiSigBalance = cli.QueryBalance(multiSigAddr, denom)
require.Equal(t, multiSigBalance, transferAmount-newTransferAmount-10)
}

func readTestAminoTxJSON(t *testing.T, aminoCodec *codec.LegacyAmino) ([]byte, *legacytx.StdTx) {
txJSONBytes, err := os.ReadFile("testdata/tx_amino1.json")
require.NoError(t, err)
var stdTx legacytx.StdTx
err = aminoCodec.UnmarshalJSON(txJSONBytes, &stdTx)
require.NoError(t, err)
return txJSONBytes, &stdTx
}

func readTestAminoTxBinary(t *testing.T, aminoCodec *codec.LegacyAmino) ([]byte, *legacytx.StdTx) {
txJSONBytes, err := os.ReadFile("testdata/tx_amino1.bin")
require.NoError(t, err)
var stdTx legacytx.StdTx
err = aminoCodec.Unmarshal(txJSONBytes, &stdTx)
require.NoError(t, err)
return txJSONBytes, &stdTx
}

0 comments on commit 9cba934

Please sign in to comment.