cleanup: remove unused await sync
## Motivation
remove unused code
MODIFY
syncer/syncer.go
syncer/syncer.go
@@ -88,10 +88,6 @@ type Syncer struct {
lastLayerSynced atomic.Value
lastATXsSynced atomic.Value
- // awaitSyncedCh is the list of subscribers' channels to notify when this node enters synced state
- awaitSyncedCh []chan struct{}
- muSyncedCh sync.Mutex // protects the slice above from concurrent access
-
// awaitATXSyncedCh is the list of subscribers' channels to notify when this node enters ATX synced state
awaitATXSyncedCh []chan struct{}
muATXSyncedCh sync.Mutex // protects the slice above from concurrent access
@@ -130,7 +126,6 @@ func NewSyncer(
patrol: patrol,
syncTimer: time.NewTicker(conf.SyncInterval),
validateTimer: time.NewTicker(conf.SyncInterval * 3),
- awaitSyncedCh: make([]chan struct{}, 0),
awaitATXSyncedCh: make([]chan struct{}, 0),
shutdownCtx: shutdownCtx,
cancelFunc: cancel,
@@ -154,19 +149,6 @@ func (s *Syncer) Close() {
s.logger.With().Info("all syncer goroutines finished", log.Err(err))
}
-// RegisterForSynced returns a channel for notification when the node enters synced state.
-func (s *Syncer) RegisterForSynced(ctx context.Context) chan struct{} {
- ch := make(chan struct{})
- if s.IsSynced(ctx) {
- close(ch)
- return ch
- }
- s.muSyncedCh.Lock()
- defer s.muSyncedCh.Unlock()
- s.awaitSyncedCh = append(s.awaitSyncedCh, ch)
- return ch
-}
-
// RegisterForATXSynced returns a channel for notification when the node enters ATX synced state.
func (s *Syncer) RegisterForATXSynced() chan struct{} {
ch := make(chan struct{})
@@ -277,13 +259,6 @@ func (s *Syncer) setSyncState(ctx context.Context, newState syncState) {
if newState != synced {
return
}
- // notify subscribes
- s.muSyncedCh.Lock()
- defer s.muSyncedCh.Unlock()
- for _, ch := range s.awaitSyncedCh {
- close(ch)
- }
- s.awaitSyncedCh = make([]chan struct{}, 0)
}
}

MODIFY
syncer/syncer_test.go
syncer/syncer_test.go
@@ -130,9 +130,6 @@ func TestStartAndShutdown(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
ts := newTestSyncer(ctx, t, time.Millisecond*5)
- syncedCh := ts.syncer.RegisterForSynced(context.TODO())
- atxSynchedCh := ts.syncer.RegisterForATXSynced()
-
require.False(t, ts.syncer.IsSynced(context.TODO()))
require.False(t, ts.syncer.ListenToATXGossip())
require.False(t, ts.syncer.ListenToGossip())
@@ -140,21 +137,9 @@ func TestStartAndShutdown(t *testing.T) {
// the node is synced when current layer is <= 1
ts.syncer.Start(context.TODO())
- select {
- case <-syncedCh:
- case <-time.After(1 * time.Second):
- require.Fail(t, "node should be synced")
- }
-
- select {
- case <-atxSynchedCh:
- case <-time.After(1 * time.Second):
- require.Fail(t, "node should be atx synced")
- }
-
- require.True(t, ts.syncer.IsSynced(context.TODO()))
- require.True(t, ts.syncer.ListenToATXGossip())
- require.True(t, ts.syncer.ListenToGossip())
+ require.Eventually(t, func() bool {
+ return ts.syncer.ListenToATXGossip() && ts.syncer.ListenToGossip() && ts.syncer.IsSynced(context.TODO())
+ }, time.Second, 10*time.Millisecond)
cancel()
require.True(t, ts.syncer.isClosed())
@@ -213,6 +198,18 @@ func TestSynchronize_AllGood(t *testing.T) {
ts.mLyrFetcher.EXPECT().PollLayerData(gomock.Any(), lid).Return(okCh())
}
+ var wg sync.WaitGroup
+ wg.Add(1)
+ go func() {
+ atxSyncedCh := ts.syncer.RegisterForATXSynced()
+ select {
+ case <-atxSyncedCh:
+ wg.Done()
+ case <-time.After(1 * time.Second):
+ require.Fail(t, "node should be atx synced")
+ }
+ }()
+
require.True(t, ts.syncer.synchronize(context.TODO()))
require.Equal(t, current.Sub(1), ts.syncer.getLastSyncedLayer())
require.Equal(t, current.GetEpoch(), ts.syncer.getLastSyncedATXs())
@@ -220,6 +217,18 @@ func TestSynchronize_AllGood(t *testing.T) {
require.True(t, ts.syncer.ListenToATXGossip())
require.True(t, ts.syncer.ListenToGossip())
require.False(t, ts.syncer.IsSynced(context.TODO()))
+
+ wg.Add(1)
+ go func() {
+ atxSyncedCh := ts.syncer.RegisterForATXSynced()
+ select {
+ case <-atxSyncedCh:
+ wg.Done()
+ case <-time.After(1 * time.Second):
+ require.Fail(t, "node should be atx synced")
+ }
+ }()
+ wg.Wait()
}
func TestSynchronize_FetchLayerDataFailed(t *testing.T) {
@@ -239,20 +248,6 @@ func TestSynchronize_FetchLayerDataFailed(t *testing.T) {
require.True(t, ts.syncer.ListenToATXGossip())
require.False(t, ts.syncer.ListenToGossip())
require.False(t, ts.syncer.IsSynced(context.TODO()))
-
- syncedCh := ts.syncer.RegisterForSynced(context.TODO())
- select {
- case <-syncedCh:
- require.Fail(t, "node should not be synced")
- case <-time.After(1 * time.Millisecond):
- }
-
- atxSyncedCh := ts.syncer.RegisterForATXSynced()
- select {
- case <-atxSyncedCh:
- case <-time.After(1 * time.Second):
- require.Fail(t, "node should be atx synced")
- }
}
func TestSynchronize_FailedInitialATXsSync(t *testing.T) {
@@ -265,6 +260,18 @@ func TestSynchronize_FailedInitialATXsSync(t *testing.T) {
}
ts.mLyrFetcher.EXPECT().GetEpochATXs(gomock.Any(), failedEpoch).Return(errors.New("no ATXs. should fail sync"))
+ var wg sync.WaitGroup
+ wg.Add(1)
+ go func() {
+ atxSyncedCh := ts.syncer.RegisterForATXSynced()
+ select {
+ case <-atxSyncedCh:
+ require.Fail(t, "node should not be atx synced")
+ case <-time.After(100 * time.Millisecond):
+ wg.Done()
+ }
+ }()
+
require.False(t, ts.syncer.synchronize(context.TODO()))
require.Equal(t, types.GetEffectiveGenesis(), ts.syncer.getLastSyncedLayer())
require.Equal(t, failedEpoch-1, ts.syncer.getLastSyncedATXs())
@@ -273,19 +280,17 @@ func TestSynchronize_FailedInitialATXsSync(t *testing.T) {
require.False(t, ts.syncer.ListenToGossip())
require.False(t, ts.syncer.IsSynced(context.TODO()))
- syncedCh := ts.syncer.RegisterForSynced(context.TODO())
- select {
- case <-syncedCh:
- require.Fail(t, "node should not be synced")
- case <-time.After(100 * time.Millisecond):
- }
-
+ wg.Add(1)
+ go func() {
atxSyncedCh := ts.syncer.RegisterForATXSynced()
select {
case <-atxSyncedCh:
require.Fail(t, "node should not be atx synced")
case <-time.After(100 * time.Millisecond):
+ wg.Done()
}
+ }()
+ wg.Wait()
}
func startWithSyncedState(t *testing.T, ts *testSyncer) types.LayerID {
@@ -442,23 +447,9 @@ func TestFromSyncedToNotSynced(t *testing.T) {
ts := newSyncerWithoutSyncTimer(t)
ts.mLyrFetcher.EXPECT().GetEpochATXs(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
- syncedCh := ts.syncer.RegisterForSynced(context.TODO())
- atxSyncedCh := ts.syncer.RegisterForATXSynced()
require.True(t, ts.syncer.synchronize(context.TODO()))
require.True(t, ts.syncer.IsSynced(context.TODO()))
- select {
- case <-syncedCh:
- case <-time.After(1 * time.Second):
- require.Fail(t, "node should be synced")
- }
-
- select {
- case <-atxSyncedCh:
- case <-time.After(1 * time.Second):
- require.Fail(t, "node should be atx synced")
- }
-
// cause the syncer to get out of synced and then wait again
lyr := types.GetEffectiveGenesis().Add(1)
current := ts.msh.LatestLayer().Add(outOfSyncThreshold)
@@ -511,8 +502,6 @@ func waitOutGossipSync(t *testing.T, current types.LayerID, ts *testSyncer) {
require.False(t, ts.syncer.IsSynced(context.TODO()))
// done one full layer of gossip sync, now it is synced
- syncedCh := ts.syncer.RegisterForSynced(context.TODO())
- atxSyncedCh := ts.syncer.RegisterForATXSynced()
lyr = lyr.Add(1)
current = current.Add(1)
ts.mTicker.advanceToLayer(current)
@@ -522,19 +511,6 @@ func waitOutGossipSync(t *testing.T, current types.LayerID, ts *testSyncer) {
return okCh()
})
require.True(t, ts.syncer.synchronize(context.TODO()))
-
- select {
- case <-syncedCh:
- case <-time.After(1 * time.Second):
- require.Fail(t, "node should be synced")
- }
-
- select {
- case <-atxSyncedCh:
- case <-time.After(1 * time.Second):
- require.Fail(t, "node should be atx synced")
- }
-
require.True(t, ts.syncer.dataSynced())
require.True(t, ts.syncer.ListenToATXGossip())
require.True(t, ts.syncer.ListenToGossip())
@@ -634,27 +610,6 @@ func TestSync_AlsoSyncProcessedLayer(t *testing.T) {
require.Equal(t, lyr, ts.syncer.getLastSyncedLayer())
}
-func TestSyncer_setSyncedTwice_NoError(t *testing.T) {
- ts := newSyncerWithoutSyncTimer(t)
-
- sync := ts.syncer.RegisterForSynced(context.TODO())
- select {
- case <-sync:
- require.Fail(t, "should not have reached synced state yet")
- case <-time.After(100 * time.Millisecond):
- }
-
- ts.syncer.setSyncState(context.TODO(), synced)
-
- select {
- case <-sync:
- case <-time.After(1 * time.Second):
- require.Fail(t, "should have reached synced state")
- }
-
- require.NotPanics(t, func() { ts.syncer.setSyncState(context.TODO(), synced) })
-}
-
func TestSyncer_setATXSyncedTwice_NoError(t *testing.T) {
ts := newSyncerWithoutSyncTimer(t)

cleanup: remove unused await sync (#3618)

## Motivation

remove unused code