From 7135a8542f3a80e8b89767294e92e0211c3fc164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Kapka?= Date: Wed, 6 Jan 2021 11:45:22 +0100 Subject: [PATCH] Check if initial sync service has been initialized (#8214) --- beacon-chain/sync/initial-sync/service.go | 7 ++++++- beacon-chain/sync/initial-sync/service_test.go | 12 +++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/beacon-chain/sync/initial-sync/service.go b/beacon-chain/sync/initial-sync/service.go index e715ec541..d0a51f129 100644 --- a/beacon-chain/sync/initial-sync/service.go +++ b/beacon-chain/sync/initial-sync/service.go @@ -127,7 +127,7 @@ func (s *Service) Stop() error { // Status of initial sync. func (s *Service) Status() error { - if s.synced.IsNotSet() && s.chainStarted.IsSet() { + if s.Syncing() { return errors.New("syncing") } return nil @@ -138,6 +138,11 @@ func (s *Service) Syncing() bool { return s.synced.IsNotSet() } +// Initialized returns true if initial sync has been started. +func (s *Service) Initialized() bool { + return s.chainStarted.IsSet() +} + // Resync allows a node to start syncing again if it has fallen // behind the current network head. func (s *Service) Resync() error { diff --git a/beacon-chain/sync/initial-sync/service_test.go b/beacon-chain/sync/initial-sync/service_test.go index b3ce8cad5..06ba18ba7 100644 --- a/beacon-chain/sync/initial-sync/service_test.go +++ b/beacon-chain/sync/initial-sync/service_test.go @@ -327,8 +327,9 @@ func TestService_markSynced(t *testing.T) { assert.Equal(t, false, s.chainStarted.IsSet()) assert.Equal(t, false, s.synced.IsSet()) assert.Equal(t, true, s.Syncing()) - assert.NoError(t, s.Status()) + assert.ErrorContains(t, "syncing", s.Status()) s.chainStarted.Set() + assert.Equal(t, true, s.Syncing()) assert.ErrorContains(t, "syncing", s.Status()) expectedGenesisTime := time.Unix(358544700, 0) @@ -359,6 +360,7 @@ func TestService_markSynced(t *testing.T) { } assert.Equal(t, expectedGenesisTime, receivedGenesisTime) assert.Equal(t, false, s.Syncing()) + assert.NoError(t, s.Status()) } func TestService_Resync(t *testing.T) { @@ -436,3 +438,11 @@ func TestService_Resync(t *testing.T) { }) } } + +func TestService_Initialized(t *testing.T) { + s := NewService(context.Background(), &Config{}) + s.chainStarted.Set() + assert.Equal(t, true, s.Initialized()) + s.chainStarted.UnSet() + assert.Equal(t, false, s.Initialized()) +}