-
Notifications
You must be signed in to change notification settings - Fork 1.5k
execution, cl/phase1: stop discarding a payload that is already built #23289
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
lystopad
wants to merge
4
commits into
main
Choose a base branch
from
feature/lystopad/builder-stop-race
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
4 commits
Select commit
Hold shift + click to select a range
3c5c212
execution, cl/phase1: stop discarding a payload that is already built
lystopad 6cb27be
execution: address review
lystopad a6cc442
execution: close the remaining window in the payload priority check
lystopad a6d145b
execution: latch which deadline ran out instead of asking afterwards
lystopad 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Copyright 2026 The Erigon Authors | ||
| // This file is part of Erigon. | ||
| // | ||
| // Erigon is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Lesser General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // Erigon is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Lesser General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Lesser General Public License | ||
| // along with Erigon. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| package builder | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/erigontech/erigon/execution/types" | ||
| ) | ||
|
|
||
| func TestBlockBuilderStopPrefersAFinishedPayloadOverAnExpiredCaller(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| built := make(chan struct{}) | ||
| b := NewBlockBuilder(func(_ *Parameters, interrupt *atomic.Bool) (*types.BlockWithReceipts, error) { | ||
| for !interrupt.Load() { | ||
| time.Sleep(time.Millisecond) | ||
| } | ||
| defer close(built) | ||
| return &types.BlockWithReceipts{Block: types.NewBlock(&types.Header{}, nil, nil, nil, nil)}, nil | ||
| }, &Parameters{}, time.Minute) | ||
|
|
||
| // Both the payload and the deadline are ready before Stop is called. Selecting over the two at | ||
| // once would discard a block that is already built about half the time. | ||
| b.interrupt.Store(true) | ||
| <-built | ||
| // Join once with a live context so the result is latched before the race below is exercised. | ||
| first, err := b.Stop(t.Context()) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, first) | ||
|
|
||
| ctx, cancel := context.WithCancel(t.Context()) | ||
| cancel() | ||
|
|
||
| for range 50 { | ||
| result, err := b.Stop(ctx) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, result) | ||
| } | ||
| } | ||
|
|
||
| // completeOnDoneCheck closes the builder's completion channel the first time the context's Done | ||
| // channel is read, which is when a select is setting itself up. It makes the payload land between | ||
| // the priority probe and the select's choice, an interleaving that is otherwise a few nanoseconds | ||
| // wide. | ||
| type completeOnDoneCheck struct { | ||
| context.Context | ||
| cancelled chan struct{} | ||
| complete func() | ||
| once sync.Once | ||
| } | ||
|
|
||
| func (c *completeOnDoneCheck) Done() <-chan struct{} { | ||
| c.once.Do(c.complete) | ||
| return c.cancelled | ||
| } | ||
|
|
||
| func (c *completeOnDoneCheck) Err() error { return context.Canceled } | ||
|
|
||
| func TestBlockBuilderStopKeepsAPayloadThatLandsWhileTheCallerGivesUp(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Both channels are ready by the time the select chooses, so it picks between them at random. | ||
| // Preferring the payload has to hold on every attempt, not most of them. | ||
| for range 300 { | ||
| done := make(chan struct{}) | ||
| cancelled := make(chan struct{}) | ||
| close(cancelled) | ||
| b := &BlockBuilder{ | ||
| done: done, | ||
| result: &types.BlockWithReceipts{Block: types.NewBlock(&types.Header{}, nil, nil, nil, nil)}, | ||
| } | ||
| ctx := &completeOnDoneCheck{ | ||
| Context: t.Context(), | ||
| cancelled: cancelled, | ||
| complete: func() { close(done) }, | ||
| } | ||
|
|
||
| result, err := b.Stop(ctx) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, result) | ||
| } | ||
| } |
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.
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.
Good catch. Acquire fails for two different reasons and I flattened them: the local 5s timeout, which does mean the module is occupied, and the caller's own context going away, which says nothing about it. Marking the second as busy invites a retry with nothing to wait for.
Only the timeout reports ErrBusy now. The conflation predates this PR, but it was harmless while the error was untyped — making it a sentinel is what turned it into something callers can branch on, so it belongs here. Covered by TestSetHeadReportsBusyOnlyWhenTheModuleIsOccupied, which fails against the previous wrapping.