Skip to content

Commit

Permalink
Engine API: shorter waits (#13821)
Browse files Browse the repository at this point in the history
See Issue #13773
  • Loading branch information
yperbasis authored Feb 15, 2025
1 parent bad5fca commit 3d90cb1
Showing 1 changed file with 17 additions and 26 deletions.
43 changes: 17 additions & 26 deletions turbo/engineapi/engine_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,26 @@ import (
"sync"
"time"

"github.com/erigontech/erigon-lib/common/hexutil"
"github.com/erigontech/erigon/cl/clparams"
"github.com/erigontech/erigon/eth/ethutils"

"github.com/erigontech/erigon-lib/log/v3"

"github.com/erigontech/erigon-lib/chain"
libcommon "github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/common/hexutil"
"github.com/erigontech/erigon-lib/common/hexutility"
"github.com/erigontech/erigon-lib/common/math"
"github.com/erigontech/erigon-lib/gointerfaces"
"github.com/erigontech/erigon-lib/gointerfaces/execution"
"github.com/erigontech/erigon-lib/gointerfaces/txpool"
"github.com/erigontech/erigon-lib/kv"
"github.com/erigontech/erigon-lib/kv/kvcache"
"github.com/erigontech/erigon-lib/log/v3"
libstate "github.com/erigontech/erigon-lib/state"

"github.com/erigontech/erigon-lib/common/math"
"github.com/erigontech/erigon/cl/clparams"
"github.com/erigontech/erigon/cmd/rpcdaemon/cli"
"github.com/erigontech/erigon/cmd/rpcdaemon/cli/httpcfg"
"github.com/erigontech/erigon/common"
"github.com/erigontech/erigon/consensus"
"github.com/erigontech/erigon/consensus/merge"
"github.com/erigontech/erigon/core/types"
"github.com/erigontech/erigon/eth/ethutils"
"github.com/erigontech/erigon/rpc"
"github.com/erigontech/erigon/turbo/engineapi/engine_block_downloader"
"github.com/erigontech/erigon/turbo/engineapi/engine_helpers"
Expand Down Expand Up @@ -388,14 +385,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 @@ -409,7 +406,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 @@ -558,7 +555,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 @@ -680,15 +677,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
}

Expand Down Expand Up @@ -813,15 +804,15 @@ func (e *EngineServer) HandlesForkChoice(
return payloadStatus, nil
}

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 3d90cb1

Please sign in to comment.