2018-12-10 05:26:44 +00:00
|
|
|
package sync
|
2018-11-19 01:59:11 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-03-27 16:15:29 +00:00
|
|
|
"math/big"
|
2018-11-19 01:59:11 +00:00
|
|
|
"testing"
|
2019-02-21 06:57:04 +00:00
|
|
|
"time"
|
|
|
|
|
2019-03-27 16:15:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-02-26 05:37:28 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
|
2018-11-19 01:59:11 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2019-03-03 17:31:29 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/event"
|
2018-11-19 01:59:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/p2p"
|
2019-04-03 15:13:19 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2018-11-19 01:59:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
2019-02-21 06:57:04 +00:00
|
|
|
type genesisPowChain struct {
|
|
|
|
feed *event.Feed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mp *genesisPowChain) HasChainStartLogOccurred() (bool, uint64, error) {
|
|
|
|
return false, 0, nil
|
|
|
|
}
|
|
|
|
|
2019-03-27 16:15:29 +00:00
|
|
|
func (mp *genesisPowChain) BlockExists(ctx context.Context, hash common.Hash) (bool, *big.Int, error) {
|
|
|
|
return true, big.NewInt(0), nil
|
|
|
|
}
|
|
|
|
|
2019-02-21 06:57:04 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-03-27 16:15:29 +00:00
|
|
|
func (mp *afterGenesisPowChain) BlockExists(ctx context.Context, hash common.Hash) (bool, *big.Int, error) {
|
|
|
|
return true, big.NewInt(0), nil
|
|
|
|
}
|
|
|
|
|
2019-02-21 06:57:04 +00:00
|
|
|
func (mp *afterGenesisPowChain) ChainStartFeed() *event.Feed {
|
|
|
|
return mp.feed
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestQuerier_StartStop(t *testing.T) {
|
2018-11-19 01:59:11 +00:00
|
|
|
hook := logTest.NewGlobal()
|
2019-02-26 05:37:28 +00:00
|
|
|
db := internal.SetupDB(t)
|
|
|
|
defer internal.TeardownDB(t, db)
|
2018-12-10 05:26:44 +00:00
|
|
|
cfg := &QuerierConfig{
|
2018-11-19 01:59:11 +00:00
|
|
|
P2P: &mockP2P{},
|
|
|
|
ResponseBufferSize: 100,
|
2019-02-21 06:57:04 +00:00
|
|
|
PowChain: &afterGenesisPowChain{},
|
2019-02-26 05:37:28 +00:00
|
|
|
BeaconDB: db,
|
|
|
|
ChainService: &mockChainService{},
|
2018-11-19 01:59:11 +00:00
|
|
|
}
|
2018-12-10 05:26:44 +00:00
|
|
|
sq := NewQuerierService(context.Background(), cfg)
|
2018-11-19 01:59:11 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2019-02-26 05:37:28 +00:00
|
|
|
func TestListenForStateInitialization_ContextCancelled(t *testing.T) {
|
|
|
|
db := internal.SetupDB(t)
|
|
|
|
defer internal.TeardownDB(t, db)
|
2019-02-21 06:57:04 +00:00
|
|
|
cfg := &QuerierConfig{
|
|
|
|
P2P: &mockP2P{},
|
|
|
|
ResponseBufferSize: 100,
|
2019-02-26 05:37:28 +00:00
|
|
|
ChainService: &mockChainService{},
|
|
|
|
BeaconDB: db,
|
2019-02-21 06:57:04 +00:00
|
|
|
}
|
|
|
|
sq := NewQuerierService(context.Background(), cfg)
|
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
close(exitRoutine)
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2019-02-26 05:37:28 +00:00
|
|
|
sq.listenForStateInitialization()
|
2019-02-21 06:57:04 +00:00
|
|
|
exitRoutine <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
sq.cancel()
|
|
|
|
<-exitRoutine
|
|
|
|
|
|
|
|
if sq.ctx.Done() == nil {
|
|
|
|
t.Error("Despite context being cancelled, the done channel is nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 05:37:28 +00:00
|
|
|
func TestListenForStateInitialization(t *testing.T) {
|
|
|
|
db := internal.SetupDB(t)
|
|
|
|
defer internal.TeardownDB(t, db)
|
2019-02-21 06:57:04 +00:00
|
|
|
cfg := &QuerierConfig{
|
|
|
|
P2P: &mockP2P{},
|
|
|
|
ResponseBufferSize: 100,
|
2019-02-26 05:37:28 +00:00
|
|
|
ChainService: &mockChainService{},
|
|
|
|
BeaconDB: db,
|
2019-02-21 06:57:04 +00:00
|
|
|
}
|
|
|
|
sq := NewQuerierService(context.Background(), cfg)
|
|
|
|
|
|
|
|
sq.chainStartBuf <- time.Now()
|
2019-02-26 05:37:28 +00:00
|
|
|
sq.listenForStateInitialization()
|
2019-02-21 06:57:04 +00:00
|
|
|
|
|
|
|
if !sq.chainStarted {
|
|
|
|
t.Fatal("ChainStart in the querier service is not true despite the log being fired")
|
|
|
|
}
|
|
|
|
sq.cancel()
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestQuerier_ChainReqResponse(t *testing.T) {
|
2018-11-19 01:59:11 +00:00
|
|
|
hook := logTest.NewGlobal()
|
2018-12-10 05:26:44 +00:00
|
|
|
cfg := &QuerierConfig{
|
2018-11-19 01:59:11 +00:00
|
|
|
P2P: &mockP2P{},
|
|
|
|
ResponseBufferSize: 100,
|
2019-02-21 06:57:04 +00:00
|
|
|
PowChain: &afterGenesisPowChain{},
|
2018-11-19 01:59:11 +00:00
|
|
|
}
|
2018-12-10 05:26:44 +00:00
|
|
|
sq := NewQuerierService(context.Background(), cfg)
|
2018-11-19 01:59:11 +00:00
|
|
|
|
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
sq.run()
|
|
|
|
exitRoutine <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
response := &pb.ChainHeadResponse{
|
2019-04-05 03:39:51 +00:00
|
|
|
CanonicalSlot: 0,
|
|
|
|
CanonicalStateRootHash32: []byte{'a', 'b'},
|
2018-11-19 01:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := p2p.Message{
|
|
|
|
Data: response,
|
|
|
|
}
|
|
|
|
|
|
|
|
sq.responseBuf <- msg
|
|
|
|
|
2019-04-03 15:13:19 +00:00
|
|
|
expMsg := fmt.Sprintf(
|
2019-04-05 03:39:51 +00:00
|
|
|
"Latest chain head is at slot: %d and state root: %#x",
|
|
|
|
response.CanonicalSlot-params.BeaconConfig().GenesisSlot, response.CanonicalStateRootHash32,
|
2019-04-03 15:13:19 +00:00
|
|
|
)
|
2018-11-19 01:59:11 +00:00
|
|
|
|
|
|
|
<-exitRoutine
|
2019-04-03 15:13:19 +00:00
|
|
|
testutil.AssertLogsContain(t, hook, expMsg)
|
|
|
|
close(exitRoutine)
|
2018-11-19 01:59:11 +00:00
|
|
|
hook.Reset()
|
|
|
|
}
|