/
sync_state.go
280 lines (222 loc) · 5.33 KB
/
sync_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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package net
import "errors"
var errTooShort = errors.New("too short")
var errUnknownSyncState = errors.New("unknown sync state")
/* +----------------+----------------+--------------------------------------------------------------+
* | From | To | Comment |
* +----------------+----------------+--------------------------------------------------------------+
* | Syncing | SyncDone | success |
* | Syncing | SyncError | chain get stuck, download error, no peers |
* | Syncing | SyncCancel | stop |
* +----------------+----------------+--------------------------------------------------------------+
* | SyncError | Syncing | new peer |
* | SyncError | SyncDone | in sync flow, taller peers disconnected, no need to sync |
* | SyncError | SyncCancel | in sync flow, stop |
* +----------------+----------------+--------------------------------------------------------------+
* | SyncDone | Syncing | new peer |
* +----------------+----------------+--------------------------------------------------------------+
* | SyncInit | Syncing | new peer |
* | SyncInit | SyncDone | no need to sync |
* | SyncInit | SyncError | no peers |
* | SyncInit | SyncCancel | stop |
* +----------------+----------------+--------------------------------------------------------------+
*/
type SyncState byte
func (s *SyncState) UnmarshalText(text []byte) error {
str := string(text)
for k, v := range syncStatus {
if v == str {
*s = k
return nil
}
}
return errUnknownSyncState
}
func (s SyncState) MarshalText() (text []byte, err error) {
return []byte(s.String()), nil
}
const (
SyncInit SyncState = iota
Syncing
SyncDone
SyncError
SyncCancel
)
func (s SyncState) syncExited() bool {
return s == SyncDone || s == SyncError || s == SyncCancel
}
var syncStatus = map[SyncState]string{
SyncInit: "Sync Not Start",
Syncing: "Synchronising",
SyncDone: "Sync done",
SyncError: "Sync error",
SyncCancel: "Sync canceled",
}
func (s SyncState) String() string {
status, ok := syncStatus[s]
if ok {
return status
}
return errUnknownSyncState.Error()
}
type syncErrorCode byte
var syncError = map[syncErrorCode]string{
syncErrorNoPeers: "no peers",
syncErrorStuck: "stuck",
syncErrorDownload: "download error",
}
const (
syncErrorNoPeers syncErrorCode = iota
syncErrorStuck
syncErrorDownload
)
func (e syncErrorCode) Error() string {
text, ok := syncError[e]
if ok {
return text
}
return "unknown sync error"
}
type syncState interface {
state() SyncState
enter()
sync()
done()
error(reason syncErrorCode)
cancel()
}
type syncStateHost interface {
setState(syncState)
}
// state init
type syncStateInit struct {
host syncStateHost
}
func (s syncStateInit) state() SyncState {
return SyncInit
}
func (s syncStateInit) enter() {
// do nothing
}
func (s syncStateInit) sync() {
s.host.setState(syncStateSyncing{
host: s.host,
})
}
func (s syncStateInit) done() {
s.host.setState(syncStateDone{
host: s.host,
})
}
func (s syncStateInit) error(reason syncErrorCode) {
s.host.setState(syncStateError{
host: s.host,
})
}
func (s syncStateInit) cancel() {
s.host.setState(syncStateCancel{
host: s.host,
})
}
// state syncing
type syncStateSyncing struct {
host syncStateHost
}
func (s syncStateSyncing) state() SyncState {
return Syncing
}
func (s syncStateSyncing) enter() {
}
func (s syncStateSyncing) sync() {
// self
// maybe get taller peers
s.host.setState(syncStateSyncing{
host: s.host,
})
}
func (s syncStateSyncing) done() {
s.host.setState(syncStateDone{
host: s.host,
})
}
func (s syncStateSyncing) error(reason syncErrorCode) {
s.host.setState(syncStateError{
host: s.host,
})
}
func (s syncStateSyncing) cancel() {
s.host.setState(syncStateCancel{
host: s.host,
})
}
// state done
type syncStateDone struct {
host syncStateHost
}
func (s syncStateDone) state() SyncState {
return SyncDone
}
func (s syncStateDone) enter() {
}
func (s syncStateDone) sync() {
s.host.setState(syncStateSyncing{
host: s.host,
})
}
func (s syncStateDone) done() {
// self
}
func (s syncStateDone) error(reason syncErrorCode) {
// cannot happen
}
func (s syncStateDone) cancel() {
// do nothing
}
// state error
type syncStateError struct {
host syncStateHost
}
func (s syncStateError) state() SyncState {
return SyncError
}
func (s syncStateError) enter() {
}
func (s syncStateError) sync() {
s.host.setState(syncStateSyncing{
host: s.host,
})
}
func (s syncStateError) done() {
s.host.setState(syncStateDone{
host: s.host,
})
}
func (s syncStateError) error(reason syncErrorCode) {
// self
}
func (s syncStateError) cancel() {
s.host.setState(syncStateCancel{
host: s.host,
})
}
// state cancel
type syncStateCancel struct {
host syncStateHost
}
func (s syncStateCancel) state() SyncState {
return SyncCancel
}
func (s syncStateCancel) enter() {
}
func (s syncStateCancel) sync() {
// cannot happen
}
func (s syncStateCancel) done() {
// cannot happen
}
func (s syncStateCancel) error(reason syncErrorCode) {
// cannot happen
}
func (s syncStateCancel) cancel() {
// self
}