-
Notifications
You must be signed in to change notification settings - Fork 4k
op-node: buffer unsafe payloads with priority queue [bedrock] #3346
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
246fb0e
op-node: buffer unsafe payloads with priority queue, pop lowest numbe…
protolambda 6c2244b
payload queue testing
protolambda eb08e30
op-node: payload queue metrics, error handling
protolambda 2f1ad26
op-node: fix payloads queue test missing pop
protolambda 451fc8c
op-node: payloads queue false semgrep case
protolambda 8e88f55
Merge branch 'develop' into p2p-payloads-buffer
mslipper 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| package derive | ||
|
|
||
| import ( | ||
| "container/heap" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/ethereum-optimism/optimism/op-node/eth" | ||
| ) | ||
|
|
||
| type payloadAndSize struct { | ||
| payload *eth.ExecutionPayload | ||
| size uint64 | ||
| } | ||
|
|
||
| // payloadsByNumber buffers payloads ordered by block number. | ||
| // The lowest block number is peeked/popped first. | ||
| // | ||
| // payloadsByNumber implements heap.Interface: use the heap package methods to modify the queue. | ||
| type payloadsByNumber []payloadAndSize | ||
|
|
||
| var _ heap.Interface = (*payloadsByNumber)(nil) | ||
|
|
||
| func (pq payloadsByNumber) Len() int { return len(pq) } | ||
|
|
||
| func (pq payloadsByNumber) Less(i, j int) bool { | ||
| return pq[i].payload.BlockNumber < pq[j].payload.BlockNumber | ||
| } | ||
|
|
||
| // Swap is a heap.Interface method. Do not use this method directly. | ||
| func (pq payloadsByNumber) Swap(i, j int) { | ||
| pq[i], pq[j] = pq[j], pq[i] | ||
| } | ||
|
|
||
| // Push is a heap.Interface method. Do not use this method directly, use heap.Push instead. | ||
| func (pq *payloadsByNumber) Push(x any) { | ||
| *pq = append(*pq, x.(payloadAndSize)) | ||
| } | ||
|
|
||
| // Pop is a heap.Interface method. Do not use this method directly, use heap.Pop instead. | ||
| func (pq *payloadsByNumber) Pop() any { | ||
| old := *pq | ||
| n := len(old) | ||
| item := old[n-1] | ||
| old[n-1] = payloadAndSize{} // avoid memory leak | ||
| *pq = old[0 : n-1] | ||
| return item | ||
| } | ||
|
|
||
| const ( | ||
| // ~580 bytes per payload, with some margin for overhead | ||
| payloadMemFixedCost uint64 = 600 | ||
| // 24 bytes per tx overhead (size of slice header in memory) | ||
| payloadTxMemOverhead uint64 = 24 | ||
| ) | ||
|
|
||
| func payloadMemSize(p *eth.ExecutionPayload) uint64 { | ||
| out := payloadMemFixedCost | ||
| if p == nil { | ||
| return out | ||
| } | ||
| // 24 byte overhead per tx | ||
| for _, tx := range p.Transactions { | ||
| out += uint64(len(tx)) + payloadTxMemOverhead | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| // PayloadsQueue buffers payloads by block number. | ||
| // PayloadsQueue is not safe to use concurrently. | ||
| // PayloadsQueue exposes typed Push/Peek/Pop methods to use the queue, | ||
| // without the need to use heap.Push/heap.Pop as caller. | ||
| // PayloadsQueue maintains a MaxSize by counting and tracking sizes of added eth.ExecutionPayload entries. | ||
| // When the size grows too large, the first (lowest block-number) payload is removed from the queue. | ||
| // PayloadsQueue allows entries with same block number, or even full duplicates. | ||
| type PayloadsQueue struct { | ||
| pq payloadsByNumber | ||
| currentSize uint64 | ||
| MaxSize uint64 | ||
| SizeFn func(p *eth.ExecutionPayload) uint64 | ||
| } | ||
|
|
||
| func (upq *PayloadsQueue) Len() int { | ||
| return len(upq.pq) | ||
| } | ||
|
|
||
| func (upq *PayloadsQueue) MemSize() uint64 { | ||
| return upq.currentSize | ||
| } | ||
|
|
||
| // Push adds the payload to the queue, in O(log(N)). | ||
| // | ||
| // Don't DoS ourselves by buffering too many unsafe payloads. | ||
| // If the queue size after pushing exceed the allowed memory, then pop payloads until memory is not exceeding anymore. | ||
| // | ||
| // We prefer higher block numbers over lower block numbers, since lower block numbers are more likely to be conflicts and/or read from L1 sooner. | ||
| // The higher payload block numbers can be preserved, and once L1 contents meets these, they can all be processed in order. | ||
| func (upq *PayloadsQueue) Push(p *eth.ExecutionPayload) error { | ||
| if p == nil { | ||
| return errors.New("cannot add nil payload") | ||
| } | ||
| size := upq.SizeFn(p) | ||
| if size > upq.MaxSize { | ||
| return fmt.Errorf("cannot add payload %s, payload mem size %d is larger than max queue size %d", p.ID(), size, upq.MaxSize) | ||
| } | ||
| heap.Push(&upq.pq, payloadAndSize{ | ||
| payload: p, | ||
| size: size, | ||
| }) | ||
| upq.currentSize += size | ||
| for upq.currentSize > upq.MaxSize { | ||
| upq.Pop() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Peek retrieves the payload with the lowest block number from the queue in O(1), or nil if the queue is empty. | ||
| func (upq *PayloadsQueue) Peek() *eth.ExecutionPayload { | ||
| if len(upq.pq) == 0 { | ||
| return nil | ||
| } | ||
| // peek into the priority queue, the first element is the highest priority (lowest block number). | ||
| // This does not apply to other elements, those are structured like a heap. | ||
| return upq.pq[0].payload | ||
| } | ||
|
|
||
| // Pop removes the payload with the lowest block number from the queue in O(log(N)), | ||
| // and may return nil if the queue is empty. | ||
| func (upq *PayloadsQueue) Pop() *eth.ExecutionPayload { | ||
| if len(upq.pq) == 0 { | ||
| return nil | ||
| } | ||
| ps := heap.Pop(&upq.pq).(payloadAndSize) | ||
|
mslipper marked this conversation as resolved.
Outdated
|
||
| upq.currentSize -= ps.size | ||
| return ps.payload | ||
| } | ||
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.
Nice 🎉