mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 23:48:06 -05:00
Remove deprecated RPC ListBlocks (#11243)
* Remove deprecated RPC ListBlocks * Fix test * Rm mock * Go imports * Rm unused Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
@@ -33,31 +33,6 @@ type blockContainer struct {
|
||||
isCanonical bool
|
||||
}
|
||||
|
||||
// ListBlocks retrieves blocks by root, slot, or epoch.
|
||||
//
|
||||
// The server may return multiple blocks in the case that a slot or epoch is
|
||||
// provided as the filter criteria. The server may return an empty list when
|
||||
// no blocks in their database match the filter criteria. This RPC should
|
||||
// not return NOT_FOUND. Only one filter criteria should be used.
|
||||
func (bs *Server) ListBlocks(
|
||||
ctx context.Context, req *ethpb.ListBlocksRequest,
|
||||
) (*ethpb.ListBlocksResponse, error) {
|
||||
ctrs, numBlks, nextPageToken, err := bs.listBlocks(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
blkContainers, err := convertToProto(ctrs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ðpb.ListBlocksResponse{
|
||||
BlockContainers: blkContainers,
|
||||
TotalSize: int32(numBlks),
|
||||
NextPageToken: nextPageToken,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ListBeaconBlocks retrieves blocks by root, slot, or epoch.
|
||||
//
|
||||
// The server may return multiple blocks in the case that a slot or epoch is
|
||||
@@ -272,23 +247,6 @@ func (bs *Server) listBlocksForGenesis(ctx context.Context, _ *ethpb.ListBlocksR
|
||||
}}, 1, strconv.Itoa(0), nil
|
||||
}
|
||||
|
||||
func convertToProto(ctrs []blockContainer) ([]*ethpb.BeaconBlockContainer, error) {
|
||||
protoCtrs := make([]*ethpb.BeaconBlockContainer, len(ctrs))
|
||||
for i, c := range ctrs {
|
||||
phBlk, err := c.blk.PbPhase0Block()
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Could not get phase 0 block: %v", err)
|
||||
}
|
||||
copiedRoot := c.root
|
||||
protoCtrs[i] = ðpb.BeaconBlockContainer{
|
||||
Block: ðpb.BeaconBlockContainer_Phase0Block{Phase0Block: phBlk},
|
||||
BlockRoot: copiedRoot[:],
|
||||
Canonical: c.isCanonical,
|
||||
}
|
||||
}
|
||||
return protoCtrs, nil
|
||||
}
|
||||
|
||||
// GetChainHead retrieves information about the head of the beacon chain from
|
||||
// the view of the beacon chain node.
|
||||
//
|
||||
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
statefeed "github.com/prysmaticlabs/prysm/v3/beacon-chain/core/feed/state"
|
||||
dbTest "github.com/prysmaticlabs/prysm/v3/beacon-chain/db/testing"
|
||||
v1 "github.com/prysmaticlabs/prysm/v3/beacon-chain/state/v1"
|
||||
"github.com/prysmaticlabs/prysm/v3/cmd"
|
||||
"github.com/prysmaticlabs/prysm/v3/config/features"
|
||||
fieldparams "github.com/prysmaticlabs/prysm/v3/config/fieldparams"
|
||||
"github.com/prysmaticlabs/prysm/v3/config/params"
|
||||
@@ -31,344 +30,6 @@ import (
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
)
|
||||
|
||||
func TestServer_ListBlocks_NoResults(t *testing.T) {
|
||||
db := dbTest.SetupDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
bs := &Server{
|
||||
BeaconDB: db,
|
||||
}
|
||||
wanted := ðpb.ListBlocksResponse{
|
||||
BlockContainers: make([]*ethpb.BeaconBlockContainer, 0),
|
||||
TotalSize: int32(0),
|
||||
NextPageToken: strconv.Itoa(0),
|
||||
}
|
||||
res, err := bs.ListBlocks(ctx, ðpb.ListBlocksRequest{
|
||||
QueryFilter: ðpb.ListBlocksRequest_Slot{
|
||||
Slot: 0,
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
if !proto.Equal(wanted, res) {
|
||||
t.Errorf("Wanted %v, received %v", wanted, res)
|
||||
}
|
||||
res, err = bs.ListBlocks(ctx, ðpb.ListBlocksRequest{
|
||||
QueryFilter: ðpb.ListBlocksRequest_Slot{
|
||||
Slot: 0,
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
if !proto.Equal(wanted, res) {
|
||||
t.Errorf("Wanted %v, received %v", wanted, res)
|
||||
}
|
||||
res, err = bs.ListBlocks(ctx, ðpb.ListBlocksRequest{
|
||||
QueryFilter: ðpb.ListBlocksRequest_Root{
|
||||
Root: make([]byte, fieldparams.RootLength),
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
if !proto.Equal(wanted, res) {
|
||||
t.Errorf("Wanted %v, received %v", wanted, res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServer_ListBlocks_Genesis(t *testing.T) {
|
||||
db := dbTest.SetupDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
bs := &Server{
|
||||
BeaconDB: db,
|
||||
}
|
||||
|
||||
// Should throw an error if no genesis block is found.
|
||||
_, err := bs.ListBlocks(ctx, ðpb.ListBlocksRequest{
|
||||
QueryFilter: ðpb.ListBlocksRequest_Genesis{
|
||||
Genesis: true,
|
||||
},
|
||||
})
|
||||
require.ErrorContains(t, "Could not find genesis", err)
|
||||
|
||||
// Should return the proper genesis block if it exists.
|
||||
parentRoot := [32]byte{'a'}
|
||||
blk := util.NewBeaconBlock()
|
||||
blk.Block.ParentRoot = parentRoot[:]
|
||||
root, err := blk.Block.HashTreeRoot()
|
||||
require.NoError(t, err)
|
||||
util.SaveBlock(t, ctx, db, blk)
|
||||
require.NoError(t, db.SaveGenesisBlockRoot(ctx, root))
|
||||
wanted := ðpb.ListBlocksResponse{
|
||||
BlockContainers: []*ethpb.BeaconBlockContainer{
|
||||
{
|
||||
Block: ðpb.BeaconBlockContainer_Phase0Block{Phase0Block: blk},
|
||||
BlockRoot: root[:],
|
||||
Canonical: true,
|
||||
},
|
||||
},
|
||||
NextPageToken: "0",
|
||||
TotalSize: 1,
|
||||
}
|
||||
res, err := bs.ListBlocks(ctx, ðpb.ListBlocksRequest{
|
||||
QueryFilter: ðpb.ListBlocksRequest_Genesis{
|
||||
Genesis: true,
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
if !proto.Equal(wanted, res) {
|
||||
t.Errorf("Wanted %v, received %v", wanted, res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServer_ListBlocks_Genesis_MultiBlocks(t *testing.T) {
|
||||
db := dbTest.SetupDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
bs := &Server{
|
||||
BeaconDB: db,
|
||||
}
|
||||
// Should return the proper genesis block if it exists.
|
||||
parentRoot := [32]byte{1, 2, 3}
|
||||
blk := util.NewBeaconBlock()
|
||||
blk.Block.ParentRoot = parentRoot[:]
|
||||
root, err := blk.Block.HashTreeRoot()
|
||||
require.NoError(t, err)
|
||||
util.SaveBlock(t, ctx, db, blk)
|
||||
require.NoError(t, db.SaveGenesisBlockRoot(ctx, root))
|
||||
|
||||
count := types.Slot(100)
|
||||
blks := make([]interfaces.SignedBeaconBlock, count)
|
||||
for i := types.Slot(0); i < count; i++ {
|
||||
b := util.NewBeaconBlock()
|
||||
b.Block.Slot = i
|
||||
require.NoError(t, err)
|
||||
blks[i], err = blocks.NewSignedBeaconBlock(b)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
require.NoError(t, db.SaveBlocks(ctx, blks))
|
||||
|
||||
// Should throw an error if more than one blk returned.
|
||||
_, err = bs.ListBlocks(ctx, ðpb.ListBlocksRequest{
|
||||
QueryFilter: ðpb.ListBlocksRequest_Genesis{
|
||||
Genesis: true,
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestServer_ListBlocks_Pagination(t *testing.T) {
|
||||
params.SetupTestConfigCleanup(t)
|
||||
params.OverrideBeaconConfig(params.MinimalSpecConfig())
|
||||
|
||||
db := dbTest.SetupDB(t)
|
||||
chain := &chainMock.ChainService{
|
||||
CanonicalRoots: map[[32]byte]bool{},
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
count := types.Slot(100)
|
||||
blks := make([]interfaces.SignedBeaconBlock, count)
|
||||
blkContainers := make([]*ethpb.BeaconBlockContainer, count)
|
||||
for i := types.Slot(0); i < count; i++ {
|
||||
b := util.NewBeaconBlock()
|
||||
b.Block.Slot = i
|
||||
root, err := b.Block.HashTreeRoot()
|
||||
require.NoError(t, err)
|
||||
chain.CanonicalRoots[root] = true
|
||||
blks[i], err = blocks.NewSignedBeaconBlock(b)
|
||||
require.NoError(t, err)
|
||||
blkContainers[i] = ðpb.BeaconBlockContainer{
|
||||
Block: ðpb.BeaconBlockContainer_Phase0Block{Phase0Block: b},
|
||||
BlockRoot: root[:],
|
||||
Canonical: true,
|
||||
}
|
||||
}
|
||||
require.NoError(t, db.SaveBlocks(ctx, blks))
|
||||
|
||||
orphanedBlk := util.NewBeaconBlock()
|
||||
orphanedBlk.Block.Slot = 300
|
||||
orphanedBlkRoot, err := orphanedBlk.Block.HashTreeRoot()
|
||||
require.NoError(t, err)
|
||||
util.SaveBlock(t, ctx, db, orphanedBlk)
|
||||
|
||||
bs := &Server{
|
||||
BeaconDB: db,
|
||||
CanonicalFetcher: chain,
|
||||
}
|
||||
|
||||
root6, err := blks[6].Block().HashTreeRoot()
|
||||
require.NoError(t, err)
|
||||
|
||||
tests := []struct {
|
||||
req *ethpb.ListBlocksRequest
|
||||
res *ethpb.ListBlocksResponse
|
||||
}{
|
||||
{req: ðpb.ListBlocksRequest{
|
||||
PageToken: strconv.Itoa(0),
|
||||
QueryFilter: ðpb.ListBlocksRequest_Slot{Slot: 5},
|
||||
PageSize: 3},
|
||||
res: ðpb.ListBlocksResponse{
|
||||
BlockContainers: []*ethpb.BeaconBlockContainer{
|
||||
{
|
||||
Block: ðpb.BeaconBlockContainer_Phase0Block{
|
||||
Phase0Block: util.HydrateSignedBeaconBlock(ðpb.SignedBeaconBlock{
|
||||
Block: ðpb.BeaconBlock{
|
||||
Slot: 5,
|
||||
},
|
||||
}),
|
||||
},
|
||||
BlockRoot: blkContainers[5].BlockRoot,
|
||||
Canonical: blkContainers[5].Canonical,
|
||||
},
|
||||
},
|
||||
NextPageToken: "",
|
||||
TotalSize: 1,
|
||||
},
|
||||
},
|
||||
{req: ðpb.ListBlocksRequest{
|
||||
PageToken: strconv.Itoa(0),
|
||||
QueryFilter: ðpb.ListBlocksRequest_Root{Root: root6[:]},
|
||||
PageSize: 3},
|
||||
res: ðpb.ListBlocksResponse{
|
||||
BlockContainers: []*ethpb.BeaconBlockContainer{
|
||||
{
|
||||
Block: ðpb.BeaconBlockContainer_Phase0Block{
|
||||
Phase0Block: util.HydrateSignedBeaconBlock(ðpb.SignedBeaconBlock{
|
||||
Block: ðpb.BeaconBlock{
|
||||
Slot: 6,
|
||||
},
|
||||
}),
|
||||
},
|
||||
BlockRoot: blkContainers[6].BlockRoot,
|
||||
Canonical: blkContainers[6].Canonical,
|
||||
},
|
||||
},
|
||||
TotalSize: 1,
|
||||
NextPageToken: strconv.Itoa(0),
|
||||
},
|
||||
},
|
||||
{req: ðpb.ListBlocksRequest{QueryFilter: ðpb.ListBlocksRequest_Root{Root: root6[:]}},
|
||||
res: ðpb.ListBlocksResponse{
|
||||
BlockContainers: []*ethpb.BeaconBlockContainer{
|
||||
{
|
||||
Block: ðpb.BeaconBlockContainer_Phase0Block{
|
||||
Phase0Block: util.HydrateSignedBeaconBlock(ðpb.SignedBeaconBlock{
|
||||
Block: ðpb.BeaconBlock{
|
||||
Slot: 6,
|
||||
},
|
||||
}),
|
||||
},
|
||||
BlockRoot: blkContainers[6].BlockRoot,
|
||||
Canonical: blkContainers[6].Canonical,
|
||||
},
|
||||
},
|
||||
TotalSize: 1,
|
||||
NextPageToken: strconv.Itoa(0),
|
||||
},
|
||||
},
|
||||
{req: ðpb.ListBlocksRequest{
|
||||
PageToken: strconv.Itoa(0),
|
||||
QueryFilter: ðpb.ListBlocksRequest_Epoch{Epoch: 0},
|
||||
PageSize: 100},
|
||||
res: ðpb.ListBlocksResponse{
|
||||
BlockContainers: blkContainers[0:params.BeaconConfig().SlotsPerEpoch],
|
||||
NextPageToken: "",
|
||||
TotalSize: int32(params.BeaconConfig().SlotsPerEpoch)}},
|
||||
{req: ðpb.ListBlocksRequest{
|
||||
PageToken: strconv.Itoa(1),
|
||||
QueryFilter: ðpb.ListBlocksRequest_Epoch{Epoch: 5},
|
||||
PageSize: 3},
|
||||
res: ðpb.ListBlocksResponse{
|
||||
BlockContainers: blkContainers[43:46],
|
||||
NextPageToken: "2",
|
||||
TotalSize: int32(params.BeaconConfig().SlotsPerEpoch)}},
|
||||
{req: ðpb.ListBlocksRequest{
|
||||
PageToken: strconv.Itoa(1),
|
||||
QueryFilter: ðpb.ListBlocksRequest_Epoch{Epoch: 11},
|
||||
PageSize: 7},
|
||||
res: ðpb.ListBlocksResponse{
|
||||
BlockContainers: blkContainers[95:96],
|
||||
NextPageToken: "",
|
||||
TotalSize: int32(params.BeaconConfig().SlotsPerEpoch)}},
|
||||
{req: ðpb.ListBlocksRequest{
|
||||
PageToken: strconv.Itoa(0),
|
||||
QueryFilter: ðpb.ListBlocksRequest_Epoch{Epoch: 12},
|
||||
PageSize: 4},
|
||||
res: ðpb.ListBlocksResponse{
|
||||
BlockContainers: blkContainers[96:100],
|
||||
NextPageToken: "",
|
||||
TotalSize: int32(params.BeaconConfig().SlotsPerEpoch / 2)}},
|
||||
{req: ðpb.ListBlocksRequest{
|
||||
PageToken: strconv.Itoa(0),
|
||||
QueryFilter: ðpb.ListBlocksRequest_Slot{Slot: 300},
|
||||
PageSize: 3},
|
||||
res: ðpb.ListBlocksResponse{
|
||||
BlockContainers: []*ethpb.BeaconBlockContainer{
|
||||
{
|
||||
Block: ðpb.BeaconBlockContainer_Phase0Block{
|
||||
Phase0Block: util.HydrateSignedBeaconBlock(ðpb.SignedBeaconBlock{
|
||||
Block: ðpb.BeaconBlock{
|
||||
Slot: 300,
|
||||
},
|
||||
}),
|
||||
},
|
||||
BlockRoot: orphanedBlkRoot[:],
|
||||
Canonical: false,
|
||||
},
|
||||
},
|
||||
NextPageToken: "",
|
||||
TotalSize: 1}},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) {
|
||||
res, err := bs.ListBlocks(ctx, test.req)
|
||||
require.NoError(t, err)
|
||||
require.DeepSSZEqual(t, res, test.res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestServer_ListBlocks_Errors(t *testing.T) {
|
||||
db := dbTest.SetupDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
bs := &Server{BeaconDB: db}
|
||||
exceedsMax := int32(cmd.Get().MaxRPCPageSize + 1)
|
||||
|
||||
wanted := fmt.Sprintf("Requested page size %d can not be greater than max size %d", exceedsMax, cmd.Get().MaxRPCPageSize)
|
||||
req := ðpb.ListBlocksRequest{PageToken: strconv.Itoa(0), PageSize: exceedsMax}
|
||||
_, err := bs.ListBlocks(ctx, req)
|
||||
assert.ErrorContains(t, wanted, err)
|
||||
|
||||
wanted = "Must specify a filter criteria for fetching"
|
||||
req = ðpb.ListBlocksRequest{}
|
||||
_, err = bs.ListBlocks(ctx, req)
|
||||
assert.ErrorContains(t, wanted, err)
|
||||
|
||||
req = ðpb.ListBlocksRequest{QueryFilter: ðpb.ListBlocksRequest_Slot{Slot: 0}}
|
||||
res, err := bs.ListBlocks(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, len(res.BlockContainers), "Wanted empty list")
|
||||
assert.Equal(t, int32(0), res.TotalSize, "Wanted total size 0")
|
||||
|
||||
req = ðpb.ListBlocksRequest{QueryFilter: ðpb.ListBlocksRequest_Slot{}}
|
||||
res, err = bs.ListBlocks(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, len(res.BlockContainers), "Wanted empty list")
|
||||
assert.Equal(t, int32(0), res.TotalSize, "Wanted total size 0")
|
||||
|
||||
req = ðpb.ListBlocksRequest{QueryFilter: ðpb.ListBlocksRequest_Root{Root: []byte{'A'}}}
|
||||
res, err = bs.ListBlocks(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, len(res.BlockContainers), "Wanted empty list")
|
||||
assert.Equal(t, int32(0), res.TotalSize, "Wanted total size 0")
|
||||
|
||||
req = ðpb.ListBlocksRequest{QueryFilter: ðpb.ListBlocksRequest_Root{Root: []byte{'A'}}}
|
||||
res, err = bs.ListBlocks(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, len(res.BlockContainers), "Wanted empty list")
|
||||
assert.Equal(t, int32(0), res.TotalSize, "Wanted total size 0")
|
||||
}
|
||||
|
||||
// ensures that if any of the checkpoints are zero-valued, an error will be generated without genesis being present
|
||||
func TestServer_GetChainHead_NoGenesis(t *testing.T) {
|
||||
db := dbTest.SetupDB(t)
|
||||
@@ -1192,47 +853,3 @@ func runListBeaconBlocksPagination(t *testing.T, orphanedBlk interfaces.SignedBe
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestServer_ListBeaconBlocks_Errors(t *testing.T) {
|
||||
db := dbTest.SetupDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
bs := &Server{
|
||||
BeaconDB: db,
|
||||
}
|
||||
exceedsMax := int32(cmd.Get().MaxRPCPageSize + 1)
|
||||
|
||||
wanted := fmt.Sprintf("Requested page size %d can not be greater than max size %d", exceedsMax, cmd.Get().MaxRPCPageSize)
|
||||
req := ðpb.ListBlocksRequest{PageToken: strconv.Itoa(0), PageSize: exceedsMax}
|
||||
_, err := bs.ListBlocks(ctx, req)
|
||||
assert.ErrorContains(t, wanted, err)
|
||||
|
||||
wanted = "Must specify a filter criteria for fetching"
|
||||
req = ðpb.ListBlocksRequest{}
|
||||
_, err = bs.ListBeaconBlocks(ctx, req)
|
||||
assert.ErrorContains(t, wanted, err)
|
||||
|
||||
req = ðpb.ListBlocksRequest{QueryFilter: ðpb.ListBlocksRequest_Slot{Slot: 0}}
|
||||
res, err := bs.ListBeaconBlocks(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, len(res.BlockContainers), "Wanted empty list")
|
||||
assert.Equal(t, int32(0), res.TotalSize, "Wanted total size 0")
|
||||
|
||||
req = ðpb.ListBlocksRequest{QueryFilter: ðpb.ListBlocksRequest_Slot{}}
|
||||
res, err = bs.ListBeaconBlocks(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, len(res.BlockContainers), "Wanted empty list")
|
||||
assert.Equal(t, int32(0), res.TotalSize, "Wanted total size 0")
|
||||
|
||||
req = ðpb.ListBlocksRequest{QueryFilter: ðpb.ListBlocksRequest_Root{Root: []byte{'A'}}}
|
||||
res, err = bs.ListBeaconBlocks(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, len(res.BlockContainers), "Wanted empty list")
|
||||
assert.Equal(t, int32(0), res.TotalSize, "Wanted total size 0")
|
||||
|
||||
req = ðpb.ListBlocksRequest{QueryFilter: ðpb.ListBlocksRequest_Root{Root: []byte{'A'}}}
|
||||
res, err = bs.ListBeaconBlocks(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, len(res.BlockContainers), "Wanted empty list")
|
||||
assert.Equal(t, int32(0), res.TotalSize, "Wanted total size 0")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user