fix: make reorg mode explicit (#1049)

This commit is contained in:
Ömer Faruk Irmak 2024-09-20 10:19:05 +03:00 committed by GitHub
parent f044582cf9
commit d599666894
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 17 deletions

View file

@ -89,6 +89,7 @@ type work struct {
cccLogger *ccc.Logger cccLogger *ccc.Logger
vmConfig vm.Config vmConfig vm.Config
reorging bool
reorgReason error reorgReason error
// accumulated state // accumulated state
@ -353,7 +354,7 @@ func (w *worker) mainLoop() {
var retryableCommitError *retryableCommitError var retryableCommitError *retryableCommitError
if errors.As(err, &retryableCommitError) { if errors.As(err, &retryableCommitError) {
log.Warn("failed to commit to a block, retrying", "err", err) log.Warn("failed to commit to a block, retrying", "err", err)
if _, err = w.tryCommitNewWork(time.Now(), w.current.header.ParentHash, w.current.reorgReason); err != nil { if _, err = w.tryCommitNewWork(time.Now(), w.current.header.ParentHash, w.current.reorging, w.current.reorgReason); err != nil {
continue continue
} }
} else if err != nil { } else if err != nil {
@ -371,20 +372,20 @@ func (w *worker) mainLoop() {
return return
} }
} }
_, err = w.tryCommitNewWork(time.Now(), w.chain.CurrentHeader().Hash(), nil) _, err = w.tryCommitNewWork(time.Now(), w.chain.CurrentHeader().Hash(), false, nil)
case trigger := <-w.reorgCh: case trigger := <-w.reorgCh:
idleTimer.UpdateSince(idleStart) idleTimer.UpdateSince(idleStart)
err = w.handleReorg(&trigger) err = w.handleReorg(&trigger)
case chainHead := <-w.chainHeadCh: case chainHead := <-w.chainHeadCh:
idleTimer.UpdateSince(idleStart) idleTimer.UpdateSince(idleStart)
if w.isCanonical(chainHead.Block.Header()) { if w.isCanonical(chainHead.Block.Header()) {
_, err = w.tryCommitNewWork(time.Now(), chainHead.Block.Hash(), nil) _, err = w.tryCommitNewWork(time.Now(), chainHead.Block.Hash(), false, nil)
} }
case <-w.current.deadlineCh(): case <-w.current.deadlineCh():
idleTimer.UpdateSince(idleStart) idleTimer.UpdateSince(idleStart)
w.current.deadlineReached = true w.current.deadlineReached = true
if len(w.current.txs) > 0 { if len(w.current.txs) > 0 {
_, err = w.commit(false) _, err = w.commit()
} }
case ev := <-w.txsCh: case ev := <-w.txsCh:
idleTimer.UpdateSince(idleStart) idleTimer.UpdateSince(idleStart)
@ -396,7 +397,7 @@ func (w *worker) mainLoop() {
if w.current != nil { if w.current != nil {
shouldCommit, _ := w.processTxnSlice(ev.Txs) shouldCommit, _ := w.processTxnSlice(ev.Txs)
if shouldCommit || w.current.deadlineReached { if shouldCommit || w.current.deadlineReached {
_, err = w.commit(false) _, err = w.commit()
} }
} }
atomic.AddInt32(&w.newTxs, int32(len(ev.Txs))) atomic.AddInt32(&w.newTxs, int32(len(ev.Txs)))
@ -435,7 +436,7 @@ func (w *worker) collectPendingL1Messages(startIndex uint64) []types.L1MessageTx
} }
// newWork // newWork
func (w *worker) newWork(now time.Time, parentHash common.Hash, reorgReason error) error { func (w *worker) newWork(now time.Time, parentHash common.Hash, reorging bool, reorgReason error) error {
parent := w.chain.GetBlockByHash(parentHash) parent := w.chain.GetBlockByHash(parentHash)
header := &types.Header{ header := &types.Header{
ParentHash: parent.Hash(), ParentHash: parent.Hash(),
@ -503,14 +504,15 @@ func (w *worker) newWork(now time.Time, parentHash common.Hash, reorgReason erro
coalescedLogs: []*types.Log{}, coalescedLogs: []*types.Log{},
gasPool: new(core.GasPool).AddGas(header.GasLimit), gasPool: new(core.GasPool).AddGas(header.GasLimit),
nextL1MsgIndex: nextL1MsgIndex, nextL1MsgIndex: nextL1MsgIndex,
reorging: reorging,
reorgReason: reorgReason, reorgReason: reorgReason,
} }
return nil return nil
} }
// tryCommitNewWork // tryCommitNewWork
func (w *worker) tryCommitNewWork(now time.Time, parent common.Hash, reorgReason error) (common.Hash, error) { func (w *worker) tryCommitNewWork(now time.Time, parent common.Hash, reorging bool, reorgReason error) (common.Hash, error) {
err := w.newWork(now, parent, reorgReason) err := w.newWork(now, parent, reorging, reorgReason)
if err != nil { if err != nil {
return common.Hash{}, fmt.Errorf("failed creating new work: %w", err) return common.Hash{}, fmt.Errorf("failed creating new work: %w", err)
} }
@ -521,8 +523,7 @@ func (w *worker) tryCommitNewWork(now time.Time, parent common.Hash, reorgReason
} }
// check if we are reorging // check if we are reorging
reorging := w.chain.GetBlockByNumber(w.current.header.Number.Uint64()) != nil if !shouldCommit && w.current.reorging {
if !shouldCommit && reorging {
shouldCommit, err = w.processReorgedTxns(w.current.reorgReason) shouldCommit, err = w.processReorgedTxns(w.current.reorgReason)
} }
if err != nil { if err != nil {
@ -540,7 +541,7 @@ func (w *worker) tryCommitNewWork(now time.Time, parent common.Hash, reorgReason
// if reorging, force committing even if we are not "running" // if reorging, force committing even if we are not "running"
// this can happen when sequencer is instructed to shutdown while handling a reorg // this can happen when sequencer is instructed to shutdown while handling a reorg
// we should make sure reorg is not interrupted // we should make sure reorg is not interrupted
if blockHash, err := w.commit(reorging); err != nil { if blockHash, err := w.commit(); err != nil {
return common.Hash{}, fmt.Errorf("failed committing new work: %w", err) return common.Hash{}, fmt.Errorf("failed committing new work: %w", err)
} else { } else {
return blockHash, nil return blockHash, nil
@ -658,6 +659,10 @@ func (w *worker) processTxnSlice(txns types.Transactions) (bool, error) {
// processReorgedTxns // processReorgedTxns
func (w *worker) processReorgedTxns(reason error) (bool, error) { func (w *worker) processReorgedTxns(reason error) (bool, error) {
reorgedBlock := w.chain.GetBlockByNumber(w.current.header.Number.Uint64()) reorgedBlock := w.chain.GetBlockByNumber(w.current.header.Number.Uint64())
if reorgedBlock == nil {
return false, nil
}
commitGasCounter.Dec(int64(reorgedBlock.GasUsed())) commitGasCounter.Dec(int64(reorgedBlock.GasUsed()))
reorgedTxns := reorgedBlock.Transactions() reorgedTxns := reorgedBlock.Transactions()
var errorWithTxnIdx *ccc.ErrorWithTxnIdx var errorWithTxnIdx *ccc.ErrorWithTxnIdx
@ -787,14 +792,14 @@ func (e retryableCommitError) Unwrap() error {
// commit runs any post-transaction state modifications, assembles the final block // commit runs any post-transaction state modifications, assembles the final block
// and commits new work if consensus engine is running. // and commits new work if consensus engine is running.
func (w *worker) commit(reorging bool) (common.Hash, error) { func (w *worker) commit() (common.Hash, error) {
sealDelay := time.Duration(0) sealDelay := time.Duration(0)
defer func(t0 time.Time) { defer func(t0 time.Time) {
l2CommitTimer.Update(time.Since(t0) - sealDelay) l2CommitTimer.Update(time.Since(t0) - sealDelay)
}(time.Now()) }(time.Now())
w.updateSnapshot() w.updateSnapshot()
if !w.isRunning() && !reorging { if !w.isRunning() && !w.current.reorging {
return common.Hash{}, nil return common.Hash{}, nil
} }
@ -871,7 +876,7 @@ func (w *worker) commit(reorging bool) (common.Hash, error) {
currentHeight := w.current.header.Number.Uint64() currentHeight := w.current.header.Number.Uint64()
maxReorgDepth := uint64(w.config.CCCMaxWorkers + 1) maxReorgDepth := uint64(w.config.CCCMaxWorkers + 1)
if !reorging && currentHeight > maxReorgDepth { if !w.current.reorging && currentHeight > maxReorgDepth {
ancestorHeight := currentHeight - maxReorgDepth ancestorHeight := currentHeight - maxReorgDepth
ancestorHash := w.chain.GetHeaderByNumber(ancestorHeight).Hash() ancestorHash := w.chain.GetHeaderByNumber(ancestorHeight).Hash()
if rawdb.ReadBlockRowConsumption(w.chain.Database(), ancestorHash) == nil { if rawdb.ReadBlockRowConsumption(w.chain.Database(), ancestorHash) == nil {
@ -1038,7 +1043,7 @@ func (w *worker) handleReorg(trigger *reorgTrigger) error {
return nil return nil
} }
newBlockHash, err := w.tryCommitNewWork(time.Now(), parentHash, reorgReason) newBlockHash, err := w.tryCommitNewWork(time.Now(), parentHash, true, reorgReason)
if err != nil { if err != nil {
return err return err
} }
@ -1047,7 +1052,7 @@ func (w *worker) handleReorg(trigger *reorgTrigger) error {
if newBlockHash == (common.Hash{}) { if newBlockHash == (common.Hash{}) {
// force committing the new canonical head to trigger a reorg in blockchain // force committing the new canonical head to trigger a reorg in blockchain
// otherwise we might ignore CCC errors from the new side chain since it is not canonical yet // otherwise we might ignore CCC errors from the new side chain since it is not canonical yet
newBlockHash, err = w.commit(true) newBlockHash, err = w.commit()
if err != nil { if err != nil {
return err return err
} }

View file

@ -24,7 +24,7 @@ import (
const ( const (
VersionMajor = 5 // Major version component of the current release VersionMajor = 5 // Major version component of the current release
VersionMinor = 7 // Minor version component of the current release VersionMinor = 7 // Minor version component of the current release
VersionPatch = 18 // Patch version component of the current release VersionPatch = 19 // Patch version component of the current release
VersionMeta = "mainnet" // Version metadata to append to the version string VersionMeta = "mainnet" // Version metadata to append to the version string
) )