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

add pagination to x/staking queries #6163

Merged
merged 24 commits into from
Jun 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e4cfc6e
add pagination
jgimeno May 7, 2020
8cb5a18
make test pass with validators
jgimeno May 7, 2020
f3330b2
add pagination option
jgimeno May 7, 2020
c362e7f
unbonding delegations test added
jgimeno May 8, 2020
b5cf036
test unbondedvalidators pagination
jgimeno May 8, 2020
53d1ec6
clean test
jgimeno May 8, 2020
1f6560e
remove comment
jgimeno May 8, 2020
caea5f4
remove comment
jgimeno May 8, 2020
212a6bc
add page and limit to unbonding delegations
jgimeno May 8, 2020
cc4016f
Merge branch 'master' into jonathan/6109-pagination-validator
jgimeno May 11, 2020
bbd11ca
add flag limit and page for delegations-to command
jgimeno May 11, 2020
459c837
refactor hardcoded string values
jgimeno May 11, 2020
0bc98fc
udpate page and limit for validator
jgimeno May 11, 2020
f394fe8
Merge branch 'master' into jonathan/6109-pagination-validator
jgimeno May 11, 2020
b62d5a3
update changelog
jgimeno May 11, 2020
d84bb75
Merge branch 'jonathan/6109-pagination-validator' of github.com:cosmo…
jgimeno May 11, 2020
b7fbb43
add pagination option
jgimeno May 11, 2020
2450c91
add defaults
jgimeno May 11, 2020
5b5732a
Update CHANGELOG.md
jgimeno May 11, 2020
a8386d8
Merge branch 'master' into jonathan/6109-pagination-validator
jgimeno May 12, 2020
1353dc0
Merge branch 'master' into jonathan/6109-pagination-validator
alexanderbez May 12, 2020
73d3dc0
Merge branch 'master' into jonathan/6109-pagination-validator
May 12, 2020
2f08cad
Merge branch 'master' into jonathan/6109-pagination-validator
May 13, 2020
878ab33
solve conflicts
jgimeno Jun 6, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ functionality that requires an online connection.
* (simulation) [\#6002](https://github.com/cosmos/cosmos-sdk/pull/6002) Add randomized consensus params into simulation.
* (x/staking) [\#6059](https://github.com/cosmos/cosmos-sdk/pull/6059) Updated `HistoricalEntries` parameter default to 100.
* (x/ibc) [\#5948](https://github.com/cosmos/cosmos-sdk/issues/5948) Add `InitGenesis` and `ExportGenesis` functions for `ibc` module.
* (types) [\#6128](https://github.com/cosmos/cosmos-sdk/pull/6137) Add String() method to GasMeter
* (x/staking) [\#6163](https://github.com/cosmos/cosmos-sdk/pull/6163) CLI and REST call to unbonding delegations and delegations now accept
pagination.
* (types) [\#6128](https://github.com/cosmos/cosmos-sdk/pull/6137) Add `String()` method to `GasMeter`.
* (types) [\#6195](https://github.com/cosmos/cosmos-sdk/pull/6195) Add codespace to broadcast(sync/async) response.
* (baseapp) [\#6053](https://github.com/cosmos/cosmos-sdk/pull/6053) Customizable panic recovery handling added for `app.runTx()` method (as proposed in the [ADR 22](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-022-custom-panic-handling.md)). Adds ability for developers to register custom panic handlers extending standard ones.
Expand Down
19 changes: 15 additions & 4 deletions x/staking/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand Down Expand Up @@ -125,7 +126,7 @@ $ %s query staking validators

// GetCmdQueryValidatorUnbondingDelegations implements the query all unbonding delegatations from a validator command.
func GetCmdQueryValidatorUnbondingDelegations(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "unbonding-delegations-from [validator-addr]",
Short: "Query all unbonding delegatations from a validator",
Long: strings.TrimSpace(
Expand All @@ -146,7 +147,7 @@ $ %s query staking unbonding-delegations-from cosmosvaloper1gghjut3ccd8ay0zduzj6
return err
}

bz, err := cdc.MarshalJSON(types.NewQueryValidatorParams(valAddr))
bz, err := cdc.MarshalJSON(types.NewQueryValidatorParams(valAddr, viper.GetInt(flags.FlagPage), viper.GetInt(flags.FlagLimit)))
if err != nil {
return err
}
Expand All @@ -162,6 +163,11 @@ $ %s query staking unbonding-delegations-from cosmosvaloper1gghjut3ccd8ay0zduzj6
return clientCtx.PrintOutput(ubds)
},
}

cmd.Flags().Int(flags.FlagPage, 1, "pagination page of unbonding delegations to query for")
cmd.Flags().Int(flags.FlagLimit, 100, "pagination limit of unbonding delegations to query for")

return cmd
}

// GetCmdQueryValidatorRedelegations implements the query all redelegatations
Expand Down Expand Up @@ -306,7 +312,7 @@ $ %s query staking delegations cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p
// GetCmdQueryValidatorDelegations implements the command to query all the
// delegations to a specific validator.
func GetCmdQueryValidatorDelegations(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "delegations-to [validator-addr]",
Short: "Query all delegations made to one validator",
Long: strings.TrimSpace(
Expand All @@ -327,7 +333,7 @@ $ %s query staking delegations-to cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ld
return err
}

bz, err := cdc.MarshalJSON(types.NewQueryValidatorParams(valAddr))
bz, err := cdc.MarshalJSON(types.NewQueryValidatorParams(valAddr, viper.GetInt(flags.FlagPage), viper.GetInt(flags.FlagLimit)))
if err != nil {
return err
}
Expand All @@ -346,6 +352,11 @@ $ %s query staking delegations-to cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ld
return clientCtx.PrintOutput(resp)
},
}

cmd.Flags().Int(flags.FlagPage, 1, "pagination page of delegations to query for")
cmd.Flags().Int(flags.FlagLimit, 100, "pagination limit of delegations to query for")

return cmd
}

// GetCmdQueryUnbondingDelegation implements the command to query a single
Expand Down
30 changes: 15 additions & 15 deletions x/staking/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ func delegatorDelegationsHandlerFn(clientCtx client.Context) http.HandlerFunc {
}

// HTTP request handler to query a delegator unbonding delegations
func delegatorUnbondingDelegationsHandlerFn(clientCtx client.Context) http.HandlerFunc {
return queryDelegator(clientCtx, "custom/staking/delegatorUnbondingDelegations")
func delegatorUnbondingDelegationsHandlerFn(cliCtx client.Context) http.HandlerFunc {
return queryDelegator(cliCtx, fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryDelegatorUnbondingDelegations))
}

// HTTP request handler to query all staking txs (msgs) from a delegator
Expand Down Expand Up @@ -189,8 +189,8 @@ func delegatorTxsHandlerFn(clientCtx client.Context) http.HandlerFunc {
}

// HTTP request handler to query an unbonding-delegation
func unbondingDelegationHandlerFn(clientCtx client.Context) http.HandlerFunc {
return queryBonds(clientCtx, "custom/staking/unbondingDelegation")
func unbondingDelegationHandlerFn(cliCtx client.Context) http.HandlerFunc {
return queryBonds(cliCtx, fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryUnbondingDelegation))
}

// HTTP request handler to query redelegations
Expand Down Expand Up @@ -239,7 +239,7 @@ func redelegationsHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}

res, height, err := clientCtx.QueryWithData("custom/staking/redelegations", bz)
res, height, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryRedelegations), bz)
if rest.CheckInternalServerError(w, err) {
return
}
Expand All @@ -255,13 +255,13 @@ func delegationHandlerFn(clientCtx client.Context) http.HandlerFunc {
}

// HTTP request handler to query all delegator bonded validators
func delegatorValidatorsHandlerFn(clientCtx client.Context) http.HandlerFunc {
return queryDelegator(clientCtx, "custom/staking/delegatorValidators")
func delegatorValidatorsHandlerFn(cliCtx client.Context) http.HandlerFunc {
return queryDelegator(cliCtx, fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryDelegatorValidators))
}

// HTTP request handler to get information from a currently bonded validator
func delegatorValidatorHandlerFn(clientCtx client.Context) http.HandlerFunc {
return queryBonds(clientCtx, "custom/staking/delegatorValidator")
func delegatorValidatorHandlerFn(cliCtx client.Context) http.HandlerFunc {
return queryBonds(cliCtx, fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryDelegatorValidator))
}

// HTTP request handler to query list of validators
Expand Down Expand Up @@ -302,8 +302,8 @@ func validatorsHandlerFn(clientCtx client.Context) http.HandlerFunc {
}

// HTTP request handler to query the validator information from a given validator address
func validatorHandlerFn(clientCtx client.Context) http.HandlerFunc {
return queryValidator(clientCtx, "custom/staking/validator")
func validatorHandlerFn(cliCtx client.Context) http.HandlerFunc {
return queryValidator(cliCtx, fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryValidator))
}

// HTTP request handler to query all unbonding delegations from a validator
Expand All @@ -312,8 +312,8 @@ func validatorDelegationsHandlerFn(clientCtx client.Context) http.HandlerFunc {
}

// HTTP request handler to query all unbonding delegations from a validator
func validatorUnbondingDelegationsHandlerFn(clientCtx client.Context) http.HandlerFunc {
return queryValidator(clientCtx, "custom/staking/validatorUnbondingDelegations")
func validatorUnbondingDelegationsHandlerFn(cliCtx client.Context) http.HandlerFunc {
return queryValidator(cliCtx, fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryValidatorUnbondingDelegations))
}

// HTTP request handler to query historical info at a given height
Expand Down Expand Up @@ -353,7 +353,7 @@ func poolHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}

res, height, err := clientCtx.QueryWithData("custom/staking/pool", nil)
res, height, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryPool), nil)
if rest.CheckInternalServerError(w, err) {
return
}
Expand All @@ -371,7 +371,7 @@ func paramsHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}

res, height, err := clientCtx.QueryWithData("custom/staking/parameters", nil)
res, height, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParameters), nil)
if rest.CheckInternalServerError(w, err) {
return
}
Expand Down
7 changes: 6 additions & 1 deletion x/staking/client/rest/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ func queryValidator(clientCtx client.Context, endpoint string) http.HandlerFunc
vars := mux.Vars(r)
bech32validatorAddr := vars["validatorAddr"]

_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
jgimeno marked this conversation as resolved.
Show resolved Hide resolved
if rest.CheckBadRequestError(w, err) {
return
}

validatorAddr, err := sdk.ValAddressFromBech32(bech32validatorAddr)
if rest.CheckBadRequestError(w, err) {
return
Expand All @@ -121,7 +126,7 @@ func queryValidator(clientCtx client.Context, endpoint string) http.HandlerFunc
return
}

params := types.NewQueryValidatorParams(validatorAddr)
params := types.NewQueryValidatorParams(validatorAddr, page, limit)

bz, err := clientCtx.Codec.MarshalJSON(params)
if rest.CheckBadRequestError(w, err) {
Expand Down
14 changes: 14 additions & 0 deletions x/staking/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ func queryValidatorDelegations(ctx sdk.Context, req abci.RequestQuery, k Keeper)

delegations := k.GetValidatorDelegations(ctx, params.ValidatorAddr)

start, end := client.Paginate(len(delegations), params.Page, params.Limit, int(k.GetParams(ctx).MaxValidators))
if start < 0 || end < 0 {
delegations = []types.Delegation{}
} else {
delegations = delegations[start:end]
}

delegationResps, err := delegationsToDelegationResponses(ctx, k, delegations)
if err != nil {
return nil, err
Expand Down Expand Up @@ -158,6 +165,13 @@ func queryValidatorUnbondingDelegations(ctx sdk.Context, req abci.RequestQuery,
unbonds = types.UnbondingDelegations{}
}

start, end := client.Paginate(len(unbonds), params.Page, params.Limit, int(k.GetParams(ctx).MaxValidators))
if start < 0 || end < 0 {
unbonds = types.UnbondingDelegations{}
} else {
unbonds = unbonds[start:end]
}

res, err := codec.MarshalJSONIndent(types.ModuleCdc, unbonds)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
Expand Down
114 changes: 110 additions & 4 deletions x/staking/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestNewQuerier(t *testing.T) {
_, err = querier(ctx, []string{"parameters"}, query)
require.NoError(t, err)

queryValParams := types.NewQueryValidatorParams(addrVal1)
queryValParams := types.NewQueryValidatorParams(addrVal1, 0, 0)
bz, errRes := cdc.MarshalJSON(queryValParams)
require.NoError(t, errRes)

Expand Down Expand Up @@ -178,7 +178,7 @@ func TestQueryValidators(t *testing.T) {

// Query each validator
for _, validator := range validators {
queryParams := types.NewQueryValidatorParams(validator.OperatorAddress)
queryParams := types.NewQueryValidatorParams(validator.OperatorAddress, 0, 0)
bz, err := cdc.MarshalJSON(queryParams)
require.NoError(t, err)

Expand Down Expand Up @@ -323,7 +323,7 @@ func TestQueryDelegation(t *testing.T) {
require.Error(t, err)

// Query validator delegations
bz, errRes = cdc.MarshalJSON(types.NewQueryValidatorParams(addrVal1))
bz, errRes = cdc.MarshalJSON(types.NewQueryValidatorParams(addrVal1, 1, 100))
require.NoError(t, errRes)

query = abci.RequestQuery{
Expand Down Expand Up @@ -424,6 +424,112 @@ func TestQueryDelegation(t *testing.T) {
require.Len(t, redel.Entries, len(redelRes[0].Entries))
}

func TestQueryValidatorDelegations_Pagination(t *testing.T) {
cases := []struct {
page int
limit int
expectedResults int
}{
{
page: 1,
limit: 75,
expectedResults: 75,
},
{
page: 2,
limit: 75,
expectedResults: 25,
},
{
page: 1,
limit: 100,
expectedResults: 100,
},
}

cdc, app, ctx := createTestInput()
querier := staking.NewQuerier(app.StakingKeeper)

addrs := simapp.AddTestAddrs(app, ctx, 100, sdk.TokensFromConsensusPower(10000))
pubKeys := simapp.CreateTestPubKeys(1)

valAddress := sdk.ValAddress(addrs[0])

val1 := types.NewValidator(valAddress, pubKeys[0], types.Description{})
app.StakingKeeper.SetValidator(ctx, val1)
app.StakingKeeper.SetValidatorByPowerIndex(ctx, val1)

// Create Validators and Delegation
for _, addr := range addrs {
validator, found := app.StakingKeeper.GetValidator(ctx, valAddress)
if !found {
t.Error("expected validator not found")
}

delTokens := sdk.TokensFromConsensusPower(20)
_, err := app.StakingKeeper.Delegate(ctx, addr, delTokens, sdk.Unbonded, validator, true)
require.NoError(t, err)
}

// apply TM updates
app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)

for _, c := range cases {
// Query Delegator bonded validators
queryParams := types.NewQueryDelegatorParams(addrs[0])
bz, errRes := cdc.MarshalJSON(queryParams)
require.NoError(t, errRes)

// Query valAddress delegations
bz, errRes = cdc.MarshalJSON(types.NewQueryValidatorParams(valAddress, c.page, c.limit))
require.NoError(t, errRes)

query := abci.RequestQuery{
Path: "custom/staking/validatorDelegations",
Data: bz,
}

res, err := querier(ctx, []string{types.QueryValidatorDelegations}, query)
require.NoError(t, err)

var delegationsRes types.DelegationResponses
errRes = cdc.UnmarshalJSON(res, &delegationsRes)
require.NoError(t, errRes)
require.Len(t, delegationsRes, c.expectedResults)
}

// Undelegate
for _, addr := range addrs {
delTokens := sdk.TokensFromConsensusPower(20)
_, err := app.StakingKeeper.Undelegate(ctx, addr, val1.GetOperator(), delTokens.ToDec())
require.NoError(t, err)
}

// apply TM updates
app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)

for _, c := range cases {
// Query Unbonding delegations with pagination.
queryParams := types.NewQueryDelegatorParams(addrs[0])
bz, errRes := cdc.MarshalJSON(queryParams)
require.NoError(t, errRes)

bz, errRes = cdc.MarshalJSON(types.NewQueryValidatorParams(valAddress, c.page, c.limit))
require.NoError(t, errRes)
query := abci.RequestQuery{
Data: bz,
}

unbondingDelegations := types.UnbondingDelegations{}
res, err := querier(ctx, []string{types.QueryValidatorUnbondingDelegations}, query)
require.NoError(t, err)

errRes = cdc.UnmarshalJSON(res, &unbondingDelegations)
require.NoError(t, errRes)
require.Len(t, unbondingDelegations, c.expectedResults)
}
}

func TestQueryRedelegations(t *testing.T) {
cdc, app, ctx := createTestInput()
querier := staking.NewQuerier(app.StakingKeeper)
Expand Down Expand Up @@ -474,7 +580,7 @@ func TestQueryRedelegations(t *testing.T) {
require.Len(t, redel.Entries, len(redelRes[0].Entries))

// validator redelegations
queryValidatorParams := types.NewQueryValidatorParams(val1.GetOperator())
queryValidatorParams := types.NewQueryValidatorParams(val1.GetOperator(), 0, 0)
bz, errRes = cdc.MarshalJSON(queryValidatorParams)
require.NoError(t, errRes)

Expand Down
6 changes: 4 additions & 2 deletions x/staking/types/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const (
// defines the params for the following queries:
// - 'custom/staking/delegatorDelegations'
// - 'custom/staking/delegatorUnbondingDelegations'
// - 'custom/staking/delegatorRedelegations'
// - 'custom/staking/delegatorValidators'
type QueryDelegatorParams struct {
DelegatorAddr sdk.AccAddress
Expand All @@ -45,11 +44,14 @@ func NewQueryDelegatorParams(delegatorAddr sdk.AccAddress) QueryDelegatorParams
// - 'custom/staking/validatorRedelegations'
type QueryValidatorParams struct {
ValidatorAddr sdk.ValAddress
Page, Limit int
}

func NewQueryValidatorParams(validatorAddr sdk.ValAddress) QueryValidatorParams {
func NewQueryValidatorParams(validatorAddr sdk.ValAddress, page, limit int) QueryValidatorParams {
return QueryValidatorParams{
ValidatorAddr: validatorAddr,
Page: page,
Limit: limit,
}
}

Expand Down