-
Notifications
You must be signed in to change notification settings - Fork 73
feat(node): add beacon RPC fallback for blob sidecar fetching #1021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
curryxbo
wants to merge
6
commits into
main
Choose a base branch
from
feat/beacon-rpc-fallback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c4b2415
feat(node): add beacon RPC fallback for blob sidecar fetching
73d4c5b
refactor(node): simplify beacon fallback and expose failures via metrics
e4481d3
fix(node): move beacon fallback to the blob-fetch layer
43b4259
fix(node): remove unused derivation context parameter
1b15817
fix(node): correct beacon metrics comment spelling
877a5f1
Merge branch 'main' into feat/beacon-rpc-fallback
curryxbo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| package derivation | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "sync/atomic" | ||
| "testing" | ||
|
|
||
| "github.com/morph-l2/go-ethereum/common" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // zero-value but correctly-sized hex so the beacon JSON decodes into a | ||
| // BlobSidecar. GetBlobSidecarsEnhanced does not verify blob contents (that | ||
| // happens downstream), it only counts sidecars, so dummy bytes are fine here. | ||
| var ( | ||
| hex32 = "0x" + strings.Repeat("00", 32) | ||
| hex48 = "0x" + strings.Repeat("00", 48) | ||
| ) | ||
|
|
||
| func sidecarJSON(index int) string { | ||
| return fmt.Sprintf(`{"block_root":%q,"slot":"1","blob":"0x00","index":"%d","kzg_commitment":%q,"kzg_proof":%q}`, | ||
| hex32, index, hex48, hex48) | ||
| } | ||
|
|
||
| // beaconBehavior controls what a stub beacon returns for blob_sidecars. | ||
| type beaconBehavior int | ||
|
|
||
| const ( | ||
| beaconServesBlob beaconBehavior = iota // 200 with one sidecar | ||
| beaconServesEmpty // 200 with an empty list (pruned / not indexed) | ||
| beaconServerError // 500 | ||
| ) | ||
|
|
||
| // newStubBeacon serves the genesis + spec endpoints (needed for slot math) and | ||
| // answers blob_sidecars according to behavior. It returns the base URL and a | ||
| // counter of blob_sidecars requests received. | ||
| func newStubBeacon(t *testing.T, behavior beaconBehavior) (string, *int32) { | ||
| t.Helper() | ||
| var blobHits int32 | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| switch { | ||
| case strings.Contains(r.URL.Path, genesisMethod): | ||
| _, _ = w.Write([]byte(`{"data":{"genesis_time":"0"}}`)) | ||
| case strings.Contains(r.URL.Path, specMethod): | ||
| _, _ = w.Write([]byte(`{"data":{"SECONDS_PER_SLOT":"12"}}`)) | ||
| case strings.Contains(r.URL.Path, sidecarsMethodPrefix): | ||
| atomic.AddInt32(&blobHits, 1) | ||
| switch behavior { | ||
| case beaconServesBlob: | ||
| _, _ = w.Write([]byte(`{"data":[` + sidecarJSON(0) + `]}`)) | ||
| case beaconServesEmpty: | ||
| _, _ = w.Write([]byte(`{"data":[]}`)) | ||
| case beaconServerError: | ||
| w.WriteHeader(http.StatusInternalServerError) | ||
| } | ||
| default: | ||
| w.WriteHeader(http.StatusNotFound) | ||
| } | ||
| })) | ||
| t.Cleanup(srv.Close) | ||
| return srv.URL, &blobHits | ||
| } | ||
|
|
||
| func oneHash() []IndexedBlobHash { | ||
| return []IndexedBlobHash{{Index: 0, Hash: common.Hash{}}} | ||
| } | ||
|
|
||
| func fetch(t *testing.T, c *FallbackBeaconClient) ([]*BlobSidecar, error) { | ||
| t.Helper() | ||
| return c.GetBlobSidecarsEnhanced(context.Background(), L1BlockRef{Time: 12}, oneHash()) | ||
| } | ||
|
|
||
| // The primary serves the blob and the fallback is never queried. | ||
| func TestFallbackBeacon_PrimaryServesBlob(t *testing.T) { | ||
| primary, primaryHits := newStubBeacon(t, beaconServesBlob) | ||
| fallback, fallbackHits := newStubBeacon(t, beaconServesBlob) | ||
|
|
||
| c := NewFallbackBeaconClient([]string{primary, fallback}, nil, nil) | ||
| sidecars, err := fetch(t, c) | ||
| require.NoError(t, err) | ||
| require.Len(t, sidecars, 1) | ||
| require.EqualValues(t, 1, atomic.LoadInt32(primaryHits)) | ||
| require.EqualValues(t, 0, atomic.LoadInt32(fallbackHits), "fallback must not be queried while primary serves the blob") | ||
| } | ||
|
|
||
| // The key case: primary answers 200 but with NO sidecars (pruned / not indexed). | ||
| // This must fall back to a beacon that actually has the blob. | ||
| func TestFallbackBeacon_FallsBackOnEmptyResult(t *testing.T) { | ||
| primary, primaryHits := newStubBeacon(t, beaconServesEmpty) | ||
| fallback, fallbackHits := newStubBeacon(t, beaconServesBlob) | ||
|
|
||
| c := NewFallbackBeaconClient([]string{primary, fallback}, nil, nil) | ||
| sidecars, err := fetch(t, c) | ||
| require.NoError(t, err) | ||
| require.Len(t, sidecars, 1) | ||
| require.Positive(t, atomic.LoadInt32(primaryHits)) | ||
| require.Positive(t, atomic.LoadInt32(fallbackHits)) | ||
| } | ||
|
|
||
| // A 5xx on the primary also falls back. | ||
| func TestFallbackBeacon_FallsBackOnServerError(t *testing.T) { | ||
| primary, _ := newStubBeacon(t, beaconServerError) | ||
| fallback, fallbackHits := newStubBeacon(t, beaconServesBlob) | ||
|
|
||
| c := NewFallbackBeaconClient([]string{primary, fallback}, nil, nil) | ||
| sidecars, err := fetch(t, c) | ||
| require.NoError(t, err) | ||
| require.Len(t, sidecars, 1) | ||
| require.Positive(t, atomic.LoadInt32(fallbackHits)) | ||
| } | ||
|
|
||
| // An unreachable primary (transport error) falls back. | ||
| func TestFallbackBeacon_FallsBackOnTransportError(t *testing.T) { | ||
| fallback, fallbackHits := newStubBeacon(t, beaconServesBlob) | ||
|
|
||
| c := NewFallbackBeaconClient([]string{"http://127.0.0.1:0", fallback}, nil, nil) | ||
| sidecars, err := fetch(t, c) | ||
| require.NoError(t, err) | ||
| require.Len(t, sidecars, 1) | ||
| require.Positive(t, atomic.LoadInt32(fallbackHits)) | ||
| } | ||
|
|
||
| // When every beacon fails to serve the blob, an error is returned and every | ||
| // endpoint's failure is recorded in metrics (exercised via a real *Metrics). | ||
| func TestFallbackBeacon_AllFailReturnsError(t *testing.T) { | ||
| primary, primaryHits := newStubBeacon(t, beaconServesEmpty) | ||
| fallback, fallbackHits := newStubBeacon(t, beaconServerError) | ||
|
|
||
| m := PrometheusMetrics("morphnode_test_" + t.Name()) | ||
| c := NewFallbackBeaconClient([]string{primary, fallback}, nil, m) | ||
| sidecars, err := fetch(t, c) | ||
| require.Error(t, err) | ||
| require.Nil(t, sidecars) | ||
| require.Positive(t, atomic.LoadInt32(primaryHits)) | ||
| require.Positive(t, atomic.LoadInt32(fallbackHits)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Fail fast on context cancellation.
If
ctxis canceled (e.g., during node shutdown or a request timeout), the current loop treats the resultingcontext.Canceledas a beacon failure, increments the metric, logs an error, and proceeds to the next endpoint (which will also immediately fail).Abort early to avoid spamming logs and artificially skewing the failure metric.
💡 Proposed fix
var lastErr error for i, cl := range c.clients { sidecars, err := cl.GetBlobSidecarsEnhanced(ctx, ref, hashes) if err == nil && len(sidecars) > 0 && len(sidecars) >= len(hashes) { return sidecars, nil } + if ctx.Err() != nil { + return nil, ctx.Err() + } if err == nil { err = fmt.Errorf("beacon returned %d sidecars, want at least %d", len(sidecars), len(hashes)) }📝 Committable suggestion
🤖 Prompt for AI Agents