Skip to content

Commit

Permalink
Engine API: shorter waits (#13839)
Browse files Browse the repository at this point in the history
Cherry pick PR #13821 into `main`.  See Issue #13773
  • Loading branch information
yperbasis authored Feb 17, 2025
1 parent 579dc63 commit b414526
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions turbo/engineapi/engine_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,14 +437,14 @@ func (s *EngineServer) getQuickPayloadStatusIfPossible(ctx context.Context, bloc
if header != nil && isCanonical {
return &engine_types.PayloadStatus{Status: engine_types.ValidStatus, LatestValidHash: &blockHash}, nil
}
if shouldWait, _ := waitForStuff(func() (bool, error) {
if shouldWait, _ := waitForStuff(50*time.Millisecond, func() (bool, error) {
return parent == nil && s.hd.PosStatus() == headerdownload.Syncing, nil
}); shouldWait {
s.logger.Info(fmt.Sprintf("[%s] Downloading some other PoS blocks", prefix), "hash", blockHash)
return &engine_types.PayloadStatus{Status: engine_types.SyncingStatus}, nil
}
} else {
if shouldWait, _ := waitForStuff(func() (bool, error) {
if shouldWait, _ := waitForStuff(50*time.Millisecond, func() (bool, error) {
return header == nil && s.hd.PosStatus() == headerdownload.Syncing, nil
}); shouldWait {
s.logger.Info(fmt.Sprintf("[%s] Downloading some other PoS stuff", prefix), "hash", blockHash)
Expand All @@ -458,7 +458,7 @@ func (s *EngineServer) getQuickPayloadStatusIfPossible(ctx context.Context, bloc
return &engine_types.PayloadStatus{Status: engine_types.ValidStatus, LatestValidHash: &blockHash}, nil
}
}
waitingForExecutionReady, err := waitForStuff(func() (bool, error) {
waitingForExecutionReady, err := waitForStuff(500*time.Millisecond, func() (bool, error) {
isReady, err := s.chainRW.Ready(ctx)
return !isReady, err
})
Expand Down Expand Up @@ -623,7 +623,7 @@ func (s *EngineServer) forkchoiceUpdated(ctx context.Context, forkchoiceState *e

var resp *execution.AssembleBlockResponse

execBusy, err := waitForStuff(func() (bool, error) {
execBusy, err := waitForStuff(500*time.Millisecond, func() (bool, error) {
resp, err = s.executionService.AssembleBlock(ctx, req)
if err != nil {
return false, err
Expand Down Expand Up @@ -753,15 +753,9 @@ func (e *EngineServer) HandleNewPayload(
if currentHeadNumber != nil {
// We try waiting until we finish downloading the PoS blocks if the distance from the head is enough,
// so that we will perform full validation.
success := false
for i := 0; i < 100; i++ {
time.Sleep(10 * time.Millisecond)
if e.blockDownloader.Status() == headerdownload.Synced {
success = true
break
}
}
if !success {
if stillSyncing, _ := waitForStuff(500*time.Millisecond, func() (bool, error) {
return e.blockDownloader.Status() != headerdownload.Synced, nil
}); stillSyncing {
return &engine_types.PayloadStatus{Status: engine_types.SyncingStatus}, nil
}
status, _, latestValidHash, err := e.chainRW.ValidateChain(ctx, headerHash, headerNumber)
Expand Down Expand Up @@ -891,15 +885,15 @@ func (e *EngineServer) SetConsuming(consuming bool) {
e.consuming.Store(consuming)
}

func waitForStuff(waitCondnF func() (bool, error)) (bool, error) {
func waitForStuff(maxWait time.Duration, waitCondnF func() (bool, error)) (bool, error) {
shouldWait, err := waitCondnF()
if err != nil || !shouldWait {
return false, err
}
// Times out after 8s - loosely based on timeouts of FCU and NewPayload for Ethereum specs
// Look for "timeout" in, for instance, https://github.com/ethereum/execution-apis/blob/main/src/engine/cancun.md
for i := 0; i < 800; i++ {
time.Sleep(10 * time.Millisecond)
checkInterval := 10 * time.Millisecond
maxChecks := int64(maxWait) / int64(checkInterval)
for i := int64(0); i < maxChecks; i++ {
time.Sleep(checkInterval)
shouldWait, err = waitCondnF()
if err != nil || !shouldWait {
return shouldWait, err
Expand Down

0 comments on commit b414526

Please sign in to comment.