prysm-pulse/shared/pagination/pagination.go
terence tsao c1eeeef853 Implement ListValidatorAssignments RPC function (#3067)
* Need to sync latest ethereum API, will do it in master

* Added pagination wrapper

* Implemented ListValidatorAssignments

* Finished tests

* Fixed tests

* Raul's feedback

* Fmt

* Pagination test

* Removed extra loggings
2019-07-25 15:45:31 -04:00

40 lines
975 B
Go

package pagination
import (
"fmt"
"strconv"
"github.com/prysmaticlabs/prysm/shared/params"
)
// StartAndEndPage takes in the requested page token, wanted page size, total page size.
// It returns start, end page and the next page token.
func StartAndEndPage(pageToken string, pageSize int, totalSize int) (int, int, string, error) {
if pageToken == "" {
pageToken = "0"
}
if pageSize == 0 {
pageSize = params.BeaconConfig().DefaultPageSize
}
token, err := strconv.Atoi(pageToken)
if err != nil {
return 0, 0, "", fmt.Errorf("could not convert page token: %v", err)
}
// Start page can not be greater than validator size.
start := token * pageSize
if start >= totalSize {
return 0, 0, "", fmt.Errorf("page start %d >= validator list %d", start, totalSize)
}
// End page can not go out of bound.
end := start + pageSize
if end > totalSize {
end = totalSize
}
nextPageToken := strconv.Itoa(token + 1)
return start, end, nextPageToken, nil
}