Skip to content

Commit

Permalink
Merge PR: fix tx query proto (#1946)
Browse files Browse the repository at this point in the history
* fix tx query proto

* fix ut

Co-authored-by: KamiD <[email protected]>
  • Loading branch information
ItsFunny and KamiD authored Apr 29, 2022
1 parent 3cd0671 commit 1344c62
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
33 changes: 30 additions & 3 deletions cmd/exchaincli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func main() {
// Add --chain-id to persistent flags and mark it required
rootCmd.PersistentFlags().String(flags.FlagChainID, "", "Chain ID of tendermint node")
rootCmd.PersistentPreRunE = func(_ *cobra.Command, _ []string) error {
utils.SetParseAppTx(parseMsgEthereumTx)
utils.SetParseAppTx(wrapDecoder(parseMsgEthereumTx, parseProtobufTx))
return client.InitConfig(rootCmd)
}
protoCdc := sdkcodec.NewProtoCodec(interfaceReg)
Expand Down Expand Up @@ -102,7 +102,7 @@ func queryCmd(proxy *sdkcodec.CodecProxy, reg interfacetypes.InterfaceRegistry)
authcmd.GetAccountCmd(cdc),
flags.LineBreak,
authcmd.QueryTxsByEventsCmd(cdc),
authcmd.QueryTxCmd(cdc),
authcmd.QueryTxCmd(proxy),
flags.LineBreak,
)

Expand Down Expand Up @@ -152,7 +152,22 @@ func txCmd(proxy *sdkcodec.CodecProxy, reg interfacetypes.InterfaceRegistry) *co
return txCmd
}

func parseMsgEthereumTx(cdc *sdkcodec.Codec, txBytes []byte) (sdk.Tx, error) {
func wrapDecoder(handlers ...utils.ParseAppTxHandler) utils.ParseAppTxHandler {
return func(cdc *sdkcodec.CodecProxy, txBytes []byte) (sdk.Tx, error) {
var (
tx sdk.Tx
err error
)
for _, handler := range handlers {
tx, err = handler(cdc, txBytes)
if nil == err && tx != nil {
return tx, err
}
}
return tx, err
}
}
func parseMsgEthereumTx(cdc *sdkcodec.CodecProxy, txBytes []byte) (sdk.Tx, error) {
var tx evmtypes.MsgEthereumTx
// try to decode through RLP first
if err := authtypes.EthereumTxDecode(txBytes, &tx); err == nil {
Expand All @@ -164,3 +179,15 @@ func parseMsgEthereumTx(cdc *sdkcodec.Codec, txBytes []byte) (sdk.Tx, error) {
}
return &tx, nil
}

func parseProtobufTx(cdc *sdkcodec.CodecProxy, txBytes []byte) (sdk.Tx, error) {
tx, err := evmtypes.TxDecoder(cdc)(txBytes, evmtypes.IGNORE_HEIGHT_CHECKING)
if nil != err {
return nil, err
}
switch realTx := tx.(type) {
case *authtypes.IbcTx:
return authtypes.FromRelayIBCTx(cdc, realTx)
}
return tx, err
}
4 changes: 2 additions & 2 deletions libs/cosmos-sdk/x/auth/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ $ %s query txs --%s 'message.sender=cosmos1...&message.action=withdraw_delegator
}

// QueryTxCmd implements the default command for a tx query.
func QueryTxCmd(cdc *codec.Codec) *cobra.Command {
func QueryTxCmd(cdc *codec.CodecProxy) *cobra.Command {
cmd := &cobra.Command{
Use: "tx [hash]",
Short: "Query for a transaction by hash in a committed block",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
cliCtx := context.NewCLIContext().WithProxy(cdc)

output, err := utils.QueryTx(cliCtx, args[0])
if err != nil {
Expand Down
13 changes: 6 additions & 7 deletions libs/cosmos-sdk/x/auth/client/utils/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/okex/exchain/libs/cosmos-sdk/x/auth/types"
)

type ParseAppTxHandler func(cdc *codec.Codec, txBytes []byte) (sdk.Tx, error)
type ParseAppTxHandler func(cdc *codec.CodecProxy, txBytes []byte) (sdk.Tx, error)

var paresAppTx ParseAppTxHandler

Expand Down Expand Up @@ -69,7 +69,7 @@ func QueryTxsByEvents(cliCtx context.CLIContext, events []string, page, limit in
return nil, err
}

txs, err := formatTxResults(cliCtx.Codec, resTxs.Txs, resBlocks)
txs, err := formatTxResults(cliCtx.CodecProy, resTxs.Txs, resBlocks)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -113,7 +113,7 @@ func QueryTx(cliCtx context.CLIContext, hashHexStr string) (sdk.TxResponse, erro
return sdk.TxResponse{}, err
}

out, err := formatTxResult(cliCtx.Codec, resTx, resBlocks[resTx.Height])
out, err := formatTxResult(cliCtx.CodecProy, resTx, resBlocks[resTx.Height])
if err != nil {
return out, err
}
Expand All @@ -122,7 +122,7 @@ func QueryTx(cliCtx context.CLIContext, hashHexStr string) (sdk.TxResponse, erro
}

// formatTxResults parses the indexed txs into a slice of TxResponse objects.
func formatTxResults(cdc *codec.Codec, resTxs []*ctypes.ResultTx, resBlocks map[int64]*ctypes.ResultBlock) ([]sdk.TxResponse, error) {
func formatTxResults(cdc *codec.CodecProxy, resTxs []*ctypes.ResultTx, resBlocks map[int64]*ctypes.ResultBlock) ([]sdk.TxResponse, error) {
var err error
out := make([]sdk.TxResponse, len(resTxs))
for i := range resTxs {
Expand Down Expand Up @@ -172,7 +172,7 @@ func getBlocksForTxResults(cliCtx context.CLIContext, resTxs []*ctypes.ResultTx)
return resBlocks, nil
}

func formatTxResult(cdc *codec.Codec, resTx *ctypes.ResultTx, resBlock *ctypes.ResultBlock) (sdk.TxResponse, error) {
func formatTxResult(cdc *codec.CodecProxy, resTx *ctypes.ResultTx, resBlock *ctypes.ResultBlock) (sdk.TxResponse, error) {
tx, err := parseTx(cdc, resTx.Tx)
if err != nil {
return sdk.TxResponse{}, err
Expand All @@ -181,9 +181,8 @@ func formatTxResult(cdc *codec.Codec, resTx *ctypes.ResultTx, resBlock *ctypes.R
return sdk.NewResponseResultTx(resTx, tx, resBlock.Block.Time.Format(time.RFC3339)), nil
}

func parseTx(cdc *codec.Codec, txBytes []byte) (sdk.Tx, error) {
func parseTx(cdc *codec.CodecProxy, txBytes []byte) (sdk.Tx, error) {
var tx types.StdTx

err := cdc.UnmarshalBinaryLengthPrefixed(txBytes, &tx)
if err != nil && paresAppTx != nil {
return paresAppTx(cdc, txBytes)
Expand Down
11 changes: 7 additions & 4 deletions libs/cosmos-sdk/x/gov/client/utils/query_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
codectypes "github.com/okex/exchain/libs/cosmos-sdk/codec/types"
"testing"

"github.com/okex/exchain/libs/tendermint/rpc/client/mock"
Expand Down Expand Up @@ -40,12 +41,14 @@ func (mock TxSearchMock) Block(height *int64) (*ctypes.ResultBlock, error) {
return &ctypes.ResultBlock{Block: &tmtypes.Block{}}, nil
}

func newTestCodec() *codec.Codec {
func newTestCodec() *codec.CodecProxy {
cdc := codec.New()
sdk.RegisterCodec(cdc)
types.RegisterCodec(cdc)
authtypes.RegisterCodec(cdc)
return cdc
reg := codectypes.NewInterfaceRegistry()
px := codec.NewCodecProxy(codec.NewProtoCodec(reg), cdc)
return px
}

func TestGetPaginatedVotes(t *testing.T) {
Expand Down Expand Up @@ -137,12 +140,12 @@ func TestGetPaginatedVotes(t *testing.T) {
cdc = newTestCodec()
)
for i := range tc.txs {
tx, err := cdc.MarshalBinaryLengthPrefixed(&tc.txs[i])
tx, err := cdc.GetCdc().MarshalBinaryLengthPrefixed(&tc.txs[i])
require.NoError(t, err)
marshalled[i] = tmtypes.Tx(tx)
}
client := TxSearchMock{txs: marshalled}
ctx := context.CLIContext{}.WithCodec(cdc).WithTrustNode(true).WithClient(client)
ctx := context.CLIContext{}.WithProxy(cdc).WithTrustNode(true).WithClient(client)

params := types.NewQueryProposalVotesParams(0, tc.page, tc.limit)
votesData, err := QueryVotesByTxQuery(ctx, params)
Expand Down

0 comments on commit 1344c62

Please sign in to comment.