/
gpo_backend.go
57 lines (49 loc) · 1.59 KB
/
gpo_backend.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
package gossip
import (
"github.com/TechPay-io/sirius-base/hash"
"github.com/TechPay-io/sirius-base/inter/idx"
"github.com/TechPay-io/go-photon/inter"
"github.com/TechPay-io/go-photon/photon"
"github.com/TechPay-io/go-photon/utils/concurrent"
)
type GPOBackend struct {
store *Store
}
func (b *GPOBackend) GetLatestBlockIndex() idx.Block {
return b.store.GetLatestBlockIndex()
}
func (b *GPOBackend) GetRules() photon.Rules {
return b.store.GetRules()
}
func (b *GPOBackend) GetPendingRules() photon.Rules {
return b.store.GetBlockState().DirtyRules
}
// TotalGasPowerLeft returns a total amount of obtained gas power by the validators, according to the latest events from each validator
func (b *GPOBackend) TotalGasPowerLeft() uint64 {
es := b.store.GetEpochState()
set := b.store.GetLastEvents(es.Epoch)
if set == nil {
set = concurrent.WrapValidatorEventsSet(map[idx.ValidatorID]hash.Event{})
}
set.RLock()
defer set.RUnlock()
metValidators := map[idx.ValidatorID]bool{}
total := uint64(0)
// count GasPowerLeft from latest events of this epoch
for _, tip := range set.Val {
e := b.store.GetEvent(tip)
total += e.GasPowerLeft().Gas[inter.LongTermGas]
metValidators[e.Creator()] = true
}
// count GasPowerLeft from last events of prev epoch if no event in current epoch is present
for i := idx.Validator(0); i < es.Validators.Len(); i++ {
vid := es.Validators.GetID(i)
if !metValidators[vid] {
prevEvent := b.store.GetEvent(es.ValidatorStates[i].PrevEpochEvent)
if prevEvent != nil {
total += prevEvent.GasPowerLeft().Gas[inter.LongTermGas]
}
}
}
return total
}