Skip to content

Commit 21b31ff

Browse files
authored
chore(syncer): improved logging (#315)
The tail estimation may take a surprisingly long time, and these logs can help to understand better what's going on. EDIT: I also added other logging improvements I've collected along the way.
1 parent f82b73b commit 21b31ff

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

sync/syncer_head.go

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,19 @@ func (s *Syncer[H]) networkHead(ctx context.Context) (H, bool, error) {
5454
if err != nil {
5555
return sbjHead, false, fmt.Errorf("subjective head: %w", err)
5656
}
57-
if isRecent(sbjHead, s.Params.blockTime, s.Params.recencyThreshold) || initialized {
57+
recent, timeDiff := isRecent(sbjHead, s.Params.blockTime, s.Params.recencyThreshold)
58+
if recent || initialized {
5859
return sbjHead, initialized, nil
5960
}
6061

6162
s.metrics.outdatedHead(ctx)
62-
log.Warnw("outdated subjective head", "outdated_height", sbjHead.Height())
63+
log.Warnw(
64+
"non recent subjective head",
65+
"height",
66+
sbjHead.Height(),
67+
"non_recent_for",
68+
timeDiff.String(),
69+
)
6370
log.Warnw("attempting to request the most recent network head...")
6471

6572
// cap the max blocking time for the request call
@@ -85,9 +92,15 @@ func (s *Syncer[H]) networkHead(ctx context.Context) (H, bool, error) {
8592

8693
return sbjHead, false, nil
8794
}
88-
// still check if even the newly requested head is outdated
89-
if !isRecent(newHead, s.Params.blockTime, s.Params.recencyThreshold) {
90-
log.Warnw("non recent head from trusted peers", "height", newHead.Height())
95+
// still check if even the newly requested head is not recent
96+
if recent, timeDiff = isRecent(newHead, s.Params.blockTime, s.Params.recencyThreshold); !recent {
97+
log.Warnw(
98+
"non recent head from trusted peers",
99+
"height",
100+
newHead.Height(),
101+
"non_recent_for",
102+
timeDiff.String(),
103+
)
91104
log.Error("trusted peers are out of sync")
92105
s.metrics.trustedPeersOutOufSync(ctx)
93106
}
@@ -115,15 +128,18 @@ func (s *Syncer[H]) networkHead(ctx context.Context) (H, bool, error) {
115128
// Reports true if initialization was performed, false otherwise.
116129
func (s *Syncer[H]) subjectiveHead(ctx context.Context) (H, bool, error) {
117130
sbjHead, err := s.localHead(ctx)
118-
switch {
119-
case errors.Is(err, header.ErrEmptyStore):
120-
log.Info("empty store, initializing...")
121-
case !sbjHead.IsZero() && isExpired(sbjHead, s.Params.TrustingPeriod):
131+
132+
switch expired, expiredFor := isExpired(sbjHead, s.Params.TrustingPeriod); {
133+
case expired:
122134
log.Infow(
123135
"subjective head expired, reinitializing...",
124136
"expired_height",
125137
sbjHead.Height(),
138+
"expired_for",
139+
expiredFor.String(),
126140
)
141+
case errors.Is(err, header.ErrEmptyStore):
142+
log.Info("empty store, initializing...")
127143
case err != nil:
128144
return sbjHead, false, fmt.Errorf("local head: %w", err)
129145
default:
@@ -136,9 +152,13 @@ func (s *Syncer[H]) subjectiveHead(ctx context.Context) (H, bool, error) {
136152
return newHead, false, fmt.Errorf("exchange head: %w", err)
137153
}
138154
// still check if even the newly requested head is expired
139-
if isExpired(newHead, s.Params.TrustingPeriod) {
155+
if expired, expiredFor := isExpired(newHead, s.Params.TrustingPeriod); expired {
140156
// forbid initializing off an expired header
141-
err := fmt.Errorf("subjective initialization with an expired header(%d)", newHead.Height())
157+
err := fmt.Errorf(
158+
"subjective initialization with header(%d) expired for %s",
159+
newHead.Height(),
160+
expiredFor.String(),
161+
)
142162
log.Error(err)
143163
log.Error("trusted peers are out of sync")
144164
s.metrics.trustedPeersOutOufSync(ctx)
@@ -304,16 +324,29 @@ func (s *Syncer[H]) verifyBifurcating(ctx context.Context, subjHead, newHead H)
304324
}
305325
}
306326

307-
// isExpired checks if header is expired against trusting period.
308-
func isExpired[H header.Header[H]](header H, period time.Duration) bool {
327+
// isExpired checks if header is expired against trusting period and reports duration it is
328+
// expired for.
329+
func isExpired[H header.Header[H]](header H, period time.Duration) (bool, time.Duration) {
330+
if header.IsZero() {
331+
return false, 0
332+
}
333+
309334
expirationTime := header.Time().Add(period)
310-
return expirationTime.Before(time.Now())
335+
diff := time.Since(expirationTime)
336+
return diff > 0, diff
311337
}
312338

313-
// isRecent checks if header is recent against the given recency threshold.
314-
func isRecent[H header.Header[H]](header H, blockTime, recencyThreshold time.Duration) bool {
339+
// isRecent checks if the given header is close to the tip against the given recency threshold and reports duration
340+
// it is outdated for.
341+
func isRecent[H header.Header[H]](
342+
header H,
343+
blockTime, recencyThreshold time.Duration,
344+
) (bool, time.Duration) {
315345
if recencyThreshold == 0 {
316346
recencyThreshold = blockTime * 2 // allow some drift by adding additional buffer of 2 blocks
317347
}
318-
return time.Since(header.Time()) <= recencyThreshold
348+
349+
recencyTime := header.Time().Add(recencyThreshold)
350+
diff := time.Since(recencyTime)
351+
return diff <= 0, diff
319352
}

sync/syncer_tail.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ func (s *Syncer[H]) renewTail(ctx context.Context, oldTail, head H) (newTail H,
7070
)
7171
}
7272

73-
log.Debugw("tail hash not available locally, fetching...", "hash", tailHash)
7473
newTail, err = s.getter.Get(ctx, tailHash)
7574
if err != nil {
7675
return newTail, fmt.Errorf("fetching SyncFromHash tail(%x): %w", tailHash, err)
7776
}
77+
log.Debugw("fetched tail header by hash", "hash", tailHash)
7878
case !useHash:
7979
tailHeight, err := s.tailHeight(ctx, oldTail, head)
8080
if err != nil {
@@ -96,11 +96,11 @@ func (s *Syncer[H]) renewTail(ctx context.Context, oldTail, head H) (newTail H,
9696
}
9797
}
9898

99-
log.Debugw("tail height not available locally, fetching...", "height", tailHeight)
10099
newTail, err = s.getter.GetByHeight(ctx, tailHeight)
101100
if err != nil {
102101
return newTail, fmt.Errorf("fetching SyncFromHeight tail(%d): %w", tailHeight, err)
103102
}
103+
log.Debugw("fetched tail header by height", "height", tailHeight)
104104
}
105105

106106
if err := s.store.Append(ctx, newTail); err != nil {

0 commit comments

Comments
 (0)