-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathcommon.go
102 lines (81 loc) · 2.46 KB
/
common.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
91
92
93
94
95
96
97
98
99
100
101
102
package presenter
import (
"math"
"math/big"
"github.com/ElrondNetwork/elrond-go/core"
)
const metricNotAvailable = "N/A"
func (psh *PresenterStatusHandler) getFromCacheAsUint64(metric string) uint64 {
val, ok := psh.presenterMetrics.Load(metric)
if !ok {
return 0
}
valUint64, ok := val.(uint64)
if !ok {
return 0
}
return valUint64
}
func (psh *PresenterStatusHandler) getFromCacheAsString(metric string) string {
val, ok := psh.presenterMetrics.Load(metric)
if !ok {
return metricNotAvailable
}
valStr, ok := val.(string)
if !ok {
return metricNotAvailable
}
return valStr
}
func (psh *PresenterStatusHandler) getBigIntFromStringMetric(metric string) *big.Int {
stringValue := psh.getFromCacheAsString(metric)
bigIntValue, ok := big.NewInt(0).SetString(stringValue, 10)
if !ok {
return big.NewInt(0)
}
return bigIntValue
}
func areEqualWithZero(parameters ...uint64) bool {
for _, param := range parameters {
if param == 0 {
return true
}
}
return false
}
func (psh *PresenterStatusHandler) computeChanceToBeInConsensus() float64 {
consensusGroupSize := psh.getFromCacheAsUint64(core.MetricConsensusGroupSize)
numValidators := psh.getFromCacheAsUint64(core.MetricNumValidators)
isChanceZero := areEqualWithZero(consensusGroupSize, numValidators)
if isChanceZero {
return 0
}
return float64(consensusGroupSize) / float64(numValidators)
}
func (psh *PresenterStatusHandler) computeRoundsPerHourAccordingToHitRate() float64 {
totalBlocks := psh.GetProbableHighestNonce()
rounds := psh.GetCurrentRound()
roundDuration := psh.GetRoundTime()
secondsInAnHour := uint64(3600)
isRoundsPerHourZero := areEqualWithZero(totalBlocks, rounds, roundDuration)
if isRoundsPerHourZero {
return 0
}
hitRate := float64(totalBlocks) / float64(rounds)
roundsPerHour := float64(secondsInAnHour) / float64(roundDuration)
return hitRate * roundsPerHour
}
func (psh *PresenterStatusHandler) computeRewardsInErd() *big.Float {
rewardsValue := psh.getBigIntFromStringMetric(core.MetricRewardsValue)
denomination := psh.getFromCacheAsUint64(core.MetricDenomination)
denominationCoefficientFloat := 1.0
if denomination > 0 {
denominationCoefficientFloat /= math.Pow10(int(denomination))
}
denominationCoefficient := big.NewFloat(denominationCoefficientFloat)
if rewardsValue.Cmp(big.NewInt(0)) <= 0 {
return big.NewFloat(0)
}
rewardsInErd := big.NewFloat(0).Mul(big.NewFloat(0).SetInt(rewardsValue), denominationCoefficient)
return rewardsInErd
}