Skip to content

Commit

Permalink
#262 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 Nov 26, 2018
1 parent 15dc727 commit a47db78
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 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 @@ -20,8 +21,10 @@ import (
)

const (
flagTags = "tag"
flagAny = "any"
flagTags = "tag"
flagAny = "any"
flagPage = "page"
flagPerPage = "perPage"
)

// default client command to search through tagged transactions
Expand All @@ -35,19 +38,21 @@ passed to the --tags option. To match any transaction, use the --any option.
For example:
$ gaiacli tendermint txs --tag test1,test2
$ gaiacli tendermint txs --tag test1,test2 --page 0 --perPage 30
will match any transaction tagged with both test1,test2. To match a transaction tagged with either
test1 or test2, use:
$ gaiacli tendermint txs --tag test1,test2 --any
$ gaiacli tendermint txs --tag test1,test2 --any --page 0 --perPage 30
`),
RunE: func(cmd *cobra.Command, args []string) error {
tags := viper.GetStringSlice(flagTags)
page := viper.GetInt(flagPage)
perPage := viper.GetInt(flagPerPage)

cliCtx := context.NewCLIContext().WithCodec(cdc)

txs, err := searchTxs(cliCtx, cdc, tags)
txs, err := searchTxs(cliCtx, cdc, tags, page, perPage)
if err != nil {
return err
}
Expand Down Expand Up @@ -79,7 +84,7 @@ $ gaiacli tendermint txs --tag test1,test2 --any
return cmd
}

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 @@ -95,9 +100,6 @@ func searchTxs(cliCtx context.CLIContext, cdc *codec.Codec, tags []string) ([]In

prove := !cliCtx.TrustNode

// TODO(#262): take these as args
page := 0
perPage := 10000
res, err := node.TxSearch(query, prove, page, perPage)
if err != nil {
return nil, err
Expand Down Expand Up @@ -145,6 +147,27 @@ func SearchTxRequestHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.
w.Write([]byte("You need to provide at least a tag as a key=value pair to search for. Postfix the key with _bech32 to search bech32-encoded addresses or public keys"))
return
}
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
}

keyValue := strings.Split(tag, "=")
key := keyValue[0]
Expand All @@ -167,7 +190,7 @@ func SearchTxRequestHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.
tag = strings.TrimRight(key, "_bech32") + "='" + sdk.AccAddress(bz).String() + "'"
}

txs, err := searchTxs(cliCtx, cdc, []string{tag})
txs, err := searchTxs(cliCtx, cdc, []string{tag}, page, perPage)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down

0 comments on commit a47db78

Please sign in to comment.