Move QueryTx functions to x/auth/tx (#8734)

* Use x/auth/client for querying Txs

* Fix lint

* Fix small test

* Update todos

* Move QueryTx functions to x/auth/tx

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
This commit is contained in:
Amaury 2021-03-01 16:57:28 +01:00 committed by GitHub
parent 5f2b90c3c7
commit a93edeef4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 49 additions and 89 deletions

View File

@ -120,7 +120,7 @@ func (s *IntegrationTestSuite) TestGRPCServer_GetTxsEvent() {
_, err := txServiceClient.GetTxsEvent(
context.Background(),
&tx.GetTxsEventRequest{
Events: []string{"message.action=send"},
Events: []string{"message.action='send'"},
},
)
s.Require().NoError(err)

View File

@ -31,7 +31,6 @@ import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
auth "github.com/cosmos/cosmos-sdk/x/auth/types"
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
@ -254,7 +253,7 @@ func (c *Client) TxOperationsAndSignersAccountIdentifiers(signed bool, txBytes [
// GetTx returns a transaction given its hash
func (c *Client) GetTx(_ context.Context, hash string) (*types.Transaction, error) {
txResp, err := authclient.QueryTx(c.clientCtx, hash)
txResp, err := authtx.QueryTx(c.clientCtx, hash)
if err != nil {
return nil, crgerrs.WrapError(crgerrs.ErrUnknown, err.Error())
}

View File

@ -12,7 +12,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/version"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/cosmos/cosmos-sdk/x/auth/types"
)
@ -189,7 +189,7 @@ $ %s query txs --%s 'message.sender=cosmos1...&message.action=withdraw_delegator
page, _ := cmd.Flags().GetInt(flags.FlagPage)
limit, _ := cmd.Flags().GetInt(flags.FlagLimit)
txs, err := authclient.QueryTxsByEvents(clientCtx, tmEvents, page, limit, "")
txs, err := authtx.QueryTxsByEvents(clientCtx, tmEvents, page, limit, "")
if err != nil {
return err
}
@ -219,7 +219,7 @@ func QueryTxCmd() *cobra.Command {
if err != nil {
return err
}
output, err := authclient.QueryTx(clientCtx, args[0])
output, err := authtx.QueryTx(clientCtx, args[0])
if err != nil {
return err
}

View File

@ -13,7 +13,7 @@ import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/cosmos/cosmos-sdk/x/auth/types"
genutilrest "github.com/cosmos/cosmos-sdk/x/genutil/client/rest"
)
@ -99,7 +99,7 @@ func QueryTxsRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}
searchResult, err := authclient.QueryTxsByEvents(clientCtx, events, page, limit, "")
searchResult, err := authtx.QueryTxsByEvents(clientCtx, events, page, limit, "")
if rest.CheckInternalServerError(w, err) {
return
}
@ -131,7 +131,7 @@ func QueryTxRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}
output, err := authclient.QueryTx(clientCtx, hashHexStr)
output, err := authtx.QueryTx(clientCtx, hashHexStr)
if err != nil {
if strings.Contains(err.Error(), hashHexStr) {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())

View File

@ -1,4 +1,4 @@
package client
package tx
import (
"context"

View File

@ -2,7 +2,6 @@ package tx
import (
"context"
"encoding/hex"
"fmt"
"strings"
@ -11,11 +10,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
abci "github.com/tendermint/tendermint/abci/types"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
pagination "github.com/cosmos/cosmos-sdk/types/query"
@ -62,57 +57,36 @@ func (s txServer) GetTxsEvent(ctx context.Context, req *txtypes.GetTxsEventReque
return nil, status.Error(codes.InvalidArgument, "must declare at least one event to search")
}
tmEvents := make([]string, len(req.Events))
for i, event := range req.Events {
if !strings.Contains(event, "=") {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("invalid event; event %s should be of the format: %s", event, eventFormat))
} else if strings.Count(event, "=") > 1 {
for _, event := range req.Events {
if !strings.Contains(event, "=") || strings.Count(event, "=") > 1 {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("invalid event; event %s should be of the format: %s", event, eventFormat))
}
tokens := strings.Split(event, "=")
if tokens[0] == tmtypes.TxHeightKey {
event = fmt.Sprintf("%s=%s", tokens[0], tokens[1])
} else {
event = fmt.Sprintf("%s='%s'", tokens[0], tokens[1])
}
tmEvents[i] = event
}
query := strings.Join(tmEvents, " AND ")
result, err := s.clientCtx.Client.TxSearch(ctx, query, false, &page, &limit, "")
result, err := QueryTxsByEvents(s.clientCtx, req.Events, page, limit, "")
if err != nil {
return nil, err
}
// Create a proto codec, we need it to unmarshal the tx bytes.
cdc := codec.NewProtoCodec(s.clientCtx.InterfaceRegistry)
txRespList := make([]*sdk.TxResponse, len(result.Txs))
txsList := make([]*txtypes.Tx, len(result.Txs))
for i, tx := range result.Txs {
txResp := txResultToTxResponse(&tx.TxResult)
txResp.Height = tx.Height
txResp.TxHash = tx.Hash.String()
txRespList[i] = txResp
var protoTx txtypes.Tx
if err := cdc.UnmarshalBinaryBare(tx.Tx, &protoTx); err != nil {
return nil, err
protoTx, ok := tx.Tx.GetCachedValue().(*txtypes.Tx)
if !ok {
return nil, status.Errorf(codes.Internal, "expected %T, got %T", txtypes.Tx{}, tx.Tx.GetCachedValue())
}
txsList[i] = &protoTx
txsList[i] = protoTx
}
return &txtypes.GetTxsEventResponse{
Txs: txsList,
TxResponses: txRespList,
TxResponses: result.Txs,
Pagination: &pagination.PageResponse{
Total: uint64(result.TotalCount),
Total: result.TotalCount,
},
}, nil
}
// Simulate implements the ServiceServer.Simulate RPC method.
@ -147,34 +121,21 @@ func (s txServer) GetTx(ctx context.Context, req *txtypes.GetTxRequest) (*txtype
return nil, status.Error(codes.InvalidArgument, "request cannot be nil")
}
// We get hash as a hex string in the request, convert it to bytes.
hash, err := hex.DecodeString(req.Hash)
if err != nil {
return nil, err
}
// TODO We should also check the proof flag in gRPC header.
// https://github.com/cosmos/cosmos-sdk/issues/7036.
result, err := s.clientCtx.Client.Tx(ctx, hash, false)
result, err := QueryTx(s.clientCtx, req.Hash)
if err != nil {
return nil, err
}
// Create a proto codec, we need it to unmarshal the tx bytes.
cdc := codec.NewProtoCodec(s.clientCtx.InterfaceRegistry)
var protoTx txtypes.Tx
if err := cdc.UnmarshalBinaryBare(result.Tx, &protoTx); err != nil {
return nil, err
protoTx, ok := result.Tx.GetCachedValue().(*txtypes.Tx)
if !ok {
return nil, status.Errorf(codes.Internal, "expected %T, got %T", txtypes.Tx{}, result.Tx.GetCachedValue())
}
txResp := txResultToTxResponse(&result.TxResult)
txResp.Height = result.Height
txResp.TxHash = result.Hash.String()
return &txtypes.GetTxResponse{
Tx: &protoTx,
TxResponse: txResp,
Tx: protoTx,
TxResponse: result,
}, nil
}
@ -200,15 +161,3 @@ func RegisterTxService(
func RegisterGRPCGatewayRoutes(clientConn gogogrpc.ClientConn, mux *runtime.ServeMux) {
txtypes.RegisterServiceHandlerClient(context.Background(), mux, txtypes.NewServiceClient(clientConn))
}
func txResultToTxResponse(respTx *abci.ResponseDeliverTx) *sdk.TxResponse {
logs, _ := sdk.ParseABCILogs(respTx.Log)
return &sdk.TxResponse{
Code: respTx.Code,
Codespace: respTx.Codespace,
GasUsed: respTx.GasUsed,
GasWanted: respTx.GasWanted,
Info: respTx.Info,
Logs: logs,
}
}

View File

@ -15,7 +15,7 @@ import (
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
query "github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
@ -176,14 +176,14 @@ func (s IntegrationTestSuite) TestGetTxEvents_GRPC() {
{
"without pagination",
&tx.GetTxsEventRequest{
Events: []string{"message.action=/cosmos.bank.v1beta1.Msg/Send"},
Events: []string{"message.action='/cosmos.bank.v1beta1.Msg/Send'"},
},
false, "",
},
{
"with pagination",
&tx.GetTxsEventRequest{
Events: []string{"message.action=/cosmos.bank.v1beta1.Msg/Send"},
Events: []string{"message.action='/cosmos.bank.v1beta1.Msg/Send'"},
Pagination: &query.PageRequest{
CountTotal: false,
Offset: 0,
@ -195,7 +195,7 @@ func (s IntegrationTestSuite) TestGetTxEvents_GRPC() {
{
"with multi events",
&tx.GetTxsEventRequest{
Events: []string{"message.action=/cosmos.bank.v1beta1.Msg/Send", "message.module=bank"},
Events: []string{"message.action='/cosmos.bank.v1beta1.Msg/Send'", "message.module='bank'"},
},
false, "",
},
@ -211,6 +211,12 @@ func (s IntegrationTestSuite) TestGetTxEvents_GRPC() {
s.Require().NoError(err)
s.Require().GreaterOrEqual(len(grpcRes.Txs), 1)
s.Require().Equal("foobar", grpcRes.Txs[0].Body.Memo)
// Make sure fields are populated.
// ref: https://github.com/cosmos/cosmos-sdk/issues/8680
// ref: https://github.com/cosmos/cosmos-sdk/issues/8681
s.Require().NotEmpty(grpcRes.TxResponses[0].Timestamp)
s.Require().NotEmpty(grpcRes.TxResponses[0].RawLog)
}
})
}
@ -232,25 +238,25 @@ func (s IntegrationTestSuite) TestGetTxEvents_GRPCGateway() {
},
{
"without pagination",
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?events=%s", val.APIAddress, "message.action=/cosmos.bank.v1beta1.Msg/Send"),
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?events=%s", val.APIAddress, "message.action='/cosmos.bank.v1beta1.Msg/Send'"),
false,
"",
},
{
"with pagination",
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?events=%s&pagination.offset=%d&pagination.limit=%d", val.APIAddress, "message.action=/cosmos.bank.v1beta1.Msg/Send", 0, 10),
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?events=%s&pagination.offset=%d&pagination.limit=%d", val.APIAddress, "message.action='/cosmos.bank.v1beta1.Msg/Send'", 0, 10),
false,
"",
},
{
"expect pass with multiple-events",
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?events=%s&events=%s", val.APIAddress, "message.action=/cosmos.bank.v1beta1.Msg/Send", "message.module=bank"),
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?events=%s&events=%s", val.APIAddress, "message.action='/cosmos.bank.v1beta1.Msg/Send'", "message.module='bank'"),
false,
"",
},
{
"expect pass with escape event",
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?events=%s", val.APIAddress, "message.action%3D%2Fcosmos.bank.v1beta1.Msg%2FSend"),
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?events=%s", val.APIAddress, "message.action%3D'%2Fcosmos.bank.v1beta1.Msg%2FSend'"),
false,
"",
},
@ -336,6 +342,12 @@ func (s IntegrationTestSuite) TestGetTx_GRPCGateway() {
s.Require().NoError(err)
s.Require().Equal("foobar", result.Tx.Body.Memo)
s.Require().NotZero(result.TxResponse.Height)
// Make sure fields are populated.
// ref: https://github.com/cosmos/cosmos-sdk/issues/8680
// ref: https://github.com/cosmos/cosmos-sdk/issues/8681
s.Require().NotEmpty(result.TxResponse.Timestamp)
s.Require().NotEmpty(result.TxResponse.RawLog)
}
})
}

View File

@ -5,7 +5,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/cosmos/cosmos-sdk/x/gov/types"
)
@ -374,7 +374,7 @@ func combineEvents(clientCtx client.Context, page int, eventGroups ...[]string)
// Only the Txs field will be populated in the final SearchTxsResult.
allTxs := []*sdk.TxResponse{}
for _, events := range eventGroups {
res, err := authclient.QueryTxsByEvents(clientCtx, events, page, defaultLimit, "")
res, err := authtx.QueryTxsByEvents(clientCtx, events, page, defaultLimit, "")
if err != nil {
return nil, err
}

View File

@ -9,7 +9,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
@ -33,7 +33,7 @@ func queryTxs(clientCtx client.Context, action string, delegatorAddr string) (*s
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, delegatorAddr),
}
return authclient.QueryTxsByEvents(clientCtx, events, page, limit, "")
return authtx.QueryTxsByEvents(clientCtx, events, page, limit, "")
}
func queryBonds(clientCtx client.Context, endpoint string) http.HandlerFunc {