Skip to content

Commit

Permalink
sync: parametrize out of sync threshold and set it to 3h for mainnet
Browse files Browse the repository at this point in the history
fix tests
  • Loading branch information
dshulyak committed Sep 20, 2023
1 parent 2b1c6d9 commit 4c69d42
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 35 deletions.
13 changes: 7 additions & 6 deletions config/mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,13 @@ func MainnetConfig() Config {
FETCH: fetch.DefaultConfig(),
LOGGING: defaultLoggingConfig(),
Sync: syncer.Config{
Interval: time.Minute,
EpochEndFraction: 0.8,
MaxStaleDuration: time.Hour,
UseNewProtocol: true,
Standalone: false,
GossipDuration: 50 * time.Second,
Interval: time.Minute,
EpochEndFraction: 0.8,
MaxStaleDuration: time.Hour,
UseNewProtocol: true,
Standalone: false,
GossipDuration: 50 * time.Second,
OutOfSyncThreshold: 36,
},
Recovery: checkpoint.DefaultConfig(),
Cache: datastore.DefaultConfig(),
Expand Down
42 changes: 20 additions & 22 deletions syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,31 @@ import (

// Config is the config params for syncer.
type Config struct {
Interval time.Duration
EpochEndFraction float64
HareDelayLayers uint32
SyncCertDistance uint32
MaxStaleDuration time.Duration
Standalone bool
UseNewProtocol bool `mapstructure:"use-new-opn"`
GossipDuration time.Duration
Interval time.Duration
EpochEndFraction float64
HareDelayLayers uint32
SyncCertDistance uint32
MaxStaleDuration time.Duration
Standalone bool
UseNewProtocol bool `mapstructure:"use-new-opn"`
GossipDuration time.Duration
OutOfSyncThreshold uint32 `mapstructure:"out-of-sync-threshold"`
}

// DefaultConfig for the syncer.
func DefaultConfig() Config {
return Config{
Interval: 10 * time.Second,
EpochEndFraction: 0.8,
HareDelayLayers: 10,
SyncCertDistance: 10,
MaxStaleDuration: time.Second,
UseNewProtocol: true,
GossipDuration: 15 * time.Second,
Interval: 10 * time.Second,
EpochEndFraction: 0.8,
HareDelayLayers: 10,
SyncCertDistance: 10,
MaxStaleDuration: time.Second,
UseNewProtocol: true,
GossipDuration: 15 * time.Second,
OutOfSyncThreshold: 3,
}
}

const (
outOfSyncThreshold uint32 = 3 // see notSynced
)

type syncState uint32

const (
Expand Down Expand Up @@ -454,7 +452,7 @@ func (s *Syncer) syncAtx(ctx context.Context) error {
return nil
}

func isTooFarBehind(ctx context.Context, logger log.Log, current, lastSynced types.LayerID) bool {
func isTooFarBehind(ctx context.Context, logger log.Log, current, lastSynced types.LayerID, outOfSyncThreshold uint32) bool {
if current.After(lastSynced) && current.Difference(lastSynced) >= outOfSyncThreshold {
logger.WithContext(ctx).With().Info("node is too far behind",
log.Stringer("current", current),
Expand All @@ -474,7 +472,7 @@ func (s *Syncer) setStateBeforeSync(ctx context.Context) {
}
return
}
if isTooFarBehind(ctx, s.logger, current, s.getLastSyncedLayer()) {
if isTooFarBehind(ctx, s.logger, current, s.getLastSyncedLayer(), s.cfg.OutOfSyncThreshold) {
s.setSyncState(ctx, notSynced)
}
}
Expand All @@ -494,7 +492,7 @@ func (s *Syncer) setStateAfterSync(ctx context.Context, success bool) {
// network outage.
switch currSyncState {
case synced:
if !success && isTooFarBehind(ctx, s.logger, current, s.getLastSyncedLayer()) {
if !success && isTooFarBehind(ctx, s.logger, current, s.getLastSyncedLayer(), s.cfg.OutOfSyncThreshold) {
s.setSyncState(ctx, notSynced)
}
case gossipSync:
Expand Down
17 changes: 10 additions & 7 deletions syncer/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
const (
layersPerEpoch = 3
never = time.Second * 60 * 24

outOfSyncThreshold = 3
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -95,12 +97,13 @@ func newTestSyncer(t *testing.T, interval time.Duration) *testSyncer {
require.NoError(t, err)

cfg := Config{
Interval: interval,
GossipDuration: 5 * time.Millisecond,
EpochEndFraction: 0.66,
SyncCertDistance: 4,
HareDelayLayers: 5,
UseNewProtocol: true,
Interval: interval,
GossipDuration: 5 * time.Millisecond,
EpochEndFraction: 0.66,
SyncCertDistance: 4,
HareDelayLayers: 5,
UseNewProtocol: true,
OutOfSyncThreshold: outOfSyncThreshold,
}
ts.syncer = NewSyncer(ts.cdb, ts.mTicker, ts.mBeacon, ts.msh, nil, nil, ts.mLyrPatrol, ts.mCertHdr,
WithConfig(cfg),
Expand Down Expand Up @@ -539,7 +542,7 @@ func TestNetworkHasNoData(t *testing.T) {
require.True(t, ts.syncer.IsSynced(context.Background()))
}
// the network hasn't received any data
require.Greater(t, ts.syncer.ticker.CurrentLayer()-ts.msh.LatestLayer(), outOfSyncThreshold)
require.Greater(t, int(ts.syncer.ticker.CurrentLayer()-ts.msh.LatestLayer()), outOfSyncThreshold)
}

// test the case where the node was originally synced, and somehow gets out of sync, but
Expand Down

0 comments on commit 4c69d42

Please sign in to comment.