rpc/core: return an error if page=0 (#4947)

* rpc/core: return an error if `page=0`

Closes #4942

affected endpoints:

- /validators
- /tx_search

* swagger: update doc for /unconfirmed_txs
This commit is contained in:
Anton Kaliaev
2020-06-03 16:51:51 +04:00
committed by GitHub
parent 994912211c
commit ce3c9c2341
15 changed files with 96 additions and 62 deletions

View File

@@ -16,7 +16,7 @@ import (
// for the validators in the set as used in computing their Merkle root.
//
// More: https://docs.tendermint.com/master/rpc/#/Info/validators
func Validators(ctx *rpctypes.Context, heightPtr *int64, page, perPage int) (*ctypes.ResultValidators, error) {
func Validators(ctx *rpctypes.Context, heightPtr *int64, pagePtr, perPagePtr *int) (*ctypes.ResultValidators, error) {
// The latest validator that we know is the NextValidator of the last block.
height, err := getHeight(latestUncommittedHeight(), heightPtr)
if err != nil {
@@ -29,8 +29,8 @@ func Validators(ctx *rpctypes.Context, heightPtr *int64, page, perPage int) (*ct
}
totalCount := len(validators.Validators)
perPage = validatePerPage(perPage)
page, err = validatePage(page, perPage, totalCount)
perPage := validatePerPage(perPagePtr)
page, err := validatePage(pagePtr, perPage, totalCount)
if err != nil {
return nil, err
}

View File

@@ -92,27 +92,33 @@ type Environment struct {
//----------------------------------------------
func validatePage(page, perPage, totalCount int) (int, error) {
func validatePage(pagePtr *int, perPage, totalCount int) (int, error) {
if perPage < 1 {
panic(fmt.Sprintf("zero or negative perPage: %d", perPage))
}
if page == 0 {
return 1, nil // default
if pagePtr == nil { // no page parameter
return 1, nil
}
pages := ((totalCount - 1) / perPage) + 1
if pages == 0 {
pages = 1 // one page (even if it's empty)
}
if page < 0 || page > pages {
return 1, fmt.Errorf("page should be within [0, %d] range, given %d", pages, page)
page := *pagePtr
if page <= 0 || page > pages {
return 1, fmt.Errorf("page should be within [1, %d] range, given %d", pages, page)
}
return page, nil
}
func validatePerPage(perPage int) int {
func validatePerPage(perPagePtr *int) int {
if perPagePtr == nil { // no per_page parameter
return defaultPerPage
}
perPage := *perPagePtr
if perPage < 1 {
return defaultPerPage
} else if perPage > maxPerPage {

View File

@@ -40,7 +40,7 @@ func TestPaginationPage(t *testing.T) {
}
for _, c := range cases {
p, err := validatePage(c.page, c.perPage, c.totalCount)
p, err := validatePage(&c.page, c.perPage, c.totalCount)
if c.expErr {
assert.Error(t, err)
continue
@@ -49,6 +49,11 @@ func TestPaginationPage(t *testing.T) {
assert.Equal(t, c.newPage, p, fmt.Sprintf("%v", c))
}
// nil case
p, err := validatePage(nil, 1, 1)
if assert.NoError(t, err) {
assert.Equal(t, 1, p)
}
}
func TestPaginationPerPage(t *testing.T) {
@@ -67,7 +72,11 @@ func TestPaginationPerPage(t *testing.T) {
}
for _, c := range cases {
p := validatePerPage(c.perPage)
p := validatePerPage(&c.perPage)
assert.Equal(t, c.newPerPage, p, fmt.Sprintf("%v", c))
}
// nil case
p := validatePerPage(nil)
assert.Equal(t, defaultPerPage, p)
}

View File

@@ -130,9 +130,9 @@ func BroadcastTxCommit(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadc
// UnconfirmedTxs gets unconfirmed transactions (maximum ?limit entries)
// including their number.
// More: https://docs.tendermint.com/master/rpc/#/Info/unconfirmed_txs
func UnconfirmedTxs(ctx *rpctypes.Context, limit int) (*ctypes.ResultUnconfirmedTxs, error) {
func UnconfirmedTxs(ctx *rpctypes.Context, limitPtr *int) (*ctypes.ResultUnconfirmedTxs, error) {
// reuse per_page validator
limit = validatePerPage(limit)
limit := validatePerPage(limitPtr)
txs := env.Mempool.ReapMaxTxs(limit)
return &ctypes.ResultUnconfirmedTxs{

View File

@@ -54,7 +54,7 @@ func Tx(ctx *rpctypes.Context, hash []byte, prove bool) (*ctypes.ResultTx, error
// TxSearch allows you to query for multiple transactions results. It returns a
// list of transactions (maximum ?per_page entries) and the total count.
// More: https://docs.tendermint.com/master/rpc/#/Info/tx_search
func TxSearch(ctx *rpctypes.Context, query string, prove bool, page, perPage int, orderBy string) (
func TxSearch(ctx *rpctypes.Context, query string, prove bool, pagePtr, perPagePtr *int, orderBy string) (
*ctypes.ResultTxSearch, error) {
// if index is disabled, return error
if _, ok := env.TxIndexer.(*null.TxIndex); ok {
@@ -93,8 +93,8 @@ func TxSearch(ctx *rpctypes.Context, query string, prove bool, page, perPage int
// paginate results
totalCount := len(results)
perPage = validatePerPage(perPage)
page, err = validatePage(page, perPage, totalCount)
perPage := validatePerPage(perPagePtr)
page, err := validatePage(pagePtr, perPage, totalCount)
if err != nil {
return nil, err
}