@@ -89,6 +89,7 @@ type work struct {
89
89
cccLogger * ccc.Logger
90
90
vmConfig vm.Config
91
91
92
+ reorging bool
92
93
reorgReason error
93
94
94
95
// accumulated state
@@ -353,7 +354,7 @@ func (w *worker) mainLoop() {
353
354
var retryableCommitError * retryableCommitError
354
355
if errors .As (err , & retryableCommitError ) {
355
356
log .Warn ("failed to commit to a block, retrying" , "err" , err )
356
- if _ , err = w .tryCommitNewWork (time .Now (), w .current .header .ParentHash , w .current .reorgReason ); err != nil {
357
+ if _ , err = w .tryCommitNewWork (time .Now (), w .current .header .ParentHash , w .current .reorging , w . current . reorgReason ); err != nil {
357
358
continue
358
359
}
359
360
} else if err != nil {
@@ -371,20 +372,20 @@ func (w *worker) mainLoop() {
371
372
return
372
373
}
373
374
}
374
- _ , err = w .tryCommitNewWork (time .Now (), w .chain .CurrentHeader ().Hash (), nil )
375
+ _ , err = w .tryCommitNewWork (time .Now (), w .chain .CurrentHeader ().Hash (), false , nil )
375
376
case trigger := <- w .reorgCh :
376
377
idleTimer .UpdateSince (idleStart )
377
378
err = w .handleReorg (& trigger )
378
379
case chainHead := <- w .chainHeadCh :
379
380
idleTimer .UpdateSince (idleStart )
380
381
if w .isCanonical (chainHead .Block .Header ()) {
381
- _ , err = w .tryCommitNewWork (time .Now (), chainHead .Block .Hash (), nil )
382
+ _ , err = w .tryCommitNewWork (time .Now (), chainHead .Block .Hash (), false , nil )
382
383
}
383
384
case <- w .current .deadlineCh ():
384
385
idleTimer .UpdateSince (idleStart )
385
386
w .current .deadlineReached = true
386
387
if len (w .current .txs ) > 0 {
387
- _ , err = w .commit (false )
388
+ _ , err = w .commit ()
388
389
}
389
390
case ev := <- w .txsCh :
390
391
idleTimer .UpdateSince (idleStart )
@@ -396,7 +397,7 @@ func (w *worker) mainLoop() {
396
397
if w .current != nil {
397
398
shouldCommit , _ := w .processTxnSlice (ev .Txs )
398
399
if shouldCommit || w .current .deadlineReached {
399
- _ , err = w .commit (false )
400
+ _ , err = w .commit ()
400
401
}
401
402
}
402
403
atomic .AddInt32 (& w .newTxs , int32 (len (ev .Txs )))
@@ -435,7 +436,7 @@ func (w *worker) collectPendingL1Messages(startIndex uint64) []types.L1MessageTx
435
436
}
436
437
437
438
// newWork
438
- func (w * worker ) newWork (now time.Time , parentHash common.Hash , reorgReason error ) error {
439
+ func (w * worker ) newWork (now time.Time , parentHash common.Hash , reorging bool , reorgReason error ) error {
439
440
parent := w .chain .GetBlockByHash (parentHash )
440
441
header := & types.Header {
441
442
ParentHash : parent .Hash (),
@@ -503,14 +504,15 @@ func (w *worker) newWork(now time.Time, parentHash common.Hash, reorgReason erro
503
504
coalescedLogs : []* types.Log {},
504
505
gasPool : new (core.GasPool ).AddGas (header .GasLimit ),
505
506
nextL1MsgIndex : nextL1MsgIndex ,
507
+ reorging : reorging ,
506
508
reorgReason : reorgReason ,
507
509
}
508
510
return nil
509
511
}
510
512
511
513
// tryCommitNewWork
512
- func (w * worker ) tryCommitNewWork (now time.Time , parent common.Hash , reorgReason error ) (common.Hash , error ) {
513
- err := w .newWork (now , parent , reorgReason )
514
+ func (w * worker ) tryCommitNewWork (now time.Time , parent common.Hash , reorging bool , reorgReason error ) (common.Hash , error ) {
515
+ err := w .newWork (now , parent , reorging , reorgReason )
514
516
if err != nil {
515
517
return common.Hash {}, fmt .Errorf ("failed creating new work: %w" , err )
516
518
}
@@ -521,8 +523,7 @@ func (w *worker) tryCommitNewWork(now time.Time, parent common.Hash, reorgReason
521
523
}
522
524
523
525
// check if we are reorging
524
- reorging := w .chain .GetBlockByNumber (w .current .header .Number .Uint64 ()) != nil
525
- if ! shouldCommit && reorging {
526
+ if ! shouldCommit && w .current .reorging {
526
527
shouldCommit , err = w .processReorgedTxns (w .current .reorgReason )
527
528
}
528
529
if err != nil {
@@ -540,7 +541,7 @@ func (w *worker) tryCommitNewWork(now time.Time, parent common.Hash, reorgReason
540
541
// if reorging, force committing even if we are not "running"
541
542
// this can happen when sequencer is instructed to shutdown while handling a reorg
542
543
// we should make sure reorg is not interrupted
543
- if blockHash , err := w .commit (reorging ); err != nil {
544
+ if blockHash , err := w .commit (); err != nil {
544
545
return common.Hash {}, fmt .Errorf ("failed committing new work: %w" , err )
545
546
} else {
546
547
return blockHash , nil
@@ -658,6 +659,10 @@ func (w *worker) processTxnSlice(txns types.Transactions) (bool, error) {
658
659
// processReorgedTxns
659
660
func (w * worker ) processReorgedTxns (reason error ) (bool , error ) {
660
661
reorgedBlock := w .chain .GetBlockByNumber (w .current .header .Number .Uint64 ())
662
+ if reorgedBlock == nil {
663
+ return false , nil
664
+ }
665
+
661
666
commitGasCounter .Dec (int64 (reorgedBlock .GasUsed ()))
662
667
reorgedTxns := reorgedBlock .Transactions ()
663
668
var errorWithTxnIdx * ccc.ErrorWithTxnIdx
@@ -787,14 +792,14 @@ func (e retryableCommitError) Unwrap() error {
787
792
788
793
// commit runs any post-transaction state modifications, assembles the final block
789
794
// and commits new work if consensus engine is running.
790
- func (w * worker ) commit (reorging bool ) (common.Hash , error ) {
795
+ func (w * worker ) commit () (common.Hash , error ) {
791
796
sealDelay := time .Duration (0 )
792
797
defer func (t0 time.Time ) {
793
798
l2CommitTimer .Update (time .Since (t0 ) - sealDelay )
794
799
}(time .Now ())
795
800
796
801
w .updateSnapshot ()
797
- if ! w .isRunning () && ! reorging {
802
+ if ! w .isRunning () && ! w . current . reorging {
798
803
return common.Hash {}, nil
799
804
}
800
805
@@ -871,7 +876,7 @@ func (w *worker) commit(reorging bool) (common.Hash, error) {
871
876
872
877
currentHeight := w .current .header .Number .Uint64 ()
873
878
maxReorgDepth := uint64 (w .config .CCCMaxWorkers + 1 )
874
- if ! reorging && currentHeight > maxReorgDepth {
879
+ if ! w . current . reorging && currentHeight > maxReorgDepth {
875
880
ancestorHeight := currentHeight - maxReorgDepth
876
881
ancestorHash := w .chain .GetHeaderByNumber (ancestorHeight ).Hash ()
877
882
if rawdb .ReadBlockRowConsumption (w .chain .Database (), ancestorHash ) == nil {
@@ -1038,7 +1043,7 @@ func (w *worker) handleReorg(trigger *reorgTrigger) error {
1038
1043
return nil
1039
1044
}
1040
1045
1041
- newBlockHash , err := w .tryCommitNewWork (time .Now (), parentHash , reorgReason )
1046
+ newBlockHash , err := w .tryCommitNewWork (time .Now (), parentHash , true , reorgReason )
1042
1047
if err != nil {
1043
1048
return err
1044
1049
}
@@ -1047,7 +1052,7 @@ func (w *worker) handleReorg(trigger *reorgTrigger) error {
1047
1052
if newBlockHash == (common.Hash {}) {
1048
1053
// force committing the new canonical head to trigger a reorg in blockchain
1049
1054
// otherwise we might ignore CCC errors from the new side chain since it is not canonical yet
1050
- newBlockHash , err = w .commit (true )
1055
+ newBlockHash , err = w .commit ()
1051
1056
if err != nil {
1052
1057
return err
1053
1058
}
0 commit comments