-
Notifications
You must be signed in to change notification settings - Fork 3
/
state.go
256 lines (199 loc) · 5.7 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package basicstates
import (
"time"
"github.com/pkg/errors"
"github.com/spikeekips/mitum/base"
"github.com/spikeekips/mitum/base/block"
"github.com/spikeekips/mitum/base/seal"
"github.com/spikeekips/mitum/network"
"github.com/spikeekips/mitum/util"
"github.com/spikeekips/mitum/util/localtime"
)
var EmptySwitchFunc = func() error { return nil }
type State interface {
Enter(StateSwitchContext) (func() error, error)
Exit(StateSwitchContext) (func() error, error)
ProcessProposal(base.Proposal) error
ProcessVoteproof(base.Voteproof) error
SetStates(*States) State
}
type BaseState struct {
state base.State
States *States
lastVoteproofFunc func() base.Voteproof
lastINITVoteproofFunc func() base.Voteproof
setLastVoteproofFunc func(base.Voteproof) bool
newProposalFunc func(base.Proposal)
newVoteproofFunc func(base.Voteproof)
broadcastSealsFunc func(seal.Seal, bool /* to local */) error
timers *localtime.Timers
switchStateFunc func(StateSwitchContext) error
newBlocksFunc func([]block.Block) error
enterFunc func(StateSwitchContext) (func() error, error)
exitFunc func(StateSwitchContext) (func() error, error)
processVoteproofFunc func(base.Voteproof) error
syncableChannelsFunc func() map[string]network.Channel
exiting *util.LockedItem
}
func NewBaseState(st base.State) *BaseState {
return &BaseState{
state: st,
exiting: util.NewLockedItem(false),
}
}
func (st *BaseState) Enter(sctx StateSwitchContext) (func() error, error) {
if sctx.ToState() != st.state {
return nil, errors.Errorf("context not for entering this state, %v", st.state)
}
_ = st.exiting.Set(false)
if st.enterFunc != nil {
return st.enterFunc(sctx)
}
return nil, nil
}
func (st *BaseState) Exit(sctx StateSwitchContext) (func() error, error) {
if sctx.FromState() != st.state {
return nil, errors.Errorf("context not for exiting this state, %v", st.state)
}
if st.exitFunc != nil {
return st.exitFunc(sctx)
}
return nil, nil
}
func (*BaseState) ProcessProposal(base.Proposal) error { return nil }
func (st *BaseState) ProcessVoteproof(voteproof base.Voteproof) error {
if st.processVoteproofFunc != nil {
return st.processVoteproofFunc(voteproof)
}
return nil
}
func (st *BaseState) SetStates(ss *States) State {
st.States = ss
return st
}
func (st *BaseState) LastVoteproof() base.Voteproof {
if st.lastVoteproofFunc != nil {
return st.lastVoteproofFunc()
}
return st.States.LastVoteproof()
}
func (st *BaseState) LastINITVoteproof() base.Voteproof {
if st.lastINITVoteproofFunc != nil {
return st.lastINITVoteproofFunc()
}
return st.States.LastINITVoteproof()
}
func (st *BaseState) SetLastVoteproof(voteproof base.Voteproof) bool {
if st.setLastVoteproofFunc != nil {
return st.setLastVoteproofFunc(voteproof)
}
return st.States.SetLastVoteproof(voteproof)
}
func (st *BaseState) NewProposal(proposal base.Proposal) {
if st.newVoteproofFunc != nil {
st.newProposalFunc(proposal)
return
}
st.States.NewProposal(proposal)
}
func (st *BaseState) NewVoteproof(voteproof base.Voteproof) {
if st.newVoteproofFunc != nil {
st.newVoteproofFunc(voteproof)
return
}
st.States.NewVoteproof(voteproof)
}
func (st *BaseState) BroadcastBallot(blt base.Ballot, toLocal bool) error {
if st.broadcastSealsFunc != nil {
return st.broadcastSealsFunc(blt, toLocal)
}
st.States.BroadcastBallot(blt, toLocal)
return nil
}
func (st *BaseState) Timers() *localtime.Timers {
if st.timers != nil {
return st.timers
}
return st.States.Timers()
}
func (st *BaseState) StateSwitch(sctx StateSwitchContext) error {
if st.switchStateFunc != nil {
return st.switchStateFunc(sctx)
}
return st.States.SwitchState(sctx)
}
func (st *BaseState) NewBlocks(blks []block.Block) error {
if st.newBlocksFunc != nil {
return st.newBlocksFunc(blks)
}
return st.States.NewBlocks(blks)
}
func (st *BaseState) NewStateSwitchContext(next base.State) StateSwitchContext {
return NewStateSwitchContext(st.state, next)
}
func (st *BaseState) syncableChannels() map[string]network.Channel {
if st.syncableChannelsFunc != nil {
return st.syncableChannelsFunc()
}
if st.States == nil {
return nil
}
pn := map[string]network.Channel{}
st.States.nodepool.TraverseAliveRemotes(func(no base.Node, ch network.Channel) bool {
pn[no.String()] = ch
return true
})
if ch := st.oldNode(); ch != nil {
pn["handover"] = ch
}
return pn
}
func (st *BaseState) oldNode() network.Channel {
if !st.underHandover() {
return nil
}
return st.States.hd.OldNode()
}
func (st *BaseState) underHandover() bool {
return st.States != nil && st.States.underHandover()
}
var ConsensusStuckError = util.NewError("consensus looks stuck")
type ConsensusStuckChecker struct {
lastVoteproof base.Voteproof
state base.State
endure time.Duration
nowFunc func() time.Time
}
func NewConsensusStuckChecker(
lastVoteproof base.Voteproof,
state base.State,
endure time.Duration,
) *ConsensusStuckChecker {
return &ConsensusStuckChecker{
lastVoteproof: lastVoteproof,
state: state,
endure: endure,
}
}
func (cc *ConsensusStuckChecker) IsValidState() (bool, error) {
if cc.state != base.StateConsensus {
return false, nil
}
return true, nil
}
func (cc *ConsensusStuckChecker) IsOldLastVoteproofTime() (bool, error) {
if cc.lastVoteproof == nil {
return true, nil
}
var now time.Time
if cc.nowFunc != nil {
now = cc.nowFunc()
} else {
now = localtime.UTCNow()
}
if since := now.Sub(cc.lastVoteproof.FinishedAt()); since > cc.endure {
return false, ConsensusStuckError.Errorf(
"last voteproof is too old, %s from now", since.String())
}
return true, nil
}