mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-25 14:52:17 +00:00
rpc: return err if page is incorrect (less than 0 or greater than tot… (#3825)
* rpc: return err if page is incorrect (less than 0 or greater than total pages) Fixes #3813 * fix rpc_test
This commit is contained in:
parent
5398420103
commit
756440e0a2
@ -27,3 +27,5 @@ program](https://hackerone.com/tendermint).
|
|||||||
- [mempool] \#3826 Make `max_msg_bytes` configurable
|
- [mempool] \#3826 Make `max_msg_bytes` configurable
|
||||||
|
|
||||||
### BUG FIXES:
|
### BUG FIXES:
|
||||||
|
|
||||||
|
- [rpc] \#3813 Return err if page is incorrect (less than 0 or greater than total pages)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
cfg "github.com/tendermint/tendermint/config"
|
cfg "github.com/tendermint/tendermint/config"
|
||||||
@ -145,19 +146,24 @@ func SetConfig(c cfg.RPCConfig) {
|
|||||||
config = c
|
config = c
|
||||||
}
|
}
|
||||||
|
|
||||||
func validatePage(page, perPage, totalCount int) int {
|
func validatePage(page, perPage, totalCount int) (int, error) {
|
||||||
if perPage < 1 {
|
if perPage < 1 {
|
||||||
return 1
|
panic(fmt.Sprintf("zero or negative perPage: %d", perPage))
|
||||||
|
}
|
||||||
|
|
||||||
|
if page == 0 {
|
||||||
|
return 1, nil // default
|
||||||
}
|
}
|
||||||
|
|
||||||
pages := ((totalCount - 1) / perPage) + 1
|
pages := ((totalCount - 1) / perPage) + 1
|
||||||
if page < 1 {
|
if pages == 0 {
|
||||||
page = 1
|
pages = 1 // one page (even if it's empty)
|
||||||
} else if page > pages {
|
}
|
||||||
page = pages
|
if page < 0 || page > pages {
|
||||||
|
return 1, fmt.Errorf("page should be within [0, %d] range, given %d", pages, page)
|
||||||
}
|
}
|
||||||
|
|
||||||
return page
|
return page, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func validatePerPage(perPage int) int {
|
func validatePerPage(perPage int) int {
|
||||||
|
@ -14,33 +14,39 @@ func TestPaginationPage(t *testing.T) {
|
|||||||
perPage int
|
perPage int
|
||||||
page int
|
page int
|
||||||
newPage int
|
newPage int
|
||||||
|
expErr bool
|
||||||
}{
|
}{
|
||||||
{0, 0, 1, 1},
|
{0, 10, 1, 1, false},
|
||||||
|
|
||||||
{0, 10, 0, 1},
|
{0, 10, 0, 1, false},
|
||||||
{0, 10, 1, 1},
|
{0, 10, 1, 1, false},
|
||||||
{0, 10, 2, 1},
|
{0, 10, 2, 0, true},
|
||||||
|
|
||||||
{5, 10, -1, 1},
|
{5, 10, -1, 0, true},
|
||||||
{5, 10, 0, 1},
|
{5, 10, 0, 1, false},
|
||||||
{5, 10, 1, 1},
|
{5, 10, 1, 1, false},
|
||||||
{5, 10, 2, 1},
|
{5, 10, 2, 0, true},
|
||||||
{5, 10, 2, 1},
|
{5, 10, 2, 0, true},
|
||||||
|
|
||||||
{5, 5, 1, 1},
|
{5, 5, 1, 1, false},
|
||||||
{5, 5, 2, 1},
|
{5, 5, 2, 0, true},
|
||||||
{5, 5, 3, 1},
|
{5, 5, 3, 0, true},
|
||||||
|
|
||||||
{5, 3, 2, 2},
|
{5, 3, 2, 2, false},
|
||||||
{5, 3, 3, 2},
|
{5, 3, 3, 0, true},
|
||||||
|
|
||||||
{5, 2, 2, 2},
|
{5, 2, 2, 2, false},
|
||||||
{5, 2, 3, 3},
|
{5, 2, 3, 3, false},
|
||||||
{5, 2, 4, 3},
|
{5, 2, 4, 0, true},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
p := validatePage(c.page, c.perPage, c.totalCount)
|
p, err := validatePage(c.page, c.perPage, c.totalCount)
|
||||||
|
if c.expErr {
|
||||||
|
assert.Error(t, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
assert.Equal(t, c.newPage, p, fmt.Sprintf("%v", c))
|
assert.Equal(t, c.newPage, p, fmt.Sprintf("%v", c))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -202,7 +202,10 @@ func TxSearch(ctx *rpctypes.Context, query string, prove bool, page, perPage int
|
|||||||
|
|
||||||
totalCount := len(results)
|
totalCount := len(results)
|
||||||
perPage = validatePerPage(perPage)
|
perPage = validatePerPage(perPage)
|
||||||
page = validatePage(page, perPage, totalCount)
|
page, err = validatePage(page, perPage, totalCount)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
skipCount := validateSkipCount(page, perPage)
|
skipCount := validateSkipCount(page, perPage)
|
||||||
|
|
||||||
apiResults := make([]*ctypes.ResultTx, cmn.MinInt(perPage, totalCount-skipCount))
|
apiResults := make([]*ctypes.ResultTx, cmn.MinInt(perPage, totalCount-skipCount))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user