mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-28 14:17:17 +00:00
9be8a456eb
* Sweeping replace of ssz.HashTreeRoot(block) to stateutil.BlockRoot, minor tweaks to critical path of proposeBlock * imports * Merge branch 'master' into better-block-htr * Add --enable-custom-block-htr to e2e flags * Merge branch 'better-block-htr' of github.com:prysmaticlabs/prysm into better-block-htr
682 lines
20 KiB
Go
682 lines
20 KiB
Go
package beacon
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
ptypes "github.com/gogo/protobuf/types"
|
|
"github.com/golang/mock/gomock"
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/feed"
|
|
blockfeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/block"
|
|
statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/flags"
|
|
mockRPC "github.com/prysmaticlabs/prysm/beacon-chain/rpc/testing"
|
|
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
|
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
)
|
|
|
|
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,
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(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,
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(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, 32),
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(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.
|
|
if _, err := bs.ListBlocks(ctx, ðpb.ListBlocksRequest{
|
|
QueryFilter: ðpb.ListBlocksRequest_Genesis{
|
|
Genesis: true,
|
|
},
|
|
}); err != nil && !strings.Contains(err.Error(), "Could not find genesis") {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Should return the proper genesis block if it exists.
|
|
parentRoot := [32]byte{1, 2, 3}
|
|
blk := ðpb.SignedBeaconBlock{
|
|
Block: ðpb.BeaconBlock{
|
|
Slot: 0,
|
|
ParentRoot: parentRoot[:],
|
|
},
|
|
}
|
|
root, err := stateutil.BlockRoot(blk.Block)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.SaveBlock(ctx, blk); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.SaveGenesisBlockRoot(ctx, root); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wanted := ðpb.ListBlocksResponse{
|
|
BlockContainers: []*ethpb.BeaconBlockContainer{
|
|
{
|
|
Block: blk,
|
|
BlockRoot: root[:],
|
|
},
|
|
},
|
|
NextPageToken: "0",
|
|
TotalSize: 1,
|
|
}
|
|
res, err := bs.ListBlocks(ctx, ðpb.ListBlocksRequest{
|
|
QueryFilter: ðpb.ListBlocksRequest_Genesis{
|
|
Genesis: true,
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(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 := ðpb.SignedBeaconBlock{
|
|
Block: ðpb.BeaconBlock{
|
|
Slot: 0,
|
|
ParentRoot: parentRoot[:],
|
|
},
|
|
}
|
|
root, err := stateutil.BlockRoot(blk.Block)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.SaveBlock(ctx, blk); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.SaveGenesisBlockRoot(ctx, root); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
count := uint64(100)
|
|
blks := make([]*ethpb.SignedBeaconBlock, count)
|
|
blkContainers := make([]*ethpb.BeaconBlockContainer, count)
|
|
for i := uint64(0); i < count; i++ {
|
|
b := ðpb.SignedBeaconBlock{
|
|
Block: ðpb.BeaconBlock{
|
|
Slot: i,
|
|
},
|
|
}
|
|
root, err := stateutil.BlockRoot(b.Block)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
blks[i] = b
|
|
blkContainers[i] = ðpb.BeaconBlockContainer{Block: b, BlockRoot: root[:]}
|
|
}
|
|
if err := db.SaveBlocks(ctx, blks); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Should throw an error if more than one blk returned.
|
|
if _, err := bs.ListBlocks(ctx, ðpb.ListBlocksRequest{
|
|
QueryFilter: ðpb.ListBlocksRequest_Genesis{
|
|
Genesis: true,
|
|
},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestServer_ListBlocks_Pagination(t *testing.T) {
|
|
db := dbTest.SetupDB(t)
|
|
ctx := context.Background()
|
|
|
|
count := uint64(100)
|
|
blks := make([]*ethpb.SignedBeaconBlock, count)
|
|
blkContainers := make([]*ethpb.BeaconBlockContainer, count)
|
|
for i := uint64(0); i < count; i++ {
|
|
b := ðpb.SignedBeaconBlock{
|
|
Block: ðpb.BeaconBlock{
|
|
Slot: i,
|
|
},
|
|
}
|
|
root, err := stateutil.BlockRoot(b.Block)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
blks[i] = b
|
|
blkContainers[i] = ðpb.BeaconBlockContainer{Block: b, BlockRoot: root[:]}
|
|
}
|
|
if err := db.SaveBlocks(ctx, blks); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
bs := &Server{
|
|
BeaconDB: db,
|
|
}
|
|
|
|
root6, err := stateutil.BlockRoot(blks[6].Block)
|
|
if err != nil {
|
|
t.Fatal(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.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 5}}, BlockRoot: blkContainers[5].BlockRoot}},
|
|
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.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 6}}, BlockRoot: blkContainers[6].BlockRoot}},
|
|
TotalSize: 1}},
|
|
{req: ðpb.ListBlocksRequest{QueryFilter: ðpb.ListBlocksRequest_Root{Root: root6[:]}},
|
|
res: ðpb.ListBlocksResponse{
|
|
BlockContainers: []*ethpb.BeaconBlockContainer{{Block: ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 6}}, BlockRoot: blkContainers[6].BlockRoot}},
|
|
TotalSize: 1}},
|
|
{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: "1",
|
|
TotalSize: int32(params.BeaconConfig().SlotsPerEpoch / 2)}},
|
|
}
|
|
|
|
for i, test := range tests {
|
|
t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) {
|
|
res, err := bs.ListBlocks(ctx, test.req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !proto.Equal(res, test.res) {
|
|
t.Errorf("Incorrect blocks response, wanted %v, received %v", test.res, res)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestServer_ListBlocks_Errors(t *testing.T) {
|
|
db := dbTest.SetupDB(t)
|
|
ctx := context.Background()
|
|
|
|
bs := &Server{BeaconDB: db}
|
|
exceedsMax := int32(flags.Get().MaxPageSize + 1)
|
|
|
|
wanted := fmt.Sprintf("Requested page size %d can not be greater than max size %d", exceedsMax, flags.Get().MaxPageSize)
|
|
req := ðpb.ListBlocksRequest{PageToken: strconv.Itoa(0), PageSize: exceedsMax}
|
|
if _, err := bs.ListBlocks(ctx, req); !strings.Contains(err.Error(), wanted) {
|
|
t.Errorf("Expected error %v, received %v", wanted, err)
|
|
}
|
|
|
|
wanted = "Must specify a filter criteria for fetching"
|
|
req = ðpb.ListBlocksRequest{}
|
|
if _, err := bs.ListBlocks(ctx, req); !strings.Contains(err.Error(), wanted) {
|
|
t.Errorf("Expected error %v, received %v", wanted, err)
|
|
}
|
|
|
|
req = ðpb.ListBlocksRequest{QueryFilter: ðpb.ListBlocksRequest_Slot{Slot: 0}}
|
|
res, err := bs.ListBlocks(ctx, req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(res.BlockContainers) != 0 {
|
|
t.Errorf("wanted empty list, got a list of %d", len(res.BlockContainers))
|
|
}
|
|
if res.TotalSize != 0 {
|
|
t.Errorf("wanted total size 0, got size %d", res.TotalSize)
|
|
}
|
|
|
|
req = ðpb.ListBlocksRequest{QueryFilter: ðpb.ListBlocksRequest_Slot{}}
|
|
res, err = bs.ListBlocks(ctx, req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(res.BlockContainers) != 0 {
|
|
t.Errorf("wanted empty list, got a list of %d", len(res.BlockContainers))
|
|
}
|
|
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.BlockContainers) != 0 {
|
|
t.Errorf("wanted empty list, got a list of %d", len(res.BlockContainers))
|
|
}
|
|
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.BlockContainers) != 0 {
|
|
t.Errorf("wanted empty list, got a list of %d", len(res.BlockContainers))
|
|
}
|
|
if res.TotalSize != 0 {
|
|
t.Errorf("wanted total size 0, got size %d", res.TotalSize)
|
|
|
|
}
|
|
}
|
|
|
|
func TestServer_GetChainHead_NoFinalizedBlock(t *testing.T) {
|
|
db := dbTest.SetupDB(t)
|
|
|
|
s, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{
|
|
Slot: 1,
|
|
PreviousJustifiedCheckpoint: ðpb.Checkpoint{Epoch: 3, Root: []byte{'A'}},
|
|
CurrentJustifiedCheckpoint: ðpb.Checkpoint{Epoch: 2, Root: []byte{'B'}},
|
|
FinalizedCheckpoint: ðpb.Checkpoint{Epoch: 1, Root: []byte{'C'}},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
bs := &Server{
|
|
BeaconDB: db,
|
|
HeadFetcher: &mock.ChainService{Block: ðpb.SignedBeaconBlock{}, State: s},
|
|
FinalizationFetcher: &mock.ChainService{
|
|
FinalizedCheckPoint: s.FinalizedCheckpoint(),
|
|
CurrentJustifiedCheckPoint: s.CurrentJustifiedCheckpoint(),
|
|
PreviousJustifiedCheckPoint: s.PreviousJustifiedCheckpoint()},
|
|
}
|
|
|
|
if _, err := bs.GetChainHead(context.Background(), nil); !strings.Contains(err.Error(), "Could not get finalized block") {
|
|
t.Fatal("Did not get wanted error")
|
|
}
|
|
}
|
|
|
|
func TestServer_GetChainHead_NoHeadBlock(t *testing.T) {
|
|
bs := &Server{
|
|
HeadFetcher: &mock.ChainService{Block: nil},
|
|
}
|
|
if _, err := bs.GetChainHead(context.Background(), nil); err != nil && !strings.Contains(
|
|
err.Error(),
|
|
"Head block of chain was nil",
|
|
) {
|
|
t.Fatal("Did not get wanted error")
|
|
} else if err == nil {
|
|
t.Error("Expected error, received nil")
|
|
}
|
|
}
|
|
|
|
func TestServer_GetChainHead(t *testing.T) {
|
|
db := dbTest.SetupDB(t)
|
|
|
|
finalizedBlock := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1, ParentRoot: []byte{'A'}}}
|
|
if err := db.SaveBlock(context.Background(), finalizedBlock); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fRoot, err := stateutil.BlockRoot(finalizedBlock.Block)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
justifiedBlock := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 2, ParentRoot: []byte{'B'}}}
|
|
if err := db.SaveBlock(context.Background(), justifiedBlock); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
jRoot, err := stateutil.BlockRoot(justifiedBlock.Block)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
prevJustifiedBlock := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 3, ParentRoot: []byte{'C'}}}
|
|
if err := db.SaveBlock(context.Background(), prevJustifiedBlock); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pjRoot, err := stateutil.BlockRoot(prevJustifiedBlock.Block)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
s, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{
|
|
Slot: 1,
|
|
PreviousJustifiedCheckpoint: ðpb.Checkpoint{Epoch: 3, Root: pjRoot[:]},
|
|
CurrentJustifiedCheckpoint: ðpb.Checkpoint{Epoch: 2, Root: jRoot[:]},
|
|
FinalizedCheckpoint: ðpb.Checkpoint{Epoch: 1, Root: fRoot[:]},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: s.PreviousJustifiedCheckpoint().Epoch*params.BeaconConfig().SlotsPerEpoch + 1}}
|
|
bs := &Server{
|
|
BeaconDB: db,
|
|
HeadFetcher: &mock.ChainService{Block: b, State: s},
|
|
FinalizationFetcher: &mock.ChainService{
|
|
FinalizedCheckPoint: s.FinalizedCheckpoint(),
|
|
CurrentJustifiedCheckPoint: s.CurrentJustifiedCheckpoint(),
|
|
PreviousJustifiedCheckPoint: s.PreviousJustifiedCheckpoint()},
|
|
}
|
|
|
|
head, err := bs.GetChainHead(context.Background(), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if head.PreviousJustifiedEpoch != 3 {
|
|
t.Errorf("Wanted PreviousJustifiedEpoch: %d, got: %d",
|
|
3*params.BeaconConfig().SlotsPerEpoch, head.PreviousJustifiedEpoch)
|
|
}
|
|
if head.JustifiedEpoch != 2 {
|
|
t.Errorf("Wanted JustifiedEpoch: %d, got: %d",
|
|
2*params.BeaconConfig().SlotsPerEpoch, head.JustifiedEpoch)
|
|
}
|
|
if head.FinalizedEpoch != 1 {
|
|
t.Errorf("Wanted FinalizedEpoch: %d, got: %d",
|
|
1*params.BeaconConfig().SlotsPerEpoch, head.FinalizedEpoch)
|
|
}
|
|
if head.PreviousJustifiedSlot != 3 {
|
|
t.Errorf("Wanted PreviousJustifiedSlot: %d, got: %d",
|
|
3, head.PreviousJustifiedSlot)
|
|
}
|
|
if head.JustifiedSlot != 2 {
|
|
t.Errorf("Wanted JustifiedSlot: %d, got: %d",
|
|
2, head.JustifiedSlot)
|
|
}
|
|
if head.FinalizedSlot != 1 {
|
|
t.Errorf("Wanted FinalizedSlot: %d, got: %d",
|
|
1, head.FinalizedSlot)
|
|
}
|
|
if !bytes.Equal(pjRoot[:], head.PreviousJustifiedBlockRoot) {
|
|
t.Errorf("Wanted PreviousJustifiedBlockRoot: %v, got: %v",
|
|
pjRoot[:], head.PreviousJustifiedBlockRoot)
|
|
}
|
|
if !bytes.Equal(jRoot[:], head.JustifiedBlockRoot) {
|
|
t.Errorf("Wanted JustifiedBlockRoot: %v, got: %v",
|
|
jRoot[:], head.JustifiedBlockRoot)
|
|
}
|
|
if !bytes.Equal(fRoot[:], head.FinalizedBlockRoot) {
|
|
t.Errorf("Wanted FinalizedBlockRoot: %v, got: %v",
|
|
fRoot[:], head.FinalizedBlockRoot)
|
|
}
|
|
}
|
|
|
|
func TestServer_StreamChainHead_ContextCanceled(t *testing.T) {
|
|
db := dbTest.SetupDB(t)
|
|
ctx := context.Background()
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
chainService := &mock.ChainService{}
|
|
server := &Server{
|
|
Ctx: ctx,
|
|
StateNotifier: chainService.StateNotifier(),
|
|
BeaconDB: db,
|
|
}
|
|
|
|
exitRoutine := make(chan bool)
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
mockStream := mockRPC.NewMockBeaconChain_StreamChainHeadServer(ctrl)
|
|
mockStream.EXPECT().Context().Return(ctx)
|
|
go func(tt *testing.T) {
|
|
if err := server.StreamChainHead(&ptypes.Empty{}, mockStream); !strings.Contains(err.Error(), "Context canceled") {
|
|
tt.Errorf("Could not call RPC method: %v", err)
|
|
}
|
|
<-exitRoutine
|
|
}(t)
|
|
cancel()
|
|
exitRoutine <- true
|
|
}
|
|
|
|
func TestServer_StreamChainHead_OnHeadUpdated(t *testing.T) {
|
|
db := dbTest.SetupDB(t)
|
|
|
|
finalizedBlock := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1, ParentRoot: []byte{'A'}}}
|
|
if err := db.SaveBlock(context.Background(), finalizedBlock); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fRoot, err := stateutil.BlockRoot(finalizedBlock.Block)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
justifiedBlock := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 2, ParentRoot: []byte{'B'}}}
|
|
if err := db.SaveBlock(context.Background(), justifiedBlock); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
jRoot, err := stateutil.BlockRoot(justifiedBlock.Block)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
prevJustifiedBlock := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 3, ParentRoot: []byte{'C'}}}
|
|
if err := db.SaveBlock(context.Background(), prevJustifiedBlock); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pjRoot, err := stateutil.BlockRoot(prevJustifiedBlock.Block)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
s, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{
|
|
Slot: 1,
|
|
PreviousJustifiedCheckpoint: ðpb.Checkpoint{Epoch: 3, Root: pjRoot[:]},
|
|
CurrentJustifiedCheckpoint: ðpb.Checkpoint{Epoch: 2, Root: jRoot[:]},
|
|
FinalizedCheckpoint: ðpb.Checkpoint{Epoch: 1, Root: fRoot[:]},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: s.PreviousJustifiedCheckpoint().Epoch*params.BeaconConfig().SlotsPerEpoch + 1}}
|
|
hRoot, err := stateutil.BlockRoot(b.Block)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
chainService := &mock.ChainService{}
|
|
ctx := context.Background()
|
|
server := &Server{
|
|
Ctx: ctx,
|
|
HeadFetcher: &mock.ChainService{Block: b, State: s},
|
|
BeaconDB: db,
|
|
StateNotifier: chainService.StateNotifier(),
|
|
FinalizationFetcher: &mock.ChainService{
|
|
FinalizedCheckPoint: s.FinalizedCheckpoint(),
|
|
CurrentJustifiedCheckPoint: s.CurrentJustifiedCheckpoint(),
|
|
PreviousJustifiedCheckPoint: s.PreviousJustifiedCheckpoint()},
|
|
}
|
|
exitRoutine := make(chan bool)
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
mockStream := mockRPC.NewMockBeaconChain_StreamChainHeadServer(ctrl)
|
|
mockStream.EXPECT().Send(
|
|
ðpb.ChainHead{
|
|
HeadSlot: b.Block.Slot,
|
|
HeadEpoch: helpers.SlotToEpoch(b.Block.Slot),
|
|
HeadBlockRoot: hRoot[:],
|
|
FinalizedSlot: 1,
|
|
FinalizedEpoch: 1,
|
|
FinalizedBlockRoot: fRoot[:],
|
|
JustifiedSlot: 2,
|
|
JustifiedEpoch: 2,
|
|
JustifiedBlockRoot: jRoot[:],
|
|
PreviousJustifiedSlot: 3,
|
|
PreviousJustifiedEpoch: 3,
|
|
PreviousJustifiedBlockRoot: pjRoot[:],
|
|
},
|
|
).Do(func(arg0 interface{}) {
|
|
exitRoutine <- true
|
|
})
|
|
mockStream.EXPECT().Context().Return(ctx).AnyTimes()
|
|
|
|
go func(tt *testing.T) {
|
|
if err := server.StreamChainHead(&ptypes.Empty{}, mockStream); err != nil {
|
|
tt.Errorf("Could not call RPC method: %v", err)
|
|
}
|
|
}(t)
|
|
|
|
// Send in a loop to ensure it is delivered (busy wait for the service to subscribe to the state feed).
|
|
for sent := 0; sent == 0; {
|
|
sent = server.StateNotifier.StateFeed().Send(&feed.Event{
|
|
Type: statefeed.BlockProcessed,
|
|
Data: &statefeed.BlockProcessedData{},
|
|
})
|
|
}
|
|
<-exitRoutine
|
|
}
|
|
|
|
func TestServer_StreamBlocks_ContextCanceled(t *testing.T) {
|
|
db := dbTest.SetupDB(t)
|
|
ctx := context.Background()
|
|
|
|
chainService := &mock.ChainService{}
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
server := &Server{
|
|
Ctx: ctx,
|
|
BlockNotifier: chainService.BlockNotifier(),
|
|
BeaconDB: db,
|
|
}
|
|
|
|
exitRoutine := make(chan bool)
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
mockStream := mockRPC.NewMockBeaconChain_StreamBlocksServer(ctrl)
|
|
mockStream.EXPECT().Context().Return(ctx)
|
|
go func(tt *testing.T) {
|
|
if err := server.StreamBlocks(&ptypes.Empty{}, mockStream); !strings.Contains(err.Error(), "Context canceled") {
|
|
tt.Errorf("Could not call RPC method: %v", err)
|
|
}
|
|
<-exitRoutine
|
|
}(t)
|
|
cancel()
|
|
exitRoutine <- true
|
|
}
|
|
|
|
func TestServer_StreamBlocks_OnHeadUpdated(t *testing.T) {
|
|
b := ðpb.SignedBeaconBlock{
|
|
Block: ðpb.BeaconBlock{
|
|
Slot: 1,
|
|
},
|
|
}
|
|
|
|
chainService := &mock.ChainService{}
|
|
ctx := context.Background()
|
|
server := &Server{
|
|
Ctx: ctx,
|
|
BlockNotifier: chainService.BlockNotifier(),
|
|
}
|
|
exitRoutine := make(chan bool)
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
mockStream := mockRPC.NewMockBeaconChain_StreamBlocksServer(ctrl)
|
|
mockStream.EXPECT().Send(b).Do(func(arg0 interface{}) {
|
|
exitRoutine <- true
|
|
})
|
|
mockStream.EXPECT().Context().Return(ctx).AnyTimes()
|
|
|
|
go func(tt *testing.T) {
|
|
if err := server.StreamBlocks(&ptypes.Empty{}, mockStream); err != nil {
|
|
tt.Errorf("Could not call RPC method: %v", err)
|
|
}
|
|
}(t)
|
|
|
|
// Send in a loop to ensure it is delivered (busy wait for the service to subscribe to the state feed).
|
|
for sent := 0; sent == 0; {
|
|
sent = server.BlockNotifier.BlockFeed().Send(&feed.Event{
|
|
Type: blockfeed.ReceivedBlock,
|
|
Data: &blockfeed.ReceivedBlockData{SignedBlock: b},
|
|
})
|
|
}
|
|
<-exitRoutine
|
|
}
|