2021-09-03 19:11:40 +00:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
chainMock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/altair"
|
|
|
|
"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"
|
|
|
|
dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2021-09-03 19:11:40 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/wrapper"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
2021-09-17 19:20:50 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/mock"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/util"
|
2021-09-03 19:11:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestServer_StreamAltairBlocksVerified_ContextCanceled(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
chainService := &chainMock.ChainService{}
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
server := &Server{
|
|
|
|
Ctx: ctx,
|
|
|
|
StateNotifier: chainService.StateNotifier(),
|
|
|
|
HeadFetcher: chainService,
|
|
|
|
}
|
|
|
|
|
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
mockStream := mock.NewMockBeaconNodeValidatorAltair_StreamBlocksServer(ctrl)
|
|
|
|
mockStream.EXPECT().Context().Return(ctx)
|
|
|
|
go func(tt *testing.T) {
|
|
|
|
assert.ErrorContains(tt, "Context canceled", server.StreamBlocksAltair(ðpb.StreamBlocksRequest{
|
|
|
|
VerifiedOnly: true,
|
|
|
|
}, mockStream))
|
|
|
|
<-exitRoutine
|
|
|
|
}(t)
|
|
|
|
cancel()
|
|
|
|
exitRoutine <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_StreamAltairBlocks_ContextCanceled(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
chainService := &chainMock.ChainService{}
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
server := &Server{
|
|
|
|
Ctx: ctx,
|
|
|
|
BlockNotifier: chainService.BlockNotifier(),
|
|
|
|
HeadFetcher: chainService,
|
|
|
|
}
|
|
|
|
|
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
mockStream := mock.NewMockBeaconNodeValidatorAltair_StreamBlocksServer(ctrl)
|
|
|
|
mockStream.EXPECT().Context().Return(ctx)
|
|
|
|
go func(tt *testing.T) {
|
|
|
|
assert.ErrorContains(tt, "Context canceled", server.StreamBlocksAltair(ðpb.StreamBlocksRequest{}, mockStream))
|
|
|
|
<-exitRoutine
|
|
|
|
}(t)
|
|
|
|
cancel()
|
|
|
|
exitRoutine <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_StreamAltairBlocks_OnHeadUpdated(t *testing.T) {
|
2021-12-08 19:24:16 +00:00
|
|
|
params.SetupTestConfigCleanup(t)
|
|
|
|
params.OverrideBeaconConfig(params.MainnetConfig())
|
2021-09-03 19:11:40 +00:00
|
|
|
ctx := context.Background()
|
2021-09-23 18:53:46 +00:00
|
|
|
beaconState, privs := util.DeterministicGenesisStateAltair(t, 64)
|
2021-09-03 19:11:40 +00:00
|
|
|
c, err := altair.NextSyncCommittee(ctx, beaconState)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, beaconState.SetCurrentSyncCommittee(c))
|
|
|
|
|
2021-09-23 18:53:46 +00:00
|
|
|
b, err := util.GenerateFullBlockAltair(beaconState, privs, util.DefaultBlockGenConfig(), 1)
|
2021-09-03 19:11:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
chainService := &chainMock.ChainService{State: beaconState}
|
|
|
|
server := &Server{
|
|
|
|
Ctx: ctx,
|
|
|
|
BlockNotifier: chainService.BlockNotifier(),
|
|
|
|
HeadFetcher: chainService,
|
|
|
|
}
|
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
mockStream := mock.NewMockBeaconNodeValidatorAltair_StreamBlocksServer(ctrl)
|
|
|
|
|
|
|
|
mockStream.EXPECT().Send(ðpb.StreamBlocksResponse{Block: ðpb.StreamBlocksResponse_AltairBlock{AltairBlock: b}}).Do(func(arg0 interface{}) {
|
|
|
|
exitRoutine <- true
|
|
|
|
})
|
|
|
|
mockStream.EXPECT().Context().Return(ctx).AnyTimes()
|
|
|
|
|
|
|
|
go func(tt *testing.T) {
|
|
|
|
assert.NoError(tt, server.StreamBlocksAltair(ðpb.StreamBlocksRequest{}, mockStream), "Could not call RPC method")
|
|
|
|
}(t)
|
2022-03-25 23:00:44 +00:00
|
|
|
wrappedBlk, err := wrapper.WrappedSignedBeaconBlock(b)
|
2021-09-03 19:11:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
// 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: wrappedBlk},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
<-exitRoutine
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_StreamAltairBlocksVerified_OnHeadUpdated(t *testing.T) {
|
2021-12-08 19:24:16 +00:00
|
|
|
params.SetupTestConfigCleanup(t)
|
2021-12-10 04:18:47 +00:00
|
|
|
params.OverrideBeaconConfig(params.MainnetConfig())
|
2021-09-03 19:11:40 +00:00
|
|
|
db := dbTest.SetupDB(t)
|
|
|
|
ctx := context.Background()
|
2021-09-23 18:53:46 +00:00
|
|
|
beaconState, privs := util.DeterministicGenesisStateAltair(t, 32)
|
2021-09-03 19:11:40 +00:00
|
|
|
c, err := altair.NextSyncCommittee(ctx, beaconState)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, beaconState.SetCurrentSyncCommittee(c))
|
|
|
|
|
2021-09-23 18:53:46 +00:00
|
|
|
b, err := util.GenerateFullBlockAltair(beaconState, privs, util.DefaultBlockGenConfig(), 1)
|
2021-09-03 19:11:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
r, err := b.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
2022-03-25 23:00:44 +00:00
|
|
|
wrappedBlk, err := wrapper.WrappedSignedBeaconBlock(b)
|
2021-09-03 19:11:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, db.SaveBlock(ctx, wrappedBlk))
|
|
|
|
chainService := &chainMock.ChainService{State: beaconState}
|
|
|
|
server := &Server{
|
|
|
|
Ctx: ctx,
|
|
|
|
StateNotifier: chainService.StateNotifier(),
|
|
|
|
HeadFetcher: chainService,
|
|
|
|
}
|
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
mockStream := mock.NewMockBeaconNodeValidatorAltair_StreamBlocksServer(ctrl)
|
|
|
|
mockStream.EXPECT().Send(ðpb.StreamBlocksResponse{Block: ðpb.StreamBlocksResponse_AltairBlock{AltairBlock: b}}).Do(func(arg0 interface{}) {
|
|
|
|
exitRoutine <- true
|
|
|
|
})
|
|
|
|
mockStream.EXPECT().Context().Return(ctx).AnyTimes()
|
|
|
|
|
|
|
|
go func(tt *testing.T) {
|
|
|
|
assert.NoError(tt, server.StreamBlocksAltair(ðpb.StreamBlocksRequest{
|
|
|
|
VerifiedOnly: true,
|
|
|
|
}, mockStream), "Could not call RPC method")
|
|
|
|
}(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{Slot: b.Block.Slot, BlockRoot: r, SignedBlock: wrappedBlk},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
<-exitRoutine
|
|
|
|
}
|