Skip to content

Commit

Permalink
syncer skips beacon check before advancing the state (#4550)
Browse files Browse the repository at this point in the history
## Motivation
Closes #4549
  • Loading branch information
countvonzero committed Jun 20, 2023
1 parent e1f02d7 commit 05b9243
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 33 deletions.
14 changes: 7 additions & 7 deletions syncer/state_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ func (s *Syncer) processLayers(ctx context.Context) error {
}

s.logger.WithContext(ctx).With().Debug("processing synced layers",
log.Stringer("current", s.ticker.CurrentLayer()),
log.Stringer("processed", s.mesh.ProcessedLayer()),
log.Stringer("in_state", s.mesh.LatestLayerInState()),
log.Stringer("last_synced", s.getLastSyncedLayer()))
log.Stringer("last_synced", s.getLastSyncedLayer()),
)

start := minLayer(s.mesh.LatestLayerInState(), s.mesh.ProcessedLayer())
start = minLayer(start, s.getLastSyncedLayer())
Expand All @@ -63,10 +65,6 @@ func (s *Syncer) processLayers(ctx context.Context) error {

// layers should be processed in order. once we skip one layer, there is no point
// continuing with later layers. return on error
if _, err := s.beacon.GetBeacon(lid.GetEpoch()); err != nil {
s.logger.WithContext(ctx).With().Debug("beacon not available", lid)
return errBeaconNotAvailable
}

if s.patrol.IsHareInCharge(lid) {
lag := types.LayerID(0)
Expand All @@ -75,7 +73,7 @@ func (s *Syncer) processLayers(ctx context.Context) error {
lag = current.Sub(lid.Uint32())
}
if lag.Uint32() < s.cfg.HareDelayLayers {
s.logger.Debug("skip validating layer: hare still working", lid)
s.logger.WithContext(ctx).With().Debug("skipping layer: hare still working", lid)
return errHareInCharge
}
}
Expand Down Expand Up @@ -108,8 +106,10 @@ func (s *Syncer) processLayers(ctx context.Context) error {
}
s.logger.WithContext(ctx).With().Debug("end of state sync",
log.Bool("state_synced", s.stateSynced()),
log.Stringer("last_synced", s.getLastSyncedLayer()),
log.Stringer("current", s.ticker.CurrentLayer()),
log.Stringer("processed", s.mesh.ProcessedLayer()),
log.Stringer("in_state", s.mesh.LatestLayerInState()),
log.Stringer("last_synced", s.getLastSyncedLayer()),
)
return nil
}
Expand Down
20 changes: 0 additions & 20 deletions syncer/state_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func TestProcessLayers_MultiLayers(t *testing.T) {
adopted := make(map[types.LayerID]types.BlockID)
for lid := gLid.Add(1); lid.Before(current); lid = lid.Add(1) {
lid := lid
ts.mBeacon.EXPECT().GetBeacon(gomock.Any()).Return(types.RandomBeacon(), nil)
ts.mLyrPatrol.EXPECT().IsHareInCharge(lid).Return(false)
ts.mDataFetcher.EXPECT().PollLayerOpinions(gomock.Any(), lid).DoAndReturn(
func(context.Context, types.LayerID) ([]*fetch.LayerOpinion, error) {
Expand Down Expand Up @@ -159,7 +158,6 @@ func TestProcessLayers_OpinionsNotAdopted(t *testing.T) {
ts.mConState.EXPECT().UpdateCache(gomock.Any(), lid, types.EmptyBlockID, nil, nil)
ts.mVm.EXPECT().GetStateRoot()
}
ts.mBeacon.EXPECT().GetBeacon(gomock.Any()).Return(types.RandomBeacon(), nil)
ts.mLyrPatrol.EXPECT().IsHareInCharge(lid).Return(false)
ts.mDataFetcher.EXPECT().PollLayerOpinions(gomock.Any(), lid).Return(tc.opns, nil)
if tc.localCert == types.EmptyBlockID && hasCert {
Expand All @@ -186,18 +184,6 @@ func TestProcessLayers_OpinionsNotAdopted(t *testing.T) {
}
}

func TestProcessLayers_BeaconNotAvailable(t *testing.T) {
ts := newSyncerWithoutSyncTimer(t)
ts.syncer.setATXSynced()
lastSynced := types.GetEffectiveGenesis().Add(1)
ts.syncer.setLastSyncedLayer(lastSynced)
ts.mTicker.advanceToLayer(lastSynced.Add(1))
ts.mBeacon.EXPECT().GetBeacon(gomock.Any()).Return(types.EmptyBeacon, errBeaconNotAvailable)
require.False(t, ts.syncer.stateSynced())
require.ErrorIs(t, ts.syncer.processLayers(context.Background()), errBeaconNotAvailable)
require.False(t, ts.syncer.stateSynced())
}

func TestProcessLayers_ATXsNotSynced(t *testing.T) {
ts := newSyncerWithoutSyncTimer(t)
glayer := types.GetEffectiveGenesis()
Expand Down Expand Up @@ -231,12 +217,10 @@ func TestProcessLayers_HareIsStillWorking(t *testing.T) {
ts.mTicker.advanceToLayer(lastSynced.Add(1))

require.False(t, ts.syncer.stateSynced())
ts.mBeacon.EXPECT().GetBeacon(gomock.Any()).Return(types.RandomBeacon(), nil)
ts.mLyrPatrol.EXPECT().IsHareInCharge(lastSynced).Return(true)
require.ErrorIs(t, ts.syncer.processLayers(context.Background()), errHareInCharge)
require.False(t, ts.syncer.stateSynced())

ts.mBeacon.EXPECT().GetBeacon(gomock.Any()).Return(types.RandomBeacon(), nil)
ts.mLyrPatrol.EXPECT().IsHareInCharge(lastSynced).Return(false)
ts.mDataFetcher.EXPECT().PollLayerOpinions(gomock.Any(), lastSynced).Return(nil, nil)
ts.mTortoise.EXPECT().TallyVotes(gomock.Any(), lastSynced)
Expand All @@ -257,7 +241,6 @@ func TestProcessLayers_HareTakesTooLong(t *testing.T) {
current := lastSynced.Add(1)
ts.mTicker.advanceToLayer(current)
for lid := glayer.Add(1); lid.Before(current); lid = lid.Add(1) {
ts.mBeacon.EXPECT().GetBeacon(gomock.Any()).Return(types.RandomBeacon(), nil)
if lid == glayer.Add(1) {
ts.mLyrPatrol.EXPECT().IsHareInCharge(lid).Return(true)
} else {
Expand All @@ -280,7 +263,6 @@ func TestProcessLayers_OpinionsOptional(t *testing.T) {
lastSynced := types.GetEffectiveGenesis().Add(1)
ts.syncer.setLastSyncedLayer(lastSynced)
ts.mTicker.advanceToLayer(lastSynced.Add(1))
ts.mBeacon.EXPECT().GetBeacon(gomock.Any()).Return(types.RandomBeacon(), nil)
ts.mLyrPatrol.EXPECT().IsHareInCharge(lastSynced).Return(false)
ts.mDataFetcher.EXPECT().PollLayerOpinions(gomock.Any(), lastSynced).Return(nil, errors.New("meh"))
ts.mTortoise.EXPECT().TallyVotes(gomock.Any(), lastSynced)
Expand Down Expand Up @@ -335,7 +317,6 @@ func TestProcessLayers_MeshHashDiverged(t *testing.T) {
epoch := instate.GetEpoch()
errUnknown := errors.New("unknown")

ts.mBeacon.EXPECT().GetBeacon(gomock.Any()).Return(types.RandomBeacon(), nil)
ts.mLyrPatrol.EXPECT().IsHareInCharge(instate).Return(false)
ts.mDataFetcher.EXPECT().PollLayerOpinions(gomock.Any(), instate).Return(opns, nil)
ts.mForkFinder.EXPECT().UpdateAgreement(opns[1].Peer(), instate.Sub(1), prevHash, gomock.Any())
Expand Down Expand Up @@ -407,7 +388,6 @@ func TestProcessLayers_SucceedOnRetry(t *testing.T) {
ts.mTicker.advanceToLayer(current)
ts.syncer.setLastSyncedLayer(current)

ts.mBeacon.EXPECT().GetBeacon(gomock.Any()).Return(types.RandomBeacon(), nil).AnyTimes()
ts.mLyrPatrol.EXPECT().IsHareInCharge(gomock.Any()).Return(false).AnyTimes()
ts.mDataFetcher.EXPECT().PollLayerOpinions(gomock.Any(), gomock.Any()).AnyTimes()
ts.mTortoise.EXPECT().TallyVotes(gomock.Any(), gomock.Any()).AnyTimes()
Expand Down
13 changes: 7 additions & 6 deletions syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ func (s syncState) String() string {
}

var (
errHareInCharge = errors.New("hare in charge of layer")
errATXsNotSynced = errors.New("ATX not synced")
errBeaconNotAvailable = errors.New("beacon not available")
errHareInCharge = errors.New("hare in charge of layer")
errATXsNotSynced = errors.New("ATX not synced")
)

// Option is a type to configure a syncer.
Expand Down Expand Up @@ -376,7 +375,8 @@ func (s *Syncer) synchronize(ctx context.Context) bool {
log.Stringer("current", s.ticker.CurrentLayer()),
log.Stringer("latest", s.mesh.LatestLayer()),
log.Stringer("in_state", s.mesh.LatestLayerInState()),
log.Stringer("processed", s.mesh.ProcessedLayer()))
log.Stringer("processed", s.mesh.ProcessedLayer()),
)

s.setStateBeforeSync(ctx)
// TODO
Expand Down Expand Up @@ -422,10 +422,11 @@ func (s *Syncer) synchronize(ctx context.Context) bool {
s.setStateAfterSync(ctx, success)
s.logger.WithContext(ctx).With().Debug("finished sync run",
log.Bool("success", success),
log.String("sync_state", s.getSyncState().String()),
log.Stringer("sync_state", s.getSyncState()),
log.Stringer("last_synced", s.getLastSyncedLayer()),
log.Stringer("current", s.ticker.CurrentLayer()),
log.Stringer("latest", s.mesh.LatestLayer()),
log.Stringer("last_synced", s.getLastSyncedLayer()),
log.Stringer("in_state", s.mesh.LatestLayerInState()),
log.Stringer("processed", s.mesh.ProcessedLayer()),
)
return success
Expand Down

0 comments on commit 05b9243

Please sign in to comment.