Skip to content

Commit

Permalink
Fix QueryByEvents with multiple events and empty pagination request (#…
Browse files Browse the repository at this point in the history
…8029)

* fix pagination issue

* Fix querying with multiple events

* reuse ParsePagination

* update tests

* change event type to array

* fix test

* review changes

* add test case for escape params

* review changes

* resolve conflicts

* fix test

* fix test

Co-authored-by: anilCSE <anil@vitwit.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Dec 1, 2020
1 parent 513daba commit a51eac4
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 87 deletions.
4 changes: 2 additions & 2 deletions proto/cosmos/tx/v1beta1/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ service Service {
// GetTxsEventRequest is the request type for the Service.TxsByEvents
// RPC method.
message GetTxsEventRequest {
// event is the transaction event type.
string event = 1;
// events is the list of transaction event type.
repeated string events = 1;
// pagination defines an pagination for the request.
cosmos.base.query.v1beta1.PageRequest pagination = 2;
}
Expand Down
4 changes: 2 additions & 2 deletions server/grpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ func (s *IntegrationTestSuite) TestGRPCServer() {
_, err = txServiceClient.GetTxsEvent(
context.Background(),
&tx.GetTxsEventRequest{
Event: "message.action=send",
Events: []string{"message.action=send"},
},
)
// TODO Once https://github.com/cosmos/cosmos-sdk/pull/8029 is merged, this
// should not error anymore.
s.Require().Error(err)
s.Require().NoError(err)
}

// Test and enforce that we upfront reject any connections to baseapp containing
Expand Down
104 changes: 54 additions & 50 deletions types/tx/service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 7 additions & 22 deletions x/auth/tx/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,17 @@ const (

// TxsByEvents implements the ServiceServer.TxsByEvents RPC method.
func (s txServer) GetTxsEvent(ctx context.Context, req *txtypes.GetTxsEventRequest) (*txtypes.GetTxsEventResponse, error) {
offset := int(req.Pagination.Offset)
limit := int(req.Pagination.Limit)
if offset < 0 {
return nil, status.Error(codes.InvalidArgument, "offset must greater than 0")
}
if len(req.Event) == 0 {
return nil, status.Error(codes.InvalidArgument, "must declare at least one event to search")
page, limit, err := pagination.ParsePagination(req.Pagination)
if err != nil {
return nil, err
}

if limit < 0 {
return nil, status.Error(codes.InvalidArgument, "limit must greater than 0")
} else if limit == 0 {
limit = pagination.DefaultLimit
if len(req.Events) == 0 {
return nil, status.Error(codes.InvalidArgument, "must declare at least one event to search")
}

page := offset/limit + 1

var events []string

if strings.Contains(req.Event, "&") {
events = strings.Split(req.Event, "&")
} else {
events = append(events, req.Event)
}
tmEvents := make([]string, len(events))
for i, event := range events {
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 {
Expand Down
73 changes: 62 additions & 11 deletions x/auth/tx/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,18 @@ func (s IntegrationTestSuite) TestGetTxEvents() {

s.Require().NoError(s.network.WaitForNextBlock())

// Query the tx via gRPC empty params.
_, err = s.queryClient.GetTxsEvent(
context.Background(),
&tx.GetTxsEventRequest{},
)
s.Require().Error(err)

// Query the tx via gRPC.
grpcRes, err := s.queryClient.GetTxsEvent(
context.Background(),
&tx.GetTxsEventRequest{
Event: "message.action=send",
Events: []string{"message.action=send"},
Pagination: &query.PageRequest{
CountTotal: false,
Offset: 0,
Expand All @@ -145,21 +152,65 @@ func (s IntegrationTestSuite) TestGetTxEvents() {
grpcRes, err = s.queryClient.GetTxsEvent(
context.Background(),
&tx.GetTxsEventRequest{
Event: "message.action=send",
Events: []string{"message.action=send"},
},
)
// TODO Once https://github.com/cosmos/cosmos-sdk/pull/8029 is merged, this
// should not error anymore.
s.Require().Error(err)

// Query the tx via grpc-gateway.
restRes, err := rest.GetRequest(fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?event=%s&pagination.offset=%d&pagination.limit=%d", val.APIAddress, "message.action=send", 0, 1))
s.Require().NoError(err)
var getTxRes tx.GetTxsEventResponse
s.Require().NoError(val.ClientCtx.JSONMarshaler.UnmarshalJSON(restRes, &getTxRes))
s.Require().Equal(len(getTxRes.Txs), 1)
s.Require().Equal("foobar", getTxRes.Txs[0].Body.Memo)
s.Require().NotZero(getTxRes.TxResponses[0].Height)

rpcTests := []struct {
name string
url string
expectErr bool
expErrMsg string
}{
{
"empty params",
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs", val.APIAddress),
true,
"must declare at least one event to search",
},
{
"without pagination",
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),
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"),
false,
"",
},
{
"expect pass with escape event",
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?events=%s", val.APIAddress, "message.action%3Dsend"),
false,
"",
},
}
for _, tc := range rpcTests {
s.Run(tc.name, func() {
res, err := rest.GetRequest(tc.url)
s.Require().NoError(err)
if tc.expectErr {
s.Require().Contains(string(res), tc.expErrMsg)
} else {
var result tx.GetTxsEventResponse
val.ClientCtx.JSONMarshaler.UnmarshalJSON(res, &result)
s.Require().GreaterOrEqual(len(result.Txs), 1)
s.Require().Equal("foobar", result.Txs[0].Body.Memo)
s.Require().NotZero(result.TxResponses[0].Height)
}
})
}
}

func (s IntegrationTestSuite) TestGetTx() {
Expand Down

0 comments on commit a51eac4

Please sign in to comment.