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

Fix QueryByEvents with multiple events and empty pagination request #8029

Merged
merged 20 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from 8 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
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;
// event is the list of transaction event type.
repeated string event = 1;
// pagination defines an pagination for the request.
cosmos.base.query.v1beta1.PageRequest pagination = 2;
}
Expand Down
2 changes: 1 addition & 1 deletion types/query/query.pb.go

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

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.

27 changes: 6 additions & 21 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")
page, limit, err := pagination.ParsePagination(req.Pagination)
if err != nil {
return nil, err
}

if len(req.Event) == 0 {
return nil, status.Error(codes.InvalidArgument, "must declare at least one event to search")
}

if limit < 0 {
return nil, status.Error(codes.InvalidArgument, "limit must greater than 0")
} else if limit == 0 {
limit = pagination.DefaultLimit
}

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.Event))
for i, event := range req.Event {
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
74 changes: 65 additions & 9 deletions x/auth/tx/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,27 @@ 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 no pagination.
_, err = s.queryClient.GetTxsEvent(
context.Background(),
&tx.GetTxsEventRequest{
Event: []string{"message.action=send", fmt.Sprintf("transfer.sender=%s", val.Address)},
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
},
)
s.Require().NoError(err)

// Query the tx via gRPC.
grpcRes, err := s.queryClient.GetTxsEvent(
context.Background(),
&tx.GetTxsEventRequest{Event: "message.action=send",
&tx.GetTxsEventRequest{
Event: []string{"message.action=send"},
Pagination: &query.PageRequest{
CountTotal: false,
Offset: 0,
Expand All @@ -139,14 +156,53 @@ func (s IntegrationTestSuite) TestGetTxEvents() {
s.Require().Equal(len(grpcRes.Txs), 1)
s.Require().Equal("foobar", grpcRes.Txs[0].Body.Memo)

// 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(grpcRes.Txs), 1)
s.Require().Equal("foobar", getTxRes.Txs[0].Body.Memo)
s.Require().NotZero(grpcRes.TxResponses[0].Height)
rpcTests := []struct {
name string
url string
expectErr bool
expected *tx.GetTxsEventResponse
}{
{
"empty params",
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs", val.APIAddress),
true,
&tx.GetTxsEventResponse{},
},
{
"without pagination",
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?event=%s", val.APIAddress, "message.action=send"),
false,
&tx.GetTxsEventResponse{},
},
{
"with pagination",
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?event=%s&pagination.offset=%d&pagination.limit=%d", val.APIAddress, "message.action=send", 0, 10),
false,
&tx.GetTxsEventResponse{},
},
{
"expect pass with multiple-events",
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?event=%s&event=%s", val.APIAddress, "message.action=send", "message.module=bank"),
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
false,
&tx.GetTxsEventResponse{},
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
},
}
for _, tc := range rpcTests {
s.Run(tc.name, func() {
res, err := rest.GetRequest(tc.url)
s.Require().NoError(err)
if tc.expectErr {
s.Require().Error(val.ClientCtx.JSONMarshaler.UnmarshalJSON(res, tc.expected))
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
} else {
s.Require().NoError(val.ClientCtx.JSONMarshaler.UnmarshalJSON(res, tc.expected))
if len(tc.expected.Txs) > 0 {
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
s.Require().Equal(1, len(tc.expected.Txs))
s.Require().Equal("foobar", tc.expected.Txs[0].Body.Memo)
s.Require().NotZero(tc.expected.TxResponses[0].Height)
}
}
})
}
}

func (s IntegrationTestSuite) TestGetTx() {
Expand Down