-
Notifications
You must be signed in to change notification settings - Fork 11
fix(evm): make EnrichedMegaTx's cached tx_size/da_size reachable #331
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
RealiCZ
wants to merge
7
commits into
main
Choose a base branch
from
cz/fix/enriched-mega-tx-cached-size
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
7 commits
Select commit
Hold shift + click to select a range
5c2ceee
fix(evm): make EnrichedMegaTx's precomputed tx_size/da_size reachable
RealiCZ f74e3c1
docs(evm): document run_transaction_with_sizes, reflow bench comment …
RealiCZ e4a23f2
fix(evm): enforce the tx_size/da_size cache contract on run_transacti…
RealiCZ 67123e5
fix(evm): gate the debug_assert-only test to debug builds, fix broken…
RealiCZ 446020e
fix(evm): delegate type_flag, close patch coverage gap on the enriche…
RealiCZ fd4e93d
refactor(evm): unify enriched tx execution into a single run_transaction
Troublor 50589a9
test(evm): cover Recovered<&MegaTxEnvelope>::tx_hash
RealiCZ 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,103 @@ | ||
| //! Benchmarks the cost of `MegaTransactionExt::{tx_size, estimated_da_size}` recompute vs the | ||
| //! `EnrichedMegaTx` cached fields. | ||
| //! | ||
| //! `EnrichedMegaTx` exists to precompute `tx_size`/`da_size` once (e.g. via `new_slow` from a | ||
| //! mempool-cached value) so block-execution callers can reuse them instead of recomputing on | ||
| //! every access. Three rows: | ||
| //! - `recompute_via_tx_unwrap` mirrors the `alloy_evm` block-execution path's call pattern | ||
| //! (`tx.tx().estimated_da_size()` / `tx.tx().tx_size()`), which unwraps `EnrichedMegaTx` down to | ||
| //! the raw inner transaction and always hits the recomputing default impl. | ||
| //! - `via_trait_dispatch` mirrors `MegaBlockExecutor::run_transaction`'s call pattern | ||
| //! (`tx.estimated_da_size()` / `tx.tx_size()` on the outer wrapper), which dispatches to the | ||
| //! stored fields for an `EnrichedMegaTx`. | ||
| //! - `cached_fields` reads the wrapper's precomputed fields directly, the floor | ||
| //! `via_trait_dispatch` should match. | ||
|
|
||
| #![allow(missing_docs)] | ||
|
|
||
| use alloy_consensus::{transaction::Recovered, Signed, TxLegacy}; | ||
| use alloy_evm::RecoveredTx; | ||
| use alloy_primitives::{address, Address, Bytes, Signature, TxKind, U256}; | ||
| use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; | ||
| use mega_evm::{EnrichedMegaTx, MegaTransactionExt, MegaTxEnvelope}; | ||
|
|
||
| const CALLER: Address = address!("2000000000000000000000000000000000000001"); | ||
| const CONTRACT: Address = address!("3000000000000000000000000000000000000001"); | ||
|
|
||
| /// Calldata sizes spanning a plain transfer up to a large multicall-style payload. | ||
| const CALLDATA_SIZES: &[usize] = &[0, 68, 180, 1000]; | ||
|
|
||
| fn enriched_tx(calldata_len: usize) -> EnrichedMegaTx<Recovered<MegaTxEnvelope>> { | ||
| let tx = TxLegacy { | ||
| chain_id: Some(1), | ||
| nonce: 7, | ||
| gas_price: 9, | ||
| gas_limit: 21_000, | ||
| to: TxKind::Call(CONTRACT), | ||
| value: U256::from(11), | ||
| input: Bytes::from(vec![0xabu8; calldata_len]), | ||
| }; | ||
| let envelope = MegaTxEnvelope::Legacy(Signed::new_unchecked( | ||
| tx, | ||
| Signature::test_signature(), | ||
| Default::default(), | ||
| )); | ||
| let recovered = Recovered::new_unchecked(envelope, CALLER); | ||
| EnrichedMegaTx::new_slow(recovered) | ||
| } | ||
|
|
||
| /// The `alloy_evm` block-execution path's pattern: `tx.tx().<method>()` unwraps `EnrichedMegaTx` | ||
| /// down to the raw inner transaction, so `tx_size`/`estimated_da_size` always hit the recomputing | ||
| /// default impl even when the wrapper carries precomputed fields. | ||
| fn bench_recompute_via_tx_unwrap(c: &mut Criterion) { | ||
| let mut group = c.benchmark_group("tx_size_da_size/recompute_via_tx_unwrap"); | ||
| for &len in CALLDATA_SIZES { | ||
| let tx = enriched_tx(len); | ||
| group.bench_with_input(BenchmarkId::new("estimated_da_size", len), &tx, |b, tx| { | ||
| b.iter(|| black_box(tx.tx()).estimated_da_size()) | ||
| }); | ||
| group.bench_with_input(BenchmarkId::new("tx_size", len), &tx, |b, tx| { | ||
| b.iter(|| black_box(tx.tx()).tx_size()) | ||
| }); | ||
| } | ||
| group.finish(); | ||
| } | ||
|
|
||
| /// `MegaBlockExecutor::run_transaction`'s call pattern: `tx.<method>()` called directly on the | ||
| /// outer `EnrichedMegaTx`, dispatching to the stored fields via `MegaTransactionExt`. | ||
| fn bench_via_trait_dispatch(c: &mut Criterion) { | ||
| let mut group = c.benchmark_group("tx_size_da_size/via_trait_dispatch"); | ||
| for &len in CALLDATA_SIZES { | ||
| let tx = enriched_tx(len); | ||
| group.bench_with_input(BenchmarkId::new("estimated_da_size", len), &tx, |b, tx| { | ||
| b.iter(|| black_box(tx).estimated_da_size()) | ||
| }); | ||
| group.bench_with_input(BenchmarkId::new("tx_size", len), &tx, |b, tx| { | ||
| b.iter(|| black_box(tx).tx_size()) | ||
| }); | ||
| } | ||
| group.finish(); | ||
| } | ||
|
|
||
| /// Reading `EnrichedMegaTx`'s precomputed fields directly. | ||
| fn bench_cached_fields(c: &mut Criterion) { | ||
| let mut group = c.benchmark_group("tx_size_da_size/cached_fields"); | ||
| for &len in CALLDATA_SIZES { | ||
| let tx = enriched_tx(len); | ||
| group.bench_with_input(BenchmarkId::new("estimated_da_size", len), &tx, |b, tx| { | ||
| b.iter(|| black_box(tx).da_size) | ||
| }); | ||
| group.bench_with_input(BenchmarkId::new("tx_size", len), &tx, |b, tx| { | ||
| b.iter(|| black_box(tx).tx_size) | ||
| }); | ||
| } | ||
| group.finish(); | ||
| } | ||
|
|
||
| criterion_group!( | ||
| benches, | ||
| bench_recompute_via_tx_unwrap, | ||
| bench_via_trait_dispatch, | ||
| bench_cached_fields | ||
| ); | ||
| criterion_main!(benches); |
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.
[Minor]
run_transaction_with_sizesis nowpub, but its docstring lacks the trust-boundary warning thatrun_transactioncarries. Sizes flow directly intoBlockLimiter::pre_execution_check(via itstx_encode_size_limit/tx_da_size_limit/block cumulative-size checks) with no validation and — unlikerun_transaction— nodebug_assertcross-check, so an external caller reaching for this as a mere "I already have the sizes, skip recomputation" optimization can silently bypass consensus-relevant limits.The only in-crate caller besides
run_transactionisexecute_transaction_with_commit_conditionin the same file (which recomputes fresh sizes on line 649–650), so thepubvisibility is only meaningful for external callers. If pub is intentional, please duplicate (or explicitly link to) the "Correctness" section fromrun_transactionso callers see the same warning they would have seen if they had implementedMegaTransactionExt. Alternatively considerpub(crate)— external callers who want the shortcut can implementMegaTransactionExtand get the debug_assert safety net for free.