/
checker_helpers.go
100 lines (85 loc) · 3.08 KB
/
checker_helpers.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
package gossip
import (
"sync/atomic"
"github.com/TechPay-io/sirius-base/hash"
"github.com/TechPay-io/sirius-base/inter/idx"
"github.com/TechPay-io/sirius-base/inter/pos"
"github.com/TechPay-io/go-photon/eventcheck/gaspowercheck"
"github.com/TechPay-io/go-photon/inter"
"github.com/TechPay-io/go-photon/inter/validatorpk"
"github.com/TechPay-io/go-photon/photon"
)
// GasPowerCheckReader is a helper to run gas power check
type GasPowerCheckReader struct {
Ctx atomic.Value
}
// GetValidationContext returns current validation context for gaspowercheck
func (r *GasPowerCheckReader) GetValidationContext() *gaspowercheck.ValidationContext {
return r.Ctx.Load().(*gaspowercheck.ValidationContext)
}
// NewGasPowerContext reads current validation context for gaspowercheck
func NewGasPowerContext(s *Store, validators *pos.Validators, epoch idx.Epoch, cfg photon.EconomyRules) *gaspowercheck.ValidationContext {
// engineMu is locked here
short := cfg.ShortGasPower
shortTermConfig := gaspowercheck.Config{
Idx: inter.ShortTermGas,
AllocPerSec: short.AllocPerSec,
MaxAllocPeriod: short.MaxAllocPeriod,
MinEnsuredAlloc: cfg.Gas.MaxEventGas,
StartupAllocPeriod: short.StartupAllocPeriod,
MinStartupGas: short.MinStartupGas,
}
long := cfg.LongGasPower
longTermConfig := gaspowercheck.Config{
Idx: inter.LongTermGas,
AllocPerSec: long.AllocPerSec,
MaxAllocPeriod: long.MaxAllocPeriod,
MinEnsuredAlloc: cfg.Gas.MaxEventGas,
StartupAllocPeriod: long.StartupAllocPeriod,
MinStartupGas: long.MinStartupGas,
}
validatorStates := make([]gaspowercheck.ValidatorState, validators.Len())
es := s.GetEpochState()
for i, val := range es.ValidatorStates {
validatorStates[i].GasRefund = val.GasRefund
if val.PrevEpochEvent != hash.ZeroEvent {
validatorStates[i].PrevEpochEvent = s.GetEvent(val.PrevEpochEvent)
}
}
return &gaspowercheck.ValidationContext{
Epoch: epoch,
Validators: validators,
EpochStart: es.EpochStart,
ValidatorStates: validatorStates,
Configs: [inter.GasPowerConfigs]gaspowercheck.Config{
inter.ShortTermGas: shortTermConfig,
inter.LongTermGas: longTermConfig,
},
}
}
// ValidatorsPubKeys stores info to authenticate validators
type ValidatorsPubKeys struct {
Epoch idx.Epoch
PubKeys map[idx.ValidatorID]validatorpk.PubKey
}
// HeavyCheckReader is a helper to run heavy power checks
type HeavyCheckReader struct {
Addrs atomic.Value
}
// GetEpochPubKeys is safe for concurrent use
func (r *HeavyCheckReader) GetEpochPubKeys() (map[idx.ValidatorID]validatorpk.PubKey, idx.Epoch) {
auth := r.Addrs.Load().(*ValidatorsPubKeys)
return auth.PubKeys, auth.Epoch
}
// NewEpochPubKeys is the same as GetEpochValidators, but returns only addresses
func NewEpochPubKeys(s *Store, epoch idx.Epoch) *ValidatorsPubKeys {
es := s.GetEpochState()
pubkeys := make(map[idx.ValidatorID]validatorpk.PubKey, len(es.ValidatorProfiles))
for id, profile := range es.ValidatorProfiles {
pubkeys[id] = profile.PubKey
}
return &ValidatorsPubKeys{
Epoch: epoch,
PubKeys: pubkeys,
}
}