forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 217
/
l1_rollup_info_producer_statistics.go
90 lines (79 loc) · 3.18 KB
/
l1_rollup_info_producer_statistics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package synchronizer
import (
"fmt"
"time"
"github.com/0xPolygonHermez/zkevm-node/synchronizer/metrics"
)
// This object keep track of the statistics of the process, to be able to estimate the ETA
type l1RollupInfoProducerStatistics struct {
initialBlockNumber uint64
lastBlockNumber uint64
numRollupInfoOk uint64
numRollupInfoErrors uint64
numRetrievedBlocks uint64
startTime time.Time
lastShowUpTime time.Time
accumulatedTimeProcessingRollup time.Duration
timeProvider TimeProvider
}
func newRollupInfoProducerStatistics(startingBlockNumber uint64, timeProvider TimeProvider) l1RollupInfoProducerStatistics {
return l1RollupInfoProducerStatistics{
initialBlockNumber: startingBlockNumber,
startTime: timeProvider.Now(),
timeProvider: timeProvider,
accumulatedTimeProcessingRollup: time.Duration(0),
}
}
func (l *l1RollupInfoProducerStatistics) reset(startingBlockNumber uint64) {
l.initialBlockNumber = startingBlockNumber
l.startTime = l.timeProvider.Now()
l.numRollupInfoOk = 0
l.numRollupInfoErrors = 0
l.numRetrievedBlocks = 0
l.lastShowUpTime = l.timeProvider.Now()
}
func (l *l1RollupInfoProducerStatistics) updateLastBlockNumber(lastBlockNumber uint64) {
l.lastBlockNumber = lastBlockNumber
}
func (l *l1RollupInfoProducerStatistics) onResponseRollupInfo(result responseRollupInfoByBlockRange) {
metrics.ReadL1DataTime(result.generic.duration)
isOk := (result.generic.err == nil)
if isOk {
l.numRollupInfoOk++
l.numRetrievedBlocks += uint64(result.result.blockRange.len())
l.accumulatedTimeProcessingRollup += result.generic.duration
} else {
l.numRollupInfoErrors++
}
}
func (l *l1RollupInfoProducerStatistics) getStatisticsDebugString() string {
numTotalOfBlocks := l.lastBlockNumber - l.initialBlockNumber
if l.numRetrievedBlocks == 0 {
return "N/A"
}
now := l.timeProvider.Now()
elapsedTime := now.Sub(l.startTime)
eta := l.getEstimatedTimeOfArrival()
percent := l.getPercent()
blocksPerSeconds := l.getBlocksPerSecond(elapsedTime)
return fmt.Sprintf(" EstimatedTimeOfArrival: %s percent:%2.2f blocks_per_seconds:%2.2f pending_block:%v/%v num_errors:%v",
eta, percent, blocksPerSeconds, l.numRetrievedBlocks, numTotalOfBlocks, l.numRollupInfoErrors)
}
func (l *l1RollupInfoProducerStatistics) getEstimatedTimeOfArrival() time.Duration {
numTotalOfBlocks := l.lastBlockNumber - l.initialBlockNumber
if l.numRetrievedBlocks == 0 {
return time.Duration(0)
}
elapsedTime := time.Since(l.startTime)
eta := time.Duration(float64(elapsedTime) / float64(l.numRetrievedBlocks) * float64(numTotalOfBlocks-l.numRetrievedBlocks))
return eta
}
func (l *l1RollupInfoProducerStatistics) getPercent() float64 {
numTotalOfBlocks := l.lastBlockNumber - l.initialBlockNumber
percent := float64(l.numRetrievedBlocks) / float64(numTotalOfBlocks) * conversionFactorPercentage
return percent
}
func (l *l1RollupInfoProducerStatistics) getBlocksPerSecond(elapsedTime time.Duration) float64 {
blocksPerSeconds := float64(l.numRetrievedBlocks) / float64(elapsedTime.Seconds())
return blocksPerSeconds
}