-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathheadersExecutor.go
More file actions
457 lines (385 loc) · 12.9 KB
/
Copy pathheadersExecutor.go
File metadata and controls
457 lines (385 loc) · 12.9 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package asyncExecution
import (
"bytes"
"context"
"errors"
"sync"
"time"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/process/asyncExecution/cache"
)
var log = logger.GetOrCreate("process/asyncExecution")
const timeToSleep = time.Millisecond * 5
const timeToSleepOnError = time.Millisecond * 300
const maxRetryAttempts = 10
const maxBackoffTime = time.Second * 5
// ArgsHeadersExecutor holds all the components needed to create a new instance of *headersExecutor
type ArgsHeadersExecutor struct {
BlocksCache BlocksCache
ExecutionTracker ExecutionResultsHandler
BlockProcessor BlockProcessor
BlockChain data.ChainHandler
SignalProcessCompletionChan chan uint64
}
type headersExecutor struct {
blocksCache BlocksCache
executionTracker ExecutionResultsHandler
blockProcessor BlockProcessor
blockChain data.ChainHandler
cancelFunc context.CancelFunc
mutPaused sync.Mutex
isPaused bool
processingDone chan struct{}
signalProcessCompletionChan chan uint64
}
// NewHeadersExecutor will create a new instance of *headersExecutor
func NewHeadersExecutor(args ArgsHeadersExecutor) (*headersExecutor, error) {
if check.IfNil(args.BlocksCache) {
return nil, ErrNilHeadersCache
}
if check.IfNil(args.ExecutionTracker) {
return nil, ErrNilExecutionTracker
}
if check.IfNil(args.BlockProcessor) {
return nil, ErrNilBlockProcessor
}
if check.IfNil(args.BlockChain) {
return nil, process.ErrNilBlockChain
}
instance := &headersExecutor{
blocksCache: args.BlocksCache,
executionTracker: args.ExecutionTracker,
blockProcessor: args.BlockProcessor,
blockChain: args.BlockChain,
signalProcessCompletionChan: args.SignalProcessCompletionChan,
}
return instance, nil
}
// StartExecution starts a goroutine to continuously process blocks from the queue
// and add their results to the execution tracker until cancelled or closed.
func (he *headersExecutor) StartExecution() {
ctx, cancelFunc := context.WithCancel(context.Background())
he.cancelFunc = cancelFunc
go he.start(ctx)
}
// PauseExecution pauses the execution and waits for any ongoing block processing to complete.
// It returns only after the processing loop has acknowledged the pause, guaranteeing
// that no block execution is in flight.
func (he *headersExecutor) PauseExecution() {
log.Debug("headersExecutor.PauseExecution: pausing execution")
if he.cancelFunc == nil {
log.Debug("headersExecutor.PauseExecution: execution not started yet or already closed")
return
}
he.mutPaused.Lock()
if he.isPaused {
he.mutPaused.Unlock()
return
}
he.isPaused = true
ch := make(chan struct{})
he.processingDone = ch
he.mutPaused.Unlock()
// Block until the processing loop acknowledges the pause by closing this channel.
// This guarantees no block execution is in flight when PauseExecution returns.
<-ch
}
// ResumeExecution resumes the execution
func (he *headersExecutor) ResumeExecution() {
log.Debug("headersExecutor.ResumeExecution: resuming execution")
he.mutPaused.Lock()
defer he.mutPaused.Unlock()
he.isPaused = false
// If PauseExecution is waiting for acknowledgement but we're resuming first,
// close the channel to unblock it.
if he.processingDone != nil {
close(he.processingDone)
he.processingDone = nil
}
}
// acknowledgePause checks if a pause has been requested and, if so, closes the
// processingDone channel to unblock PauseExecution. This must only be called
// between block processing iterations, ensuring no execution is in flight.
func (he *headersExecutor) acknowledgePause() {
he.mutPaused.Lock()
defer he.mutPaused.Unlock()
if !he.isPaused {
return
}
if he.processingDone != nil {
close(he.processingDone)
he.processingDone = nil
}
}
func (he *headersExecutor) start(ctx context.Context) {
log.Debug("headersExecutor.start: starting execution")
for {
select {
case <-ctx.Done():
he.acknowledgePause()
return
default:
}
he.acknowledgePause()
he.mutPaused.Lock()
isPaused := he.isPaused
he.mutPaused.Unlock()
if isPaused {
time.Sleep(timeToSleep)
continue
}
lastExecutedNonce, lastExecutedHeaderHash, _ := he.blockChain.GetLastExecutedBlockInfo()
if len(lastExecutedHeaderHash) == 0 {
he.acknowledgePause()
time.Sleep(timeToSleep)
continue
}
// check if we need to execute another block for the same nonce (replacement block)
headerBodyPair, ok := he.blocksCache.GetByNonce(lastExecutedNonce)
if !ok {
// Either block not in cache (genesis or cleaned) or same hash (already executed)
// Try to get the next block to execute
headerBodyPair, ok = he.blocksCache.GetByNonce(lastExecutedNonce + 1)
if !ok {
he.acknowledgePause()
time.Sleep(timeToSleep)
continue
}
}
if headerBodyPair.Header.GetNonce() == lastExecutedNonce && !bytes.Equal(lastExecutedHeaderHash, headerBodyPair.HeaderHash) {
// Different block at same nonce - this is a replacement block and needs to be executed
log.Debug("headersExecutor.start: detected replacement block at same nonce",
"nonce", lastExecutedNonce,
"executed_hash", lastExecutedHeaderHash,
"replacement_hash", headerBodyPair.HeaderHash,
)
}
if bytes.Equal(lastExecutedHeaderHash, headerBodyPair.HeaderHash) {
// Already executed this block, try to get the next one
headerBodyPair, ok = he.blocksCache.GetByNonce(lastExecutedNonce + 1)
if !ok {
he.acknowledgePause()
time.Sleep(timeToSleep)
continue
}
}
err := he.process(headerBodyPair)
if err != nil {
if errors.Is(err, ErrContextMismatch) {
he.acknowledgePause()
time.Sleep(timeToSleep)
continue
}
he.handleProcessError(ctx, headerBodyPair)
}
}
}
func (he *headersExecutor) handleProcessError(ctx context.Context, pair cache.HeaderBodyPair) {
retryCount := 0
backoffTime := timeToSleepOnError
for retryCount < maxRetryAttempts {
he.mutPaused.Lock()
isPaused := he.isPaused
he.mutPaused.Unlock()
if isPaused {
return
}
pairFromQueue, ok := he.blocksCache.GetByNonce(pair.Header.GetNonce())
if ok && !bytes.Equal(pair.HeaderHash, pairFromQueue.HeaderHash) {
// continue the processing (pop the next header from queue)
return
}
select {
case <-ctx.Done():
return
default:
he.mutPaused.Lock()
isPausedRetry := he.isPaused
he.mutPaused.Unlock()
if isPausedRetry {
return
}
// Exponential backoff with maximum limit
time.Sleep(backoffTime)
backoffTime = backoffTime * 2
if backoffTime > maxBackoffTime {
backoffTime = maxBackoffTime
}
he.mutPaused.Lock()
isPausedRetry = he.isPaused
he.mutPaused.Unlock()
if isPausedRetry {
return
}
// retry with the same pair
err := he.process(pair)
if err == nil {
log.Debug("headersExecutor.handleProcessError - retry succeeded",
"nonce", pair.Header.GetNonce(),
"retry_count", retryCount)
return
}
retryCount++
log.Warn("headersExecutor.handleProcessError - retry failed",
"nonce", pair.Header.GetNonce(),
"retry_count", retryCount,
"max_retries", maxRetryAttempts,
"err", err)
}
}
log.Error("headersExecutor.handleProcessError - max retries exceeded, skipping block",
"nonce", pair.Header.GetNonce(),
"max_retries", maxRetryAttempts)
}
func (he *headersExecutor) process(pair cache.HeaderBodyPair) error {
ok := he.checkLastExecutionResultContext(pair.Header, pair.HeaderHash)
if !ok {
return ErrContextMismatch
}
executionResult, err := he.blockProcessor.ProcessBlockProposal(pair.Header, pair.HeaderHash, pair.Body)
if err != nil {
log.Warn("headersExecutor.process process block failed",
"nonce", pair.Header.GetNonce(),
"prevHash", pair.Header.GetPrevHash(),
"err", err,
)
return err
}
// Validate execution result
if check.IfNil(executionResult) {
log.Warn("headersExecutor.process - nil execution result received",
"nonce", pair.Header.GetNonce())
he.blockProcessor.RevertBlockProposalState()
return ErrNilExecutionResult
}
ok = he.checkLastExecutionResultContext(pair.Header, pair.HeaderHash)
if !ok {
he.blockProcessor.RevertBlockProposalState()
return nil
}
lastCommittedBlockHash := he.blockChain.GetCurrentBlockHeaderHash()
lastCommittedBlockHeader := he.blockChain.GetCurrentBlockHeader()
if !check.IfNil(lastCommittedBlockHeader) &&
executionResult.GetHeaderNonce() == lastCommittedBlockHeader.GetNonce() &&
!bytes.Equal(executionResult.GetHeaderHash(), lastCommittedBlockHash) {
log.Debug("headersExecutor.process - execution result header hash does not match last committed block hash",
"nonce", pair.Header.GetNonce(),
"exec_header_hash", executionResult.GetHeaderHash(),
"committed_block_hash", lastCommittedBlockHash,
)
he.blockProcessor.RevertBlockProposalState()
return nil
}
lastExecutionResult := he.blockChain.GetLastExecutionResult()
if !check.IfNil(lastExecutionResult) {
if !bytes.Equal(lastExecutionResult.GetHeaderHash(), pair.Header.GetPrevHash()) {
log.Error("headersExecutor.process - header hash mismatch")
he.blockProcessor.RevertBlockProposalState()
return nil
}
}
// All post-execution checks passed, commit the state now
err = he.blockProcessor.CommitBlockProposalState(pair.Header)
if err != nil {
log.Warn("headersExecutor.process commit block proposal state failed",
"nonce", pair.Header.GetNonce(),
"err", err,
)
he.blockProcessor.RevertBlockProposalState()
return err
}
// Add to execution tracker only after state is committed, so the tracker never
// holds a result whose state was not persisted.
added, err := he.executionTracker.AddExecutionResult(executionResult)
if err != nil {
log.Warn("headersExecutor.process add execution result failed",
"nonce", pair.Header.GetNonce(),
"err", err,
)
return err
}
if !added {
// Result was rejected because consensus already committed a different block for this nonce.
// State was already committed but the corrective flow on the next processing iteration
// will recreate the trie from the expected root hash.
log.Debug("headersExecutor.process execution result not added, skipping blockchain updates",
"nonce", pair.Header.GetNonce(),
)
return nil
}
he.blockProcessor.PruneTrieAsyncHeader(he.blockChain.GetCurrentBlockHeader())
he.blockChain.SetFinalBlockInfo(
executionResult.GetHeaderNonce(),
executionResult.GetHeaderHash(),
executionResult.GetRootHash(),
)
he.blockChain.SetLastExecutedBlockHeaderAndRootHash(pair.Header, executionResult.GetHeaderHash(), executionResult.GetRootHash())
he.blockChain.SetLastExecutionResult(executionResult)
he.signalProcessCompletion(pair.Header.GetNonce())
log.Debug("headersExecutor.process completed",
"nonce", pair.Header.GetNonce(),
"exec nonce", executionResult.GetHeaderNonce(),
"exec rootHash", executionResult.GetRootHash(),
)
return nil
}
func (he *headersExecutor) signalProcessCompletion(currentNonce uint64) {
if he.signalProcessCompletionChan == nil {
return
}
select {
case he.signalProcessCompletionChan <- currentNonce:
default:
}
}
func (he *headersExecutor) checkLastExecutionResultContext(
currentHeader data.HeaderHandler,
currentHeaderHash []byte,
) bool {
if check.IfNil(currentHeader) {
return false
}
lastExecutionResult := he.blockChain.GetLastExecutionResult()
if check.IfNil(lastExecutionResult) {
return true
}
if process.IsReplacementBlockForExecution(currentHeader, currentHeaderHash, lastExecutionResult) {
return true
}
if currentHeader.GetNonce() != lastExecutionResult.GetHeaderNonce()+1 {
log.Debug("headersExecutor.process: concurrent revert event",
"previous nonce", lastExecutionResult.GetHeaderNonce(),
"current nonce", currentHeader.GetNonce(),
"err", process.ErrWrongNonceInBlock,
)
return false
}
lastExecResultHeaderHash := lastExecutionResult.GetHeaderHash()
if !bytes.Equal(lastExecResultHeaderHash, currentHeader.GetPrevHash()) {
log.Debug("headersExecutor.process: concurrent revert event",
"header previous hash", currentHeader.GetPrevHash(),
"last execution result hash", lastExecResultHeaderHash,
"err", process.ErrBlockHashDoesNotMatch,
)
return false
}
return true
}
// GetSignalProcessCompletionChan returns the channel used to signal the sync loop after execution completes
func (he *headersExecutor) GetSignalProcessCompletionChan() chan uint64 {
return he.signalProcessCompletionChan
}
// Close will close the blocks execution loop
func (he *headersExecutor) Close() error {
if he.cancelFunc != nil {
he.cancelFunc()
}
return nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (he *headersExecutor) IsInterfaceNil() bool {
return he == nil
}