Test Syncing with Another Fully Synced Service (#1594)

* clean up test

* adding test setup

* add chainstart check to sync querier

* goimports

* goimports

* backend cleanup

* adding getters

* lint

* moving to db

* gazelle

* adding more services setup

* adding more changes

* adding different test setups

* other merge issues

* imports

* fixing keys

* get intial sync able to be set up

* new changes and gazelle

* use simulated p2p

* everything finally works

* unexport fields

* revert changes from merge

* remove mock server

* add documentation

* gazelle

* add another node

* review comments

* fix tests
This commit is contained in:
Nishant Das
2019-02-23 11:36:20 +05:30
committed by GitHub
parent e118713a2f
commit f5c88e1bcb
34 changed files with 365 additions and 128 deletions

View File

@@ -7,11 +7,10 @@ import (
"strings"
"testing"
"github.com/prysmaticlabs/prysm/shared/ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/ssz"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/sirupsen/logrus"
logTest "github.com/sirupsen/logrus/hooks/test"

View File

@@ -6,8 +6,6 @@ import (
"testing"
"time"
"github.com/prysmaticlabs/prysm/shared/ssz"
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
@@ -15,6 +13,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/ssz"
)
// Generates an initial genesis block and state using a custom number of initial

View File

@@ -5,13 +5,12 @@ go_library(
srcs = [
"fork_choice_test_format.go",
"helpers.go",
"setup_db.go",
"shuffle_test_format.go",
"simulated_backend.go",
"state_test_format.go",
],
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/chaintest/backend",
visibility = ["//beacon-chain/chaintest:__subpackages__"],
visibility = ["//beacon-chain:__subpackages__"],
deps = [
"//beacon-chain/blockchain:go_default_library",
"//beacon-chain/core/blocks:go_default_library",
@@ -36,7 +35,7 @@ go_test(
srcs = ["simulated_backend_test.go"],
embed = [":go_default_library"],
deps = [
"//beacon-chain/db:go_default_library",
"//shared/params:go_default_library",
"//shared/trieutil:go_default_library",
],
)

View File

@@ -127,7 +127,7 @@ func generateSimulatedBlock(
return block, blockRoot, nil
}
// Generates initial deposits for creating a beacon state in the simulated
// generateInitialSimulatedDeposits generates initial deposits for creating a beacon state in the simulated
// backend based on the yaml configuration.
func generateInitialSimulatedDeposits(numDeposits uint64) ([]*pb.Deposit, []*bls.SecretKey, error) {
genesisTime := time.Date(2018, 9, 0, 0, 0, 0, 0, time.UTC).Unix()

View File

@@ -9,10 +9,6 @@ import (
"reflect"
"time"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/ssz"
"github.com/ethereum/go-ethereum/common"
"github.com/prysmaticlabs/prysm/beacon-chain/blockchain"
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
@@ -20,8 +16,10 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/db"
"github.com/prysmaticlabs/prysm/beacon-chain/utils"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/sliceutil"
"github.com/prysmaticlabs/prysm/shared/ssz"
"github.com/prysmaticlabs/prysm/shared/trieutil"
log "github.com/sirupsen/logrus"
)
@@ -52,7 +50,7 @@ type SimulatedObjects struct {
// utilizing a mockDB which will act according to test run parameters specified
// in the common ETH 2.0 client test YAML format.
func NewSimulatedBackend() (*SimulatedBackend, error) {
db, err := setupDB()
db, err := db.SetupDB()
if err != nil {
return nil, fmt.Errorf("could not setup simulated backend db: %v", err)
}
@@ -67,10 +65,30 @@ func NewSimulatedBackend() (*SimulatedBackend, error) {
return &SimulatedBackend{
chainService: cs,
beaconDB: db,
inMemoryBlocks: make([]*pb.BeaconBlock, 1000),
inMemoryBlocks: make([]*pb.BeaconBlock, 0),
}, nil
}
// SetupBackend sets up the simulated backend with simulated deposits, and initializes the
// state and genesis block.
func (sb *SimulatedBackend) SetupBackend(numOfDeposits uint64) ([]*bls.SecretKey, error) {
initialDeposits, privKeys, err := generateInitialSimulatedDeposits(numOfDeposits)
if err != nil {
return nil, fmt.Errorf("could not simulate initial validator deposits: %v", err)
}
if err := sb.setupBeaconStateAndGenesisBlock(initialDeposits); err != nil {
return nil, fmt.Errorf("could not set up beacon state and initialize genesis block %v", err)
}
sb.depositTrie = trieutil.NewDepositTrie()
return privKeys, nil
}
// DB returns the underlying db instance in the simulated
// backend.
func (sb *SimulatedBackend) DB() *db.BeaconDB {
return sb.beaconDB
}
// GenerateBlockAndAdvanceChain generates a simulated block and runs that block though
// state transition.
func (sb *SimulatedBackend) GenerateBlockAndAdvanceChain(objects *SimulatedObjects, privKeys []*bls.SecretKey) error {
@@ -131,11 +149,23 @@ func (sb *SimulatedBackend) Shutdown() error {
return sb.beaconDB.Close()
}
// State is a getter to return the current beacon state
// of the backend.
func (sb *SimulatedBackend) State() *pb.BeaconState {
return sb.state
}
// InMemoryBlocks returns the blocks that have been processed by the simulated
// backend.
func (sb *SimulatedBackend) InMemoryBlocks() []*pb.BeaconBlock {
return sb.inMemoryBlocks
}
// RunForkChoiceTest uses a parsed set of chaintests from a YAML file
// according to the ETH 2.0 client chain test specification and runs them
// against the simulated backend.
func (sb *SimulatedBackend) RunForkChoiceTest(testCase *ForkChoiceTestCase) error {
defer teardownDB(sb.beaconDB)
defer db.TeardownDB(sb.beaconDB)
// Utilize the config parameters in the test case to setup
// the DB and set global config parameters accordingly.
// Config parameters include: ValidatorCount, ShardCount,
@@ -166,7 +196,7 @@ func (sb *SimulatedBackend) RunForkChoiceTest(testCase *ForkChoiceTestCase) erro
// RunShuffleTest uses validator set specified from a YAML file, runs the validator shuffle
// algorithm, then compare the output with the expected output from the YAML file.
func (sb *SimulatedBackend) RunShuffleTest(testCase *ShuffleTestCase) error {
defer teardownDB(sb.beaconDB)
defer db.TeardownDB(sb.beaconDB)
seed := common.BytesToHash([]byte(testCase.Seed))
output, err := utils.ShuffleIndices(seed, testCase.Input)
if err != nil {
@@ -182,7 +212,7 @@ func (sb *SimulatedBackend) RunShuffleTest(testCase *ShuffleTestCase) error {
// slots from a genesis state, with a block being processed at every iteration
// of the state transition function.
func (sb *SimulatedBackend) RunStateTransitionTest(testCase *StateTestCase) error {
defer teardownDB(sb.beaconDB)
defer db.TeardownDB(sb.beaconDB)
setTestConfig(testCase)
privKeys, err := sb.initializeStateTest(testCase)
@@ -266,6 +296,7 @@ func (sb *SimulatedBackend) setupBeaconStateAndGenesisBlock(initialDeposits []*p
// We now keep track of generated blocks for each state transition in
// a slice.
sb.prevBlockRoots = [][32]byte{genesisBlockRoot}
sb.inMemoryBlocks = append(sb.inMemoryBlocks, genesisBlock)
return nil
}

View File

@@ -3,8 +3,9 @@ package backend
import (
"testing"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/trieutil"
)
func TestSimulatedBackendStop_ShutsDown(t *testing.T) {
@@ -16,22 +17,22 @@ func TestSimulatedBackendStop_ShutsDown(t *testing.T) {
if err := backend.Shutdown(); err != nil {
t.Errorf("Could not successfully shutdown simulated backend %v", err)
}
db.TeardownDB(backend.beaconDB)
}
func TestGenerateBlockAndAdvanceChain_IncreasesSlot(t *testing.T) {
backend, err := NewSimulatedBackend()
if err != nil {
t.Fatalf("Could not create a new simulated backedn %v", err)
t.Fatalf("Could not create a new simulated backend %v", err)
}
initialDeposits, privKeys, err := generateInitialSimulatedDeposits(params.BeaconConfig().SlotsPerEpoch)
privKeys, err := backend.SetupBackend(100)
if err != nil {
t.Fatalf("Could not simulate initial validator deposits: %v", err)
t.Fatalf("Could not set up backend %v", err)
}
if err := backend.setupBeaconStateAndGenesisBlock(initialDeposits); err != nil {
t.Fatalf("Could not set up beacon state and initialize genesis block %v", err)
}
backend.depositTrie = trieutil.NewDepositTrie()
defer backend.Shutdown()
defer db.TeardownDB(backend.beaconDB)
slotLimit := params.BeaconConfig().SlotsPerEpoch + uint64(1)
@@ -57,14 +58,11 @@ func TestGenerateNilBlockAndAdvanceChain_IncreasesSlot(t *testing.T) {
t.Fatalf("Could not create a new simulated backedn %v", err)
}
initialDeposits, _, err := generateInitialSimulatedDeposits(params.BeaconConfig().SlotsPerEpoch)
if err != nil {
t.Fatalf("Could not simulate initial validator deposits: %v", err)
if _, err := backend.SetupBackend(100); err != nil {
t.Fatalf("Could not set up backend %v", err)
}
if err := backend.setupBeaconStateAndGenesisBlock(initialDeposits); err != nil {
t.Fatalf("Could not set up beacon state and initialize genesis block %v", err)
}
backend.depositTrie = trieutil.NewDepositTrie()
defer backend.Shutdown()
defer db.TeardownDB(backend.beaconDB)
slotLimit := params.BeaconConfig().SlotsPerEpoch + uint64(1)

View File

@@ -6,12 +6,11 @@ import (
"reflect"
"testing"
"github.com/prysmaticlabs/prysm/shared/ssz"
"github.com/gogo/protobuf/proto"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/ssz"
)
func TestGenesisBlock_InitializedCorrectly(t *testing.T) {

View File

@@ -8,12 +8,11 @@ import (
"fmt"
"time"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/ethereum/go-ethereum/common"
gethTypes "github.com/ethereum/go-ethereum/core/types"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
)
// IsValidBlock ensures that the block is compliant with the block processing validity conditions.

View File

@@ -5,11 +5,10 @@ import (
"testing"
"time"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/ethereum/go-ethereum/common"
gethTypes "github.com/ethereum/go-ethereum/core/types"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/sirupsen/logrus"
)

View File

@@ -7,12 +7,11 @@ import (
"fmt"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/shared/ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state/stateutils"
v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/ssz"
)
// GenesisBeaconState gets called when DepositsForChainStart count of

View File

@@ -7,13 +7,11 @@ import (
"testing"
"time"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/shared/ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/ssz"
)
func TestGenesisBeaconState_OK(t *testing.T) {

View File

@@ -10,6 +10,7 @@ go_library(
"db.go",
"pending_deposits.go",
"schema.go",
"setup_db.go",
"state.go",
],
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/db",

View File

@@ -4,10 +4,9 @@ import (
"testing"
"time"
"github.com/prysmaticlabs/prysm/shared/ssz"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/ssz"
)
func TestNilDB_OK(t *testing.T) {

View File

@@ -1,4 +1,4 @@
package backend
package db
import (
"crypto/rand"
@@ -6,13 +6,10 @@ import (
"math/big"
"os"
"path"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
log "github.com/sirupsen/logrus"
)
// setupDB instantiates and returns a simulated backend BeaconDB instance.
func setupDB() (*db.BeaconDB, error) {
// SetupDB instantiates and returns a simulated backend BeaconDB instance.
func SetupDB() (*BeaconDB, error) {
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
if err != nil {
return nil, fmt.Errorf("could not generate random file path: %v", err)
@@ -21,11 +18,11 @@ func setupDB() (*db.BeaconDB, error) {
if err := os.RemoveAll(path); err != nil {
return nil, fmt.Errorf("failed to remove directory: %v", err)
}
return db.NewDB(path)
return NewDB(path)
}
// teardownDB cleans up a simulated backend BeaconDB instance.
func teardownDB(db *db.BeaconDB) {
// TeardownDB cleans up a simulated backend BeaconDB instance.
func TeardownDB(db *BeaconDB) {
if err := db.Close(); err != nil {
log.Fatalf("failed to close database: %v", err)
}

View File

@@ -4,14 +4,13 @@ import (
"fmt"
"time"
"github.com/prysmaticlabs/prysm/shared/ssz"
"github.com/boltdb/bolt"
"github.com/gogo/protobuf/proto"
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/ssz"
)
// InitializeState creates an initial genesis state for the beacon

View File

@@ -5,16 +5,13 @@ import (
"strings"
"testing"
"github.com/prysmaticlabs/prysm/shared/ssz"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/gogo/protobuf/proto"
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/ssz"
)
func TestAttestHead_OK(t *testing.T) {

View File

@@ -31,13 +31,14 @@ go_test(
"querier_test.go",
"regular_sync_test.go",
"service_test.go",
"simulated_sync_test.go",
],
embed = [":go_default_library"],
deps = [
"//beacon-chain/chaintest/backend:go_default_library",
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/db:go_default_library",
"//beacon-chain/internal:go_default_library",
"//beacon-chain/sync/initial-sync:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/bls:go_default_library",
"//shared/bytesutil:go_default_library",

View File

@@ -16,8 +16,6 @@ import (
"fmt"
"time"
"github.com/prysmaticlabs/prysm/shared/ssz"
"github.com/gogo/protobuf/proto"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
@@ -25,6 +23,7 @@ import (
"github.com/prysmaticlabs/prysm/shared/event"
"github.com/prysmaticlabs/prysm/shared/p2p"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/ssz"
"github.com/sirupsen/logrus"
)
@@ -93,6 +92,7 @@ type InitialSync struct {
syncPollingInterval time.Duration
genesisStateRootHash32 [32]byte
inMemoryBlocks map[uint64]*pb.BeaconBlock
syncedFeed *event.Feed
}
// NewInitialSyncService constructs a new InitialSyncService.
@@ -122,6 +122,7 @@ func NewInitialSyncService(ctx context.Context,
blockAnnounceBuf: blockAnnounceBuf,
syncPollingInterval: cfg.SyncPollingInterval,
inMemoryBlocks: map[uint64]*pb.BeaconBlock{},
syncedFeed: new(event.Feed),
}
}
@@ -142,6 +143,16 @@ func (s *InitialSync) Stop() error {
return nil
}
// InitializeObservedSlot sets the highest observed slot.
func (s *InitialSync) InitializeObservedSlot(slot uint64) {
s.highestObservedSlot = slot
}
// SyncedFeed returns a feed which fires a message once the node is synced
func (s *InitialSync) SyncedFeed() *event.Feed {
return s.syncedFeed
}
// run is the main goroutine for the initial sync service.
// delayChan is explicitly passed into this function to facilitate tests that don't require a timeout.
// It is assumed that the goroutine `run` is only called once per instance.
@@ -168,11 +179,13 @@ func (s *InitialSync) run(delayChan <-chan time.Time) {
return
case <-delayChan:
if s.currentSlot == params.BeaconConfig().GenesisSlot {
s.requestBatchedBlocks(s.highestObservedSlot)
continue
}
if s.highestObservedSlot == s.currentSlot {
log.Info("Exiting initial sync and starting normal sync")
s.syncedFeed.Send(s.currentSlot)
s.syncService.ResumeSync()
return
}

View File

@@ -6,8 +6,6 @@ import (
"testing"
"time"
"github.com/prysmaticlabs/prysm/shared/ssz"
"github.com/gogo/protobuf/proto"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
@@ -16,6 +14,7 @@ import (
"github.com/prysmaticlabs/prysm/shared/event"
"github.com/prysmaticlabs/prysm/shared/p2p"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/ssz"
"github.com/prysmaticlabs/prysm/shared/testutil"
logTest "github.com/sirupsen/logrus/hooks/test"
)

View File

@@ -4,10 +4,9 @@ import (
"context"
"time"
"github.com/prysmaticlabs/prysm/shared/event"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/event"
"github.com/prysmaticlabs/prysm/shared/p2p"
"github.com/sirupsen/logrus"
)
@@ -24,8 +23,8 @@ type QuerierConfig struct {
ResponseBufferSize int
P2P p2pAPI
BeaconDB *db.BeaconDB
CurentHeadSlot uint64
PowChain powChainService
CurrentHeadSlot uint64
}
// DefaultQuerierConfig provides the default configuration for a sync service.
@@ -66,7 +65,7 @@ func NewQuerierService(ctx context.Context,
p2p: cfg.P2P,
db: cfg.BeaconDB,
responseBuf: responseBuf,
curentHeadSlot: cfg.CurentHeadSlot,
curentHeadSlot: cfg.CurrentHeadSlot,
chainStarted: false,
powchain: cfg.PowChain,
chainStartBuf: make(chan time.Time, 1),
@@ -114,6 +113,7 @@ func (q *Querier) listenForChainStart() {
}
func (q *Querier) run() {
responseSub := q.p2p.Subscribe(&pb.ChainHeadResponse{}, q.responseBuf)
// Ticker so that service will keep on requesting for chain head
@@ -171,5 +171,4 @@ func (q *Querier) IsSynced() (bool, error) {
}
return false, err
}

View File

@@ -5,8 +5,6 @@ import (
"context"
"fmt"
"github.com/prysmaticlabs/prysm/shared/ssz"
"github.com/gogo/protobuf/proto"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
@@ -15,6 +13,7 @@ import (
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/p2p"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/ssz"
"github.com/sirupsen/logrus"
"go.opencensus.io/trace"
)
@@ -425,18 +424,18 @@ func (rs *RegularSync) handleBatchedBlockRequest(msg p2p.Message) {
response := make([]*pb.BeaconBlock, 0, endSlot-startSlot)
for i := startSlot; i <= endSlot; i++ {
block, err := rs.db.BlockBySlot(i)
retBlock, err := rs.db.BlockBySlot(i)
if err != nil {
log.Errorf("Unable to retrieve block from db %v", err)
continue
}
if block == nil {
if retBlock == nil {
log.Debug("Block does not exist in db")
continue
}
response = append(response, block)
response = append(response, retBlock)
}
log.Debugf("Sending response for batch blocks to peer %v", msg.Peer)

View File

@@ -40,10 +40,15 @@ func (mp *mockP2P) Broadcast(msg proto.Message) {}
func (mp *mockP2P) Send(msg proto.Message, peer p2p.Peer) {
}
type mockChainService struct{}
type mockChainService struct {
feed *event.Feed
}
func (ms *mockChainService) IncomingBlockFeed() *event.Feed {
return new(event.Feed)
if ms.feed == nil {
return new(event.Feed)
}
return ms.feed
}
type mockOperationService struct{}

View File

@@ -38,6 +38,7 @@ func NewSyncService(ctx context.Context, cfg *Config) *Service {
isCfg := initialsync.DefaultConfig()
isCfg.BeaconDB = cfg.BeaconDB
isCfg.P2P = cfg.P2P
isCfg.ChainService = cfg.ChainService
rsCfg := DefaultRegularSyncConfig()
rsCfg.ChainService = cfg.ChainService
@@ -100,5 +101,8 @@ func (ss *Service) run() {
return
}
// Sets the highest observed slot from querier.
ss.InitialSync.InitializeObservedSlot(ss.Querier.curentHeadSlot)
ss.InitialSync.Start()
}

View File

@@ -7,19 +7,17 @@ import (
"time"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
initialsync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/params"
)
func NotSyncQuerierConfig() *QuerierConfig {
return &QuerierConfig{
ResponseBufferSize: 100,
CurentHeadSlot: 10,
CurrentHeadSlot: 10,
}
}
@@ -30,29 +28,16 @@ func initializeTestSyncService(ctx context.Context, cfg *Config, synced bool) *S
} else {
sqCfg = NotSyncQuerierConfig()
}
services := NewSyncService(ctx, cfg)
sqCfg.BeaconDB = cfg.BeaconDB
sqCfg.P2P = cfg.P2P
isCfg := initialsync.DefaultConfig()
isCfg.BeaconDB = cfg.BeaconDB
isCfg.P2P = cfg.P2P
rsCfg := DefaultRegularSyncConfig()
rsCfg.ChainService = cfg.ChainService
rsCfg.BeaconDB = cfg.BeaconDB
rsCfg.P2P = cfg.P2P
sq := NewQuerierService(ctx, sqCfg)
rs := NewRegularSyncService(ctx, rsCfg)
isCfg.SyncService = rs
is := initialsync.NewInitialSyncService(ctx, isCfg)
services.Querier = sq
return &Service{
RegularSync: rs,
InitialSync: is,
Querier: sq,
}
return services
}
func setupInitialDeposits(t *testing.T, numDeposits int) ([]*pb.Deposit, []*bls.SecretKey) {

View File

@@ -0,0 +1,233 @@
package sync
import (
"context"
"reflect"
"sync"
"testing"
"time"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/gogo/protobuf/proto"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/beacon-chain/chaintest/backend"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/event"
"github.com/prysmaticlabs/prysm/shared/p2p"
)
type simulatedP2P struct {
subsChannels map[reflect.Type]*event.Feed
mutex *sync.RWMutex
ctx context.Context
}
func (sim *simulatedP2P) Subscribe(msg proto.Message, channel chan p2p.Message) event.Subscription {
sim.mutex.Lock()
defer sim.mutex.Unlock()
protoType := reflect.TypeOf(msg)
feed, ok := sim.subsChannels[protoType]
if !ok {
nFeed := new(event.Feed)
sim.subsChannels[protoType] = nFeed
return nFeed.Subscribe(channel)
}
return feed.Subscribe(channel)
}
func (sim *simulatedP2P) Broadcast(msg proto.Message) {
sim.mutex.Lock()
defer sim.mutex.Unlock()
protoType := reflect.TypeOf(msg)
feed, ok := sim.subsChannels[protoType]
if !ok {
return
}
feed.Send(p2p.Message{Ctx: sim.ctx, Data: msg})
}
func (sim *simulatedP2P) Send(msg proto.Message, peer p2p.Peer) {
sim.mutex.Lock()
defer sim.mutex.Unlock()
protoType := reflect.TypeOf(msg)
feed, ok := sim.subsChannels[protoType]
if !ok {
return
}
feed.Send(p2p.Message{Ctx: sim.ctx, Data: msg})
}
func setupSimBackendAndDB(t *testing.T) (*backend.SimulatedBackend, *db.BeaconDB, []*bls.SecretKey) {
bd, err := backend.NewSimulatedBackend()
if err != nil {
t.Fatalf("Could not set up simulated backend %v", err)
}
privKeys, err := bd.SetupBackend(100)
if err != nil {
t.Fatalf("Could not set up backend %v", err)
}
beacondb, err := db.SetupDB()
if err != nil {
t.Fatalf("Could not setup beacon db %v", err)
}
if err := beacondb.SaveState(bd.State()); err != nil {
t.Fatalf("Could not save state %v", err)
}
memBlocks := bd.InMemoryBlocks()
if err := beacondb.SaveBlock(memBlocks[0]); err != nil {
t.Fatalf("Could not save block %v", err)
}
if err := beacondb.UpdateChainHead(memBlocks[0], bd.State()); err != nil {
t.Fatalf("Could not update chain head %v", err)
}
return bd, beacondb, privKeys
}
func setUpSyncedService(numOfBlocks int, simP2P *simulatedP2P, t *testing.T) (*Service, *db.BeaconDB) {
bd, beacondb, privKeys := setupSimBackendAndDB(t)
defer bd.Shutdown()
defer db.TeardownDB(bd.DB())
mockPow := &genesisPowChain{
feed: new(event.Feed),
}
mockChain := &mockChainService{
feed: new(event.Feed),
}
cfg := &Config{
ChainService: mockChain,
BeaconDB: beacondb,
OperationService: &mockOperationService{},
P2P: simP2P,
PowChainService: mockPow,
}
ss := NewSyncService(context.Background(), cfg)
go ss.run()
for !ss.Querier.chainStarted {
mockPow.feed.Send(time.Now())
}
for i := 1; i <= numOfBlocks; i++ {
if err := bd.GenerateBlockAndAdvanceChain(&backend.SimulatedObjects{}, privKeys); err != nil {
t.Fatalf("Unable to generate block in simulated backend %v", err)
}
blocks := bd.InMemoryBlocks()
if err := beacondb.SaveBlock(blocks[i]); err != nil {
t.Fatalf("Unable to save block %v", err)
}
if err := beacondb.UpdateChainHead(blocks[i], bd.State()); err != nil {
t.Fatalf("Unable to update chain head %v", err)
}
}
return ss, beacondb
}
func setUpUnSyncedService(simP2P *simulatedP2P, t *testing.T) (*Service, *db.BeaconDB) {
bd, beacondb, _ := setupSimBackendAndDB(t)
defer bd.Shutdown()
defer db.TeardownDB(bd.DB())
mockPow := &afterGenesisPowChain{
feed: new(event.Feed),
}
mockChain := &mockChainService{
feed: new(event.Feed),
}
cfg := &Config{
ChainService: mockChain,
BeaconDB: beacondb,
OperationService: &mockOperationService{},
P2P: simP2P,
PowChainService: mockPow,
}
ss := NewSyncService(context.Background(), cfg)
go ss.run()
for ss.Querier.curentHeadSlot == 0 {
simP2P.Send(&pb.ChainHeadResponse{
Slot: params.BeaconConfig().GenesisSlot + 10,
Hash: []byte{'t', 'e', 's', 't'},
}, p2p.Peer{})
}
return ss, beacondb
}
func TestSync_AFullySyncedNode(t *testing.T) {
numOfBlocks := 10
newP2P := &simulatedP2P{
subsChannels: make(map[reflect.Type]*event.Feed),
mutex: new(sync.RWMutex),
ctx: context.Background(),
}
// Sets up a synced service which has its head at the current
// numOfBlocks from genesis. The blocks are generated through
// simulated backend.
ss, syncedDB := setUpSyncedService(numOfBlocks, newP2P, t)
defer ss.Stop()
defer db.TeardownDB(syncedDB)
// Sets up a sync service which has its current head at genesis.
us, unSyncedDB := setUpUnSyncedService(newP2P, t)
defer us.Stop()
defer db.TeardownDB(unSyncedDB)
// Sets up another sync service which has its current head at genesis.
us2, unSyncedDB2 := setUpUnSyncedService(newP2P, t)
defer us2.Stop()
defer db.TeardownDB(unSyncedDB2)
syncedChan := make(chan uint64)
// Waits for the unsynced node to fire a message signifying it is
// synced with its current slot number.
sub := us.InitialSync.SyncedFeed().Subscribe(syncedChan)
defer sub.Unsubscribe()
syncedChan2 := make(chan uint64)
sub2 := us2.InitialSync.SyncedFeed().Subscribe(syncedChan2)
defer sub2.Unsubscribe()
highestSlot := <-syncedChan
highestSlot2 := <-syncedChan2
if highestSlot != uint64(numOfBlocks)+params.BeaconConfig().GenesisSlot {
t.Errorf("Sync services didn't sync to expectecd slot, expected %d but got %d",
uint64(numOfBlocks)+params.BeaconConfig().GenesisSlot, highestSlot)
}
if highestSlot2 != uint64(numOfBlocks)+params.BeaconConfig().GenesisSlot {
t.Errorf("Sync services didn't sync to expectecd slot, expected %d but got %d",
uint64(numOfBlocks)+params.BeaconConfig().GenesisSlot, highestSlot2)
}
}

View File

@@ -6,9 +6,8 @@ import (
"errors"
"fmt"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/keystore"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/ssz"
"github.com/sirupsen/logrus"
)

View File

@@ -5,9 +5,8 @@ import (
"os"
"testing"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/keystore"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
)

View File

@@ -3,10 +3,9 @@ package client
import (
"context"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/opentracing/opentracing-go"
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/sirupsen/logrus"
)

View File

@@ -7,14 +7,11 @@ import (
"io"
"time"
"github.com/opentracing/opentracing-go"
"github.com/prysmaticlabs/prysm/shared/keystore"
ptypes "github.com/gogo/protobuf/types"
"github.com/opentracing/opentracing-go"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/prysmaticlabs/prysm/shared/keystore"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/slotutil"
)

View File

@@ -5,12 +5,10 @@ import (
"fmt"
"time"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/opentracing/opentracing-go"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/opentracing/opentracing-go"
"github.com/prysmaticlabs/prysm/shared/params"
)
var delay = params.BeaconConfig().SecondsPerSlot / 2

View File

@@ -7,12 +7,11 @@ import (
"testing"
"time"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/gogo/protobuf/proto"
"github.com/golang/mock/gomock"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
logTest "github.com/sirupsen/logrus/hooks/test"
)

View File

@@ -7,13 +7,11 @@ import (
"encoding/binary"
"fmt"
ptypes "github.com/gogo/protobuf/types"
"github.com/opentracing/opentracing-go"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/forkutils"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/opentracing/opentracing-go"
ptypes "github.com/gogo/protobuf/types"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/ssz"
)

View File

@@ -8,14 +8,12 @@ import (
"testing"
"time"
"github.com/sirupsen/logrus"
ptypes "github.com/gogo/protobuf/types"
"github.com/golang/mock/gomock"
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/validator/internal"
"github.com/sirupsen/logrus"
)
func init() {

View File

@@ -6,11 +6,10 @@ import (
"os"
"runtime"
"github.com/prysmaticlabs/prysm/validator/accounts"
"github.com/prysmaticlabs/prysm/shared/cmd"
"github.com/prysmaticlabs/prysm/shared/debug"
"github.com/prysmaticlabs/prysm/shared/version"
"github.com/prysmaticlabs/prysm/validator/accounts"
"github.com/prysmaticlabs/prysm/validator/node"
"github.com/prysmaticlabs/prysm/validator/types"
"github.com/sirupsen/logrus"