-
Notifications
You must be signed in to change notification settings - Fork 658
/
finalizer.go
1368 lines (1214 loc) · 49.4 KB
/
finalizer.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
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package sequencer
import (
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
"sync"
"sync/atomic"
"time"
"github.com/0xPolygonHermez/zkevm-data-streamer/datastreamer"
"github.com/0xPolygonHermez/zkevm-node/event"
"github.com/0xPolygonHermez/zkevm-node/hex"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/pool"
"github.com/0xPolygonHermez/zkevm-node/sequencer/metrics"
"github.com/0xPolygonHermez/zkevm-node/state"
stateMetrics "github.com/0xPolygonHermez/zkevm-node/state/metrics"
"github.com/0xPolygonHermez/zkevm-node/state/runtime"
"github.com/0xPolygonHermez/zkevm-node/state/runtime/executor"
"github.com/ethereum/go-ethereum/common"
"github.com/jackc/pgx/v4"
)
const (
oneHundred = 100
pendingTxsBufferSizeMultiplier = 10
forkId5 uint64 = 5
)
var (
now = time.Now
)
// finalizer represents the finalizer component of the sequencer.
type finalizer struct {
cfg FinalizerCfg
effectiveGasPriceCfg EffectiveGasPriceCfg
closingSignalCh ClosingSignalCh
isSynced func(ctx context.Context) bool
sequencerAddress common.Address
worker workerInterface
dbManager dbManagerInterface
executor stateInterface
batch *WipBatch
batchConstraints batchConstraints
processRequest state.ProcessRequest
sharedResourcesMux *sync.RWMutex
lastGERHash common.Hash
reprocessFullBatchError atomic.Bool
// closing signals
nextGER common.Hash
nextGERDeadline int64
nextGERMux *sync.RWMutex
nextForcedBatches []state.ForcedBatch
nextForcedBatchDeadline int64
nextForcedBatchesMux *sync.RWMutex
handlingL2Reorg bool
// event log
eventLog *event.EventLog
// effective gas price calculation
maxBreakEvenGasPriceDeviationPercentage *big.Int
defaultMinGasPriceAllowed uint64
// Processed txs
pendingTransactionsToStore chan transactionToStore
pendingTransactionsToStoreWG *sync.WaitGroup
storedFlushID uint64
storedFlushIDCond *sync.Cond //Condition to wait until storedFlushID has been updated
proverID string
lastPendingFlushID uint64
pendingFlushIDCond *sync.Cond
streamServer *datastreamer.StreamServer
}
type transactionToStore struct {
hash common.Hash
from common.Address
response *state.ProcessTransactionResponse
batchResponse *state.ProcessBatchResponse
batchNumber uint64
timestamp time.Time
coinbase common.Address
oldStateRoot common.Hash
isForcedBatch bool
flushId uint64
}
// WipBatch represents a work-in-progress batch.
type WipBatch struct {
batchNumber uint64
coinbase common.Address
initialStateRoot common.Hash
stateRoot common.Hash
localExitRoot common.Hash
timestamp time.Time
globalExitRoot common.Hash // 0x000...0 (ZeroHash) means to not update
remainingResources state.BatchResources
countOfTxs int
closingReason state.ClosingReason
}
func (w *WipBatch) isEmpty() bool {
return w.countOfTxs == 0
}
// newFinalizer returns a new instance of Finalizer.
func newFinalizer(
cfg FinalizerCfg,
effectiveGasPriceCfg EffectiveGasPriceCfg,
worker workerInterface,
dbManager dbManagerInterface,
executor stateInterface,
sequencerAddr common.Address,
isSynced func(ctx context.Context) bool,
closingSignalCh ClosingSignalCh,
batchConstraints batchConstraints,
eventLog *event.EventLog,
streamServer *datastreamer.StreamServer,
) *finalizer {
f := finalizer{
cfg: cfg,
effectiveGasPriceCfg: effectiveGasPriceCfg,
closingSignalCh: closingSignalCh,
isSynced: isSynced,
sequencerAddress: sequencerAddr,
worker: worker,
dbManager: dbManager,
executor: executor,
batch: new(WipBatch),
batchConstraints: batchConstraints,
processRequest: state.ProcessRequest{},
sharedResourcesMux: new(sync.RWMutex),
lastGERHash: state.ZeroHash,
// closing signals
nextGER: common.Hash{},
nextGERDeadline: 0,
nextGERMux: new(sync.RWMutex),
nextForcedBatches: make([]state.ForcedBatch, 0),
nextForcedBatchDeadline: 0,
nextForcedBatchesMux: new(sync.RWMutex),
handlingL2Reorg: false,
// event log
eventLog: eventLog,
maxBreakEvenGasPriceDeviationPercentage: new(big.Int).SetUint64(effectiveGasPriceCfg.MaxBreakEvenGasPriceDeviationPercentage),
pendingTransactionsToStore: make(chan transactionToStore, batchConstraints.MaxTxsPerBatch*pendingTxsBufferSizeMultiplier),
pendingTransactionsToStoreWG: new(sync.WaitGroup),
storedFlushID: 0,
// Mutex is unlocked when the condition is broadcasted
storedFlushIDCond: sync.NewCond(&sync.Mutex{}),
proverID: "",
lastPendingFlushID: 0,
pendingFlushIDCond: sync.NewCond(&sync.Mutex{}),
streamServer: streamServer,
}
f.reprocessFullBatchError.Store(false)
return &f
}
// Start starts the finalizer.
func (f *finalizer) Start(ctx context.Context, batch *WipBatch, processingReq *state.ProcessRequest) {
f.defaultMinGasPriceAllowed = f.dbManager.GetDefaultMinGasPriceAllowed()
var err error
if batch != nil {
f.batch = batch
} else {
f.batch, err = f.dbManager.GetWIPBatch(ctx)
if err != nil {
log.Fatalf("failed to get work-in-progress batch from DB, Err: %s", err)
}
}
if processingReq == nil {
log.Fatal("processingReq should not be nil")
} else {
f.processRequest = *processingReq
}
// Closing signals receiver
go f.listenForClosingSignals(ctx)
// Update the prover id and flush id
go f.updateProverIdAndFlushId(ctx)
// Store Pending transactions
go f.storePendingTransactions(ctx)
// Processing transactions and finalizing batches
f.finalizeBatches(ctx)
}
// storePendingTransactions stores the pending transactions in the database
func (f *finalizer) storePendingTransactions(ctx context.Context) {
for {
select {
case tx, ok := <-f.pendingTransactionsToStore:
if !ok {
// Channel is closed
return
}
// Wait until f.storedFlushID >= tx.flushId
f.storedFlushIDCond.L.Lock()
for f.storedFlushID < tx.flushId {
f.storedFlushIDCond.Wait()
// check if context is done after waking up
if ctx.Err() != nil {
f.storedFlushIDCond.L.Unlock()
return
}
}
f.storedFlushIDCond.L.Unlock()
// Now f.storedFlushID >= tx.flushId, we can store tx
f.storeProcessedTx(ctx, tx)
// Delete the tx from the pending list in the worker (addrQueue)
f.worker.DeletePendingTxToStore(tx.hash, tx.from)
f.pendingTransactionsToStoreWG.Done()
case <-ctx.Done():
// The context was cancelled from outside, Wait for all goroutines to finish, cleanup and exit
f.pendingTransactionsToStoreWG.Wait()
return
default:
time.Sleep(100 * time.Millisecond) //nolint:gomnd
}
}
}
// updateProverIdAndFlushId updates the prover id and flush id
func (f *finalizer) updateProverIdAndFlushId(ctx context.Context) {
for {
f.pendingFlushIDCond.L.Lock()
// f.storedFlushID is >= than f.lastPendingFlushID, this means all pending txs (flushid) are stored by the executor.
// We are "synced" with the flush id, therefore we need to wait for new tx (new pending flush id to be stored by the executor)
for f.storedFlushID >= f.lastPendingFlushID {
f.pendingFlushIDCond.Wait()
}
f.pendingFlushIDCond.L.Unlock()
for f.storedFlushID < f.lastPendingFlushID {
storedFlushID, proverID, err := f.dbManager.GetStoredFlushID(ctx)
if err != nil {
log.Errorf("failed to get stored flush id, Err: %v", err)
} else {
if storedFlushID != f.storedFlushID {
// Check if prover/Executor has been restarted
f.checkIfProverRestarted(proverID)
// Update f.storeFlushID and signal condition f.storedFlushIDCond
f.storedFlushIDCond.L.Lock()
f.storedFlushID = storedFlushID
f.storedFlushIDCond.Broadcast()
f.storedFlushIDCond.L.Unlock()
}
}
}
}
}
// listenForClosingSignals listens for signals for the batch and sets the deadline for when they need to be closed.
func (f *finalizer) listenForClosingSignals(ctx context.Context) {
for {
select {
case <-ctx.Done():
log.Infof("finalizer closing signal listener received context done, Err: %s", ctx.Err())
return
// ForcedBatch ch
case fb := <-f.closingSignalCh.ForcedBatchCh:
log.Debugf("finalizer received forced batch at block number: %v", fb.BlockNumber)
f.nextForcedBatchesMux.Lock()
f.nextForcedBatches = f.sortForcedBatches(append(f.nextForcedBatches, fb))
if f.nextForcedBatchDeadline == 0 {
f.setNextForcedBatchDeadline()
}
f.nextForcedBatchesMux.Unlock()
// GlobalExitRoot ch
case ger := <-f.closingSignalCh.GERCh:
log.Debugf("finalizer received global exit root: %s", ger.String())
f.nextGERMux.Lock()
f.nextGER = ger
if f.nextGERDeadline == 0 {
f.setNextGERDeadline()
}
f.nextGERMux.Unlock()
// L2Reorg ch
case <-f.closingSignalCh.L2ReorgCh:
log.Debug("finalizer received L2 reorg event")
f.handlingL2Reorg = true
f.halt(ctx, fmt.Errorf("L2 reorg event received"))
return
}
}
}
// updateLastPendingFLushID updates f.lastPendingFLushID with newFlushID value (it it has changed) and sends
// the signal condition f.pendingFlushIDCond to notify other go funcs that the f.lastPendingFlushID value has changed
func (f *finalizer) updateLastPendingFlushID(newFlushID uint64) {
if newFlushID > f.lastPendingFlushID {
f.lastPendingFlushID = newFlushID
f.pendingFlushIDCond.Broadcast()
}
}
// addPendingTxToStore adds a pending tx that is ready to be stored in the state DB once its flushid has been stored by the executor
func (f *finalizer) addPendingTxToStore(ctx context.Context, txToStore transactionToStore) {
f.pendingTransactionsToStoreWG.Add(1)
f.worker.AddPendingTxToStore(txToStore.hash, txToStore.from)
select {
case f.pendingTransactionsToStore <- txToStore:
case <-ctx.Done():
// If context is cancelled before we can send to the channel, we must decrement the WaitGroup count and
// delete the pending TxToStore added in the worker
f.pendingTransactionsToStoreWG.Done()
f.worker.DeletePendingTxToStore(txToStore.hash, txToStore.from)
}
}
// finalizeBatches runs the endless loop for processing transactions finalizing batches.
func (f *finalizer) finalizeBatches(ctx context.Context) {
log.Debug("finalizer init loop")
for {
start := now()
if f.batch.batchNumber == f.cfg.StopSequencerOnBatchNum {
f.halt(ctx, fmt.Errorf("finalizer reached stop sequencer batch number: %v", f.cfg.StopSequencerOnBatchNum))
}
tx := f.worker.GetBestFittingTx(f.batch.remainingResources)
metrics.WorkerProcessingTime(time.Since(start))
if tx != nil {
log.Debugf("processing tx: %s", tx.Hash.Hex())
// reset the count of effective GasPrice process attempts (since the tx may have been tried to be processed before)
tx.EffectiveGasPriceProcessCount = 0
f.sharedResourcesMux.Lock()
for {
_, err := f.processTransaction(ctx, tx)
if err != nil {
if err == ErrEffectiveGasPriceReprocess {
log.Info("reprocessing tx because of effective gas price calculation: %s", tx.Hash.Hex())
continue
} else {
log.Errorf("failed to process transaction in finalizeBatches, Err: %v", err)
break
}
}
break
}
f.sharedResourcesMux.Unlock()
} else {
// wait for new txs
log.Debugf("no transactions to be processed. Sleeping for %v", f.cfg.SleepDuration.Duration)
if f.cfg.SleepDuration.Duration > 0 {
time.Sleep(f.cfg.SleepDuration.Duration)
}
}
if !f.cfg.SequentialReprocessFullBatch && f.reprocessFullBatchError.Load() {
// There is an error reprocessing previous batch closed (parallel sanity check)
// We halt the execution of the Sequencer at this point
f.halt(ctx, fmt.Errorf("halting Sequencer because of error reprocessing full batch (sanity check). Check previous errors in logs to know which was the cause"))
}
if f.isDeadlineEncountered() {
log.Infof("closing batch %d because deadline was encountered.", f.batch.batchNumber)
f.finalizeBatch(ctx)
} else if f.isBatchFull() || f.isBatchAlmostFull() {
log.Infof("closing batch %d because it's almost full.", f.batch.batchNumber)
f.finalizeBatch(ctx)
}
if err := ctx.Err(); err != nil {
log.Infof("stopping finalizer because of context, err: %s", err)
return
}
}
}
// sortForcedBatches sorts the forced batches by ForcedBatchNumber
func (f *finalizer) sortForcedBatches(fb []state.ForcedBatch) []state.ForcedBatch {
if len(fb) == 0 {
return fb
}
// Sort by ForcedBatchNumber
for i := 0; i < len(fb)-1; i++ {
for j := i + 1; j < len(fb); j++ {
if fb[i].ForcedBatchNumber > fb[j].ForcedBatchNumber {
fb[i], fb[j] = fb[j], fb[i]
}
}
}
return fb
}
// isBatchFull checks if the batch is full
func (f *finalizer) isBatchFull() bool {
if f.batch.countOfTxs >= int(f.batchConstraints.MaxTxsPerBatch) {
log.Infof("Closing batch: %d, because it's full.", f.batch.batchNumber)
f.batch.closingReason = state.BatchFullClosingReason
return true
}
return false
}
// finalizeBatch retries to until successful closes the current batch and opens a new one, potentially processing forced batches between the batch is closed and the resulting new empty batch
func (f *finalizer) finalizeBatch(ctx context.Context) {
start := time.Now()
defer func() {
metrics.ProcessingTime(time.Since(start))
}()
var err error
f.batch, err = f.newWIPBatch(ctx)
for err != nil {
log.Errorf("failed to create new work-in-progress batch, Err: %s", err)
f.batch, err = f.newWIPBatch(ctx)
}
}
// halt halts the finalizer
func (f *finalizer) halt(ctx context.Context, err error) {
event := &event.Event{
ReceivedAt: time.Now(),
Source: event.Source_Node,
Component: event.Component_Sequencer,
Level: event.Level_Critical,
EventID: event.EventID_FinalizerHalt,
Description: fmt.Sprintf("finalizer halted due to error: %s", err),
}
eventErr := f.eventLog.LogEvent(ctx, event)
if eventErr != nil {
log.Errorf("error storing finalizer halt event: %v", eventErr)
}
for {
log.Errorf("fatal error: %s", err)
log.Error("halting the finalizer")
time.Sleep(5 * time.Second) //nolint:gomnd
}
}
// checkIfProverRestarted checks if the proverID changed
func (f *finalizer) checkIfProverRestarted(proverID string) {
if f.proverID != "" && f.proverID != proverID {
event := &event.Event{
ReceivedAt: time.Now(),
Source: event.Source_Node,
Component: event.Component_Sequencer,
Level: event.Level_Critical,
EventID: event.EventID_FinalizerRestart,
Description: fmt.Sprintf("proverID changed from %s to %s, restarting sequencer to discard current WIP batch and work with new executor", f.proverID, proverID),
}
err := f.eventLog.LogEvent(context.Background(), event)
if err != nil {
log.Errorf("error storing payload: %v", err)
}
log.Fatal("restarting sequencer to discard current WIP batch and work with new executor")
}
}
// newWIPBatch closes the current batch and opens a new one, potentially processing forced batches between the batch is closed and the resulting new empty batch
func (f *finalizer) newWIPBatch(ctx context.Context) (*WipBatch, error) {
f.sharedResourcesMux.Lock()
defer f.sharedResourcesMux.Unlock()
// Wait until all processed transactions are saved
startWait := time.Now()
f.pendingTransactionsToStoreWG.Wait()
endWait := time.Now()
log.Info("waiting for pending transactions to be stored took: ", endWait.Sub(startWait).String())
var err error
if f.batch.stateRoot == state.ZeroHash {
return nil, errors.New("state root must have value to close batch")
}
// We need to process the batch to update the state root before closing the batch
if f.batch.initialStateRoot == f.batch.stateRoot {
log.Info("reprocessing batch because the state root has not changed...")
_, err = f.processTransaction(ctx, nil)
if err != nil {
return nil, err
}
}
// Reprocess full batch as sanity check
if f.cfg.SequentialReprocessFullBatch {
// Do the full batch reprocess now
_, err := f.reprocessFullBatch(ctx, f.batch.batchNumber, f.batch.initialStateRoot, f.batch.stateRoot)
if err != nil {
// There is an error reprocessing the batch. We halt the execution of the Sequencer at this point
f.halt(ctx, fmt.Errorf("halting Sequencer because of error reprocessing full batch %d (sanity check). Error: %s ", f.batch.batchNumber, err))
}
} else {
// Do the full batch reprocess in parallel
go func() {
_, _ = f.reprocessFullBatch(ctx, f.batch.batchNumber, f.batch.initialStateRoot, f.batch.stateRoot)
}()
}
// Close the current batch
err = f.closeBatch(ctx)
if err != nil {
return nil, fmt.Errorf("failed to close batch, err: %w", err)
}
// Metadata for the next batch
stateRoot := f.batch.stateRoot
lastBatchNumber := f.batch.batchNumber
// Process Forced Batches
if len(f.nextForcedBatches) > 0 {
lastBatchNumber, stateRoot, err = f.processForcedBatches(ctx, lastBatchNumber, stateRoot)
if err != nil {
log.Warnf("failed to process forced batch, err: %s", err)
}
}
// Take into consideration the GER
f.nextGERMux.Lock()
if f.nextGER != state.ZeroHash {
f.lastGERHash = f.nextGER
}
f.nextGER = state.ZeroHash
f.nextGERDeadline = 0
f.nextGERMux.Unlock()
batch, err := f.openWIPBatch(ctx, lastBatchNumber+1, f.lastGERHash, stateRoot)
if err == nil {
f.processRequest.Timestamp = batch.timestamp
f.processRequest.BatchNumber = batch.batchNumber
f.processRequest.OldStateRoot = stateRoot
f.processRequest.GlobalExitRoot = batch.globalExitRoot
f.processRequest.Transactions = make([]byte, 0, 1)
}
return batch, err
}
// processTransaction processes a single transaction.
func (f *finalizer) processTransaction(ctx context.Context, tx *TxTracker) (errWg *sync.WaitGroup, err error) {
var txHash string
if tx != nil {
txHash = tx.Hash.String()
}
log := log.WithFields("txHash", txHash, "batchNumber", f.processRequest.BatchNumber)
start := time.Now()
defer func() {
metrics.ProcessingTime(time.Since(start))
}()
if f.batch.isEmpty() {
f.processRequest.GlobalExitRoot = f.batch.globalExitRoot
} else {
f.processRequest.GlobalExitRoot = state.ZeroHash
}
hashStr := "nil"
if tx != nil {
f.processRequest.Transactions = tx.RawTx
hashStr = tx.HashStr
log.Infof("EffectiveGasPriceProcessCount=%d", tx.EffectiveGasPriceProcessCount)
// If it is the first time we process this tx then we calculate the BreakEvenGasPrice
if tx.EffectiveGasPriceProcessCount == 0 {
// Get L1 gas price and store in txTracker to make it consistent during the lifespan of the transaction
tx.L1GasPrice = f.dbManager.GetL1GasPrice()
log.Infof("tx.L1GasPrice=%d", tx.L1GasPrice)
// Calculate the new breakEvenPrice
tx.BreakEvenGasPrice, err = f.CalculateTxBreakEvenGasPrice(tx, tx.BatchResources.ZKCounters.CumulativeGasUsed)
if err != nil {
if f.effectiveGasPriceCfg.Enabled {
return nil, err
} else {
log.Warnf("EffectiveGasPrice is disabled, but failed to calculate BreakEvenGasPrice: %s", err)
}
}
}
effectivePercentage := state.MaxEffectivePercentage
if tx.BreakEvenGasPrice != nil && tx.BreakEvenGasPrice.Uint64() != 0 {
// If the tx gas price is lower than the break even gas price, we process the tx with the user gas price (100%)
if tx.GasPrice.Cmp(tx.BreakEvenGasPrice) <= 0 {
tx.IsEffectiveGasPriceFinalExecution = true
} else {
effectivePercentage, err = CalculateEffectiveGasPricePercentage(tx.GasPrice, tx.BreakEvenGasPrice)
if err != nil {
log.Errorf("failed to calculate effective percentage: %s", err)
return nil, err
}
}
}
log.Infof("calculated breakEvenGasPrice: %d, gasPrice: %d, effectivePercentage: %d for tx: %s", tx.BreakEvenGasPrice, tx.GasPrice, effectivePercentage, tx.HashStr)
// If EGP is disabled we use tx GasPrice (MaxEffectivePercentage=255)
if !f.effectiveGasPriceCfg.Enabled {
effectivePercentage = state.MaxEffectivePercentage
}
var effectivePercentageAsDecodedHex []byte
effectivePercentageAsDecodedHex, err = hex.DecodeHex(fmt.Sprintf("%x", effectivePercentage))
if err != nil {
return nil, err
}
forkId := f.dbManager.GetForkIDByBatchNumber(f.processRequest.BatchNumber)
if forkId >= forkId5 {
f.processRequest.Transactions = append(f.processRequest.Transactions, effectivePercentageAsDecodedHex...)
}
} else {
f.processRequest.Transactions = []byte{}
}
log.Infof("processTransaction: single tx. Batch.BatchNumber: %d, BatchNumber: %d, OldStateRoot: %s, txHash: %s, GER: %s", f.batch.batchNumber, f.processRequest.BatchNumber, f.processRequest.OldStateRoot, hashStr, f.processRequest.GlobalExitRoot.String())
processBatchResponse, err := f.executor.ProcessBatch(ctx, f.processRequest, true)
if err != nil && errors.Is(err, runtime.ErrExecutorDBError) {
log.Errorf("failed to process transaction: %s", err)
return nil, err
} else if err == nil && !processBatchResponse.IsRomLevelError && len(processBatchResponse.Responses) == 0 && tx != nil {
err = fmt.Errorf("executor returned no errors and no responses for tx: %s", tx.HashStr)
f.halt(ctx, err)
} else if processBatchResponse.IsExecutorLevelError && tx != nil {
log.Errorf("error received from executor. Error: %v", err)
// Delete tx from the worker
f.worker.DeleteTx(tx.Hash, tx.From)
// Set tx as invalid in the pool
errMsg := processBatchResponse.ExecutorError.Error()
err = f.dbManager.UpdateTxStatus(ctx, tx.Hash, pool.TxStatusInvalid, false, &errMsg)
if err != nil {
log.Errorf("failed to update status to invalid in the pool for tx: %s, err: %s", tx.Hash.String(), err)
} else {
metrics.TxProcessed(metrics.TxProcessedLabelInvalid, 1)
}
return nil, err
}
oldStateRoot := f.batch.stateRoot
if len(processBatchResponse.Responses) > 0 && tx != nil {
errWg, err = f.handleProcessTransactionResponse(ctx, tx, processBatchResponse, oldStateRoot)
if err != nil {
return errWg, err
}
}
// Update in-memory batch and processRequest
f.processRequest.OldStateRoot = processBatchResponse.NewStateRoot
f.batch.stateRoot = processBatchResponse.NewStateRoot
f.batch.localExitRoot = processBatchResponse.NewLocalExitRoot
log.Infof("processTransaction: data loaded in memory. batch.batchNumber: %d, batchNumber: %d, result.NewStateRoot: %s, result.NewLocalExitRoot: %s, oldStateRoot: %s", f.batch.batchNumber, f.processRequest.BatchNumber, processBatchResponse.NewStateRoot.String(), processBatchResponse.NewLocalExitRoot.String(), oldStateRoot.String())
return nil, nil
}
// handleProcessTransactionResponse handles the response of transaction processing.
func (f *finalizer) handleProcessTransactionResponse(ctx context.Context, tx *TxTracker, result *state.ProcessBatchResponse, oldStateRoot common.Hash) (errWg *sync.WaitGroup, err error) {
// Handle Transaction Error
errorCode := executor.RomErrorCode(result.Responses[0].RomError)
if !state.IsStateRootChanged(errorCode) {
// If intrinsic error or OOC error, we skip adding the transaction to the batch
errWg = f.handleProcessTransactionError(ctx, result, tx)
return errWg, result.Responses[0].RomError
}
// Check remaining resources
err = f.checkRemainingResources(result, tx)
if err != nil {
return nil, err
}
if f.effectiveGasPriceCfg.Enabled && !tx.IsEffectiveGasPriceFinalExecution {
err := f.CompareTxBreakEvenGasPrice(ctx, tx, result.Responses[0].GasUsed)
if err != nil {
return nil, err
}
} else if !f.effectiveGasPriceCfg.Enabled {
reprocessNeeded := false
newBreakEvenGasPrice, err := f.CalculateTxBreakEvenGasPrice(tx, result.Responses[0].GasUsed)
if err != nil {
log.Warnf("EffectiveGasPrice is disabled, but failed to calculate BreakEvenGasPrice: %s", err)
} else {
// Compute the absolute difference between tx.BreakEvenGasPrice - newBreakEvenGasPrice
diff := new(big.Int).Abs(new(big.Int).Sub(tx.BreakEvenGasPrice, newBreakEvenGasPrice))
// Compute max difference allowed of breakEvenGasPrice
maxDiff := new(big.Int).Div(new(big.Int).Mul(tx.BreakEvenGasPrice, f.maxBreakEvenGasPriceDeviationPercentage), big.NewInt(100)) //nolint:gomnd
// if diff is greater than the maxDiff allowed
if diff.Cmp(maxDiff) == 1 {
reprocessNeeded = true
}
log.Infof("calculated newBreakEvenGasPrice: %d, tx.BreakEvenGasPrice: %d for tx: %s", newBreakEvenGasPrice, tx.BreakEvenGasPrice, tx.HashStr)
log.Infof("Would need reprocess: %t, diff: %d, maxDiff: %d", reprocessNeeded, diff, maxDiff)
}
}
txToStore := transactionToStore{
hash: tx.Hash,
from: tx.From,
response: result.Responses[0],
batchResponse: result,
batchNumber: f.batch.batchNumber,
timestamp: f.batch.timestamp,
coinbase: f.batch.coinbase,
oldStateRoot: oldStateRoot,
isForcedBatch: false,
flushId: result.FlushID,
}
f.updateLastPendingFlushID(result.FlushID)
f.addPendingTxToStore(ctx, txToStore)
f.batch.countOfTxs++
f.updateWorkerAfterSuccessfulProcessing(ctx, tx.Hash, tx.From, false, result)
return nil, nil
}
// handleForcedTxsProcessResp handles the transactions responses for the processed forced batch.
func (f *finalizer) handleForcedTxsProcessResp(ctx context.Context, request state.ProcessRequest, result *state.ProcessBatchResponse, oldStateRoot common.Hash) {
log.Infof("handleForcedTxsProcessResp: batchNumber: %d, oldStateRoot: %s, newStateRoot: %s", request.BatchNumber, oldStateRoot.String(), result.NewStateRoot.String())
for _, txResp := range result.Responses {
// Handle Transaction Error
if txResp.RomError != nil {
romErr := executor.RomErrorCode(txResp.RomError)
if executor.IsIntrinsicError(romErr) || romErr == executor.RomError_ROM_ERROR_INVALID_RLP {
// If we have an intrinsic error or the RLP is invalid
// we should continue processing the batch, but skip the transaction
log.Errorf("handleForcedTxsProcessResp: ROM error: %s", txResp.RomError)
continue
}
}
from, err := state.GetSender(txResp.Tx)
if err != nil {
log.Warnf("handleForcedTxsProcessResp: failed to get sender for tx (%s): %v", txResp.TxHash, err)
}
txToStore := transactionToStore{
hash: txResp.TxHash,
from: from,
response: txResp,
batchResponse: result,
batchNumber: request.BatchNumber,
timestamp: request.Timestamp,
coinbase: request.Coinbase,
oldStateRoot: oldStateRoot,
isForcedBatch: true,
flushId: result.FlushID,
}
oldStateRoot = txResp.StateRoot
f.updateLastPendingFlushID(result.FlushID)
f.addPendingTxToStore(ctx, txToStore)
if err == nil {
f.updateWorkerAfterSuccessfulProcessing(ctx, txResp.TxHash, from, true, result)
}
}
}
// storeProcessedTx stores the processed transaction in the database.
func (f *finalizer) storeProcessedTx(ctx context.Context, txToStore transactionToStore) {
if txToStore.response != nil {
log.Infof("storeProcessedTx: storing processed txToStore: %s", txToStore.response.TxHash.String())
} else {
log.Info("storeProcessedTx: storing processed txToStore")
}
err := f.dbManager.StoreProcessedTxAndDeleteFromPool(ctx, txToStore)
if err != nil {
log.Info("halting the finalizer because of a database error on storing processed transaction")
f.halt(ctx, err)
}
metrics.TxProcessed(metrics.TxProcessedLabelSuccessful, 1)
}
func (f *finalizer) updateWorkerAfterSuccessfulProcessing(ctx context.Context, txHash common.Hash, txFrom common.Address, isForced bool, result *state.ProcessBatchResponse) {
// Delete the transaction from the worker
if isForced {
f.worker.DeleteForcedTx(txHash, txFrom)
log.Debug("forced tx deleted from worker", "txHash", txHash.String(), "from", txFrom.Hex())
return
} else {
f.worker.DeleteTx(txHash, txFrom)
log.Debug("tx deleted from worker", "txHash", txHash.String(), "from", txFrom.Hex())
}
start := time.Now()
txsToDelete := f.worker.UpdateAfterSingleSuccessfulTxExecution(txFrom, result.ReadWriteAddresses)
for _, txToDelete := range txsToDelete {
err := f.dbManager.UpdateTxStatus(ctx, txToDelete.Hash, pool.TxStatusFailed, false, txToDelete.FailedReason)
if err != nil {
log.Errorf("failed to update status to failed in the pool for tx: %s, err: %s", txToDelete.Hash.String(), err)
continue
}
metrics.TxProcessed(metrics.TxProcessedLabelFailed, 1)
}
metrics.WorkerProcessingTime(time.Since(start))
}
// handleProcessTransactionError handles the error of a transaction
func (f *finalizer) handleProcessTransactionError(ctx context.Context, result *state.ProcessBatchResponse, tx *TxTracker) *sync.WaitGroup {
txResponse := result.Responses[0]
errorCode := executor.RomErrorCode(txResponse.RomError)
addressInfo := result.ReadWriteAddresses[tx.From]
log.Infof("handleTransactionError: error in tx: %s, errorCode: %d", tx.Hash.String(), errorCode)
wg := new(sync.WaitGroup)
failedReason := executor.RomErr(errorCode).Error()
if executor.IsROMOutOfCountersError(errorCode) {
log.Errorf("ROM out of counters error, marking tx with Hash: %s as INVALID, errorCode: %s", tx.Hash.String(), errorCode.String())
start := time.Now()
f.worker.DeleteTx(tx.Hash, tx.From)
metrics.WorkerProcessingTime(time.Since(start))
wg.Add(1)
go func() {
defer wg.Done()
err := f.dbManager.UpdateTxStatus(ctx, tx.Hash, pool.TxStatusInvalid, false, &failedReason)
if err != nil {
log.Errorf("failed to update status to failed in the pool for tx: %s, err: %s", tx.Hash.String(), err)
} else {
metrics.TxProcessed(metrics.TxProcessedLabelInvalid, 1)
}
}()
} else if executor.IsInvalidNonceError(errorCode) || executor.IsInvalidBalanceError(errorCode) {
var (
nonce *uint64
balance *big.Int
)
if addressInfo != nil {
nonce = addressInfo.Nonce
balance = addressInfo.Balance
}
start := time.Now()
log.Errorf("intrinsic error, moving tx with Hash: %s to NOT READY nonce(%d) balance(%d) gasPrice(%d), err: %s", tx.Hash, nonce, balance, tx.GasPrice, txResponse.RomError)
txsToDelete := f.worker.MoveTxToNotReady(tx.Hash, tx.From, nonce, balance)
for _, txToDelete := range txsToDelete {
wg.Add(1)
txToDelete := txToDelete
go func() {
defer wg.Done()
err := f.dbManager.UpdateTxStatus(ctx, txToDelete.Hash, pool.TxStatusFailed, false, &failedReason)
metrics.TxProcessed(metrics.TxProcessedLabelFailed, 1)
if err != nil {
log.Errorf("failed to update status to failed in the pool for tx: %s, err: %s", txToDelete.Hash.String(), err)
}
}()
}
metrics.WorkerProcessingTime(time.Since(start))
} else {
// Delete the transaction from the txSorted list
f.worker.DeleteTx(tx.Hash, tx.From)
log.Debug("tx deleted from txSorted list", "txHash", tx.Hash.String(), "from", tx.From.Hex())
wg.Add(1)
go func() {
defer wg.Done()
// Update the status of the transaction to failed
err := f.dbManager.UpdateTxStatus(ctx, tx.Hash, pool.TxStatusFailed, false, &failedReason)
if err != nil {
log.Errorf("failed to update status to failed in the pool for tx: %s, err: %s", tx.Hash.String(), err)
} else {
metrics.TxProcessed(metrics.TxProcessedLabelFailed, 1)
}
}()
}
return wg
}
// syncWithState syncs the WIP batch and processRequest with the state
func (f *finalizer) syncWithState(ctx context.Context, lastBatchNum *uint64) error {
f.sharedResourcesMux.Lock()
defer f.sharedResourcesMux.Unlock()
var lastBatch *state.Batch
var err error
for !f.isSynced(ctx) {
log.Info("wait for synchronizer to sync last batch")
time.Sleep(time.Second)
}
if lastBatchNum == nil {
lastBatch, err = f.dbManager.GetLastBatch(ctx)
if err != nil {
return fmt.Errorf("failed to get last batch, err: %w", err)
}
} else {
lastBatch, err = f.dbManager.GetBatchByNumber(ctx, *lastBatchNum, nil)
if err != nil {
return fmt.Errorf("failed to get last batch, err: %w", err)
}
}
batchNum := lastBatch.BatchNumber
lastBatchNum = &batchNum
isClosed, err := f.dbManager.IsBatchClosed(ctx, *lastBatchNum)
if err != nil {
return fmt.Errorf("failed to check if batch is closed, err: %w", err)
}
log.Infof("Batch %d isClosed: %v", batchNum, isClosed)
if isClosed {
ger, _, err := f.dbManager.GetLatestGer(ctx, f.cfg.GERFinalityNumberOfBlocks)
if err != nil {
return fmt.Errorf("failed to get latest ger, err: %w", err)
}
oldStateRoot := lastBatch.StateRoot
f.batch, err = f.openWIPBatch(ctx, *lastBatchNum+1, ger.GlobalExitRoot, oldStateRoot)
if err != nil {
return err
}
} else {
f.batch, err = f.dbManager.GetWIPBatch(ctx)
if err != nil {
return fmt.Errorf("failed to get work-in-progress batch, err: %w", err)
}
}
log.Infof("Initial Batch: %+v", f.batch)
log.Infof("Initial Batch.StateRoot: %s", f.batch.stateRoot.String())
log.Infof("Initial Batch.GER: %s", f.batch.globalExitRoot.String())
log.Infof("Initial Batch.Coinbase: %s", f.batch.coinbase.String())
log.Infof("Initial Batch.InitialStateRoot: %s", f.batch.initialStateRoot.String())
log.Infof("Initial Batch.localExitRoot: %s", f.batch.localExitRoot.String())
f.processRequest = state.ProcessRequest{
BatchNumber: *lastBatchNum,
OldStateRoot: f.batch.stateRoot,
GlobalExitRoot: f.batch.globalExitRoot,
Coinbase: f.sequencerAddress,
Timestamp: f.batch.timestamp,
Transactions: make([]byte, 0, 1),
Caller: stateMetrics.SequencerCallerLabel,
}
log.Infof("synced with state, lastBatchNum: %d. State root: %s", *lastBatchNum, f.batch.initialStateRoot.Hex())
return nil
}
// processForcedBatches processes all the forced batches that are pending to be processed
func (f *finalizer) processForcedBatches(ctx context.Context, lastBatchNumberInState uint64, stateRoot common.Hash) (uint64, common.Hash, error) {
f.nextForcedBatchesMux.Lock()
defer f.nextForcedBatchesMux.Unlock()
f.nextForcedBatchDeadline = 0
lastTrustedForcedBatchNumber, err := f.dbManager.GetLastTrustedForcedBatchNumber(ctx, nil)
if err != nil {
return 0, common.Hash{}, fmt.Errorf("failed to get last trusted forced batch number, err: %w", err)
}
nextForcedBatchNum := lastTrustedForcedBatchNumber + 1
for _, forcedBatch := range f.nextForcedBatches {
// Skip already processed forced batches
if forcedBatch.ForcedBatchNumber < nextForcedBatchNum {
continue
}
// Process in-between unprocessed forced batches
for forcedBatch.ForcedBatchNumber > nextForcedBatchNum {
inBetweenForcedBatch, err := f.dbManager.GetForcedBatch(ctx, nextForcedBatchNum, nil)
if err != nil {
return 0, common.Hash{}, fmt.Errorf("failed to get in-between forced batch %d, err: %w", nextForcedBatchNum, err)
}
lastBatchNumberInState, stateRoot = f.processForcedBatch(ctx, lastBatchNumberInState, stateRoot, *inBetweenForcedBatch)
nextForcedBatchNum += 1
}
// Process the current forced batch from the channel queue
lastBatchNumberInState, stateRoot = f.processForcedBatch(ctx, lastBatchNumberInState, stateRoot, forcedBatch)
nextForcedBatchNum += 1
}
f.nextForcedBatches = make([]state.ForcedBatch, 0)
return lastBatchNumberInState, stateRoot, nil
}
func (f *finalizer) processForcedBatch(ctx context.Context, lastBatchNumberInState uint64, stateRoot common.Hash, forcedBatch state.ForcedBatch) (uint64, common.Hash) {
request := state.ProcessRequest{
BatchNumber: lastBatchNumberInState + 1,
OldStateRoot: stateRoot,
GlobalExitRoot: forcedBatch.GlobalExitRoot,
Transactions: forcedBatch.RawTxsData,
Coinbase: f.sequencerAddress,