-
Notifications
You must be signed in to change notification settings - Fork 471
refactor(test-benchmark): update test_account_access, add keccak256 overhead scenario
#2947
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
LouisTsai-Csie
wants to merge
7
commits into
ethereum:forks/amsterdam
Choose a base branch
from
LouisTsai-Csie:refactor-account-access-bench
base: forks/amsterdam
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
be591ea
chore: remove account access benchmark
LouisTsai-Csie 259dbff
feat: account creation helper function
LouisTsai-Csie 841fc93
feat: add account access benchmark
LouisTsai-Csie 9719544
refactor: ether transfer benchmark
LouisTsai-Csie 4727dce
feat: add contract deployment script
LouisTsai-Csie 4a66a2e
feat: add eoa delegation target
LouisTsai-Csie 5c3eb9e
temp
LouisTsai-Csie 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 @@ | ||
| """Shared helpers reused across benchmark test suites.""" |
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,319 @@ | ||
| """Benchmark target accounts of various kinds for creation and location..""" | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
| from enum import Enum, auto | ||
| from functools import partial | ||
| from typing import ClassVar, Self | ||
|
|
||
| from execution_testing import ( | ||
| DETERMINISTIC_FACTORY_ADDRESS, | ||
| Bytecode, | ||
| Create2PreimageLayout, | ||
| Hash, | ||
| Op, | ||
| SequentialAddressLayout, | ||
| keccak256, | ||
| ) | ||
|
|
||
| # Runtime code size of the jochemnet contract (EIP-170 limit). | ||
| JOCHEMNET_RUNTIME_SIZE = 0x6000 | ||
|
|
||
|
|
||
| class AccountMode(Enum): | ||
| """Benchmark target account variant.""" | ||
|
|
||
| # Minimal contract: single STOP byte. | ||
| EXISTING_CONTRACT_MINIMAL = auto() | ||
|
|
||
| # Max-size contract: byte-identical across copies. | ||
| EXISTING_CONTRACT_SAME = auto() | ||
|
|
||
| # Max-size contract: ADDRESS-embedded, each copy unique. | ||
| EXISTING_CONTRACT_DIFF = auto() | ||
|
|
||
| # Max-size contract: exercises JUMPDEST analysis. | ||
| EXISTING_CONTRACT_JUMPDEST = auto() | ||
|
|
||
| # EOA with balance. | ||
| EXISTING_EOA = auto() | ||
|
|
||
| # Empty account | ||
| NON_EXISTING_ACCOUNT = auto() | ||
|
|
||
|
|
||
| class ContractInitcode(Bytecode): | ||
| """Initcode for target contract receiver.""" | ||
|
|
||
| @property | ||
| def runtime_size(self) -> int: | ||
| """Size in bytes of the deployed runtime.""" | ||
| raise NotImplementedError | ||
|
|
||
| @property | ||
| def runtime_code(self) -> Bytecode: | ||
| """Runtime executed when the deployed contract is called.""" | ||
| raise NotImplementedError | ||
|
|
||
|
|
||
| class MinimalContractInitcode(ContractInitcode): | ||
| """Initcode whose deployed runtime is a single STOP opcode.""" | ||
|
|
||
| def __new__(cls) -> Self: | ||
| """Assemble the initcode.""" | ||
| return super().__new__(cls, Op.RETURN(Op.PUSH1(0), Op.PUSH1(1))) | ||
|
|
||
| @property | ||
| def runtime_size(self) -> int: | ||
| """Size in bytes of the deployed runtime.""" | ||
| return len(Op.STOP) | ||
|
|
||
| @property | ||
| def runtime_code(self) -> Bytecode: | ||
| """Runtime executed when the deployed contract is called.""" | ||
| return Op.STOP | ||
|
|
||
|
|
||
| class UniqueMaxContractInitcode(ContractInitcode): | ||
| """ | ||
| Initcode for a max code size JUMPDEST-filled runtime contract. | ||
|
|
||
| offset size contents | ||
| ------ ---- -------------------------------- | ||
| 0x0000 1 STOP <- a call halts here | ||
| 0x0001 11 00 padding <- diff=True only | ||
| 0x000C 20 contract ADDRESS <- diff=True only | ||
| 0x0020 24544 JUMPDEST <- fills up to 0x6000 | ||
|
|
||
| *diff* embeds ADDRESS (bytes 12-31), making each copy unique. | ||
| Without it, all copies are identical (JUMPDEST bytes 1-31) | ||
| """ | ||
|
|
||
| def __new__(cls, *, diff: bool = False) -> Self: | ||
| """Assemble the initcode.""" | ||
| # Each MCOPY doubles the JUMPDEST-filled span up to MEM[0:0x8000]; | ||
| # the deployed runtime only uses MEM[0:0x6000]. | ||
| code = Op.MSTORE(0, bytes(Op.JUMPDEST * 32)) | ||
| for size in (1 << s for s in range(5, 15)): | ||
| code += Op.MCOPY(size, 0, size) | ||
|
|
||
| if diff: | ||
| # Embeds ADDRESS in the runtime to make each copy unique | ||
| code += Op.MSTORE(0, Op.ADDRESS) | ||
| else: | ||
| # Without embedding, all copies are byte-identical; | ||
| code += Op.MSTORE8(0, 0) | ||
| code += Op.RETURN(0, JOCHEMNET_RUNTIME_SIZE) | ||
| return super().__new__(cls, code) | ||
|
|
||
| @property | ||
| def runtime_size(self) -> int: | ||
| """Size in bytes of the deployed runtime.""" | ||
| return JOCHEMNET_RUNTIME_SIZE | ||
|
|
||
| @property | ||
| def runtime_code(self) -> Bytecode: | ||
| """Runtime executed when the deployed contract is called.""" | ||
| return Op.STOP | ||
|
|
||
|
|
||
| class JochemnetPredeployContractInitcode(ContractInitcode): | ||
| """ | ||
| Initcode whose deployed runtime embeds its own contract ADDRESS. | ||
|
|
||
| offset size contents | ||
| ------ ---- -------------------------------- | ||
| 0x0000 4 PUSH2 0x5FFF; JUMP <- entry | ||
| 0x0004 28 JUMPDEST padding | ||
| 0x0020 12 JUMPDEST padding | ||
| 0x002C 20 contract ADDRESS <- unique | ||
| 0x0040 24512 JUMPDEST <- 0x5FFF lands here | ||
| 0x6000 STOP | ||
|
|
||
| Embedded ADDRESS makes the runtime unique per contract; initcode and | ||
| its CREATE2 hash are shared across all salts. | ||
| """ | ||
|
|
||
| def __new__(cls) -> Self: | ||
| """Assemble the initcode.""" | ||
| max_code_size = JOCHEMNET_RUNTIME_SIZE | ||
|
|
||
| # Each MCOPY doubles the JUMPDEST-filled span up to MEM[0:0x8000]; | ||
| # the deployed runtime only uses MEM[0:0x6000]. | ||
| code = Op.MSTORE(0, bytes(Op.JUMPDEST * 32)) | ||
| for size in (1 << s for s in range(5, 15)): | ||
| code += Op.MCOPY(size, 0, size) | ||
|
|
||
| # Runtime entry: JUMP to final JUMPDEST, then STOP. | ||
| entry = Op.JUMP(max_code_size - 1) | ||
| entry += Op.JUMPDEST * (32 - len(entry)) # Padding | ||
|
|
||
| code += Op.MSTORE(0, bytes(entry)) | ||
|
|
||
| # Mask ADDRESS into a JUMPDEST template via OR: | ||
| # bytes 0..12 bytes 12..32 | ||
| # ----------- ------------ | ||
| # ADDRESS 00 .. 00 <20-byte address> | ||
| # addr_slot 5b .. 5b 00 .. 00 | ||
| # OR result 5b .. 5b <20-byte address> | ||
| addr_slot = Op.JUMPDEST * 12 + Op.STOP * 20 | ||
| code += Op.MSTORE(0x20, Op.OR(Op.ADDRESS, bytes(addr_slot))) | ||
|
|
||
| code += Op.RETURN(0, max_code_size) | ||
| return super().__new__(cls, code) | ||
|
|
||
| @property | ||
| def runtime_size(self) -> int: | ||
| """Size in bytes of the deployed runtime.""" | ||
| return JOCHEMNET_RUNTIME_SIZE | ||
|
|
||
| @property | ||
| def runtime_code(self) -> Bytecode: | ||
| """Runtime executed when the deployed contract is called.""" | ||
| # Entry jumps to the final JUMPDEST, then halts. | ||
| return Op.JUMP(Op.PUSH2(JOCHEMNET_RUNTIME_SIZE - 1)) + Op.JUMPDEST | ||
|
|
||
|
|
||
| class AddressSource(ABC): | ||
| """ | ||
| Locates and iterates over target addresses. | ||
|
|
||
| Provides a unified interface for layout initialization, | ||
| reading the current target, and advancing to the next one. | ||
| """ | ||
|
|
||
| @property | ||
| @abstractmethod | ||
| def setup(self) -> Bytecode: | ||
| """Bytecode that initializes the in-memory address layout.""" | ||
|
|
||
| @abstractmethod | ||
| def address_op(self) -> Bytecode: | ||
| """Bytecode that reads the current target address.""" | ||
|
|
||
| @abstractmethod | ||
| def next_op(self) -> Bytecode: | ||
| """Bytecode that advances to the next target address.""" | ||
|
|
||
|
|
||
| class Create2AddressSource(AddressSource): | ||
| """Targets derived from a CREATE2 factory deployment.""" | ||
|
|
||
| def __init__(self, *, init_code: bytes, index_op: Bytecode) -> None: | ||
| """Build the CREATE2 preimage layout for *init_code*.""" | ||
| self._layout = Create2PreimageLayout( | ||
| factory_address=DETERMINISTIC_FACTORY_ADDRESS, | ||
| salt=index_op, | ||
| init_code_hash=keccak256(init_code), | ||
| ) | ||
|
|
||
| @property | ||
| def setup(self) -> Bytecode: | ||
| """Bytecode that initializes the in-memory address layout.""" | ||
| return self._layout | ||
|
|
||
| def address_op(self) -> Bytecode: | ||
| """Bytecode that reads the current target address.""" | ||
| return self._layout.address_op() | ||
|
|
||
| def next_op(self) -> Bytecode: | ||
| """Bytecode that advances to the next target address.""" | ||
| return self._layout.increment_salt_op() | ||
|
|
||
|
|
||
| class SequentialAddressSource(AddressSource): | ||
| """Targets at a contiguous address range starting from a base.""" | ||
|
|
||
| def __init__(self, *, base_addr: Hash, index_op: Bytecode) -> None: | ||
| """Build a sequential layout starting at *base_addr*.""" | ||
| self._layout = SequentialAddressLayout( | ||
| starting_address=Op.ADD(base_addr, index_op), | ||
| increment=1, | ||
| ) | ||
|
|
||
| @property | ||
| def setup(self) -> Bytecode: | ||
| """Bytecode that initializes the in-memory address layout.""" | ||
| return self._layout | ||
|
|
||
| def address_op(self) -> Bytecode: | ||
| """Bytecode that reads the current target address.""" | ||
| return self._layout.address_op() | ||
|
|
||
| def next_op(self) -> Bytecode: | ||
| """Bytecode that advances to the next target address.""" | ||
| return self._layout.increment_address_op() | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class AccountCreator: | ||
| """Account creation and location helper with address iteration.""" | ||
|
|
||
| # Maps CREATE2 modes to initcode builders. | ||
| initcode_factories: ClassVar[ | ||
| dict[AccountMode, Callable[[], ContractInitcode]] | ||
| ] = { | ||
| AccountMode.EXISTING_CONTRACT_MINIMAL: MinimalContractInitcode, | ||
| AccountMode.EXISTING_CONTRACT_SAME: partial( | ||
| UniqueMaxContractInitcode, diff=False | ||
| ), | ||
| AccountMode.EXISTING_CONTRACT_DIFF: partial( | ||
| UniqueMaxContractInitcode, diff=True | ||
| ), | ||
| AccountMode.EXISTING_CONTRACT_JUMPDEST: ( | ||
| JochemnetPredeployContractInitcode | ||
| ), | ||
| } | ||
|
|
||
| mode: AccountMode | ||
|
|
||
| def __post_init__(self) -> None: | ||
| """Reject anything that is not a known `AccountMode`.""" | ||
| if not isinstance(self.mode, AccountMode): | ||
| raise ValueError(f"unknown account mode: {self.mode!r}") | ||
|
|
||
| @property | ||
| def derives_address_via_create2(self) -> bool: | ||
| """Whether the target address is derived via CREATE2.""" | ||
| return self.mode in self.initcode_factories | ||
|
|
||
| @property | ||
| def contract_initcode(self) -> ContractInitcode: | ||
| """Return the initcode generator that deploys this account.""" | ||
| if self.mode not in self.initcode_factories: | ||
| raise ValueError(f"{self.mode.name} is not a contract") | ||
| return self.initcode_factories[self.mode]() | ||
|
|
||
| @property | ||
| def initcode(self) -> bytes: | ||
| """Return the CREATE2 initcode that deploys this account.""" | ||
| return bytes(self.contract_initcode) | ||
|
|
||
| @property | ||
| def runtime_size(self) -> int: | ||
| """Return the deployed runtime size in bytes.""" | ||
| return self.contract_initcode.runtime_size | ||
|
|
||
| @property | ||
| def runtime_code(self) -> Bytecode: | ||
| """Return the runtime executed when this account receives a call.""" | ||
| return self.contract_initcode.runtime_code | ||
|
|
||
| def address_source(self, index_op: Bytecode) -> AddressSource: | ||
| """Return the source that yields successive target addresses.""" | ||
| if self.derives_address_via_create2: | ||
| return Create2AddressSource( | ||
| init_code=self.initcode, index_op=index_op | ||
| ) | ||
| match self.mode: | ||
| case AccountMode.EXISTING_EOA: | ||
| # Spamoor EOA creator starts created accounts at 0x1000. | ||
| # https://github.com/CPerezz/spamoor/pull/12 | ||
| base_addr = Hash(0x1000) | ||
| case AccountMode.NON_EXISTING_ACCOUNT: | ||
| # An address range that is never funded. | ||
| base_addr = keccak256(b"random") | ||
| case _: | ||
| raise ValueError(f"{self.mode.name} has no address source") | ||
| return SequentialAddressSource(base_addr=base_addr, index_op=index_op) | ||
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.
Based on my current understanding, we want to do the repricing based on the old max code size value (0x6000), could you please confirm? cc @jochem-brouwer
This is applied to all the following contract variants. Note that if i use
fork.max_code_size()instead, this would be affected by eip-7954.