-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
221 lines (165 loc) · 3.43 KB
/
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package core
import (
"sync"
"github.com/nnlgsakib/go-ibft/messages"
"github.com/nnlgsakib/go-ibft/messages/proto"
)
type stateType uint8
const (
newRound stateType = iota
prepare
commit
fin
)
func (s stateType) String() string {
switch s {
case newRound:
return "new round"
case prepare:
return "prepare"
case commit:
return "commit"
case fin:
return "fin"
}
return ""
}
type state struct {
sync.RWMutex
// current view (sequence, round)
view *proto.View
// latestPC is the latest prepared certificate
latestPC *proto.PreparedCertificate
// latestPreparedProposal is the proposal
// for which Q(N)-1 PREPARE messages were received
latestPreparedProposal *proto.Proposal
// accepted proposal for current round
proposalMessage *proto.Message
// validated commit seals
seals []*messages.CommittedSeal
// flags for different states
roundStarted bool
name stateType
}
func (s *state) getView() *proto.View {
s.RLock()
defer s.RUnlock()
return &proto.View{
Height: s.view.Height,
Round: s.view.Round,
}
}
func (s *state) reset(height uint64) {
s.Lock()
defer s.Unlock()
s.seals = nil
s.roundStarted = false
s.name = newRound
s.proposalMessage = nil
s.latestPC = nil
s.latestPreparedProposal = nil
s.view = &proto.View{
Height: height,
Round: 0,
}
}
func (s *state) getLatestPC() *proto.PreparedCertificate {
s.RLock()
defer s.RUnlock()
return s.latestPC
}
func (s *state) getLatestPreparedProposal() *proto.Proposal {
s.RLock()
defer s.RUnlock()
return s.latestPreparedProposal
}
func (s *state) getProposalMessage() *proto.Message {
s.RLock()
defer s.RUnlock()
return s.proposalMessage
}
func (s *state) getProposalHash() []byte {
s.RLock()
defer s.RUnlock()
return messages.ExtractProposalHash(s.proposalMessage)
}
func (s *state) setProposalMessage(proposalMessage *proto.Message) {
s.Lock()
defer s.Unlock()
s.proposalMessage = proposalMessage
}
func (s *state) getRound() uint64 {
s.RLock()
defer s.RUnlock()
return s.view.Round
}
func (s *state) getHeight() uint64 {
s.RLock()
defer s.RUnlock()
return s.view.Height
}
func (s *state) getProposal() *proto.Proposal {
s.RLock()
defer s.RUnlock()
if s.proposalMessage != nil {
return messages.ExtractProposal(s.proposalMessage)
}
return nil
}
func (s *state) getRawDataFromProposal() []byte {
proposal := s.getProposal()
if proposal != nil {
return proposal.RawProposal
}
return nil
}
func (s *state) getCommittedSeals() []*messages.CommittedSeal {
s.RLock()
defer s.RUnlock()
return s.seals
}
func (s *state) getStateName() stateType {
s.RLock()
defer s.RUnlock()
return s.name
}
func (s *state) changeState(name stateType) {
s.Lock()
defer s.Unlock()
s.name = name
}
func (s *state) setRoundStarted(started bool) {
s.Lock()
defer s.Unlock()
s.roundStarted = started
}
func (s *state) setView(view *proto.View) {
s.Lock()
defer s.Unlock()
s.view = view
}
func (s *state) setCommittedSeals(seals []*messages.CommittedSeal) {
s.Lock()
defer s.Unlock()
s.seals = seals
}
func (s *state) newRound() {
s.Lock()
defer s.Unlock()
if !s.roundStarted {
// Round is not yet started, kick the round off
s.name = newRound
s.roundStarted = true
}
}
func (s *state) finalizePrepare(
certificate *proto.PreparedCertificate,
latestPPB *proto.Proposal,
) {
s.Lock()
defer s.Unlock()
s.latestPC = certificate
s.latestPreparedProposal = latestPPB
// Move to the commit state
s.name = commit
}