Skip to content

Commit

Permalink
Add canonical flag to ListBlocks response
Browse files Browse the repository at this point in the history
  • Loading branch information
pinglamb committed Jan 19, 2021
1 parent b33a8eb commit 49ef829
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 14 deletions.
22 changes: 21 additions & 1 deletion beacon-chain/rpc/beacon/blocks.go
Expand Up @@ -63,9 +63,14 @@ func (bs *Server) ListBlocks(
if err != nil {
return nil, err
}
canonical, err := bs.CanonicalFetcher.IsCanonical(ctx, root)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not determine if block is canonical: %v", err)
}
containers[i] = &ethpb.BeaconBlockContainer{
Block: b,
BlockRoot: root[:],
Canonical: canonical,
}
}

Expand All @@ -90,11 +95,16 @@ func (bs *Server) ListBlocks(
if err != nil {
return nil, err
}
canonical, err := bs.CanonicalFetcher.IsCanonical(ctx, root)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not determine if block is canonical: %v", err)
}

return &ethpb.ListBlocksResponse{
BlockContainers: []*ethpb.BeaconBlockContainer{{
Block: blk,
BlockRoot: root[:]},
BlockRoot: root[:],
Canonical: canonical},
},
TotalSize: 1,
}, nil
Expand Down Expand Up @@ -126,9 +136,14 @@ func (bs *Server) ListBlocks(
if err != nil {
return nil, err
}
canonical, err := bs.CanonicalFetcher.IsCanonical(ctx, root)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not determine if block is canonical: %v", err)
}
containers[i] = &ethpb.BeaconBlockContainer{
Block: b,
BlockRoot: root[:],
Canonical: canonical,
}
}

Expand All @@ -149,10 +164,15 @@ func (bs *Server) ListBlocks(
if err != nil {
return nil, err
}
canonical, err := bs.CanonicalFetcher.IsCanonical(ctx, root)
if err != nil {
return nil, err
}
containers := []*ethpb.BeaconBlockContainer{
{
Block: genBlk,
BlockRoot: root[:],
Canonical: canonical,
},
}

Expand Down
54 changes: 44 additions & 10 deletions beacon-chain/rpc/beacon/blocks_test.go
Expand Up @@ -71,10 +71,14 @@ func TestServer_ListBlocks_NoResults(t *testing.T) {

func TestServer_ListBlocks_Genesis(t *testing.T) {
db := dbTest.SetupDB(t)
chain := &chainMock.ChainService{
CanonicalRoots: map[[32]byte]bool{},
}
ctx := context.Background()

bs := &Server{
BeaconDB: db,
BeaconDB: db,
CanonicalFetcher: chain,
}

// Should throw an error if no genesis block is found.
Expand All @@ -93,11 +97,13 @@ func TestServer_ListBlocks_Genesis(t *testing.T) {
require.NoError(t, err)
require.NoError(t, db.SaveBlock(ctx, blk))
require.NoError(t, db.SaveGenesisBlockRoot(ctx, root))
chain.CanonicalRoots[root] = true
wanted := &ethpb.ListBlocksResponse{
BlockContainers: []*ethpb.BeaconBlockContainer{
{
Block: blk,
BlockRoot: root[:],
Canonical: true,
},
},
NextPageToken: "0",
Expand All @@ -116,10 +122,14 @@ func TestServer_ListBlocks_Genesis(t *testing.T) {

func TestServer_ListBlocks_Genesis_MultiBlocks(t *testing.T) {
db := dbTest.SetupDB(t)
chain := &chainMock.ChainService{
CanonicalRoots: map[[32]byte]bool{},
}
ctx := context.Background()

bs := &Server{
BeaconDB: db,
BeaconDB: db,
CanonicalFetcher: chain,
}
// Should return the proper genesis block if it exists.
parentRoot := [32]byte{1, 2, 3}
Expand All @@ -129,17 +139,15 @@ func TestServer_ListBlocks_Genesis_MultiBlocks(t *testing.T) {
require.NoError(t, err)
require.NoError(t, db.SaveBlock(ctx, blk))
require.NoError(t, db.SaveGenesisBlockRoot(ctx, root))
chain.CanonicalRoots[root] = true

count := uint64(100)
blks := make([]*ethpb.SignedBeaconBlock, count)
blkContainers := make([]*ethpb.BeaconBlockContainer, count)
for i := uint64(0); i < count; i++ {
b := testutil.NewBeaconBlock()
b.Block.Slot = i
root, err := b.Block.HashTreeRoot()
require.NoError(t, err)
blks[i] = b
blkContainers[i] = &ethpb.BeaconBlockContainer{Block: b, BlockRoot: root[:]}
}
require.NoError(t, db.SaveBlocks(ctx, blks))

Expand All @@ -154,6 +162,9 @@ func TestServer_ListBlocks_Genesis_MultiBlocks(t *testing.T) {

func TestServer_ListBlocks_Pagination(t *testing.T) {
db := dbTest.SetupDB(t)
chain := &chainMock.ChainService{
CanonicalRoots: map[[32]byte]bool{},
}
ctx := context.Background()

count := uint64(100)
Expand All @@ -164,13 +175,21 @@ func TestServer_ListBlocks_Pagination(t *testing.T) {
b.Block.Slot = i
root, err := b.Block.HashTreeRoot()
require.NoError(t, err)
chain.CanonicalRoots[root] = true
blks[i] = b
blkContainers[i] = &ethpb.BeaconBlockContainer{Block: b, BlockRoot: root[:]}
blkContainers[i] = &ethpb.BeaconBlockContainer{Block: b, BlockRoot: root[:], Canonical: true}
}
require.NoError(t, db.SaveBlocks(ctx, blks))

orphanedBlk := testutil.NewBeaconBlock()
orphanedBlk.Block.Slot = 300
orphanedBlkRoot, err := orphanedBlk.Block.HashTreeRoot()
require.NoError(t, err)
require.NoError(t, db.SaveBlock(ctx, orphanedBlk))

bs := &Server{
BeaconDB: db,
BeaconDB: db,
CanonicalFetcher: chain,
}

root6, err := blks[6].Block.HashTreeRoot()
Expand All @@ -188,7 +207,8 @@ func TestServer_ListBlocks_Pagination(t *testing.T) {
BlockContainers: []*ethpb.BeaconBlockContainer{{Block: testutil.HydrateSignedBeaconBlock(&ethpb.SignedBeaconBlock{
Block: &ethpb.BeaconBlock{
Slot: 5}}),
BlockRoot: blkContainers[5].BlockRoot}},
BlockRoot: blkContainers[5].BlockRoot,
Canonical: blkContainers[5].Canonical}},
NextPageToken: "",
TotalSize: 1}},
{req: &ethpb.ListBlocksRequest{
Expand All @@ -199,14 +219,16 @@ func TestServer_ListBlocks_Pagination(t *testing.T) {
BlockContainers: []*ethpb.BeaconBlockContainer{{Block: testutil.HydrateSignedBeaconBlock(&ethpb.SignedBeaconBlock{
Block: &ethpb.BeaconBlock{
Slot: 6}}),
BlockRoot: blkContainers[6].BlockRoot}},
BlockRoot: blkContainers[6].BlockRoot,
Canonical: blkContainers[6].Canonical}},
TotalSize: 1}},
{req: &ethpb.ListBlocksRequest{QueryFilter: &ethpb.ListBlocksRequest_Root{Root: root6[:]}},
res: &ethpb.ListBlocksResponse{
BlockContainers: []*ethpb.BeaconBlockContainer{{Block: testutil.HydrateSignedBeaconBlock(&ethpb.SignedBeaconBlock{
Block: &ethpb.BeaconBlock{
Slot: 6}}),
BlockRoot: blkContainers[6].BlockRoot}},
BlockRoot: blkContainers[6].BlockRoot,
Canonical: blkContainers[6].Canonical}},
TotalSize: 1}},
{req: &ethpb.ListBlocksRequest{
PageToken: strconv.Itoa(0),
Expand Down Expand Up @@ -240,6 +262,18 @@ func TestServer_ListBlocks_Pagination(t *testing.T) {
BlockContainers: blkContainers[96:100],
NextPageToken: "",
TotalSize: int32(params.BeaconConfig().SlotsPerEpoch / 2)}},
{req: &ethpb.ListBlocksRequest{
PageToken: strconv.Itoa(0),
QueryFilter: &ethpb.ListBlocksRequest_Slot{Slot: 300},
PageSize: 3},
res: &ethpb.ListBlocksResponse{
BlockContainers: []*ethpb.BeaconBlockContainer{{Block: testutil.HydrateSignedBeaconBlock(&ethpb.SignedBeaconBlock{
Block: &ethpb.BeaconBlock{
Slot: 300}}),
BlockRoot: orphanedBlkRoot[:],
Canonical: false}},
NextPageToken: "",
TotalSize: 1}},
}

for i, test := range tests {
Expand Down
4 changes: 2 additions & 2 deletions deps.bzl
Expand Up @@ -2580,8 +2580,8 @@ def prysm_deps():
name = "com_github_prysmaticlabs_ethereumapis",
build_file_generation = "off",
importpath = "github.com/prysmaticlabs/ethereumapis",
sum = "h1:69URSziUFhCVzHIDtPGMwYeP1G3JWhuBdB3enLRne6Y=",
version = "v0.0.0-20210105190001-13193818c0df",
sum = "h1:jJEtD9/flUo1EzhLB1orQ6XfDFfwENl5VhQg0UVVh94=",
version = "v0.0.0-20210118163152-3569d231d255",
)
go_repository(
name = "com_github_prysmaticlabs_go_bitfield",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -84,7 +84,7 @@ require (
github.com/prometheus/client_golang v1.9.0
github.com/prometheus/tsdb v0.10.0 // indirect
github.com/protolambda/zssz v0.1.5
github.com/prysmaticlabs/ethereumapis v0.0.0-20210105190001-13193818c0df
github.com/prysmaticlabs/ethereumapis v0.0.0-20210118163152-3569d231d255
github.com/prysmaticlabs/go-bitfield v0.0.0-20210107162333-9e9cf77d4921
github.com/prysmaticlabs/go-ssz v0.0.0-20200612203617-6d5c9aa213ae
github.com/prysmaticlabs/prombbolt v0.0.0-20200324184628-09789ef63796
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -1109,6 +1109,8 @@ github.com/prysmaticlabs/bazel-go-ethereum v0.0.0-20201126065335-1fb46e307951 h1
github.com/prysmaticlabs/bazel-go-ethereum v0.0.0-20201126065335-1fb46e307951/go.mod h1:JIfVb6esrqALTExdz9hRYvrP0xBDf6wCncIu1hNwHpM=
github.com/prysmaticlabs/ethereumapis v0.0.0-20210105190001-13193818c0df h1:69URSziUFhCVzHIDtPGMwYeP1G3JWhuBdB3enLRne6Y=
github.com/prysmaticlabs/ethereumapis v0.0.0-20210105190001-13193818c0df/go.mod h1:k7b2dxy6RppCG6kmOJkNOXzRpEoTdsPygc2aQhsUsZk=
github.com/prysmaticlabs/ethereumapis v0.0.0-20210118163152-3569d231d255 h1:jJEtD9/flUo1EzhLB1orQ6XfDFfwENl5VhQg0UVVh94=
github.com/prysmaticlabs/ethereumapis v0.0.0-20210118163152-3569d231d255/go.mod h1:k7b2dxy6RppCG6kmOJkNOXzRpEoTdsPygc2aQhsUsZk=
github.com/prysmaticlabs/go-bitfield v0.0.0-20200322041314-62c2aee71669 h1:cX6YRZnZ9sgMqM5U14llxUiXVNJ3u07Res1IIjTOgtI=
github.com/prysmaticlabs/go-bitfield v0.0.0-20200322041314-62c2aee71669/go.mod h1:hCwmef+4qXWjv0jLDbQdWnL0Ol7cS7/lCSS26WR+u6s=
github.com/prysmaticlabs/go-bitfield v0.0.0-20210107162333-9e9cf77d4921 h1:T86rPg76THT+xHx54zNu/4dgR3gWDm3VTdYocAj1NCE=
Expand Down

0 comments on commit 49ef829

Please sign in to comment.