Skip to content

Commit

Permalink
cosmos#2730 add tx search pagination related CLI/REST API parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
ackratos committed Jan 6, 2019
1 parent 23819b1 commit 06027f0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ FEATURES

* Gaia CLI (`gaiacli`)
* \#2399 Implement `params` command to query slashing parameters.
* [\#2730](https://github.com/cosmos/cosmos-sdk/issues/2730) Add tx search pagination parameter
* [\#3027](https://github.com/cosmos/cosmos-sdk/issues/3027) Implement
`query gov proposer [proposal-id]` to query for a proposal's proposer.

Expand Down
38 changes: 31 additions & 7 deletions client/tx/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -22,6 +23,8 @@ import (
const (
flagTags = "tags"
flagAny = "any"
flagPage = "page"
flagPerPage = "perPage"
)

// default client command to search through tagged transactions
Expand All @@ -32,7 +35,7 @@ func SearchTxCmd(cdc *codec.Codec) *cobra.Command {
Long: strings.TrimSpace(`
Search for transactions that match exactly the given tags. For example:
$ gaiacli query txs --tags '<tag1>:<value1>&<tag2>:<value2>'
$ gaiacli query txs --tags '<tag1>:<value1>&<tag2>:<value2>' --page 0 --perPage 30
`),
RunE: func(cmd *cobra.Command, args []string) error {
tagsStr := viper.GetString(flagTags)
Expand All @@ -56,9 +59,11 @@ $ gaiacli query txs --tags '<tag1>:<value1>&<tag2>:<value2>'
tag = fmt.Sprintf("%s='%s'", keyValue[0], keyValue[1])
tmTags = append(tmTags, tag)
}
page := viper.GetInt(flagPage)
perPage := viper.GetInt(flagPerPage)

cliCtx := context.NewCLIContext().WithCodec(cdc)
txs, err := SearchTxs(cliCtx, cdc, tmTags)
txs, err := SearchTxs(cliCtx, cdc, tmTags, page, perPage)
if err != nil {
return err
}
Expand Down Expand Up @@ -92,7 +97,7 @@ $ gaiacli query txs --tags '<tag1>:<value1>&<tag2>:<value2>'
// SearchTxs performs a search for transactions for a given set of tags via
// Tendermint RPC. It returns a slice of Info object containing txs and metadata.
// An error is returned if the query fails.
func SearchTxs(cliCtx context.CLIContext, cdc *codec.Codec, tags []string) ([]Info, error) {
func SearchTxs(cliCtx context.CLIContext, cdc *codec.Codec, tags []string, page, perPage int) ([]Info, error) {
if len(tags) == 0 {
return nil, errors.New("must declare at least one tag to search")
}
Expand All @@ -108,9 +113,6 @@ func SearchTxs(cliCtx context.CLIContext, cdc *codec.Codec, tags []string) ([]In

prove := !cliCtx.TrustNode

// TODO: take these as args
page := 0
perPage := 100
res, err := node.TxSearch(query, prove, page, perPage)
if err != nil {
return nil, err
Expand Down Expand Up @@ -175,7 +177,29 @@ func SearchTxRequestHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.
tags = append(tags, tag)
}

txs, err = SearchTxs(cliCtx, cdc, tags)
pageStr := r.FormValue("page")
if pageStr == "" {
pageStr = "0"
}
page, err := strconv.Atoi(pageStr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("page parameter is not a valid integer"))
return
}

perPageStr := r.FormValue("perPage")
if perPageStr == "" {
perPageStr = "30" // should be consistent with tendermint/tendermint/rpc/core/pipe.go:19
}
perPage, err := strconv.Atoi(perPageStr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("perPage parameter is not a valid integer"))
return
}

txs, err = SearchTxs(cliCtx, cdc, tags, page, perPage)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down

0 comments on commit 06027f0

Please sign in to comment.