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 21, 2021
1 parent 9ec4f72 commit f7a8903
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 16 deletions.
22 changes: 21 additions & 1 deletion beacon-chain/rpc/beacon/blocks.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -2573,8 +2573,8 @@ def prysm_deps():
name = "com_github_prysmaticlabs_ethereumapis",
build_file_generation = "off",
importpath = "github.com/prysmaticlabs/ethereumapis",
sum = "h1:roqXwVG8cKjq6sOCbB+T5Kh+dYr1wpkk00c7/DdrqLg=",
version = "v0.0.0-20210115110118-c595a4e0c0a5",
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
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ require (
github.com/prestonvanloon/go-recaptcha v0.0.0-20190217191114-0834cef6e8bd
github.com/prometheus/client_golang v1.9.0
github.com/prometheus/tsdb v0.10.0 // indirect
github.com/prysmaticlabs/ethereumapis v0.0.0-20210115110118-c595a4e0c0a5
github.com/prysmaticlabs/ethereumapis v0.0.0-20210118163152-3569d231d255
github.com/prysmaticlabs/go-bitfield v0.0.0-20210120104942-c3214972eb2e
github.com/prysmaticlabs/prombbolt v0.0.0-20200324184628-09789ef63796
github.com/rs/cors v1.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1105,8 +1105,8 @@ github.com/prometheus/tsdb v0.10.0 h1:If5rVCMTp6W2SiRAQFlbpJNgVlgMEd+U2GZckwK38i
github.com/prometheus/tsdb v0.10.0/go.mod h1:oi49uRhEe9dPUTlS3JRZOwJuVi6tmh10QSgwXEyGCt4=
github.com/prysmaticlabs/bazel-go-ethereum v0.0.0-20201126065335-1fb46e307951 h1:Jncuyb/nIJgXbEe0iGz3MN5JmijPVGzwk3G5FR01phI=
github.com/prysmaticlabs/bazel-go-ethereum v0.0.0-20201126065335-1fb46e307951/go.mod h1:JIfVb6esrqALTExdz9hRYvrP0xBDf6wCncIu1hNwHpM=
github.com/prysmaticlabs/ethereumapis v0.0.0-20210115110118-c595a4e0c0a5 h1:roqXwVG8cKjq6sOCbB+T5Kh+dYr1wpkk00c7/DdrqLg=
github.com/prysmaticlabs/ethereumapis v0.0.0-20210115110118-c595a4e0c0a5/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-20210120104942-c3214972eb2e h1:teHQyJxTD1ZOLmdnIgNYFBruxZX9Cjo/NlSU2AmumwI=
Expand Down

0 comments on commit f7a8903

Please sign in to comment.