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

feat: implement pagination for x/wasm query #85

Merged
merged 13 commits into from
Mar 15, 2021
Merged

feat: implement pagination for x/wasm query #85

merged 13 commits into from
Mar 15, 2021

Conversation

shiki-tak
Copy link
Contributor

@shiki-tak shiki-tak commented Mar 10, 2021

Closes: https://github.com/line/link-modules/issues/85
Ref: https://github.com/line/link-modules/pull/87

Description

I changed the structure of contract_history store and added pagination query.
In cosmos-sdk v0.40.0 and later, the sdk defines a flag and protobuf for pagination, but this is not implemented in the current version, so I added my own flag by referring to the following PR.

Format

# not use flag
❯ linkcli query wasm contract-history $CONTRACT
{
  "Entries": [
    {
      "operation": "Init",
      "code_id": "1",
      "msg": {}
    },
    {
      "operation": "Migrate",
      "code_id": "2",
      "msg": {
        "payout": "link1vrgaj0qzjpjfsmanjzljdkwclas8zatcmnrqfg"
      }
    }
  ],
  "Pagination": {
    "NextKey": null,
    "Total": "2"
  }
}

# use flag
❯ linkcli query wasm contract-history $CONTRACT --page 2 --limit 1 --count-total=true
{
  "Entries": [
    {
      "operation": "Migrate",
      "code_id": "2",
      "msg": {
        "payout": "link1vrgaj0qzjpjfsmanjzljdkwclas8zatcmnrqfg"
      }
    }
  ],
  "Pagination": {
    "NextKey": null,
    "Total": "2"
  }
}

Motivation and context

How has this been tested?

Checklist:

  • I followed the contributing guidelines.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.

@shiki-tak shiki-tak self-assigned this Mar 10, 2021
@LINK-Network-Dev
Copy link

tps:389

if err != nil {
return pageKey, offset, limit, page, countTotal, err
}
if offset <= 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

other than here, used else if

Copy link
Contributor

Choose a reason for hiding this comment

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

if err != nil {
return err
}

route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, keeper.QueryContractHistory, addr.String())
Copy link
Contributor

Choose a reason for hiding this comment

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

I think address information is now included in the data field so that we can diet route path.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes! it removed.

Comment on lines 53 to 62
data := &types.QueryCodesRequest{
Pagination: pageReq,
}
bs, err := cliCtx.Codec.MarshalJSON(data)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

res, height, err := cliCtx.QueryWithData(route, bs)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we reduce the duplicate codes?
cli/query.go#L60-L68

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is summarized in utils.

return
}

data := &types.QueryContractsByCodeRequest{
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ditto

return
}

data := &types.QueryContractHistoryRequest{
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ditto

}, nil
}

func ParseHTTPArgs(r *http.Request) (pageKey string, offset, limit, page uint64, countTotal bool, err error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The pagination utility for HTTP args does already exist. Can we use it for API consistency?

https://github.com/line/lbm-sdk/blob/develop/types/rest/rest.go#L382

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The limit and page could be parseed from the already existing ParseHTTPArgs.

@LINK-Network-Dev
Copy link

tps:309

Copy link
Contributor

@whylee259 whylee259 left a comment

Choose a reason for hiding this comment

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

I have left a minor comment.


// NOTE: This function is implemented in cosmos-sdk v0.40.0.
// If you want to update cosmos-sdk to 0.40.0 or later, you can use the sdk function.
func FilteredPaginate(
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this function is part of the query feature, so seems to be better to move it to keeper/querier.go

@LINK-Network-Dev
Copy link

tps:304

page, err = strconv.ParseUint(pageStr, 10, 64)
if err != nil {
return pageKey, offset, limit, page, countTotal, err
} else if page <= 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor comment. As the previous doc says, the following suggestion is better.

Suggested change
} else if page <= 0 {
}
if page <= 0 {

Copy link
Contributor

Choose a reason for hiding this comment

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

Question: does page index start from 1, not 0?

limit, err = strconv.ParseUint(limitStr, 10, 64)
if err != nil {
return pageKey, offset, limit, page, countTotal, err
} else if limit <= 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
} else if limit <= 0 {
}
if limit <= 0 {

ditto

@LINK-Network-Dev
Copy link

tps:364

@LINK-Network-Dev
Copy link

tps:340

@LINK-Network-Dev
Copy link

tps:358

@shiki-tak shiki-tak merged commit d06d4ee into Finschia:develop Mar 15, 2021
brew0722 pushed a commit that referenced this pull request Apr 27, 2021
The original is the PR #85.
cherry-pick from v1/develop by Jiyong Ha<[email protected]>
brew0722 added a commit that referenced this pull request Apr 29, 2021
* feat(wasm): add pagination for rest api

The original is the PR #85.
cherry-pick from v1/develop by Jiyong Ha<[email protected]>

* feat(wasm): make hardcoded params to gov params

The original is the PR #86.
with excessive gas consumption issue needs further modification.

cherry-pick from v1/develop by Jiyong Ha<[email protected]>

* test(wasm): adjust gas by gov params

Limit values are greatly eased due to the known issue of gas consumption.

* chore: go fmt

* chore: fix typo

Co-authored-by: shiki <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants