-
Notifications
You must be signed in to change notification settings - Fork 4
/
store_decided_state.go
87 lines (71 loc) · 2.49 KB
/
store_decided_state.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
package gossip
import (
"github.com/TechPay-io/sirius-base/inter/idx"
"github.com/TechPay-io/sirius-base/inter/pos"
"github.com/ethereum/go-ethereum/log"
"github.com/TechPay-io/go-photon/gossip/blockproc"
"github.com/TechPay-io/go-photon/photon"
)
const sKey = "s"
type BlockEpochState struct {
BlockState *blockproc.BlockState
EpochState *blockproc.EpochState
}
// SetBlockEpochState stores the latest block and epoch state in memory
func (s *Store) SetBlockEpochState(bs blockproc.BlockState, es blockproc.EpochState) {
bs, es = bs.Copy(), es.Copy()
s.cache.BlockEpochState.Store(&BlockEpochState{&bs, &es})
}
func (s *Store) getBlockEpochState() BlockEpochState {
if v := s.cache.BlockEpochState.Load(); v != nil {
return *v.(*BlockEpochState)
}
v, ok := s.rlp.Get(s.table.BlockEpochState, []byte(sKey), &BlockEpochState{}).(*BlockEpochState)
if !ok {
log.Crit("Epoch state reading failed: genesis not applied")
}
s.cache.BlockEpochState.Store(v)
return *v
}
// FlushBlockEpochState stores the latest epoch and block state in DB
func (s *Store) FlushBlockEpochState() {
s.rlp.Set(s.table.BlockEpochState, []byte(sKey), s.getBlockEpochState())
}
// GetBlockState retrieves the latest block state
func (s *Store) GetBlockState() blockproc.BlockState {
return *s.getBlockEpochState().BlockState
}
// GetEpochState retrieves the latest epoch state
func (s *Store) GetEpochState() blockproc.EpochState {
return *s.getBlockEpochState().EpochState
}
func (s *Store) GetBlockEpochState() (blockproc.BlockState, blockproc.EpochState) {
bes := s.getBlockEpochState()
return *bes.BlockState, *bes.EpochState
}
// GetEpoch retrieves the current epoch
func (s *Store) GetEpoch() idx.Epoch {
return s.GetEpochState().Epoch
}
// GetValidators retrieves current validators
func (s *Store) GetValidators() *pos.Validators {
return s.GetEpochState().Validators
}
// GetEpochValidators retrieves the current epoch and validators atomically
func (s *Store) GetEpochValidators() (*pos.Validators, idx.Epoch) {
es := s.GetEpochState()
return es.Validators, es.Epoch
}
// GetLatestBlockIndex retrieves the current block number
func (s *Store) GetLatestBlockIndex() idx.Block {
return s.GetBlockState().LastBlock.Idx
}
// GetRules retrieves current network rules
func (s *Store) GetRules() photon.Rules {
return s.GetEpochState().Rules
}
// GetEpochRules retrieves current network rules and epoch atomically
func (s *Store) GetEpochRules() (photon.Rules, idx.Epoch) {
es := s.GetEpochState()
return es.Rules, es.Epoch
}