diff --git a/sync/metrics.go b/sync/metrics.go index c0fe7e4e..5dc00248 100644 --- a/sync/metrics.go +++ b/sync/metrics.go @@ -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 @@ -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 { @@ -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, } @@ -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) }) } diff --git a/sync/sync_head.go b/sync/sync_head.go index 9fd94807..b4e2afe6 100644 --- a/sync/sync_head.go +++ b/sync/sync_head.go @@ -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: @@ -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) @@ -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 @@ -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 }