Skip to content

Commit

Permalink
cosmos#2730 refactor review comment
Browse files Browse the repository at this point in the history
  • Loading branch information
ackratos committed Jan 6, 2019
1 parent f752ac2 commit 8ca0553
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 39 deletions.
2 changes: 1 addition & 1 deletion baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var _ abci.Application = (*BaseApp)(nil)
// NewBaseApp returns a reference to an initialized BaseApp.
//
// NOTE: The db is used to store the version number for now.
// Accepts a user-defined txDecoder
// Accepts a usemake install_examplesr-defined txDecoder
// Accepts variable number of option functions, which act on the BaseApp to set configuration choices
func NewBaseApp(name string, logger log.Logger, db dbm.DB, txDecoder sdk.TxDecoder, options ...func(*BaseApp)) *BaseApp {
app := &BaseApp{
Expand Down
40 changes: 19 additions & 21 deletions client/tx/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
)

const (
flagTags = "tags"
flagAny = "any"
flagPage = "page"
flagPerPage = "perPage"
flagTags = "tags"
flagAny = "any"
flagPage = "page"
flagLimit = "limit"
)

// default client command to search through tagged transactions
Expand All @@ -36,7 +36,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>' --page 1 --perPage 30
$ gaiacli query txs --tags '<tag1>:<value1>&<tag2>:<value2>' --page 1 --limit 30
`),
RunE: func(cmd *cobra.Command, args []string) error {
tagsStr := viper.GetString(flagTags)
Expand Down Expand Up @@ -65,10 +65,10 @@ $ gaiacli query txs --tags '<tag1>:<value1>&<tag2>:<value2>' --page 1 --perPage
tmTags = append(tmTags, tag)
}
page := viper.GetInt(flagPage)
perPage := viper.GetInt(flagPerPage)
limit := viper.GetInt(flagLimit)

cliCtx := context.NewCLIContext().WithCodec(cdc)
txs, err := SearchTxs(cliCtx, cdc, tmTags, page, perPage)
txs, err := SearchTxs(cliCtx, cdc, tmTags, page, limit)
if err != nil {
return err
}
Expand Down Expand Up @@ -96,15 +96,15 @@ $ gaiacli query txs --tags '<tag1>:<value1>&<tag2>:<value2>' --page 1 --perPage
cmd.Flags().Bool(client.FlagTrustNode, false, "Trust connected full node (don't verify proofs for responses)")
viper.BindPFlag(client.FlagTrustNode, cmd.Flags().Lookup(client.FlagTrustNode))
cmd.Flags().String(flagTags, "", "tag:value list of tags that must match")
cmd.Flags().String(flagPage, "", "page of pagination parameter")
cmd.Flags().String(flagPerPage, "", "num of transactions per page returned")
cmd.Flags().Int32(flagPage, 1, "page of pagination parameter")
cmd.Flags().Int32(flagLimit, 30, "num of transactions per page returned")
return cmd
}

// 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, page, perPage int) ([]Info, error) {
func SearchTxs(cliCtx context.CLIContext, cdc *codec.Codec, tags []string, page, limit int) ([]Info, error) {
if len(tags) == 0 {
return nil, errors.New("must declare at least one tag to search")
}
Expand All @@ -120,7 +120,7 @@ func SearchTxs(cliCtx context.CLIContext, cdc *codec.Codec, tags []string, page,

prove := !cliCtx.TrustNode

res, err := node.TxSearch(query, prove, page, perPage)
res, err := node.TxSearch(query, prove, page, limit)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -174,7 +174,7 @@ func SearchTxRequestHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.
}

for key, values := range r.Form {
if key == "page" || key == "perPage" {
if key == "page" || key == "limit" {
continue
}
value, err := url.QueryUnescape(values[0])
Expand All @@ -198,23 +198,21 @@ func SearchTxRequestHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.
}
page, err := strconv.Atoi(pageStr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("page parameter is not a valid integer"))
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

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

txs, err = SearchTxs(cliCtx, cdc, tags, page, perPage)
txs, err = SearchTxs(cliCtx, cdc, tags, page, limit)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down
76 changes: 65 additions & 11 deletions cmd/gaia/cli_test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/stretchr/testify/require"

abci "github.com/tendermint/tendermint/abci/types"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/crypto"
cmn "github.com/tendermint/tendermint/libs/common"

Expand All @@ -36,7 +37,7 @@ import (

func TestGaiaCLIMinimumFees(t *testing.T) {
t.Parallel()
chainID, servAddr, port, gaiadHome, gaiacliHome, p2pAddr := initializeFixtures(t)
chainID, servAddr, port, gaiadHome, gaiacliHome, p2pAddr := initializeFixtures(t, nil)
flags := fmt.Sprintf("--home=%s --node=%v --chain-id=%v", gaiacliHome, servAddr, chainID)

// start gaiad server with minimum fees
Expand All @@ -60,7 +61,7 @@ func TestGaiaCLIMinimumFees(t *testing.T) {

func TestGaiaCLIFeesDeduction(t *testing.T) {
t.Parallel()
chainID, servAddr, port, gaiadHome, gaiacliHome, p2pAddr := initializeFixtures(t)
chainID, servAddr, port, gaiadHome, gaiacliHome, p2pAddr := initializeFixtures(t, nil)
flags := fmt.Sprintf("--home=%s --node=%v --chain-id=%v", gaiacliHome, servAddr, chainID)

// start gaiad server with minimum fees
Expand Down Expand Up @@ -103,7 +104,7 @@ func TestGaiaCLIFeesDeduction(t *testing.T) {

func TestGaiaCLISend(t *testing.T) {
t.Parallel()
chainID, servAddr, port, gaiadHome, gaiacliHome, p2pAddr := initializeFixtures(t)
chainID, servAddr, port, gaiadHome, gaiacliHome, p2pAddr := initializeFixtures(t, nil)
flags := fmt.Sprintf("--home=%s --node=%v --chain-id=%v", gaiacliHome, servAddr, chainID)

// start gaiad server
Expand Down Expand Up @@ -155,7 +156,7 @@ func TestGaiaCLISend(t *testing.T) {

func TestGaiaCLIGasAuto(t *testing.T) {
t.Parallel()
chainID, servAddr, port, gaiadHome, gaiacliHome, p2pAddr := initializeFixtures(t)
chainID, servAddr, port, gaiadHome, gaiacliHome, p2pAddr := initializeFixtures(t, nil)
flags := fmt.Sprintf("--home=%s --node=%v --chain-id=%v", gaiacliHome, servAddr, chainID)

// start gaiad server
Expand Down Expand Up @@ -208,7 +209,7 @@ func TestGaiaCLIGasAuto(t *testing.T) {

func TestGaiaCLICreateValidator(t *testing.T) {
t.Parallel()
chainID, servAddr, port, gaiadHome, gaiacliHome, p2pAddr := initializeFixtures(t)
chainID, servAddr, port, gaiadHome, gaiacliHome, p2pAddr := initializeFixtures(t, nil)
flags := fmt.Sprintf("--home=%s --chain-id=%v --node=%s", gaiacliHome, chainID, servAddr)

// start gaiad server
Expand Down Expand Up @@ -306,7 +307,7 @@ func TestGaiaCLICreateValidator(t *testing.T) {

func TestGaiaCLISubmitProposal(t *testing.T) {
t.Parallel()
chainID, servAddr, port, gaiadHome, gaiacliHome, p2pAddr := initializeFixtures(t)
chainID, servAddr, port, gaiadHome, gaiacliHome, p2pAddr := initializeFixtures(t, nil)
flags := fmt.Sprintf("--home=%s --node=%v --chain-id=%v", gaiacliHome, servAddr, chainID)

// start gaiad server
Expand Down Expand Up @@ -457,7 +458,7 @@ func TestGaiaCLISubmitProposal(t *testing.T) {

func TestGaiaCLIValidateSignatures(t *testing.T) {
t.Parallel()
chainID, servAddr, port, gaiadHome, gaiacliHome, p2pAddr := initializeFixtures(t)
chainID, servAddr, port, gaiadHome, gaiacliHome, p2pAddr := initializeFixtures(t, nil)
flags := fmt.Sprintf("--home=%s --node=%v --chain-id=%v", gaiacliHome, servAddr, chainID)

// start gaiad server
Expand Down Expand Up @@ -526,7 +527,7 @@ func TestGaiaCLIValidateSignatures(t *testing.T) {

func TestGaiaCLISendGenerateSignAndBroadcast(t *testing.T) {
t.Parallel()
chainID, servAddr, port, gaiadHome, gaiacliHome, p2pAddr := initializeFixtures(t)
chainID, servAddr, port, gaiadHome, gaiacliHome, p2pAddr := initializeFixtures(t, nil)
flags := fmt.Sprintf("--home=%s --node=%v --chain-id=%v", gaiacliHome, servAddr, chainID)

// start gaiad server
Expand Down Expand Up @@ -628,7 +629,7 @@ func TestGaiaCLISendGenerateSignAndBroadcast(t *testing.T) {

func TestGaiaCLIConfig(t *testing.T) {
t.Parallel()
chainID, servAddr, port, gaiadHome, gaiacliHome, _ := initializeFixtures(t)
chainID, servAddr, port, gaiadHome, gaiacliHome, _ := initializeFixtures(t, nil)
node := fmt.Sprintf("%s:%s", servAddr, port)
executeWrite(t, fmt.Sprintf(`gaiacli --home=%s config node %s`, gaiacliHome, node))
executeWrite(t, fmt.Sprintf(`gaiacli --home=%s config output text`, gaiacliHome))
Expand Down Expand Up @@ -685,7 +686,7 @@ func TestSlashingGetParams(t *testing.T) {
t.Parallel()

cdc := app.MakeCodec()
chainID, servAddr, port, gaiadHome, gaiacliHome, p2pAddr := initializeFixtures(t)
chainID, servAddr, port, gaiadHome, gaiacliHome, p2pAddr := initializeFixtures(t, nil)
flags := fmt.Sprintf("--home=%s --node=%v --chain-id=%v", gaiacliHome, servAddr, chainID)

// start gaiad server
Expand All @@ -709,6 +710,56 @@ func TestSlashingGetParams(t *testing.T) {
require.NoError(t, err)
}

func TestGaiaCLIQueryTxPagination(t *testing.T) {
t.Parallel()
configOverride := cfg.DefaultConfig()
configOverride.TxIndex.IndexAllTags = true
configOverride.Consensus.SkipTimeoutCommit = true
chainID, servAddr, port, gaiadHome, gaiacliHome, _ := initializeFixtures(t, configOverride)
flags := fmt.Sprintf("--home=%s --node=%v --chain-id=%v", gaiacliHome, servAddr, chainID)

// start gaiad git server
proc := tests.GoExecuteTWithStdout(t, fmt.Sprintf(
"gaiad start --home=%s --rpc.laddr=%v", gaiadHome, servAddr))

defer proc.Stop(false)
tests.WaitForTMStart(port)
tests.WaitForNextNBlocksTM(2, port)

fooAddr, _ := executeGetAddrPK(t, fmt.Sprintf("gaiacli keys show foo --output=json --home=%s", gaiacliHome))
barAddr, _ := executeGetAddrPK(t, fmt.Sprintf("gaiacli keys show bar --output=json --home=%s", gaiacliHome))

for i := 1; i <= 30; i++ {
success := executeWrite(t, fmt.Sprintf(
"gaiacli tx send %v --amount=%dfootoken --to=%s --from=foo",
flags, i, barAddr), app.DefaultKeyPass)
require.True(t, success)
tests.WaitForNextNBlocksTM(1, port)
}

// perPage = 15, 2 pages
txsPage1 := executeGetTxs(t, fmt.Sprintf("gaiacli query txs --tags='sender:%s' %v --limit=15 --page=1", fooAddr, flags))
require.Len(t, txsPage1, 15)
txsPage2 := executeGetTxs(t, fmt.Sprintf("gaiacli query txs --tags='sender:%s' %v --limit=15 --page=2", fooAddr, flags))
require.Len(t, txsPage2, 15)
require.NotEqual(t, txsPage1, txsPage2)
txsPage3 := executeGetTxs(t, fmt.Sprintf("gaiacli query txs --tags='sender:%s' %v --limit=15 --page=3", fooAddr, flags))
require.Len(t, txsPage3, 15)
require.Equal(t, txsPage2, txsPage3)

// perPage = 16, 2 pages
txsPage1 = executeGetTxs(t, fmt.Sprintf("gaiacli query txs --tags='sender:%s' %v --limit=16 --page=1", fooAddr, flags))
require.Len(t, txsPage1, 16)
txsPage2 = executeGetTxs(t, fmt.Sprintf("gaiacli query txs --tags='sender:%s' %v --limit=16 --page=2", fooAddr, flags))
require.Len(t, txsPage2, 14)
require.NotEqual(t, txsPage1, txsPage2)

// perPage = 50
txsPageFull := executeGetTxs(t, fmt.Sprintf("gaiacli query txs --tags='sender:%s' %v --limit=50 --page=1", fooAddr, flags))
require.Len(t, txsPageFull, 30)
require.Equal(t, txsPageFull, append(txsPage1, txsPage2...))
}

//___________________________________________________________________________________
// helper methods

Expand All @@ -719,7 +770,7 @@ func getTestingHomeDirs(name string) (string, string) {
return gaiadHome, gaiacliHome
}

func initializeFixtures(t *testing.T) (chainID, servAddr, port, gaiadHome, gaiacliHome, p2pAddr string) {
func initializeFixtures(t *testing.T, configOverride *cfg.Config) (chainID, servAddr, port, gaiadHome, gaiacliHome, p2pAddr string) {
gaiadHome, gaiacliHome = getTestingHomeDirs(t.Name())
tests.ExecuteT(t, fmt.Sprintf("gaiad --home=%s unsafe-reset-all", gaiadHome), "")
os.RemoveAll(filepath.Join(gaiadHome, "config", "gentx"))
Expand All @@ -731,6 +782,9 @@ func initializeFixtures(t *testing.T) (chainID, servAddr, port, gaiadHome, gaiac
fooAddr, _ := executeGetAddrPK(t, fmt.Sprintf("gaiacli keys show foo --home=%s", gaiacliHome))
chainID = executeInit(t, fmt.Sprintf("gaiad init -o --moniker=foo --home=%s", gaiadHome))

if configOverride != nil {
cfg.WriteConfigFile(filepath.Join(gaiadHome, "config", "config.toml"), configOverride)
}
executeWriteCheckErr(t, fmt.Sprintf(
"gaiad add-genesis-account %s 150%s,1000footoken --home=%s", fooAddr, stakeTypes.DefaultBondDenom, gaiadHome))
executeWrite(t, fmt.Sprintf("cat %s%sconfig%sgenesis.json", gaiadHome, string(os.PathSeparator), string(os.PathSeparator)))
Expand Down
5 changes: 5 additions & 0 deletions docs/gaia/gaiacli.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ And for using multiple `tags`:
gaiacli query txs --tags='<tag1>:<value1>&<tag2>:<value2>'
```

The pagination is supported as well via `page` and `limit`:
```bash
gaiacli query txs --tags='<tag>:<value>' --page=1 --limit=20
```

::: tip Note

The action tag always equals the message type returned by the `Type()` function of the relevant message.
Expand Down
21 changes: 15 additions & 6 deletions x/gov/client/utils/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utils

import (
"fmt"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -32,7 +31,9 @@ func QueryDepositsByTxQuery(
fmt.Sprintf("%s='%s'", tags.ProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))),
}

infos, err := tx.SearchTxs(cliCtx, cdc, tags)
// NOTE: SearchTxs is used to facilitate the txs query which does not currently
// support configurable pagination.
infos, err := tx.SearchTxs(cliCtx, cdc, tags, 1, 30)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -75,7 +76,9 @@ func QueryVotesByTxQuery(
fmt.Sprintf("%s='%s'", tags.ProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))),
}

infos, err := tx.SearchTxs(cliCtx, cdc, tags)
// NOTE: SearchTxs is used to facilitate the txs query which does not currently
// support configurable pagination.
infos, err := tx.SearchTxs(cliCtx, cdc, tags, 1, 30)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -114,7 +117,9 @@ func QueryVoteByTxQuery(
fmt.Sprintf("%s='%s'", tags.Voter, []byte(params.Voter.String())),
}

infos, err := tx.SearchTxs(cliCtx, cdc, tags)
// NOTE: SearchTxs is used to facilitate the txs query which does not currently
// support configurable pagination.
infos, err := tx.SearchTxs(cliCtx, cdc, tags, 1, 30)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -155,7 +160,9 @@ func QueryDepositByTxQuery(
fmt.Sprintf("%s='%s'", tags.Depositor, []byte(params.Depositor.String())),
}

infos, err := tx.SearchTxs(cliCtx, cdc, tags)
// NOTE: SearchTxs is used to facilitate the txs query which does not currently
// support configurable pagination.
infos, err := tx.SearchTxs(cliCtx, cdc, tags, 1, 30)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -195,7 +202,9 @@ func QueryProposerByTxQuery(
fmt.Sprintf("%s='%s'", tags.ProposalID, []byte(fmt.Sprintf("%d", proposalID))),
}

infos, err := tx.SearchTxs(cliCtx, cdc, tags)
// NOTE: SearchTxs is used to facilitate the txs query which does not currently
// support configurable pagination.
infos, err := tx.SearchTxs(cliCtx, cdc, tags, 1, 30)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 8ca0553

Please sign in to comment.