Skip to content

Commit

Permalink
chore: add error define for forced head events
Browse files Browse the repository at this point in the history
  • Loading branch information
krish-nr committed Aug 6, 2024
1 parent e1be871 commit 2828964
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
3 changes: 1 addition & 2 deletions eth/catalyst/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"errors"
"fmt"
"math/big"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -682,7 +681,7 @@ func (api *ConsensusAPI) delayPayloadImport(block *types.Block) (engine.PayloadS
// that the parent state is missing and the syncer rejected extending the
// current cycle with the new payload.
log.Warn("Ignoring payload with missing parent", "number", block.NumberU64(), "hash", block.Hash(), "parent", block.ParentHash(), "reason", err)
if strings.Contains(err.Error(), "forced head needed for startup") {
if errors.Is(err, downloader.ErrForcedNeeded) {
return engine.PayloadStatusV1{Status: engine.SYNCING}, err
}
} else {
Expand Down
10 changes: 5 additions & 5 deletions eth/catalyst/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
"bytes"
"context"
crand "crypto/rand"
"errors"
"fmt"
"math/big"
"math/rand"
"reflect"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -719,7 +719,7 @@ func TestEmptyBlocks(t *testing.T) {
payload := getNewPayload(t, api, commonAncestor, nil)

status, err := api.NewPayloadV1(*payload)
if err != nil && !strings.Contains(err.Error(), "forced head needed for startup") {
if err != nil && !errors.Is(err, downloader.ErrForcedNeeded) {
t.Fatal(err)
}
if status.Status != engine.VALID {
Expand All @@ -735,7 +735,7 @@ func TestEmptyBlocks(t *testing.T) {
payload = setBlockhash(payload)
// Now latestValidHash should be the common ancestor
status, err = api.NewPayloadV1(*payload)
if err != nil && !strings.Contains(err.Error(), "forced head needed for startup") {
if err != nil && !errors.Is(err, downloader.ErrForcedNeeded) {
t.Fatal(err)
}
if status.Status != engine.INVALID {
Expand All @@ -753,7 +753,7 @@ func TestEmptyBlocks(t *testing.T) {
payload = setBlockhash(payload)
// Now latestValidHash should be the common ancestor
status, err = api.NewPayloadV1(*payload)
if err != nil && !strings.Contains(err.Error(), "forced head needed for startup") {
if err != nil && !errors.Is(err, downloader.ErrForcedNeeded) {
t.Fatal(err)
}
if status.Status != engine.SYNCING {
Expand Down Expand Up @@ -865,7 +865,7 @@ func TestTrickRemoteBlockCache(t *testing.T) {
// feed the payloads to node B
for _, payload := range invalidChain {
status, err := apiB.NewPayloadV1(*payload)
if err != nil && !strings.Contains(err.Error(), "forced head needed for startup") {
if err != nil && !errors.Is(err, downloader.ErrForcedNeeded) {
panic(err)
}
if status.Status == engine.VALID {
Expand Down
6 changes: 5 additions & 1 deletion eth/downloader/skeleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ var errChainGapped = errors.New("chain gapped")
// of the current sync cycle is forked with the one advertised by consensus client.
var errChainForked = errors.New("chain forked")

// ErrForcedNeeded is a public error to signal that the header chain
// of the current sync cycle needs a forced flag at startup.
var ErrForcedNeeded = errors.New("forced head needed for startup")

// maxBlockNumGapTolerance is the max gap tolerance by peer
var maxBlockNumGapTolerance = uint64(30)

Expand Down Expand Up @@ -269,7 +273,7 @@ func (s *skeleton) startup() {
// New head announced, start syncing to it, looping every time a current
// cycle is terminated due to a chain event (head reorg, old chain merge).
if !event.force {
event.errc <- errors.New("forced head needed for startup")
event.errc <- ErrForcedNeeded
continue
}
event.errc <- nil // forced head accepted for startup
Expand Down

0 comments on commit 2828964

Please sign in to comment.