Fix ListBlock RPC bugs (#3126)

This commit is contained in:
terence tsao 2019-08-03 09:22:13 -07:00 committed by GitHub
parent ea09a918d8
commit d6b311ab84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 25 deletions

View File

@ -3,7 +3,6 @@ package rpc
import (
"context"
"sort"
"strconv"
ptypes "github.com/gogo/protobuf/types"
"github.com/prysmaticlabs/prysm/beacon-chain/core/epoch"
@ -135,7 +134,7 @@ func (bs *BeaconChainServer) ListBlocks(
numBlks := len(blks)
if numBlks == 0 {
return nil, status.Errorf(codes.NotFound, "block for epoch %d does not exists in DB", q.Epoch)
return &ethpb.ListBlocksResponse{Blocks: []*ethpb.BeaconBlock{}, TotalSize: 0}, nil
}
start, end, nextPageToken, err := pagination.StartAndEndPage(req.PageToken, int(req.PageSize), numBlks)
@ -156,18 +155,12 @@ func (bs *BeaconChainServer) ListBlocks(
}
if blk == nil {
return nil, status.Errorf(codes.NotFound, "block for root %#x does not exists in DB", q.Root)
}
token, err := strconv.Atoi(req.PageToken)
if err != nil {
return nil, status.Errorf(codes.Internal, "could not convert page token: %v", err)
return &ethpb.ListBlocksResponse{Blocks: []*ethpb.BeaconBlock{}, TotalSize: 0}, nil
}
return &ethpb.ListBlocksResponse{
Blocks: []*ethpb.BeaconBlock{blk},
TotalSize: 1,
NextPageToken: strconv.Itoa(token + 1),
}, nil
case *ethpb.ListBlocksRequest_Slot:
@ -178,7 +171,7 @@ func (bs *BeaconChainServer) ListBlocks(
numBlks := len(blks)
if numBlks == 0 {
return nil, status.Errorf(codes.NotFound, "block for slot %d does not exists in DB", q.Slot)
return &ethpb.ListBlocksResponse{Blocks: []*ethpb.BeaconBlock{}, TotalSize: 0}, nil
}
start, end, nextPageToken, err := pagination.StartAndEndPage(req.PageToken, int(req.PageSize), numBlks)

View File

@ -879,7 +879,7 @@ func TestBeaconChainServer_GetValidatorsParticipation(t *testing.T) {
}
}
func TestBeaconChainServer_ListBlocoksPagination(t *testing.T) {
func TestBeaconChainServer_ListBlocksPagination(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
ctx := context.Background()
@ -923,7 +923,10 @@ func TestBeaconChainServer_ListBlocoksPagination(t *testing.T) {
PageSize: 3},
res: &ethpb.ListBlocksResponse{
Blocks: []*ethpb.BeaconBlock{{Slot: 6}},
NextPageToken: strconv.Itoa(1),
TotalSize: 1}},
{req: &ethpb.ListBlocksRequest{QueryFilter: &ethpb.ListBlocksRequest_Root{Root: root6[:]}},
res: &ethpb.ListBlocksResponse{
Blocks: []*ethpb.BeaconBlock{{Slot: 6}},
TotalSize: 1}},
{req: &ethpb.ListBlocksRequest{
PageToken: strconv.Itoa(0),
@ -990,21 +993,55 @@ func TestBeaconChainServer_ListBlocksErrors(t *testing.T) {
t.Errorf("Expected error %v, received %v", wanted, err)
}
wanted = "block for epoch 0 does not exists in DB"
req = &ethpb.ListBlocksRequest{QueryFilter: &ethpb.ListBlocksRequest_Epoch{}}
if _, err := bs.ListBlocks(ctx, req); !strings.Contains(err.Error(), wanted) {
t.Errorf("Expected error %v, received %v", wanted, err)
res, err := bs.ListBlocks(ctx, req)
if err != nil {
t.Fatal(err)
}
if len(res.Blocks) != 0 {
t.Errorf("wanted empty list, got a list of %d", len(res.Blocks))
}
if res.TotalSize != 0 {
t.Errorf("wanted total size 0, got size %d", res.TotalSize)
}
wanted = "block for slot 0 does not exists in DB"
req = &ethpb.ListBlocksRequest{QueryFilter: &ethpb.ListBlocksRequest_Slot{}}
if _, err := bs.ListBlocks(ctx, req); !strings.Contains(err.Error(), wanted) {
t.Errorf("Expected error %v, received %v", wanted, err)
res, err = bs.ListBlocks(ctx, req)
if err != nil {
t.Fatal(err)
}
if len(res.Blocks) != 0 {
t.Errorf("wanted empty list, got a list of %d", len(res.Blocks))
}
if res.TotalSize != 0 {
t.Errorf("wanted total size 0, got size %d", res.TotalSize)
}
wanted = "block for root 0x41 does not exists in DB"
req = &ethpb.ListBlocksRequest{QueryFilter: &ethpb.ListBlocksRequest_Root{Root: []byte{'A'}}}
if _, err := bs.ListBlocks(ctx, req); !strings.Contains(err.Error(), wanted) {
t.Errorf("Expected error %v, received %v", wanted, err)
res, err = bs.ListBlocks(ctx, req)
if err != nil {
t.Fatal(err)
}
if len(res.Blocks) != 0 {
t.Errorf("wanted empty list, got a list of %d", len(res.Blocks))
}
if res.TotalSize != 0 {
t.Errorf("wanted total size 0, got size %d", res.TotalSize)
}
req = &ethpb.ListBlocksRequest{QueryFilter: &ethpb.ListBlocksRequest_Root{Root: []byte{'A'}}}
res, err = bs.ListBlocks(ctx, req)
if err != nil {
t.Fatal(err)
}
if len(res.Blocks) != 0 {
t.Errorf("wanted empty list, got a list of %d", len(res.Blocks))
}
if res.TotalSize != 0 {
t.Errorf("wanted total size 0, got size %d", res.TotalSize)
}
}