@@ -89,6 +89,7 @@ type work struct {
8989 cccLogger * ccc.Logger
9090 vmConfig vm.Config
9191
92+ reorging bool
9293 reorgReason error
9394
9495 // accumulated state
@@ -353,7 +354,7 @@ func (w *worker) mainLoop() {
353354 var retryableCommitError * retryableCommitError
354355 if errors .As (err , & retryableCommitError ) {
355356 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 {
357358 continue
358359 }
359360 } else if err != nil {
@@ -371,20 +372,20 @@ func (w *worker) mainLoop() {
371372 return
372373 }
373374 }
374- _ , err = w .tryCommitNewWork (time .Now (), w .chain .CurrentHeader ().Hash (), nil )
375+ _ , err = w .tryCommitNewWork (time .Now (), w .chain .CurrentHeader ().Hash (), false , nil )
375376 case trigger := <- w .reorgCh :
376377 idleTimer .UpdateSince (idleStart )
377378 err = w .handleReorg (& trigger )
378379 case chainHead := <- w .chainHeadCh :
379380 idleTimer .UpdateSince (idleStart )
380381 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 )
382383 }
383384 case <- w .current .deadlineCh ():
384385 idleTimer .UpdateSince (idleStart )
385386 w .current .deadlineReached = true
386387 if len (w .current .txs ) > 0 {
387- _ , err = w .commit (false )
388+ _ , err = w .commit ()
388389 }
389390 case ev := <- w .txsCh :
390391 idleTimer .UpdateSince (idleStart )
@@ -396,7 +397,7 @@ func (w *worker) mainLoop() {
396397 if w .current != nil {
397398 shouldCommit , _ := w .processTxnSlice (ev .Txs )
398399 if shouldCommit || w .current .deadlineReached {
399- _ , err = w .commit (false )
400+ _ , err = w .commit ()
400401 }
401402 }
402403 atomic .AddInt32 (& w .newTxs , int32 (len (ev .Txs )))
@@ -435,7 +436,7 @@ func (w *worker) collectPendingL1Messages(startIndex uint64) []types.L1MessageTx
435436}
436437
437438// 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 {
439440 parent := w .chain .GetBlockByHash (parentHash )
440441 header := & types.Header {
441442 ParentHash : parent .Hash (),
@@ -503,14 +504,15 @@ func (w *worker) newWork(now time.Time, parentHash common.Hash, reorgReason erro
503504 coalescedLogs : []* types.Log {},
504505 gasPool : new (core.GasPool ).AddGas (header .GasLimit ),
505506 nextL1MsgIndex : nextL1MsgIndex ,
507+ reorging : reorging ,
506508 reorgReason : reorgReason ,
507509 }
508510 return nil
509511}
510512
511513// 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 )
514516 if err != nil {
515517 return common.Hash {}, fmt .Errorf ("failed creating new work: %w" , err )
516518 }
@@ -521,8 +523,7 @@ func (w *worker) tryCommitNewWork(now time.Time, parent common.Hash, reorgReason
521523 }
522524
523525 // 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 {
526527 shouldCommit , err = w .processReorgedTxns (w .current .reorgReason )
527528 }
528529 if err != nil {
@@ -540,7 +541,7 @@ func (w *worker) tryCommitNewWork(now time.Time, parent common.Hash, reorgReason
540541 // if reorging, force committing even if we are not "running"
541542 // this can happen when sequencer is instructed to shutdown while handling a reorg
542543 // 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 {
544545 return common.Hash {}, fmt .Errorf ("failed committing new work: %w" , err )
545546 } else {
546547 return blockHash , nil
@@ -658,6 +659,10 @@ func (w *worker) processTxnSlice(txns types.Transactions) (bool, error) {
658659// processReorgedTxns
659660func (w * worker ) processReorgedTxns (reason error ) (bool , error ) {
660661 reorgedBlock := w .chain .GetBlockByNumber (w .current .header .Number .Uint64 ())
662+ if reorgedBlock == nil {
663+ return false , nil
664+ }
665+
661666 commitGasCounter .Dec (int64 (reorgedBlock .GasUsed ()))
662667 reorgedTxns := reorgedBlock .Transactions ()
663668 var errorWithTxnIdx * ccc.ErrorWithTxnIdx
@@ -787,14 +792,14 @@ func (e retryableCommitError) Unwrap() error {
787792
788793// commit runs any post-transaction state modifications, assembles the final block
789794// 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 ) {
791796 sealDelay := time .Duration (0 )
792797 defer func (t0 time.Time ) {
793798 l2CommitTimer .Update (time .Since (t0 ) - sealDelay )
794799 }(time .Now ())
795800
796801 w .updateSnapshot ()
797- if ! w .isRunning () && ! reorging {
802+ if ! w .isRunning () && ! w . current . reorging {
798803 return common.Hash {}, nil
799804 }
800805
@@ -871,7 +876,7 @@ func (w *worker) commit(reorging bool) (common.Hash, error) {
871876
872877 currentHeight := w .current .header .Number .Uint64 ()
873878 maxReorgDepth := uint64 (w .config .CCCMaxWorkers + 1 )
874- if ! reorging && currentHeight > maxReorgDepth {
879+ if ! w . current . reorging && currentHeight > maxReorgDepth {
875880 ancestorHeight := currentHeight - maxReorgDepth
876881 ancestorHash := w .chain .GetHeaderByNumber (ancestorHeight ).Hash ()
877882 if rawdb .ReadBlockRowConsumption (w .chain .Database (), ancestorHash ) == nil {
@@ -1038,7 +1043,7 @@ func (w *worker) handleReorg(trigger *reorgTrigger) error {
10381043 return nil
10391044 }
10401045
1041- newBlockHash , err := w .tryCommitNewWork (time .Now (), parentHash , reorgReason )
1046+ newBlockHash , err := w .tryCommitNewWork (time .Now (), parentHash , true , reorgReason )
10421047 if err != nil {
10431048 return err
10441049 }
@@ -1047,7 +1052,7 @@ func (w *worker) handleReorg(trigger *reorgTrigger) error {
10471052 if newBlockHash == (common.Hash {}) {
10481053 // force committing the new canonical head to trigger a reorg in blockchain
10491054 // 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 ()
10511056 if err != nil {
10521057 return err
10531058 }
0 commit comments