-
Notifications
You must be signed in to change notification settings - Fork 639
/
l1_syncstatus.go
338 lines (305 loc) · 10.9 KB
/
l1_syncstatus.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
package l1_parallel_sync
import (
"errors"
"fmt"
"sync"
"github.com/0xPolygonHermez/zkevm-node/log"
)
const (
invalidLastBlock = 0
)
var (
errSyncChunkSizeMustBeGreaterThanZero = errors.New("SyncChunkSize must be greater than 0")
errStartingBlockNumberMustBeDefined = errors.New("startingBlockNumber must be defined, call producer ResetAndStop() to set a new starting point")
)
type syncStatus struct {
mutex sync.Mutex
lastBlockStoreOnStateDB uint64
highestBlockRequested uint64
lastBlockOnL1 uint64
amountOfBlocksInEachRange uint64
// This ranges are being processed
processingRanges liveBlockRanges
// This ranges need to be retried because the last execution was an error
errorRanges liveBlockRanges
}
func (s *syncStatus) String() string {
return fmt.Sprintf(" lastBlockStoreOnStateDB: %s, highestBlockRequested:%s, lastBlockOnL1: %s, amountOfBlocksInEachRange: %d, processingRanges: %s, errorRanges: %s",
blockNumberToString(s.lastBlockStoreOnStateDB),
blockNumberToString(s.highestBlockRequested),
blockNumberToString(s.lastBlockOnL1), s.amountOfBlocksInEachRange, s.processingRanges.toStringBrief(), s.errorRanges.toStringBrief())
}
func (s *syncStatus) toString() string {
brief := s.String()
brief += fmt.Sprintf(" processingRanges:{ %s }", s.processingRanges.String())
brief += fmt.Sprintf(" errorRanges:{ %s }", s.errorRanges.String())
return brief
}
// newSyncStatus create a new syncStatus object
// lastBlockStoreOnStateDB: last block stored on stateDB
// amountOfBlocksInEachRange: amount of blocks to be retrieved in each range
// lastBlockTTLDuration: TTL of the last block on L1 (it could be ttlOfLastBlockInfinity that means that is no renewed)
func newSyncStatus(lastBlockStoreOnStateDB uint64, amountOfBlocksInEachRange uint64) *syncStatus {
return &syncStatus{
lastBlockStoreOnStateDB: lastBlockStoreOnStateDB,
highestBlockRequested: lastBlockStoreOnStateDB,
amountOfBlocksInEachRange: amountOfBlocksInEachRange,
lastBlockOnL1: invalidLastBlock,
processingRanges: newLiveBlockRanges(),
}
}
func (s *syncStatus) Reset(lastBlockStoreOnStateDB uint64) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.lastBlockStoreOnStateDB = lastBlockStoreOnStateDB
s.highestBlockRequested = lastBlockStoreOnStateDB
s.processingRanges = newLiveBlockRanges()
//s.lastBlockOnL1 = invalidLastBlock
}
func (s *syncStatus) GetLastBlockOnL1() uint64 {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.lastBlockOnL1
}
// All pending blocks have been requested or are currently being requested
func (s *syncStatus) HaveRequiredAllBlocksToBeSynchronized() bool {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.lastBlockOnL1 <= s.highestBlockRequested
}
// IsNodeFullySynchronizedWithL1 returns true if the node is fully synchronized with L1
// it means that all blocks until the last block on L1 are requested (maybe not finish yet) and there are no pending errors
func (s *syncStatus) IsNodeFullySynchronizedWithL1() bool {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.lastBlockOnL1 == invalidLastBlock {
log.Warnf("Can't decide if it's fully synced because last block on L1 is no valid: %d", s.lastBlockOnL1)
return false
}
if s.lastBlockOnL1 <= s.highestBlockRequested && s.errorRanges.len() == 0 && s.processingRanges.len() == 0 {
log.Debug("No blocks to ask, we have requested and responsed all blocks from L1!")
return true
}
return false
}
func (s *syncStatus) GetNextRangeOnlyRetries() *blockRange {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.getNextRangeOnlyRetriesUnsafe()
}
func (s *syncStatus) getNextRangeOnlyRetriesUnsafe() *blockRange {
// Check if there are any range that need to be retried
blockRangeToRetry, err := s.errorRanges.getFirstBlockRange()
if err == nil {
if blockRangeToRetry.toBlock == latestBlockNumber {
// If is a latestBlockNumber must be discarded
log.Debugf("Discarding error block range: %s because it's a latestBlockNumber", blockRangeToRetry.String())
err := s.errorRanges.removeBlockRange(blockRangeToRetry)
if err != nil {
log.Errorf("syncstatus: error removing an error br: %s current_status:%s err:%s", blockRangeToRetry.String(), s.String(), err.Error())
}
return nil
}
return &blockRangeToRetry
}
return nil
}
func (s *syncStatus) getHighestBlockRequestedUnsafe() uint64 {
res := invalidBlockNumber
for _, r := range s.processingRanges.ranges {
if r.blockRange.toBlock > res {
res = r.blockRange.toBlock
}
}
for _, r := range s.errorRanges.ranges {
if r.blockRange.toBlock > res {
res = r.blockRange.toBlock
}
}
return res
}
// GetNextRange: if there are pending work it returns the next block to ask for
//
// it could be a retry from a previous error or a new range
func (s *syncStatus) GetNextRange() *blockRange {
s.mutex.Lock()
defer s.mutex.Unlock()
// Check if there are any range that need to be retried
blockRangeToRetry := s.getNextRangeOnlyRetriesUnsafe()
if blockRangeToRetry != nil {
return blockRangeToRetry
}
if s.lastBlockOnL1 == invalidLastBlock {
log.Debug("Last block is no valid: ", s.lastBlockOnL1)
return nil
}
if s.lastBlockOnL1 <= s.highestBlockRequested {
log.Debug("No blocks to ask, we have requested all blocks from L1!")
return nil
}
highestBlockInProcess := s.getHighestBlockRequestedUnsafe()
if highestBlockInProcess == latestBlockNumber {
log.Debug("No blocks to ask, we have requested all blocks from L1!")
return nil
}
br := getNextBlockRangeFromUnsafe(max(s.lastBlockStoreOnStateDB, s.getHighestBlockRequestedUnsafe()), s.lastBlockOnL1, s.amountOfBlocksInEachRange)
err := br.isValid()
if err != nil {
log.Error(s.toString())
log.Fatal(err)
}
return br
}
func (s *syncStatus) OnStartedNewWorker(br blockRange) {
s.mutex.Lock()
defer s.mutex.Unlock()
// Try to remove from error Blocks
err := s.errorRanges.removeBlockRange(br)
if err == nil {
log.Infof("Retrying ranges: %s ", br.String())
}
err = s.processingRanges.addBlockRange(br)
if err != nil {
log.Error(s.toString())
log.Fatal(err)
}
if br.toBlock == latestBlockNumber {
s.highestBlockRequested = s.lastBlockOnL1
} else if br.toBlock > s.highestBlockRequested {
s.highestBlockRequested = br.toBlock
}
}
// return true is a valid blockRange
func (s *syncStatus) OnFinishWorker(br blockRange, successful bool, highestBlockNumberInResponse uint64) bool {
s.mutex.Lock()
defer s.mutex.Unlock()
log.Debugf("onFinishWorker(br=%s, successful=%v) initial_status: %s", br.String(), successful, s.String())
// The work have been done, remove the range from pending list
// also move the s.lastBlockStoreOnStateDB to the end of the range if needed
err := s.processingRanges.removeBlockRange(br)
if err != nil {
log.Infof("Unexpected finished block_range %s, ignoring it: %s", br.String(), err)
return false
}
if successful {
// If this range is the first in the window, we need to move the s.lastBlockStoreOnStateDB to next range
// example:
// lbs = 99
// pending = [100, 200], [201, 300], [301, 400]
// if process the [100,200] -> lbs = 200
if highestBlockNumberInResponse != invalidBlockNumber && highestBlockNumberInResponse > s.lastBlockStoreOnStateDB {
newValue := highestBlockNumberInResponse
log.Debugf("Moving s.lastBlockStoreOnStateDB from %d to %d (diff %d)", s.lastBlockStoreOnStateDB, newValue, newValue-s.lastBlockStoreOnStateDB)
s.lastBlockStoreOnStateDB = newValue
}
} else {
log.Infof("Range %s was not successful, adding to errorRanges to be retried", br.String())
err := s.errorRanges.addBlockRange(br)
if err != nil {
log.Error(s.toString())
log.Fatal(err)
}
}
log.Debugf("onFinishWorker final_status: %s", s.String())
return true
}
func getNextBlockRangeFromUnsafe(lastBlockInState uint64, lastBlockInL1 uint64, amountOfBlocksInEachRange uint64) *blockRange {
fromBlock := lastBlockInState + 1
toBlock := min(lastBlockInL1, fromBlock+amountOfBlocksInEachRange)
if toBlock == lastBlockInL1 {
toBlock = latestBlockNumber
}
return &blockRange{fromBlock: fromBlock, toBlock: toBlock}
}
func (s *syncStatus) setLastBlockOnL1(lastBlock uint64) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.setLastBlockOnL1Unsafe(lastBlock)
}
func (s *syncStatus) setLastBlockOnL1Unsafe(lastBlock uint64) {
s.lastBlockOnL1 = lastBlock
}
type onNewLastBlockResponse struct {
// New fullRange of pending blocks
fullRange blockRange
// New extendedRange of pending blocks due to new last block
extendedRange *blockRange
}
func (n *onNewLastBlockResponse) toString() string {
res := fmt.Sprintf("fullRange: %s", n.fullRange.String())
if n.extendedRange != nil {
res += fmt.Sprintf(" extendedRange: %s", n.extendedRange.String())
} else {
res += " extendedRange: nil"
}
return res
}
func (s *syncStatus) OnNewLastBlockOnL1(lastBlock uint64) onNewLastBlockResponse {
s.mutex.Lock()
defer s.mutex.Unlock()
log.Debugf("onNewLastBlockOnL1(%v) initial_status: %s", lastBlock, s.String())
response := onNewLastBlockResponse{
fullRange: blockRange{fromBlock: s.lastBlockStoreOnStateDB, toBlock: lastBlock},
}
if s.lastBlockOnL1 == invalidLastBlock {
// No previous last block
response.extendedRange = &blockRange{
fromBlock: s.lastBlockStoreOnStateDB,
toBlock: lastBlock,
}
s.setLastBlockOnL1Unsafe(lastBlock)
return response
}
oldLastBlock := s.lastBlockOnL1
if lastBlock > oldLastBlock {
response.extendedRange = &blockRange{
fromBlock: oldLastBlock + 1,
toBlock: lastBlock,
}
s.setLastBlockOnL1Unsafe(lastBlock)
return response
}
if lastBlock == oldLastBlock {
response.extendedRange = nil
s.setLastBlockOnL1Unsafe(lastBlock)
return response
}
if lastBlock < oldLastBlock {
log.Warnf("new block [%d] is less than old block [%d]!", lastBlock, oldLastBlock)
lastBlock = oldLastBlock
response.fullRange = blockRange{fromBlock: s.lastBlockStoreOnStateDB, toBlock: lastBlock}
return response
}
log.Debugf("onNewLastBlockOnL1(%d) final_status: %s", lastBlock, s.String())
return response
}
func (s *syncStatus) DoesItHaveAllTheNeedDataToWork() bool {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.lastBlockOnL1 != invalidLastBlock && s.lastBlockStoreOnStateDB != invalidBlockNumber
}
func (s *syncStatus) Verify() error {
if s.amountOfBlocksInEachRange == 0 {
return errSyncChunkSizeMustBeGreaterThanZero
}
if s.lastBlockStoreOnStateDB == invalidBlockNumber {
return errStartingBlockNumberMustBeDefined
}
return nil
}
// It returns if this block is beyond Finalized (so it could be reorg)
// If blockNumber == invalidBlockNumber then it uses the highestBlockRequested (the last block requested)
func (s *syncStatus) BlockNumberIsInsideUnsafeArea(blockNumber uint64) bool {
s.mutex.Lock()
defer s.mutex.Unlock()
if blockNumber == invalidBlockNumber {
blockNumber = s.highestBlockRequested
}
distanceInBlockToLatest := s.lastBlockOnL1 - blockNumber
return distanceInBlockToLatest < maximumBlockDistanceFromLatestToFinalized
}
func (s *syncStatus) GetHighestBlockReceived() uint64 {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.lastBlockStoreOnStateDB
}