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
56 changes: 47 additions & 9 deletions x/wasm/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/line/lbm-sdk/codec"
sdk "github.com/line/lbm-sdk/types"

"github.com/line/lbm-sdk/x/wasm/client/utils"
"github.com/line/lbm-sdk/x/wasm/internal/keeper"
"github.com/line/lbm-sdk/x/wasm/internal/types"
)
Expand All @@ -44,28 +45,35 @@ func GetQueryCmd(cdc *codec.Codec) *cobra.Command {

// GetCmdListCode lists all wasm code uploaded
func GetCmdListCode(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "list-code",
Short: "List all wasm bytecode on the chain",
Long: "List all wasm bytecode on the chain",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, keeper.QueryListCode)
res, _, err := cliCtx.Query(route)
pageReq, err := utils.ReadPageRequest(withPageKeyDecoded(cmd.Flags()))
if err != nil {
return err
}

res, _, err := utils.QueryCodeList(cliCtx, keeper.QueryListCode, pageReq)
if err != nil {
return err
}
fmt.Println(string(res))
return nil
},
}

utils.AddPaginationFlagsToCmd(cmd, "list codes")
return cmd
}

// GetCmdListContractByCode lists all wasm code uploaded for given code id
func GetCmdListContractByCode(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "list-contract-by-code [code_id]",
Short: "List wasm all bytecode on the chain for given code id",
Long: "List wasm all bytecode on the chain for given code id",
Expand All @@ -78,15 +86,21 @@ func GetCmdListContractByCode(cdc *codec.Codec) *cobra.Command {
return err
}

route := fmt.Sprintf("custom/%s/%s/%d", types.QuerierRoute, keeper.QueryListContractByCode, codeID)
res, _, err := cliCtx.Query(route)
pageReq, err := utils.ReadPageRequest(withPageKeyDecoded(cmd.Flags()))
if err != nil {
return err
}

res, _, err := utils.QueryContractsByCode(cliCtx, keeper.QueryListContractByCode, codeID, pageReq)
if err != nil {
return err
}
fmt.Println(string(res))
return nil
},
}
utils.AddPaginationFlagsToCmd(cmd, "list contracts by code")
return cmd
}

// GetCmdQueryCode returns the bytecode for a given contract
Expand Down Expand Up @@ -269,7 +283,7 @@ func GetCmdGetContractStateSmart(cdc *codec.Codec) *cobra.Command {

// GetCmdGetContractHistory prints the code history for a given contract
func GetCmdGetContractHistory(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "contract-history [bech32_address]",
Short: "Prints out the code history for a contract given its address",
Long: "Prints out the code history for a contract given its address",
Expand All @@ -282,15 +296,22 @@ func GetCmdGetContractHistory(cdc *codec.Codec) *cobra.Command {
return err
}

route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, keeper.QueryContractHistory, addr.String())
res, _, err := cliCtx.Query(route)
pageReq, err := utils.ReadPageRequest(withPageKeyDecoded(cmd.Flags()))
if err != nil {
return err
}

res, _, err := utils.QueryContractHistory(cliCtx, keeper.QueryContractHistory, addr, pageReq)
if err != nil {
return err
}
fmt.Println(string(res))
return nil
},
}

utils.AddPaginationFlagsToCmd(cmd, "contract history")
return cmd
}

type argumentDecoder struct {
Expand Down Expand Up @@ -335,3 +356,20 @@ func (a *argumentDecoder) DecodeString(s string) ([]byte, error) {
func asciiDecodeString(s string) ([]byte, error) {
return []byte(s), nil
}

// sdk ReadPageRequest expects binary but we encoded to base64 in our marshaller
func withPageKeyDecoded(flagSet *flag.FlagSet) *flag.FlagSet {
encoded, err := flagSet.GetString(utils.FlagPageKey)
if err != nil {
panic(err.Error())
}
raw, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
panic(err.Error())
}
err = flagSet.Set(utils.FlagPageKey, string(raw))
if err != nil {
panic(err.Error())
}
return flagSet
}
46 changes: 40 additions & 6 deletions x/wasm/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
sdk "github.com/line/lbm-sdk/types"
"github.com/line/lbm-sdk/types/rest"

"github.com/line/lbm-sdk/x/wasm/client/utils"
"github.com/line/lbm-sdk/x/wasm/internal/keeper"
"github.com/line/lbm-sdk/x/wasm/internal/types"
)
Expand All @@ -36,8 +37,19 @@ func listCodesHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return
}

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, keeper.QueryListCode)
res, height, err := cliCtx.Query(route)
pageKey, offset, limit, page, countTotal, err := utils.ParseHTTPArgs(r)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

pageReq, err := utils.NewPageRequest(pageKey, offset, limit, page, countTotal)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

res, height, err := utils.QueryCodeList(cliCtx, keeper.QueryListCode, pageReq)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down Expand Up @@ -88,8 +100,19 @@ func listContractsByCodeHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return
}

route := fmt.Sprintf("custom/%s/%s/%d", types.QuerierRoute, keeper.QueryListContractByCode, codeID)
res, height, err := cliCtx.Query(route)
pageKey, offset, limit, page, countTotal, err := utils.ParseHTTPArgs(r)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

pageReq, err := utils.NewPageRequest(pageKey, offset, limit, page, countTotal)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

res, height, err := utils.QueryContractsByCode(cliCtx, keeper.QueryListContractByCode, codeID, pageReq)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down Expand Up @@ -237,8 +260,19 @@ func queryContractHistoryFn(cliCtx context.CLIContext) http.HandlerFunc {
return
}

route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, keeper.QueryContractHistory, addr.String())
res, height, err := cliCtx.Query(route)
pageKey, offset, limit, page, countTotal, err := utils.ParseHTTPArgs(r)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

pageReq, err := utils.NewPageRequest(pageKey, offset, limit, page, countTotal)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

res, height, err := utils.QueryContractHistory(cliCtx, keeper.QueryContractHistory, addr, pageReq)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down
160 changes: 160 additions & 0 deletions x/wasm/client/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,36 @@ package utils
import (
"bytes"
"compress/gzip"
"encoding/binary"
"fmt"
"net/http"
"strconv"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

sdk "github.com/line/lbm-sdk/types"

"github.com/line/lbm-sdk/client/context"
sdkerrors "github.com/line/lbm-sdk/types/errors"
"github.com/line/lbm-sdk/types/rest"
"github.com/line/lbm-sdk/x/wasm/internal/types"
)

var (
gzipIdent = []byte("\x1F\x8B\x08")
wasmIdent = []byte("\x00\x61\x73\x6D")
)

// List of CLI flags
const (
FlagPage = "page"
FlagLimit = "limit"
FlagPageKey = "page-key"
FlagOffset = "offset"
FlagCountTotal = "count-total"
)

// IsGzip returns checks if the file contents are gzip compressed
func IsGzip(input []byte) bool {
return bytes.Equal(input[:3], gzipIdent)
Expand All @@ -36,3 +59,140 @@ func GzipIt(input []byte) ([]byte, error) {

return b.Bytes(), nil
}

// AddPaginationFlagsToCmd adds common pagination flags to cmd
func AddPaginationFlagsToCmd(cmd *cobra.Command, query string) {
cmd.Flags().Uint64(FlagPage, 1, fmt.Sprintf("pagination page of %s to query for. This sets offset to a multiple of limit", query))
cmd.Flags().String(FlagPageKey, "", fmt.Sprintf("pagination page-key of %s to query for", query))
cmd.Flags().Uint64(FlagOffset, 0, fmt.Sprintf("pagination offset of %s to query for", query))
cmd.Flags().Uint64(FlagLimit, 100, fmt.Sprintf("pagination limit of %s to query for", query))
cmd.Flags().Bool(
FlagCountTotal, false, fmt.Sprintf("count total number of records in %s to query for", query))
}

// BigEndianToUint64 returns an uint64 from big endian encoded bytes. If encoding
// is empty, zero is returned.
// This function is included in cosmos-sdk v0.40.0
// Once cosmos-sdk is updated, use the sdk functions.
func BigEndianToUint64(bz []byte) uint64 {
if len(bz) == 0 {
return 0
}

return binary.BigEndian.Uint64(bz)
}

// ReadPageRequest reads and builds the necessary page request flags for pagination.
func ReadPageRequest(flagSet *pflag.FlagSet) (*types.PageRequest, error) {
pageKey, err := flagSet.GetString(FlagPageKey)
if err != nil {
return nil, err
}
offset, err := flagSet.GetUint64(FlagOffset)
if err != nil {
return nil, err
}
limit, err := flagSet.GetUint64(FlagLimit)
if err != nil {
return nil, err
}
countTotal, err := flagSet.GetBool(FlagCountTotal)
if err != nil {
return nil, err
}
page, err := flagSet.GetUint64(FlagPage)
if err != nil {
return nil, err
}

return NewPageRequest(pageKey, offset, limit, page, countTotal)
}

func NewPageRequest(pageKey string, offset, limit, page uint64, countTotal bool) (*types.PageRequest, error) {
if page > 1 && offset > 0 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "page and offset cannot be used together")
}

if page > 1 {
brew0722 marked this conversation as resolved.
Show resolved Hide resolved
offset = (page - 1) * limit
}

return &types.PageRequest{
Key: []byte(pageKey),
Offset: offset,
Limit: limit,
CountTotal: countTotal,
}, nil
}

func QueryCodeList(cliCtx context.CLIContext, path string, pageReq *types.PageRequest) ([]byte, int64, error) {
data := &types.QueryCodesRequest{
Pagination: pageReq,
}
bs, err := cliCtx.Codec.MarshalJSON(data)
if err != nil {
return nil, 0, err
}

return Query(cliCtx, bs, path)
}

func QueryContractsByCode(cliCtx context.CLIContext, path string, codeID uint64, pageReq *types.PageRequest) ([]byte, int64, error) {
data := &types.QueryContractsByCodeRequest{
CodeID: codeID,
Pagination: pageReq,
}
bs, err := cliCtx.Codec.MarshalJSON(data)
if err != nil {
return nil, 0, err
}

return Query(cliCtx, bs, path)
}

func QueryContractHistory(cliCtx context.CLIContext, path string, addr sdk.AccAddress, pageReq *types.PageRequest) ([]byte, int64, error) {
data := &types.QueryContractHistoryRequest{
Address: addr,
Pagination: pageReq,
}
bs, err := cliCtx.Codec.MarshalJSON(data)
if err != nil {
return nil, 0, err
}

return Query(cliCtx, bs, path)
}

func Query(cliCtx context.CLIContext, data []byte, queryPath string) ([]byte, int64, error) {
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, queryPath)
return cliCtx.QueryWithData(route, data)
}

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.

_, p, l, err := rest.ParseHTTPArgs(r)
if err != nil {
return pageKey, offset, limit, page, countTotal, err
}
page = uint64(p)
limit = uint64(l)

pageKey = r.FormValue("page-key")

offsetStr := r.FormValue("offset")
if offsetStr != "" {
offset, err = strconv.ParseUint(offsetStr, 10, 64)
if err != nil {
return pageKey, offset, limit, page, countTotal, err
}
}

countTotalStr := r.FormValue("count-total")
if countTotalStr != "" {
countTotal, err = strconv.ParseBool(countTotalStr)
if err != nil {
return pageKey, offset, limit, page, countTotal, err
}
}

return pageKey, offset, limit, page, countTotal, nil
}
Loading