Skip to content

Commit

Permalink
Move QueryTx functions to x/auth/tx (#8734)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
amaury1093 and Alessio Treglia authored Mar 1, 2021
1 parent 5f2b90c commit a93edee
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 89 deletions.
2 changes: 1 addition & 1 deletion server/grpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions server/rosetta/client_online.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
}
Expand Down
6 changes: 3 additions & 3 deletions x/auth/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions x/auth/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion x/auth/client/query.go → x/auth/tx/query.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package tx

import (
"context"
Expand Down
83 changes: 16 additions & 67 deletions x/auth/tx/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tx

import (
"context"
"encoding/hex"
"fmt"
"strings"

Expand All @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

Expand All @@ -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,
}
}
28 changes: 20 additions & 8 deletions x/auth/tx/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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, "",
},
Expand All @@ -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)
}
})
}
Expand All @@ -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,
"",
},
Expand Down Expand Up @@ -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)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions x/gov/client/utils/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions x/staking/client/rest/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand Down

0 comments on commit a93edee

Please sign in to comment.