diff --git a/beacon-chain/rpc/beacon_chain_server.go b/beacon-chain/rpc/beacon_chain_server.go index 0e99a80ec..67592c7f3 100644 --- a/beacon-chain/rpc/beacon_chain_server.go +++ b/beacon-chain/rpc/beacon_chain_server.go @@ -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 ðpb.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 ðpb.ListBlocksResponse{Blocks: []*ethpb.BeaconBlock{}, TotalSize: 0}, nil } return ðpb.ListBlocksResponse{ - Blocks: []*ethpb.BeaconBlock{blk}, - TotalSize: 1, - NextPageToken: strconv.Itoa(token + 1), + Blocks: []*ethpb.BeaconBlock{blk}, + TotalSize: 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 ðpb.ListBlocksResponse{Blocks: []*ethpb.BeaconBlock{}, TotalSize: 0}, nil } start, end, nextPageToken, err := pagination.StartAndEndPage(req.PageToken, int(req.PageSize), numBlks) diff --git a/beacon-chain/rpc/beacon_chain_server_test.go b/beacon-chain/rpc/beacon_chain_server_test.go index 857599aaa..63d9e2745 100644 --- a/beacon-chain/rpc/beacon_chain_server_test.go +++ b/beacon-chain/rpc/beacon_chain_server_test.go @@ -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() @@ -922,9 +922,12 @@ func TestBeaconChainServer_ListBlocoksPagination(t *testing.T) { QueryFilter: ðpb.ListBlocksRequest_Root{Root: root6[:]}, PageSize: 3}, res: ðpb.ListBlocksResponse{ - Blocks: []*ethpb.BeaconBlock{{Slot: 6}}, - NextPageToken: strconv.Itoa(1), - TotalSize: 1}}, + Blocks: []*ethpb.BeaconBlock{{Slot: 6}}, + TotalSize: 1}}, + {req: ðpb.ListBlocksRequest{QueryFilter: ðpb.ListBlocksRequest_Root{Root: root6[:]}}, + res: ðpb.ListBlocksResponse{ + Blocks: []*ethpb.BeaconBlock{{Slot: 6}}, + TotalSize: 1}}, {req: ðpb.ListBlocksRequest{ PageToken: strconv.Itoa(0), QueryFilter: ðpb.ListBlocksRequest_Epoch{Epoch: 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 = ðpb.ListBlocksRequest{QueryFilter: ðpb.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 = ðpb.ListBlocksRequest{QueryFilter: ðpb.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 = ðpb.ListBlocksRequest{QueryFilter: ðpb.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 = ðpb.ListBlocksRequest{QueryFilter: ðpb.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) + } }