Skip to content

Commit

Permalink
chore: refine comments
Browse files Browse the repository at this point in the history
  • Loading branch information
krish-nr committed Aug 6, 2024
1 parent 87b9c90 commit 379dccb
Showing 1 changed file with 68 additions and 35 deletions.
103 changes: 68 additions & 35 deletions op-node/rollup/derive/engine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,22 +391,17 @@ func (e *EngineController) InsertUnsafePayload(ctx context.Context, envelope *et
}
}

var (
needResetSafeHead bool
needResetFinalizedHead bool
)
//process inconsistent state
if status.Status == eth.ExecutionInconsistent || e.checkELSyncTriggered(status.Status, err) {
currentL2Info, err := e.getCurrentL2Info(ctx)
if err != nil {
return NewTemporaryError(fmt.Errorf("failed to process inconsistent state: %w", err))
} else {
log.Info("engine has inconsistent state", "unsafe", currentL2Info.Unsafe.Number, "safe", currentL2Info.Safe.Number, "final", currentL2Info.Finalized.Number)
e.SetUnsafeHead(currentL2Info.Unsafe)
if currentL2Info.Safe.Number > currentL2Info.Unsafe.Number {
log.Info("current safe is higher than unsafe block, reset it", "set safe after", currentL2Info.Unsafe.Number, "set safe before", e.safeHead.Number)
e.SetSafeHead(currentL2Info.Unsafe)
}
if currentL2Info.Finalized.Number > currentL2Info.Unsafe.Number {
log.Info("current finalized is higher than unsafe block, reset it", "set Finalized after", currentL2Info.Unsafe.Number, "set Finalized before", e.safeHead.Number)
e.SetFinalizedHead(currentL2Info.Unsafe)
}
needResetSafeHead, needResetFinalizedHead = e.resetSafeAndFinalizedHead(currentL2Info)
}

fcuReq := eth.ForkchoiceState{
Expand All @@ -415,25 +410,9 @@ func (e *EngineController) InsertUnsafePayload(ctx context.Context, envelope *et
FinalizedBlockHash: e.finalizedHead.Hash,
}

for attempts := 0; attempts < maxFCURetryAttempts; attempts++ {
fcuRes, err := e.engine.ForkchoiceUpdate(ctx, &fcuReq, nil)
if err != nil {
if strings.Contains(err.Error(), "context deadline exceeded") {
log.Warn("Failed to share forkchoice-updated signal, attempt %d: %v", attempts+1, err)
time.Sleep(fcuRetryDelay)
continue
}
return NewTemporaryError(fmt.Errorf("engine failed to process due to error: %w", err))
}

if fcuRes.PayloadStatus.Status == eth.ExecutionValid {
log.Info("engine processed data successfully")
e.needFCUCall = false
needSyncWithEngine = true
break
} else {
return NewTemporaryError(fmt.Errorf("engine failed to process inconsistent data"))
}
needSyncWithEngine, err = e.trySyncingWithEngine(ctx, fcuReq)
if err != nil {
return NewTemporaryError(err)
}
}

Expand All @@ -456,13 +435,19 @@ func (e *EngineController) InsertUnsafePayload(ctx context.Context, envelope *et
currentUnsafe, _ := e.engine.L2BlockRefByLabel(ctx, eth.Unsafe)
//reset unsafe
e.SetUnsafeHead(currentUnsafe)
//force reset safe,finalize
e.SetSafeHead(currentUnsafe)
e.SetFinalizedHead(currentUnsafe)

fc.HeadBlockHash = currentUnsafe.Hash
fc.SafeBlockHash = currentUnsafe.Hash
fc.FinalizedBlockHash = currentUnsafe.Hash

//force reset safe,finalize if needed
if needResetFinalizedHead {
e.SetFinalizedHead(currentUnsafe)
fc.FinalizedBlockHash = currentUnsafe.Hash
needResetFinalizedHead = false
}
if needResetSafeHead {
e.SetSafeHead(currentUnsafe)
fc.SafeBlockHash = currentUnsafe.Hash
needResetSafeHead = false
}

needSyncWithEngine = false
}
Expand Down Expand Up @@ -494,6 +479,7 @@ func (e *EngineController) InsertUnsafePayload(ctx context.Context, envelope *et
}

e.needFCUCall = false
// unsafe will update to the latest broadcast block anyway, this will trigger an el sync in geth when meet an inconsistent state and accelerate recover progress.
if e.checkUpdateUnsafeHead(fcRes.PayloadStatus.Status) {
e.SetUnsafeHead(ref)
}
Expand Down Expand Up @@ -619,3 +605,50 @@ func (e *EngineController) getCurrentL2Info(ctx context.Context) (*sync.FindHead
Finalized: finalized,
}, nil
}

// resetSafeAndFinalizedHead will reset current safe/finalized head to keep consistent with unsafe head from engine, reset safe/finalized head if current unsafe is behind them
func (e *EngineController) resetSafeAndFinalizedHead(currentL2Info *sync.FindHeadsResult) (bool, bool) {
var needResetSafeHead, needResetFinalizedHead bool

log.Info("engine has inconsistent state", "unsafe", currentL2Info.Unsafe.Number, "safe", currentL2Info.Safe.Number, "final", currentL2Info.Finalized.Number)
e.SetUnsafeHead(currentL2Info.Unsafe)

if currentL2Info.Safe.Number > currentL2Info.Unsafe.Number {
log.Info("current safe is higher than unsafe block, reset it", "set safe after", currentL2Info.Unsafe.Number, "set safe before", e.safeHead.Number)
e.SetSafeHead(currentL2Info.Unsafe)
needResetSafeHead = true
}

if currentL2Info.Finalized.Number > currentL2Info.Unsafe.Number {
log.Info("current finalized is higher than unsafe block, reset it", "set Finalized after", currentL2Info.Unsafe.Number, "set Finalized before", e.safeHead.Number)
e.SetFinalizedHead(currentL2Info.Unsafe)
needResetFinalizedHead = true
}

return needResetSafeHead, needResetFinalizedHead
}

// trySyncingWithEngine will request engine to deleting data beyond diskroot to keep synced with current node status
func (e *EngineController) trySyncingWithEngine(ctx context.Context, fcuReq eth.ForkchoiceState) (bool, error) {
for attempts := 0; attempts < maxFCURetryAttempts; attempts++ {
fcuRes, err := e.engine.ForkchoiceUpdate(ctx, &fcuReq, nil)
if err != nil {
if strings.Contains(err.Error(), "context deadline exceeded") {
log.Warn("Failed to share forkchoice-updated signal", "attempt:", attempts+1, "err", err)
time.Sleep(fcuRetryDelay)
continue
}
return false, fmt.Errorf("engine failed to process due to error: %w", err)
}

if fcuRes.PayloadStatus.Status == eth.ExecutionValid {
log.Info("engine processed data successfully")
e.needFCUCall = false
return true, nil
} else {
return false, fmt.Errorf("engine failed to process inconsistent data")
}
}

return false, fmt.Errorf("max retry attempts reached for trySyncingWithEngine")
}

0 comments on commit 379dccb

Please sign in to comment.