prysm-pulse/beacon-chain/sync/querier_test.go
Raul Jordan eb900a8193
Refactor Initial Sync, Enable Catching Up to Chain (#2111)
* refactor initial sync to prevent reorg infinite loops

* lint

* fixed build

* passing tests

* tests passing

* terence suggestion

* new attempt

* clean up and refactor sync service

* complete the new initial sync logic

* revert head

* init sync working

* config for blockchain receive block

* all works

* builds

* fix a few more tests

* init sync tests pass

* revert scripts

* revert accounts changes

* lint

* lint2

* travis lint

* fix build

* fix single use argument

* any peer

* imports spacing

* imports

* ready for a rolling restart

* add todo

* fork choice in blocks when exiting sync

* readd finalized state root to requests

* successful build

* revert blockchain config

* old config reversion

* initial sync tests pass

* initial sync full test works

* lint

* use the new block processing api

* new proto defs

* init sync functions again

* remove sync polling

* tests fixed

* fixed catching up with chain

* tests pass

* spacing

* lint

* goimports

* add changes

* add lock and conditional to prevent multiple goroutines

* make reg sync synchronous

* add

* fixed the parent block issue

* fix errors in chain service

* tests pass

* check nil block

* typo

* fix nil state

* merge & conflicts

* revert synchronus reg sync

* add more spans to state db

* fix lint

* lint
2019-04-03 10:13:19 -05:00

169 lines
3.6 KiB
Go

package sync
import (
"context"
"fmt"
"math/big"
"testing"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/event"
"github.com/prysmaticlabs/prysm/shared/p2p"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
logTest "github.com/sirupsen/logrus/hooks/test"
)
type genesisPowChain struct {
feed *event.Feed
}
func (mp *genesisPowChain) HasChainStartLogOccurred() (bool, uint64, error) {
return false, 0, nil
}
func (mp *genesisPowChain) BlockExists(ctx context.Context, hash common.Hash) (bool, *big.Int, error) {
return true, big.NewInt(0), nil
}
func (mp *genesisPowChain) ChainStartFeed() *event.Feed {
return mp.feed
}
type afterGenesisPowChain struct {
feed *event.Feed
}
func (mp *afterGenesisPowChain) HasChainStartLogOccurred() (bool, uint64, error) {
return true, 0, nil
}
func (mp *afterGenesisPowChain) BlockExists(ctx context.Context, hash common.Hash) (bool, *big.Int, error) {
return true, big.NewInt(0), nil
}
func (mp *afterGenesisPowChain) ChainStartFeed() *event.Feed {
return mp.feed
}
func TestQuerier_StartStop(t *testing.T) {
hook := logTest.NewGlobal()
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
cfg := &QuerierConfig{
P2P: &mockP2P{},
ResponseBufferSize: 100,
PowChain: &afterGenesisPowChain{},
BeaconDB: db,
ChainService: &mockChainService{},
}
sq := NewQuerierService(context.Background(), cfg)
exitRoutine := make(chan bool)
defer func() {
close(exitRoutine)
}()
go func() {
sq.Start()
exitRoutine <- true
}()
sq.Stop()
<-exitRoutine
testutil.AssertLogsContain(t, hook, "Stopping service")
hook.Reset()
}
func TestListenForStateInitialization_ContextCancelled(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
cfg := &QuerierConfig{
P2P: &mockP2P{},
ResponseBufferSize: 100,
ChainService: &mockChainService{},
BeaconDB: db,
}
sq := NewQuerierService(context.Background(), cfg)
exitRoutine := make(chan bool)
defer func() {
close(exitRoutine)
}()
go func() {
sq.listenForStateInitialization()
exitRoutine <- true
}()
sq.cancel()
<-exitRoutine
if sq.ctx.Done() == nil {
t.Error("Despite context being cancelled, the done channel is nil")
}
}
func TestListenForStateInitialization(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
cfg := &QuerierConfig{
P2P: &mockP2P{},
ResponseBufferSize: 100,
ChainService: &mockChainService{},
BeaconDB: db,
}
sq := NewQuerierService(context.Background(), cfg)
sq.chainStartBuf <- time.Now()
sq.listenForStateInitialization()
if !sq.chainStarted {
t.Fatal("ChainStart in the querier service is not true despite the log being fired")
}
sq.cancel()
}
func TestQuerier_ChainReqResponse(t *testing.T) {
hook := logTest.NewGlobal()
cfg := &QuerierConfig{
P2P: &mockP2P{},
ResponseBufferSize: 100,
PowChain: &afterGenesisPowChain{},
}
sq := NewQuerierService(context.Background(), cfg)
exitRoutine := make(chan bool)
go func() {
sq.run()
exitRoutine <- true
}()
response := &pb.ChainHeadResponse{
Slot: 0,
Hash: []byte{'a', 'b'},
}
msg := p2p.Message{
Data: response,
}
sq.responseBuf <- msg
expMsg := fmt.Sprintf(
"Latest chain head is at slot: %d and hash %#x",
response.Slot-params.BeaconConfig().GenesisSlot, response.Hash,
)
<-exitRoutine
testutil.AssertLogsContain(t, hook, expMsg)
close(exitRoutine)
hook.Reset()
}