Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced HasStoredState with IsSyncedWithNetwork #662

Merged
merged 3 commits into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions beacon-chain/db/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,3 @@ func (db *BeaconDB) SaveCrystallizedState(crystallizedState *types.CrystallizedS
}
return db.put(crystallizedStateLookupKey, encodedState)
}

// HasStoredState checks if state has been stored to the database.
func (db *BeaconDB) HasStoredState() (bool, error) {
hasState, err := db.has(crystallizedStateLookupKey)
if err != nil {
return false, err
}

return hasState, nil
}
15 changes: 4 additions & 11 deletions beacon-chain/sync/initial-sync/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ type p2pAPI interface {
}

type beaconDB interface {
HasStoredState() (bool, error)
SaveBlock(*types.Block) error
}

// SyncService is the interface for the Sync service.
// InitialSync calls `Start` when initial sync completes.
type syncService interface {
Start()
IsSyncedWithNetwork() bool
}

// InitialSync defines the main class in this package.
Expand Down Expand Up @@ -105,14 +105,8 @@ func NewInitialSyncService(ctx context.Context,

// Start begins the goroutine.
func (s *InitialSync) Start() {
stored, err := s.db.HasStoredState()
if err != nil {
log.Errorf("error retrieving stored state: %v", err)
return
}

if stored {
// TODO(555): Bail out of the sync service if the chain is only partially synced.
if s.syncService.IsSyncedWithNetwork() {
// TODO(#661): Bail out of the sync service if the chain is only partially synced.
log.Info("Chain state detected, exiting initial sync")
return
}
Expand Down Expand Up @@ -154,8 +148,7 @@ func (s *InitialSync) run(delaychan <-chan time.Time) {
case <-delaychan:
if highestObservedSlot == s.currentSlot {
log.Info("Exiting initial sync and starting normal sync")
// TODO(#426): Resume sync after completion of initial sync.
// See comment in Sync service's Start function for explanation.
// TODO(#661): Resume sync after completion of initial sync.
return
}
case msg := <-s.blockBuf:
Expand Down
51 changes: 47 additions & 4 deletions beacon-chain/sync/initial-sync/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@ func (mp *mockP2P) Send(msg proto.Message, peer p2p.Peer) {

type mockSyncService struct {
hasStarted bool
isSynced bool
}

func (ms *mockSyncService) Start() {
ms.hasStarted = true
}

type mockDB struct{}

func (m *mockDB) HasStoredState() (bool, error) {
return true, nil
func (ms *mockSyncService) IsSyncedWithNetwork() bool {
return ms.isSynced
}

type mockDB struct{}

func (m *mockDB) SaveBlock(*types.Block) error {
return nil
}
Expand Down Expand Up @@ -285,3 +286,45 @@ func TestDelayChan(t *testing.T) {

hook.Reset()
}

func TestIsSyncedWithNetwork(t *testing.T) {
hook := logTest.NewGlobal()
mockSync := &mockSyncService{}
cfg := Config{
P2P: &mockP2P{},
SyncService: mockSync,
BeaconDB: &mockDB{},
SyncPollingInterval: 1,
}
ss := NewInitialSyncService(context.Background(), cfg)

mockSync.isSynced = true
ss.Start()
ss.Stop()

testutil.AssertLogsContain(t, hook, "Chain state detected, exiting initial sync")
testutil.AssertLogsContain(t, hook, "Stopping service")

hook.Reset()
}

func TestIsNotSyncedWithNetwork(t *testing.T) {
hook := logTest.NewGlobal()
mockSync := &mockSyncService{}
cfg := Config{
P2P: &mockP2P{},
SyncService: mockSync,
BeaconDB: &mockDB{},
SyncPollingInterval: 1,
}
ss := NewInitialSyncService(context.Background(), cfg)

mockSync.isSynced = false
ss.Start()
ss.Stop()

testutil.AssertLogsDoNotContain(t, hook, "Chain state detected, exiting initial sync")
testutil.AssertLogsContain(t, hook, "Stopping service")

hook.Reset()
}
23 changes: 11 additions & 12 deletions beacon-chain/sync/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ type beaconDB interface {
GetBlock([32]byte) (*types.Block, error)
GetAttestation([32]byte) (*types.Attestation, error)
HasAttestation([32]byte) (bool, error)
HasStoredState() (bool, error)
HasBlock([32]byte) (bool, error)
HasCanonicalBlockForSlot(uint64) (bool, error)
GetCanonicalBlockForSlot(uint64) (*types.Block, error)
Expand Down Expand Up @@ -111,19 +110,19 @@ func NewSyncService(ctx context.Context, cfg Config) *Service {
}
}

// IsSyncedWithNetwork polls other nodes in the network
// to determine whether or not the local chain is synced
// with the rest of the network.
// TODO(#661): Implement this method.
func (ss *Service) IsSyncedWithNetwork() bool {
return false
}

// Start begins the block processing goroutine.
func (ss *Service) Start() {
stored, err := ss.db.HasStoredState()
if err != nil {
log.Errorf("error retrieving stored state: %v", err)
return
}

if !stored {
// TODO(#426): Resume sync after completion of initial sync.
// Currently, `Simulator` only supports sync from genesis block, therefore
// new nodes with a fresh database must skip InitialSync and immediately run the Sync goroutine.
log.Info("Empty chain state, but continue sync")
if !ss.IsSyncedWithNetwork() {
log.Info("Not caught up with network, but continue sync")
// TODO(#661): Exit early if not synced.
}

go ss.run()
Expand Down
14 changes: 5 additions & 9 deletions beacon-chain/sync/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func TestReceiveAttestation(t *testing.T) {
testutil.AssertLogsContain(t, hook, "Forwarding attestation to subscribed services")
}

func TestStartEmptyState(t *testing.T) {
func TestStartNotSynced(t *testing.T) {
hook := logTest.NewGlobal()
db := setupDB(t)
cfg := DefaultConfig()
Expand All @@ -348,14 +348,10 @@ func TestStartEmptyState(t *testing.T) {
ss := NewSyncService(context.Background(), cfg)

ss.Start()
testutil.AssertLogsContain(t, hook, "Empty chain state, but continue sync")
ss.Stop()

hook.Reset()

db.SaveCrystallizedState(db.GetCrystallizedState())

ss.Start()
testutil.AssertLogsDoNotContain(t, hook, "Empty chain state, but continue sync")
testutil.AssertLogsContain(t, hook, "Not caught up with network, but continue sync")
testutil.AssertLogsContain(t, hook, "Stopping service")

ss.cancel()
hook.Reset()
}
15 changes: 0 additions & 15 deletions beacon-chain/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,6 @@ type P2P interface {
Broadcast(msg proto.Message)
}

// ChainService is the interface for the local beacon chain.
type ChainService interface {
BlockChainService
}

// BlockChainService is the interface for block related functions in local beacon chain.
type BlockChainService interface {
ProcessedBlockHashes() [][32]byte
ProcessBlock(b *Block)
HasStoredState() (bool, error)
ContainsBlock(h [32]byte) (bool, error)
SaveBlock(b *Block) error
GetBlockSlotNumber(h [32]byte) (uint64, error)
}

// CrystallizedStateChainService is the interface for crystallized state related functions in local beacon chain.
type CrystallizedStateChainService interface {
ProcessedCrystallizedStateHashes() [][32]byte
Expand Down