Check if initial sync service has been initialized (#8214)

This commit is contained in:
Radosław Kapka 2021-01-06 11:45:22 +01:00 committed by GitHub
parent c354871762
commit 7135a8542f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -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 {

View File

@ -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())
}