Skip to content

Commit

Permalink
fix: fix issues found by @Wondertan
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Jan 30, 2024
1 parent 0070afe commit 0fc8a99
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
32 changes: 16 additions & 16 deletions sync/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type metrics struct {

syncLoopStarted metric.Int64Counter
trustedPeersOutOfSync metric.Int64Counter
laggingHeadersStart metric.Int64Counter
readHeader metric.Int64Counter
unrecentHeader metric.Int64Counter
subjectiveInit metric.Int64Counter

subjectiveHead atomic.Int64
subjectiveHeadReg metric.Registration
Expand All @@ -36,25 +36,25 @@ func newMetrics() (*metrics, error) {
}

trustedPeersOutOfSync, err := meter.Int64Counter(
"hdr_tr_peers_out_of_sync",
metric.WithDescription("trusted peers out of sync"),
"hdr_sync_trust_peers_out_of_sync",
metric.WithDescription("trusted peers out of sync and gave outdated header"),
)
if err != nil {
return nil, err
}

laggingHeadersStart, err := meter.Int64Counter(
"hdr_sync_lagging_hdr_start",
metric.WithDescription("lagging header start"),
unrecentHeader, err := meter.Int64Counter(
"hdr_sync_unrecent_header",
metric.WithDescription("tracks every time Syncer returns an unrecent header"),
)
if err != nil {
return nil, err
}

readHeader, err := meter.Int64Counter(
"hdr_sync_getter_read",
subjectiveInit, err := meter.Int64Counter(
"hdr_sync_subjective_init",
metric.WithDescription(
"sync getter used to get the header instead of receiving it through the subscription",
"tracks how many times is the node initialized ",
),
)
if err != nil {
Expand All @@ -80,8 +80,8 @@ func newMetrics() (*metrics, error) {
m := &metrics{
syncLoopStarted: syncLoopStarted,
trustedPeersOutOfSync: trustedPeersOutOfSync,
laggingHeadersStart: laggingHeadersStart,
readHeader: readHeader,
unrecentHeader: unrecentHeader,
subjectiveInit: subjectiveInit,
blockTime: blockTime,
subjectiveHeadInst: subjectiveHead,
}
Expand All @@ -107,19 +107,19 @@ func (m *metrics) syncingStarted(ctx context.Context) {

func (m *metrics) laggingNetworkHead(ctx context.Context) {
m.observe(ctx, func(ctx context.Context) {
m.laggingHeadersStart.Add(ctx, 1)
m.unrecentHeader.Add(ctx, 1)
})
}

func (m *metrics) peersOutOufSync(ctx context.Context) {
func (m *metrics) trustedPeersOutOufSync(ctx context.Context) {
m.observe(ctx, func(ctx context.Context) {
m.trustedPeersOutOfSync.Add(ctx, 1)
})
}

func (m *metrics) readHeaderGetter(ctx context.Context) {
func (m *metrics) subjectiveInitialization(ctx context.Context) {
m.observe(ctx, func(ctx context.Context) {
m.readHeader.Add(ctx, 1)
m.subjectiveInit.Add(ctx, 1)
})
}

Expand Down
9 changes: 4 additions & 5 deletions sync/sync_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func (s *Syncer[H]) Head(ctx context.Context, _ ...header.HeadOption[H]) (H, err
if isRecent(sbjHead, s.Params.blockTime, s.Params.recencyThreshold) {
return sbjHead, nil
}
s.metrics.laggingNetworkHead(s.ctx)
// otherwise, request head from the network
//
// TODO(@Wondertan): Here is another potential networking optimization:
Expand All @@ -38,8 +37,8 @@ func (s *Syncer[H]) Head(ctx context.Context, _ ...header.HeadOption[H]) (H, err
// so just recursively get it
return s.Head(ctx)
}
s.metrics.laggingNetworkHead(s.ctx)
defer s.getter.Unlock()
s.metrics.readHeaderGetter(s.ctx)
netHead, err := s.getter.Head(ctx, header.WithTrustedHead[H](sbjHead))
if err != nil {
log.Warnw("failed to get recent head, returning current subjective", "sbjHead", sbjHead.Height(), "err", err)
Expand Down Expand Up @@ -86,11 +85,12 @@ func (s *Syncer[H]) subjectiveHead(ctx context.Context) (H, error) {
return s.subjectiveHead(ctx)
}
defer s.getter.Unlock()
s.metrics.readHeaderGetter(s.ctx)

trustHead, err := s.getter.Head(ctx)
if err != nil {
return trustHead, err
}
s.metrics.subjectiveInitialization(s.ctx)
// and set it as the new subjective head without validation,
// or, in other words, do 'automatic subjective initialization'
// NOTE: we avoid validation as the head expired to prevent possibility of the Long-Range Attack
Expand All @@ -102,11 +102,10 @@ func (s *Syncer[H]) subjectiveHead(ctx context.Context) (H, error) {
case isExpired(trustHead, s.Params.TrustingPeriod):
log.Warnw("subjective initialization with an expired header", "height", trustHead.Height())
case !isRecent(trustHead, s.Params.blockTime, s.Params.recencyThreshold):
s.metrics.laggingNetworkHead(s.ctx)
log.Warnw("subjective initialization with an old header", "height", trustHead.Height())
}
log.Warn("trusted peer is out of sync")
s.metrics.peersOutOufSync(s.ctx)
s.metrics.trustedPeersOutOufSync(s.ctx)
return trustHead, nil
}

Expand Down

0 comments on commit 0fc8a99

Please sign in to comment.