Update API to return empty next page token on last page response (#4176)

* Update API to return empty next page token on last page response
* Update tests
* Merge branch 'master' into api-return-empty-page-token-on-last-page
This commit is contained in:
Peter Pratscher
2019-12-03 18:45:43 +01:00
committed by prylabs-bulldozer[bot]
parent 8e4022f8aa
commit a2d4701f6e
7 changed files with 23 additions and 21 deletions

View File

@@ -513,7 +513,7 @@ func TestServer_ListAssignments_CanFilterPubkeysIndices_WithPagination(t *testin
wantedRes = &ethpb.ValidatorAssignments{
Assignments: assignments,
TotalSize: int32(len(req.Indices)),
NextPageToken: "2",
NextPageToken: "",
}
if !reflect.DeepEqual(res, wantedRes) {

View File

@@ -93,7 +93,7 @@ func TestServer_ListAttestations_Genesis(t *testing.T) {
}
wanted := &ethpb.ListAttestationsResponse{
Attestations: []*ethpb.Attestation{att},
NextPageToken: "1",
NextPageToken: "",
TotalSize: 1,
}
@@ -398,7 +398,7 @@ func TestServer_ListAttestations_Pagination_CustomPageParameters(t *testing.T) {
AggregationBits: bitfield.Bitlist{0b11},
CustodyBits: bitfield.NewBitlist(1)},
},
NextPageToken: strconv.Itoa(34),
NextPageToken: "",
TotalSize: int32(count)}},
{
req: &ethpb.ListAttestationsRequest{

View File

@@ -174,7 +174,7 @@ func TestServer_ListBlocks_Pagination(t *testing.T) {
PageSize: 3},
res: &ethpb.ListBlocksResponse{
BlockContainers: []*ethpb.BeaconBlockContainer{{Block: &ethpb.BeaconBlock{Slot: 5}, BlockRoot: blkContainers[5].BlockRoot}},
NextPageToken: strconv.Itoa(1),
NextPageToken: "",
TotalSize: 1}},
{req: &ethpb.ListBlocksRequest{
PageToken: strconv.Itoa(0),
@@ -193,7 +193,7 @@ func TestServer_ListBlocks_Pagination(t *testing.T) {
PageSize: 100},
res: &ethpb.ListBlocksResponse{
BlockContainers: blkContainers[0:params.BeaconConfig().SlotsPerEpoch],
NextPageToken: strconv.Itoa(1),
NextPageToken: "",
TotalSize: int32(params.BeaconConfig().SlotsPerEpoch)}},
{req: &ethpb.ListBlocksRequest{
PageToken: strconv.Itoa(1),
@@ -201,7 +201,7 @@ func TestServer_ListBlocks_Pagination(t *testing.T) {
PageSize: 3},
res: &ethpb.ListBlocksResponse{
BlockContainers: blkContainers[43:46],
NextPageToken: strconv.Itoa(2),
NextPageToken: "2",
TotalSize: int32(params.BeaconConfig().SlotsPerEpoch)}},
{req: &ethpb.ListBlocksRequest{
PageToken: strconv.Itoa(1),
@@ -209,7 +209,7 @@ func TestServer_ListBlocks_Pagination(t *testing.T) {
PageSize: 7},
res: &ethpb.ListBlocksResponse{
BlockContainers: blkContainers[95:96],
NextPageToken: strconv.Itoa(2),
NextPageToken: "",
TotalSize: int32(params.BeaconConfig().SlotsPerEpoch)}},
{req: &ethpb.ListBlocksRequest{
PageToken: strconv.Itoa(0),
@@ -217,7 +217,7 @@ func TestServer_ListBlocks_Pagination(t *testing.T) {
PageSize: 4},
res: &ethpb.ListBlocksResponse{
BlockContainers: blkContainers[96:100],
NextPageToken: strconv.Itoa(1),
NextPageToken: "1",
TotalSize: int32(params.BeaconConfig().SlotsPerEpoch / 2)}},
}
@@ -227,7 +227,7 @@ func TestServer_ListBlocks_Pagination(t *testing.T) {
t.Fatal(err)
}
if !proto.Equal(res, test.res) {
t.Errorf("Incorrect blocks response, wanted %d, received %d", len(test.res.BlockContainers), len(res.BlockContainers))
t.Errorf("Incorrect blocks response, wanted %v, received %v", test.res, res)
}
}
}

View File

@@ -252,7 +252,7 @@ func TestServer_ListBeaconCommittees_FromArchive(t *testing.T) {
Epoch: 0,
Committees: wanted,
ActiveValidatorCount: uint64(numValidators),
NextPageToken: strconv.Itoa(1),
NextPageToken: "",
TotalSize: int32(len(wanted)),
}
if !reflect.DeepEqual(wantedRes, res1) {

View File

@@ -248,7 +248,7 @@ func TestServer_ListValidatorBalances_Pagination_Default(t *testing.T) {
Balances: []*ethpb.ValidatorBalances_Balance{
{Index: 99, PublicKey: []byte{99}, Balance: 99},
},
NextPageToken: strconv.Itoa(1),
NextPageToken: "",
TotalSize: 1,
},
},
@@ -259,7 +259,7 @@ func TestServer_ListValidatorBalances_Pagination_Default(t *testing.T) {
{Index: 2, PublicKey: []byte{2}, Balance: 2},
{Index: 3, PublicKey: []byte{3}, Balance: 3},
},
NextPageToken: strconv.Itoa(1),
NextPageToken: "",
TotalSize: 3,
},
},
@@ -270,7 +270,7 @@ func TestServer_ListValidatorBalances_Pagination_Default(t *testing.T) {
{Index: 11, PublicKey: []byte{11}, Balance: 11},
{Index: 12, PublicKey: []byte{12}, Balance: 12},
},
NextPageToken: strconv.Itoa(1),
NextPageToken: "",
TotalSize: 3,
}},
{req: &ethpb.ListValidatorBalancesRequest{PublicKeys: [][]byte{{2}, {3}}, Indices: []uint64{3, 4}}, // Duplication
@@ -280,7 +280,7 @@ func TestServer_ListValidatorBalances_Pagination_Default(t *testing.T) {
{Index: 3, PublicKey: []byte{3}, Balance: 3},
{Index: 4, PublicKey: []byte{4}, Balance: 4},
},
NextPageToken: strconv.Itoa(1),
NextPageToken: "",
TotalSize: 3,
}},
{req: &ethpb.ListValidatorBalancesRequest{PublicKeys: [][]byte{{}}, Indices: []uint64{3, 4}}, // Public key has a blank value
@@ -289,7 +289,7 @@ func TestServer_ListValidatorBalances_Pagination_Default(t *testing.T) {
{Index: 3, PublicKey: []byte{3}, Balance: 3},
{Index: 4, PublicKey: []byte{4}, Balance: 4},
},
NextPageToken: strconv.Itoa(1),
NextPageToken: "",
TotalSize: 2,
}},
}
@@ -351,7 +351,7 @@ func TestServer_ListValidatorBalances_Pagination_CustomPageSizes(t *testing.T) {
{PublicKey: []byte{100}, Index: 100, Balance: uint64(100)},
{PublicKey: []byte{101}, Index: 101, Balance: uint64(101)},
},
NextPageToken: strconv.Itoa(34),
NextPageToken: "34",
TotalSize: int32(count)}},
{req: &ethpb.ListValidatorBalancesRequest{PageSize: 2},
res: &ethpb.ValidatorBalances{
@@ -663,7 +663,7 @@ func TestServer_ListValidators_Pagination(t *testing.T) {
res: &ethpb.Validators{
Validators: []*ethpb.Validator{
{PublicKey: []byte{99}}},
NextPageToken: strconv.Itoa(34),
NextPageToken: "",
TotalSize: int32(count)}},
{req: &ethpb.ListValidatorsRequest{PageSize: 2},
res: &ethpb.Validators{
@@ -679,7 +679,7 @@ func TestServer_ListValidators_Pagination(t *testing.T) {
t.Fatal(err)
}
if !proto.Equal(res, test.res) {
t.Error("Incorrect respond of validators")
t.Errorf("Incorrect validator response, wanted %v, received %v", test.res, res)
}
}
}

View File

@@ -23,7 +23,7 @@ func StartAndEndPage(pageToken string, pageSize int, totalSize int) (int, int, s
return 0, 0, "", errors.Wrap(err, "could not convert page token")
}
// Start page can not be greater than validator size.
// Start page can not be greater than set size.
start := token * pageSize
if start >= totalSize {
return 0, 0, "", fmt.Errorf("page start %d >= list %d", start, totalSize)
@@ -31,10 +31,12 @@ func StartAndEndPage(pageToken string, pageSize int, totalSize int) (int, int, s
// End page can not go out of bound.
end := start + pageSize
nextPageToken := strconv.Itoa(token + 1)
if end > totalSize {
end = totalSize
nextPageToken = "" // Return an empty next page token for the last page of a set
}
nextPageToken := strconv.Itoa(token + 1)
return start, end, nextPageToken, nil
}

View File

@@ -44,7 +44,7 @@ func TestStartAndEndPage(t *testing.T) {
token: "3",
pageSize: 33,
totalSize: 100,
nextToken: "4",
nextToken: "",
start: 99,
end: 100,
},