-
Notifications
You must be signed in to change notification settings - Fork 199
/
basicForkDetector.go
361 lines (305 loc) · 10.2 KB
/
basicForkDetector.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package sync
import (
"bytes"
"math"
"sync"
"github.com/ElrondNetwork/elrond-go/consensus"
"github.com/ElrondNetwork/elrond-go/data"
"github.com/ElrondNetwork/elrond-go/process"
)
type headerInfo struct {
nonce uint64
round uint32
hash []byte
state process.BlockHeaderState
}
type forkInfo struct {
checkpointNonce uint64
lastCheckpointNonce uint64
checkpointRound int32
lastCheckpointRound int32
probableHighestNonce uint64
lastBlockRound int32
}
// basicForkDetector defines a struct with necessary data needed for fork detection
type basicForkDetector struct {
rounder consensus.Rounder
headers map[uint64][]*headerInfo
mutHeaders sync.RWMutex
fork forkInfo
mutFork sync.RWMutex
}
// NewBasicForkDetector method creates a new BasicForkDetector object
func NewBasicForkDetector(rounder consensus.Rounder,
) (*basicForkDetector, error) {
if rounder == nil {
return nil, process.ErrNilRounder
}
bfd := &basicForkDetector{
rounder: rounder,
}
bfd.fork.checkpointRound = -1
bfd.fork.lastCheckpointRound = -1
bfd.fork.lastBlockRound = -1
bfd.headers = make(map[uint64][]*headerInfo)
return bfd, nil
}
// AddHeader method adds a new header to headers map
func (bfd *basicForkDetector) AddHeader(header data.HeaderHandler, hash []byte, state process.BlockHeaderState) error {
if header == nil {
return ErrNilHeader
}
if hash == nil {
return ErrNilHash
}
err := bfd.checkBlockValidity(header, state)
if err != nil {
return err
}
if state == process.BHProcessed {
// create a check point and remove all the past headers
bfd.mutFork.Lock()
bfd.fork.lastCheckpointNonce = bfd.fork.checkpointNonce
bfd.fork.lastCheckpointRound = bfd.fork.checkpointRound
bfd.fork.checkpointNonce = header.GetNonce()
bfd.fork.checkpointRound = int32(header.GetRound())
bfd.mutFork.Unlock()
bfd.removePastHeaders()
bfd.removeInvalidHeaders()
}
bfd.append(&headerInfo{
nonce: header.GetNonce(),
round: header.GetRound(),
hash: hash,
state: state,
})
probableHighestNonce := bfd.computeProbableHighestNonce()
bfd.mutFork.RLock()
bfd.fork.lastBlockRound = bfd.rounder.Index()
bfd.fork.probableHighestNonce = probableHighestNonce
bfd.mutFork.RUnlock()
return nil
}
func (bfd *basicForkDetector) checkBlockValidity(header data.HeaderHandler, state process.BlockHeaderState) error {
bfd.mutFork.RLock()
roundDif := int32(header.GetRound()) - bfd.fork.lastCheckpointRound
nonceDif := int64(header.GetNonce() - bfd.fork.lastCheckpointNonce)
bfd.mutFork.RUnlock()
if roundDif < 0 {
return ErrLowerRoundInBlock
}
if nonceDif < 0 {
return ErrLowerNonceInBlock
}
if int32(header.GetRound()) > bfd.rounder.Index() {
return ErrHigherRoundInBlock
}
if int64(roundDif) < nonceDif {
return ErrHigherNonceInBlock
}
if state == process.BHProposed {
if !isRandomSeedValid(header) {
return ErrRandomSeedNotValid
}
}
if state == process.BHReceived || state == process.BHProcessed {
if !isSigned(header) {
return ErrBlockIsNotSigned
}
}
return nil
}
func (bfd *basicForkDetector) removePastHeaders() {
bfd.mutFork.RLock()
lastCheckpointNonce := bfd.fork.lastCheckpointNonce
bfd.mutFork.RUnlock()
bfd.mutHeaders.Lock()
for nonce := range bfd.headers {
if nonce < lastCheckpointNonce {
delete(bfd.headers, nonce)
}
}
bfd.mutHeaders.Unlock()
}
func (bfd *basicForkDetector) removeInvalidHeaders() {
bfd.mutFork.RLock()
lastCheckpointRound := bfd.fork.lastCheckpointRound
lastCheckpointNonce := bfd.fork.lastCheckpointNonce
bfd.mutFork.RUnlock()
var validHdrInfos []*headerInfo
bfd.mutHeaders.Lock()
for nonce, hdrInfos := range bfd.headers {
validHdrInfos = nil
for i := 0; i < len(hdrInfos); i++ {
roundDif := int32(hdrInfos[i].round) - lastCheckpointRound
nonceDif := int64(hdrInfos[i].nonce - lastCheckpointNonce)
if int64(roundDif) >= nonceDif {
validHdrInfos = append(validHdrInfos, hdrInfos[i])
}
}
if validHdrInfos == nil {
delete(bfd.headers, nonce)
continue
}
bfd.headers[nonce] = validHdrInfos
}
bfd.mutHeaders.Unlock()
}
// computeProbableHighestNonce computes the probable highest nonce from the valid received/processed headers
func (bfd *basicForkDetector) computeProbableHighestNonce() uint64 {
bfd.mutFork.RLock()
probableHighestNonce := bfd.fork.lastCheckpointNonce
bfd.mutFork.RUnlock()
bfd.mutHeaders.RLock()
for _, headersInfo := range bfd.headers {
nonce := bfd.getProbableHighestNonce(headersInfo)
if nonce <= probableHighestNonce {
continue
}
probableHighestNonce = nonce
}
bfd.mutHeaders.RUnlock()
return probableHighestNonce
}
func (bfd *basicForkDetector) getProbableHighestNonce(headersInfo []*headerInfo) uint64 {
maxNonce := uint64(0)
for _, headerInfo := range headersInfo {
nonce := headerInfo.nonce
// if header stored state is BHProposed, then the probable highest nonce should be set to its nonce-1, because
// at that point the consensus was not achieved on this block and the only certainty is that the probable
// highest nonce is nonce-1 on which this proposed block is constructed. This approach would avoid a situation
// in which a proposed block on which the consensus would not be achieved would set all the nodes in sync mode,
// because of the probable highest nonce set with its nonce instead of nonce-1.
if headerInfo.state == process.BHProposed {
nonce--
}
if nonce > maxNonce {
maxNonce = nonce
}
}
return maxNonce
}
// RemoveHeaders removes all the stored headers with a given nonce
func (bfd *basicForkDetector) RemoveHeaders(nonce uint64, hash []byte) {
bfd.mutFork.Lock()
if nonce == bfd.fork.checkpointNonce {
bfd.fork.checkpointNonce = bfd.fork.lastCheckpointNonce
bfd.fork.checkpointRound = bfd.fork.lastCheckpointRound
}
bfd.mutFork.Unlock()
var preservedHdrInfos []*headerInfo
bfd.mutHeaders.RLock()
hdrInfos := bfd.headers[nonce]
bfd.mutHeaders.RUnlock()
for _, hdrInfoStored := range hdrInfos {
if bytes.Equal(hdrInfoStored.hash, hash) {
continue
}
preservedHdrInfos = append(preservedHdrInfos, hdrInfoStored)
}
bfd.mutHeaders.Lock()
if preservedHdrInfos == nil {
delete(bfd.headers, nonce)
} else {
bfd.headers[nonce] = preservedHdrInfos
}
bfd.mutHeaders.Unlock()
}
// append adds a new header in the slice found in nonce position
// it not adds the header if its hash is already stored in the slice
func (bfd *basicForkDetector) append(hdrInfo *headerInfo) {
bfd.mutHeaders.Lock()
defer bfd.mutHeaders.Unlock()
hdrInfos := bfd.headers[hdrInfo.nonce]
isHdrInfosNilOrEmpty := hdrInfos == nil || len(hdrInfos) == 0
if isHdrInfosNilOrEmpty {
bfd.headers[hdrInfo.nonce] = []*headerInfo{hdrInfo}
return
}
for _, hdrInfoStored := range hdrInfos {
if bytes.Equal(hdrInfoStored.hash, hdrInfo.hash) {
if hdrInfoStored.state != process.BHProcessed && hdrInfo.state == process.BHProcessed {
// if the stored and received headers processed at the same time have equal hashes, that the old record
// will be replaced with the processed one. This nonce is marked at bootsrapping as processed, but as it
// is also received through broadcasting, the system stores as received.
hdrInfoStored.state = process.BHProcessed
}
return
}
}
bfd.headers[hdrInfo.nonce] = append(bfd.headers[hdrInfo.nonce], hdrInfo)
}
// CheckFork method checks if the node could be on the fork
func (bfd *basicForkDetector) CheckFork() (bool, uint64) {
var lowestForkNonce uint64
var lowestRoundInForkNonce uint32
var selfHdrInfo *headerInfo
lowestForkNonce = math.MaxUint64
forkDetected := false
bfd.mutHeaders.Lock()
for nonce, hdrInfos := range bfd.headers {
if len(hdrInfos) == 1 {
continue
}
selfHdrInfo = nil
lowestRoundInForkNonce = math.MaxUint32
for i := 0; i < len(hdrInfos); i++ {
// Proposed blocks received do not count for fork choice, as they are not valid until the consensus
// is achieved. They should be received afterwards through sync mechanism.
if hdrInfos[i].state == process.BHProposed {
continue
}
if hdrInfos[i].state == process.BHProcessed {
selfHdrInfo = hdrInfos[i]
continue
}
if hdrInfos[i].round < lowestRoundInForkNonce {
lowestRoundInForkNonce = hdrInfos[i].round
}
}
if selfHdrInfo == nil {
// if current nonce has not been processed yet, then skip and check the next one.
continue
}
if selfHdrInfo.round <= lowestRoundInForkNonce {
// keep it clean so next time this position will be processed faster
delete(bfd.headers, nonce)
bfd.headers[nonce] = []*headerInfo{selfHdrInfo}
continue
}
forkDetected = true
if nonce < lowestForkNonce {
lowestForkNonce = nonce
}
}
bfd.mutHeaders.Unlock()
return forkDetected, lowestForkNonce
}
// GetHighestFinalBlockNonce gets the highest nonce of the block which is final and it can not be reverted anymore
func (bfd *basicForkDetector) GetHighestFinalBlockNonce() uint64 {
bfd.mutFork.RLock()
lastCheckpointNonce := bfd.fork.lastCheckpointNonce
bfd.mutFork.RUnlock()
return lastCheckpointNonce
}
// ProbableHighestNonce gets the probable highest nonce
func (bfd *basicForkDetector) ProbableHighestNonce() uint64 {
// TODO: This fallback mechanism should be improved
// This mechanism is necessary to manage the case when the node will act as synchronized because no new block,
// higher than its checkpoint, would be received anymore (this could be the case when during an epoch, the number of
// validators in one shard decrease under the size of 2/3 + 1 of the consensus group. In this case no new block would
// be proposed anymore and any node which would try to boostrap, would be stuck at the genesis block. This case could
// be solved, if the proposed blocks received from leaders would also call the AddHeader method of this class).
// If after maxRoundsToWait nothing is received, the probableHighestNonce will be set to checkpoint,
// so the node will act as synchronized
bfd.mutFork.Lock()
roundsWithoutReceivedBlock := bfd.rounder.Index() - bfd.fork.lastBlockRound
if roundsWithoutReceivedBlock > maxRoundsToWait {
if bfd.fork.probableHighestNonce > bfd.fork.checkpointNonce {
bfd.fork.probableHighestNonce = bfd.fork.checkpointNonce
}
}
probableHighestNonce := bfd.fork.probableHighestNonce
bfd.mutFork.Unlock()
return probableHighestNonce
}