-
Notifications
You must be signed in to change notification settings - Fork 2
feat: eth_getTransactionReceipt waits for txs in mempool
#235
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
ARR4N
wants to merge
1
commit into
main
Choose a base branch
from
arr4n/wait-for-receipts
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
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 |
|---|---|---|
|
|
@@ -9,31 +9,54 @@ import ( | |
| "github.com/ava-labs/libevm/common" | ||
| "github.com/ava-labs/libevm/libevm/ethapi" | ||
|
|
||
| "github.com/ava-labs/strevm/blocks" | ||
| "github.com/ava-labs/strevm/saexec" | ||
| ) | ||
|
|
||
| type immediateReceipts struct { | ||
| exec *saexec.Executor | ||
| vm *VM | ||
| *ethapi.TransactionAPI | ||
| } | ||
|
|
||
| func (ir immediateReceipts) GetTransactionReceipt(ctx context.Context, h common.Hash) (map[string]any, error) { | ||
| r, ok, err := ir.exec.RecentReceipt(ctx, h) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if !ok { | ||
| // The transaction has either not been included yet, or it was cleared | ||
| // from the [saexec.Executor] cache but is on disk. The standard | ||
| // mechanism already differentiates between these scenarios. | ||
| return ir.TransactionAPI.GetTransactionReceipt(ctx, h) | ||
| var _ *saexec.Executor // protect the import for IDE comment resolution | ||
|
|
||
| // [saexec.Executor.RecentReceipt] will only return false if the transaction | ||
| // is yet to be included in an accepted block, or if it was executed so long | ||
| // ago that it is no longer in the cache. Assuming a tx "known" to the | ||
| // network, the former implies that it is in the mempool and the latter | ||
| // requires a fallback to regular receipt retrieval. Since | ||
| // [saexec.Executor.Enqueue] returning without error guarantees that | ||
| // RecentReceipt() will return known, still-cached receipts, we only have to | ||
| // handle the unknown ones. | ||
|
|
||
| // A buffer of 1 avoids a race between our call to RecentReceipt() and a | ||
| // concurrent call to Enqueue() by [VM.AcceptBlock], which only sends an | ||
| // `accepted` event after enqueuing. | ||
| ch := make(chan *blocks.Block, 1) | ||
| sub := ir.vm.exec.SubscribeEnqueueEvent(ch) | ||
| defer sub.Unsubscribe() | ||
|
|
||
| for ; ; <-ch { | ||
| // The mempool might have dropped the tx for reasons other than block | ||
| // inclusion, in which case we'll fall back on regular receipt retrival, | ||
| // which already handles unknown transactions. | ||
| known := ir.vm.mempool.Pool.Has(h) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've been tracing code for a while, and I don't understand when transactions may be dropped from the mempool. Obviously if they're outpriced or something, but when are they removed explicitly? |
||
|
|
||
| r, ok, err := ir.vm.exec.RecentReceipt(ctx, h) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if ok { | ||
| return r.MarshalForRPC(), nil | ||
| } | ||
| if !known { | ||
| return ir.TransactionAPI.GetTransactionReceipt(ctx, h) | ||
| } | ||
|
|
||
| // 'Cause if at first you don't succeed | ||
| // You can dust it off and try again | ||
| // Dust yourself off and try again, try again | ||
| // 𝄢 ♪♩♪♩♩|♪♩♪♩♩ | ||
| } | ||
| return ethapi.MarshalReceipt( | ||
| r.Receipt, | ||
| r.BlockHash, | ||
| r.BlockNumber.Uint64(), | ||
| r.Signer, | ||
| r.Tx, | ||
| int(r.TransactionIndex), //nolint:gosec // Known to not overflow | ||
| ), nil | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
This for loop construction was super strange to me. It might just be because I've never seen it, but I had to whip out go.dev/play to double-check the behavior for this. I now think is equivalent of trying the inner loop once before waiting on the channel at all.
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.
What if we reformat it like this:
I think the behavior is clearer