github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/gossip/checker_helpers.go (about) 1 package gossip 2 3 import ( 4 "sync/atomic" 5 6 "github.com/unicornultrafoundation/go-helios/native/idx" 7 "github.com/unicornultrafoundation/go-helios/native/pos" 8 9 "github.com/unicornultrafoundation/go-u2u/eventcheck/gaspowercheck" 10 "github.com/unicornultrafoundation/go-u2u/native" 11 "github.com/unicornultrafoundation/go-u2u/native/validatorpk" 12 "github.com/unicornultrafoundation/go-u2u/u2u" 13 ) 14 15 // GasPowerCheckReader is a helper to run gas power check 16 type GasPowerCheckReader struct { 17 Ctx atomic.Value 18 } 19 20 // GetValidationContext returns current validation context for gaspowercheck 21 func (r *GasPowerCheckReader) GetValidationContext() *gaspowercheck.ValidationContext { 22 return r.Ctx.Load().(*gaspowercheck.ValidationContext) 23 } 24 25 // NewGasPowerContext reads current validation context for gaspowercheck 26 func NewGasPowerContext(s *Store, validators *pos.Validators, epoch idx.Epoch, cfg u2u.EconomyRules) *gaspowercheck.ValidationContext { 27 // engineMu is locked here 28 29 short := cfg.ShortGasPower 30 shortTermConfig := gaspowercheck.Config{ 31 Idx: native.ShortTermGas, 32 AllocPerSec: short.AllocPerSec, 33 MaxAllocPeriod: short.MaxAllocPeriod, 34 MinEnsuredAlloc: cfg.Gas.MaxEventGas, 35 StartupAllocPeriod: short.StartupAllocPeriod, 36 MinStartupGas: short.MinStartupGas, 37 } 38 39 long := cfg.LongGasPower 40 longTermConfig := gaspowercheck.Config{ 41 Idx: native.LongTermGas, 42 AllocPerSec: long.AllocPerSec, 43 MaxAllocPeriod: long.MaxAllocPeriod, 44 MinEnsuredAlloc: cfg.Gas.MaxEventGas, 45 StartupAllocPeriod: long.StartupAllocPeriod, 46 MinStartupGas: long.MinStartupGas, 47 } 48 49 validatorStates := make([]gaspowercheck.ValidatorState, validators.Len()) 50 es := s.GetEpochState() 51 for i, val := range es.ValidatorStates { 52 validatorStates[i].GasRefund = val.GasRefund 53 validatorStates[i].PrevEpochEvent = val.PrevEpochEvent 54 } 55 56 return &gaspowercheck.ValidationContext{ 57 Epoch: epoch, 58 Validators: validators, 59 EpochStart: es.EpochStart, 60 ValidatorStates: validatorStates, 61 Configs: [native.GasPowerConfigs]gaspowercheck.Config{ 62 native.ShortTermGas: shortTermConfig, 63 native.LongTermGas: longTermConfig, 64 }, 65 } 66 } 67 68 // ValidatorsPubKeys stores info to authenticate validators 69 type ValidatorsPubKeys struct { 70 Epoch idx.Epoch 71 PubKeys map[idx.ValidatorID]validatorpk.PubKey 72 } 73 74 // HeavyCheckReader is a helper to run heavy power checks 75 type HeavyCheckReader struct { 76 Pubkeys atomic.Value 77 Store *Store 78 } 79 80 // GetEpochPubKeys is safe for concurrent use 81 func (r *HeavyCheckReader) GetEpochPubKeys() (map[idx.ValidatorID]validatorpk.PubKey, idx.Epoch) { 82 auth := r.Pubkeys.Load().(*ValidatorsPubKeys) 83 84 return auth.PubKeys, auth.Epoch 85 } 86 87 // GetEpochPubKeysOf is safe for concurrent use 88 func (r *HeavyCheckReader) GetEpochPubKeysOf(epoch idx.Epoch) map[idx.ValidatorID]validatorpk.PubKey { 89 auth := readEpochPubKeys(r.Store, epoch) 90 if auth == nil { 91 return nil 92 } 93 return auth.PubKeys 94 } 95 96 // GetEpochBlockStart is safe for concurrent use 97 func (r *HeavyCheckReader) GetEpochBlockStart(epoch idx.Epoch) idx.Block { 98 bs, _ := r.Store.GetHistoryBlockEpochState(epoch) 99 if bs == nil { 100 return 0 101 } 102 return bs.LastBlock.Idx 103 } 104 105 // readEpochPubKeys reads epoch pubkeys 106 func readEpochPubKeys(s *Store, epoch idx.Epoch) *ValidatorsPubKeys { 107 es := s.GetHistoryEpochState(epoch) 108 if es == nil { 109 return nil 110 } 111 var pubkeys = make(map[idx.ValidatorID]validatorpk.PubKey, len(es.ValidatorProfiles)) 112 for id, profile := range es.ValidatorProfiles { 113 pubkeys[id] = profile.PubKey 114 } 115 return &ValidatorsPubKeys{ 116 Epoch: epoch, 117 PubKeys: pubkeys, 118 } 119 }