Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use x/auth/client for querying Txs #8732

Merged
merged 9 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/grpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,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
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.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(ctx, 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(ctx, 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 @@ -14,7 +14,7 @@ import (
"github.com/cosmos/cosmos-sdk/testutil/testdata"
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 @@ -175,14 +175,14 @@ func (s IntegrationTestSuite) TestGetTxEvents_GRPC() {
{
"without pagination",
&tx.GetTxsEventRequest{
Events: []string{"message.action=send"},
Events: []string{"message.action='send'"},
},
false, "",
},
{
"with pagination",
&tx.GetTxsEventRequest{
Events: []string{"message.action=send"},
Events: []string{"message.action='send'"},
Pagination: &query.PageRequest{
CountTotal: false,
Offset: 0,
Expand All @@ -194,7 +194,7 @@ func (s IntegrationTestSuite) TestGetTxEvents_GRPC() {
{
"with multi events",
&tx.GetTxsEventRequest{
Events: []string{"message.action=send", "message.module=bank"},
Events: []string{"message.action='send'", "message.module='bank'"},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is ' required?

Copy link
Contributor Author

@amaury1093 amaury1093 Mar 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If i'm not mistaken, it's required by tendermint.

Somehow, in the gRPC handler, we added a logic saying that "if there's no ', we add a '". I think people are used to ', so I'm proposing to do exactly what tendermint expects, and remove the "add ' automatically" logic.

},
false, "",
},
Expand All @@ -210,6 +210,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 @@ -231,25 +237,25 @@ func (s IntegrationTestSuite) TestGetTxEvents_GRPCGateway() {
},
{
"without pagination",
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?events=%s", val.APIAddress, "message.action=send"),
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?events=%s", val.APIAddress, "message.action='send'"),
false,
"",
},
{
"with pagination",
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?events=%s&pagination.offset=%d&pagination.limit=%d", val.APIAddress, "message.action=send", 0, 10),
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?events=%s&pagination.offset=%d&pagination.limit=%d", val.APIAddress, "message.action='send'", 0, 10),
false,
"",
},
{
"expect pass with multiple-events",
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?events=%s&events=%s", val.APIAddress, "message.action=send", "message.module=bank"),
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?events=%s&events=%s", val.APIAddress, "message.action='send'", "message.module='bank'"),
false,
"",
},
{
"expect pass with escape event",
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?events=%s", val.APIAddress, "message.action%3Dsend"),
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?events=%s", val.APIAddress, "message.action%3D'send'"),
false,
"",
},
Expand Down Expand Up @@ -335,6 +341,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
Loading