Skip to content

Commit

Permalink
Added metrics to track wiggles in polygon (#12888)
Browse files Browse the repository at this point in the history
  • Loading branch information
eastorski authored Nov 28, 2024
1 parent 16db7ba commit 1823fa2
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 7 deletions.
12 changes: 11 additions & 1 deletion eth/stagedsync/stage_polygon_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (

"golang.org/x/sync/errgroup"

lru "github.com/hashicorp/golang-lru/arc/v2"

"github.com/erigontech/erigon-lib/chain"
"github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/common/generics"
Expand All @@ -44,6 +46,7 @@ import (
"github.com/erigontech/erigon/polygon/bridge"
"github.com/erigontech/erigon/polygon/heimdall"
"github.com/erigontech/erigon/polygon/p2p"
"github.com/erigontech/erigon/polygon/sync"
polygonsync "github.com/erigontech/erigon/polygon/sync"
"github.com/erigontech/erigon/turbo/services"
"github.com/erigontech/erigon/turbo/shards"
Expand Down Expand Up @@ -115,6 +118,12 @@ func NewPolygonSyncStageCfg(
checkpointVerifier := polygonsync.VerifyCheckpointHeaders
milestoneVerifier := polygonsync.VerifyMilestoneHeaders
blocksVerifier := polygonsync.VerifyBlocks

signaturesCache, err := lru.NewARC[common.Hash, common.Address](sync.InMemorySignatures)
if err != nil {
panic(err)
}

syncStore := polygonsync.NewStore(logger, executionEngine, bridgeService)
blockDownloader := polygonsync.NewBlockDownloader(
logger,
Expand All @@ -135,11 +144,12 @@ func NewPolygonSyncStageCfg(
blocksVerifier,
p2pService,
blockDownloader,
polygonsync.NewCanonicalChainBuilderFactory(chainConfig, borConfig, heimdallService),
polygonsync.NewCanonicalChainBuilderFactory(chainConfig, borConfig, heimdallService, signaturesCache),
heimdallService,
bridgeService,
events.Events(),
notifications,
sync.NewWiggleCalculator(borConfig, signaturesCache, heimdallService),
)
syncService := &polygonSyncStageService{
logger: logger,
Expand Down
6 changes: 1 addition & 5 deletions polygon/sync/canonical_chain_builder_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@ func NewCanonicalChainBuilderFactory(
chainConfig *chain.Config,
borConfig *borcfg.BorConfig,
blockProducersReader blockProducersReader,
signaturesCache *lru.ARCCache[common.Hash, common.Address],
) CanonicalChainBuilderFactory {
signaturesCache, err := lru.NewARC[common.Hash, common.Address](InMemorySignatures)
if err != nil {
panic(err)
}

difficultyCalculator := &DifficultyCalculator{
borConfig: borConfig,
signaturesCache: signaturesCache,
Expand Down
31 changes: 31 additions & 0 deletions polygon/sync/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 The Erigon Authors
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.

package sync

import (
"time"

"github.com/erigontech/erigon-lib/metrics"
)

var (
wiggleDuration = metrics.NewSummary(`wiggle_duration`)
)

func UpdateWiggleDuration(duration time.Duration) {
wiggleDuration.ObserveDuration(time.Now().Add(-duration))
}
12 changes: 11 additions & 1 deletion polygon/sync/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import (

"golang.org/x/sync/errgroup"

lru "github.com/hashicorp/golang-lru/arc/v2"

"github.com/erigontech/erigon-lib/chain"
"github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/gointerfaces/executionproto"
"github.com/erigontech/erigon-lib/gointerfaces/sentryproto"
"github.com/erigontech/erigon-lib/log/v3"
Expand Down Expand Up @@ -52,6 +55,12 @@ func NewService(
blocksVerifier := VerifyBlocks
p2pService := p2p.NewService(logger, maxPeers, sentryClient, statusDataProvider.GetStatusData)
execution := newExecutionClient(executionClient)

signaturesCache, err := lru.NewARC[common.Hash, common.Address](InMemorySignatures)
if err != nil {
panic(err)
}

store := NewStore(logger, execution, bridgeService)
blockDownloader := NewBlockDownloader(
logger,
Expand All @@ -63,7 +72,7 @@ func NewService(
store,
blockLimit,
)
ccBuilderFactory := NewCanonicalChainBuilderFactory(chainConfig, borConfig, heimdallService)
ccBuilderFactory := NewCanonicalChainBuilderFactory(chainConfig, borConfig, heimdallService, signaturesCache)
events := NewTipEvents(logger, p2pService, heimdallService)
sync := NewSync(
logger,
Expand All @@ -78,6 +87,7 @@ func NewService(
bridgeService,
events.Events(),
notifications,
NewWiggleCalculator(borConfig, signaturesCache, heimdallService),
)
return &Service{
logger: logger,
Expand Down
27 changes: 27 additions & 0 deletions polygon/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ type bridgeSynchronizer interface {
Ready(ctx context.Context) <-chan error
}

type wiggleCalculator interface {
CalculateWiggle(ctx context.Context, header *types.Header) (time.Duration, error)
}

func NewSync(
logger log.Logger,
store Store,
Expand All @@ -59,6 +63,7 @@ func NewSync(
bridgeSync bridgeSynchronizer,
events <-chan Event,
notifications *shards.Notifications,
wiggleCalculator wiggleCalculator,
) *Sync {
badBlocksLru, err := simplelru.NewLRU[common.Hash, struct{}](1024, nil)
if err != nil {
Expand All @@ -79,6 +84,7 @@ func NewSync(
events: events,
badBlocks: badBlocksLru,
notifications: notifications,
wiggleCalculator: wiggleCalculator,
}
}

Expand All @@ -96,6 +102,7 @@ type Sync struct {
events <-chan Event
badBlocks *simplelru.LRU[common.Hash, struct{}]
notifications *shards.Notifications
wiggleCalculator wiggleCalculator
}

func (s *Sync) commitExecution(ctx context.Context, newTip *types.Header, finalizedHeader *types.Header) error {
Expand Down Expand Up @@ -306,6 +313,26 @@ func (s *Sync) applyNewBlockOnTip(ctx context.Context, event EventNewBlock, ccb
return nil
}

go func() {
for i := range newConnectedHeaders {
select {
case <-ctx.Done():
return
default:
wiggle, err := s.wiggleCalculator.CalculateWiggle(ctx, newConnectedHeaders[i])
if err != nil {
s.logger.Error(
syncLogPrefix("failed update wiggle metrics"),
"err", err,
)
continue
}

UpdateWiggleDuration(wiggle)
}
}
}()

newTip := ccb.Tip()
firstNewConnectedHeader := newConnectedHeaders[0]
if newTip != oldTip && oldTip.Hash() != firstNewConnectedHeader.ParentHash {
Expand Down
62 changes: 62 additions & 0 deletions polygon/sync/wiggle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2024 The Erigon Authors
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.

package sync

import (
"context"
"time"

libcommon "github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon/core/types"
"github.com/erigontech/erigon/polygon/bor"
"github.com/erigontech/erigon/polygon/bor/borcfg"
lru "github.com/hashicorp/golang-lru/arc/v2"
)

type wiggleCalc struct {
borConfig *borcfg.BorConfig
signaturesCache *lru.ARCCache[libcommon.Hash, libcommon.Address]
blockProducersReader blockProducersReader
}

func NewWiggleCalculator(borConfig *borcfg.BorConfig, signaturesCache *lru.ARCCache[libcommon.Hash, libcommon.Address], blockProducersReader blockProducersReader) *wiggleCalc {
return &wiggleCalc{
borConfig: borConfig,
signaturesCache: signaturesCache,
blockProducersReader: blockProducersReader,
}
}

func (calc *wiggleCalc) CalculateWiggle(ctx context.Context, header *types.Header) (time.Duration, error) {
signer, err := bor.Ecrecover(header, calc.signaturesCache, calc.borConfig)
if err != nil {
return 0, err
}

producers, err := calc.blockProducersReader.Producers(ctx, header.Number.Uint64())
if err != nil {
return 0, err
}

succession, err := producers.GetSignerSuccessionNumber(signer, header.Number.Uint64())
if err != nil {
return 0, err
}

wiggle := time.Duration(succession) * time.Duration(calc.borConfig.CalculateBackupMultiplier(header.Number.Uint64())) * time.Second
return wiggle, nil
}

0 comments on commit 1823fa2

Please sign in to comment.