From a93edeef4cd96cfb465c970cfb1c9bae8137ca2b Mon Sep 17 00:00:00 2001 From: Amaury <1293565+amaurym@users.noreply.github.com> Date: Mon, 1 Mar 2021 16:57:28 +0100 Subject: [PATCH] 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 --- server/grpc/server_test.go | 2 +- server/rosetta/client_online.go | 3 +- x/auth/client/cli/query.go | 6 +-- x/auth/client/rest/query.go | 6 +-- x/auth/{client => tx}/query.go | 2 +- x/auth/tx/service.go | 83 +++++++-------------------------- x/auth/tx/service_test.go | 28 +++++++---- x/gov/client/utils/query.go | 4 +- x/staking/client/rest/utils.go | 4 +- 9 files changed, 49 insertions(+), 89 deletions(-) rename x/auth/{client => tx}/query.go (99%) diff --git a/server/grpc/server_test.go b/server/grpc/server_test.go index 6766d9c97ce5..6c9752741a75 100644 --- a/server/grpc/server_test.go +++ b/server/grpc/server_test.go @@ -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) diff --git a/server/rosetta/client_online.go b/server/rosetta/client_online.go index 110430bb1806..f5e5cfafeebf 100644 --- a/server/rosetta/client_online.go +++ b/server/rosetta/client_online.go @@ -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()) } diff --git a/x/auth/client/cli/query.go b/x/auth/client/cli/query.go index 8ee3a05758d6..9604959cbdb5 100644 --- a/x/auth/client/cli/query.go +++ b/x/auth/client/cli/query.go @@ -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 } diff --git a/x/auth/client/rest/query.go b/x/auth/client/rest/query.go index d11d4b3416cc..cf292fd7f7e5 100644 --- a/x/auth/client/rest/query.go +++ b/x/auth/client/rest/query.go @@ -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()) diff --git a/x/auth/client/query.go b/x/auth/tx/query.go similarity index 99% rename from x/auth/client/query.go rename to x/auth/tx/query.go index bc1b7f6f4512..756f0db07828 100644 --- a/x/auth/client/query.go +++ b/x/auth/tx/query.go @@ -1,4 +1,4 @@ -package client +package tx import ( "context" diff --git a/x/auth/tx/service.go b/x/auth/tx/service.go index 853d401beccc..d33bcac91329 100644 --- a/x/auth/tx/service.go +++ b/x/auth/tx/service.go @@ -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, - } -} diff --git a/x/auth/tx/service_test.go b/x/auth/tx/service_test.go index 58b21c139772..b460867b4b25 100644 --- a/x/auth/tx/service_test.go +++ b/x/auth/tx/service_test.go @@ -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) } }) } diff --git a/x/gov/client/utils/query.go b/x/gov/client/utils/query.go index de5f5f7c5e47..9b103e3468c9 100644 --- a/x/gov/client/utils/query.go +++ b/x/gov/client/utils/query.go @@ -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 } diff --git a/x/staking/client/rest/utils.go b/x/staking/client/rest/utils.go index 2b8a1c4dcfc1..f8c6fe113255 100644 --- a/x/staking/client/rest/utils.go +++ b/x/staking/client/rest/utils.go @@ -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 {