-
Notifications
You must be signed in to change notification settings - Fork 415
feat(benchmark): support --fixed-opcode-count flag and tests
#1747
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
LouisTsai-Csie
merged 8 commits into
ethereum:forks/osaka
from
LouisTsai-Csie:feat/fixed-op-count
Nov 13, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
34aa6f1
feat: implement fixed opcode count benchmark
LouisTsai-Csie 4adf837
feat: implement reference test marker for repricing
LouisTsai-Csie 9345f2c
feat: support execute mode for repricing test
LouisTsai-Csie 5e49846
feat: add gas repricing test marker
LouisTsai-Csie a52daa2
refactor: benchmark marker conflict logic
LouisTsai-Csie 94f3c7e
chore: rename reference test marker to repricing
LouisTsai-Csie acbb889
feat: filter reference test with specified parameter
LouisTsai-Csie fa69c08
chore: fix linting
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
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 |
|---|---|---|
|
|
@@ -53,6 +53,7 @@ class BenchmarkCodeGenerator(ABC): | |
| setup: Bytecode = field(default_factory=Bytecode) | ||
| cleanup: Bytecode = field(default_factory=Bytecode) | ||
| tx_kwargs: Dict[str, Any] = field(default_factory=dict) | ||
| fixed_opcode_count: int | None = None | ||
| code_padding_opcode: Op | None = None | ||
| _contract_address: Address | None = None | ||
|
|
||
|
|
@@ -61,6 +62,50 @@ def deploy_contracts(self, *, pre: Alloc, fork: Fork) -> Address: | |
| """Deploy any contracts needed for the benchmark.""" | ||
| ... | ||
|
|
||
| def deploy_fix_count_contracts(self, *, pre: Alloc, fork: Fork) -> Address: | ||
| """Deploy the contract with a fixed opcode count.""" | ||
| code = self.generate_repeated_code( | ||
| repeated_code=self.attack_block, | ||
| setup=self.setup, | ||
| cleanup=self.cleanup, | ||
| fork=fork, | ||
| ) | ||
| self._target_contract_address = pre.deploy_contract(code=code) | ||
|
|
||
| iterations = self.fixed_opcode_count | ||
| assert iterations is not None, "fixed_opcode_count is not set" | ||
|
|
||
| prefix = Op.CALLDATACOPY( | ||
| Op.PUSH0, Op.PUSH0, Op.CALLDATASIZE | ||
| ) + Op.PUSH4(iterations) | ||
| opcode = ( | ||
| prefix | ||
| + Op.JUMPDEST | ||
| + Op.POP( | ||
| Op.STATICCALL( | ||
| gas=Op.GAS, | ||
| address=self._target_contract_address, | ||
| args_offset=0, | ||
| args_size=Op.CALLDATASIZE, | ||
| ret_offset=0, | ||
| ret_size=0, | ||
| ) | ||
| ) | ||
| + Op.PUSH1(1) | ||
| + Op.SWAP1 | ||
| + Op.SUB | ||
| + Op.DUP1 | ||
| + Op.ISZERO | ||
| + Op.ISZERO | ||
| + Op.PUSH1(len(prefix)) | ||
| + Op.JUMPI | ||
| + Op.STOP | ||
| ) | ||
| self._validate_code_size(opcode, fork) | ||
|
|
||
| self._contract_address = pre.deploy_contract(code=opcode) | ||
| return self._contract_address | ||
|
|
||
| def generate_transaction( | ||
| self, *, pre: Alloc, gas_benchmark_value: int | ||
| ) -> Transaction: | ||
|
|
@@ -102,9 +147,18 @@ def generate_repeated_code( | |
| available_space = max_code_size - overhead | ||
| max_iterations = available_space // len(repeated_code) | ||
|
|
||
| # Use fixed_opcode_count if provided, otherwise fill to max | ||
| if self.fixed_opcode_count is not None: | ||
| max_iterations = min(max_iterations, 1000) | ||
|
|
||
| print(f"max_iterations: {max_iterations}") | ||
|
Member
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. This print could be a bit annoying, we should remove IMO. |
||
|
|
||
| # TODO: Unify the PUSH0 and PUSH1 usage. | ||
| code = setup + Op.JUMPDEST + repeated_code * max_iterations + cleanup | ||
| code += Op.JUMP(len(setup)) if len(setup) > 0 else Op.PUSH0 + Op.JUMP | ||
| code = setup + Op.JUMPDEST + repeated_code * max_iterations | ||
| if self.fixed_opcode_count is None: | ||
| code += cleanup + ( | ||
| Op.JUMP(len(setup)) if len(setup) > 0 else Op.PUSH0 + Op.JUMP | ||
| ) | ||
| # Pad the code to the maximum code size. | ||
| if self.code_padding_opcode is not None: | ||
| code += self.code_padding_opcode * (max_code_size - len(code)) | ||
|
|
@@ -142,6 +196,7 @@ class BenchmarkTest(BaseTest): | |
| gas_benchmark_value: int = Field( | ||
| default_factory=lambda: int(Environment().gas_limit) | ||
| ) | ||
| fixed_opcode_count: int | None = None | ||
| code_generator: BenchmarkCodeGenerator | None = None | ||
|
|
||
| supported_fixture_formats: ClassVar[ | ||
|
|
@@ -163,6 +218,7 @@ class BenchmarkTest(BaseTest): | |
| supported_markers: ClassVar[Dict[str, str]] = { | ||
| "blockchain_test_engine_only": "Only generate a blockchain test engine fixture", | ||
| "blockchain_test_only": "Only generate a blockchain test fixture", | ||
| "repricing": "Mark test as reference test for gas repricing analysis", | ||
| } | ||
|
|
||
| def model_post_init(self, __context: Any, /) -> None: | ||
|
|
@@ -193,7 +249,18 @@ def model_post_init(self, __context: Any, /) -> None: | |
| blocks: List[Block] = self.setup_blocks | ||
|
|
||
| if self.code_generator is not None: | ||
| generated_blocks = self.generate_blocks_from_code_generator() | ||
| # Inject fixed_opcode_count into the code generator if provided | ||
| self.code_generator.fixed_opcode_count = self.fixed_opcode_count | ||
|
|
||
| # In fixed opcode count mode, skip gas validation since we're | ||
| # measuring performance by operation count, not gas usage | ||
| if self.fixed_opcode_count is not None: | ||
| self.skip_gas_used_validation = True | ||
| generated_blocks = ( | ||
| self.generate_fixed_opcode_count_transactions() | ||
| ) | ||
| else: | ||
| generated_blocks = self.generate_blocks_from_code_generator() | ||
| blocks += generated_blocks | ||
|
|
||
| elif self.blocks is not None: | ||
|
|
@@ -294,6 +361,22 @@ def generate_blocks_from_code_generator(self) -> List[Block]: | |
|
|
||
| return [execution_block] | ||
|
|
||
| def generate_fixed_opcode_count_transactions(self) -> List[Block]: | ||
| """Generate transactions with a fixed opcode count.""" | ||
| if self.code_generator is None: | ||
| raise Exception("Code generator is not set") | ||
| self.code_generator.deploy_fix_count_contracts( | ||
| pre=self.pre, fork=self.fork | ||
| ) | ||
| gas_limit = ( | ||
| self.fork.transaction_gas_limit_cap() or self.gas_benchmark_value | ||
| ) | ||
spencer-tb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| benchmark_tx = self.code_generator.generate_transaction( | ||
| pre=self.pre, gas_benchmark_value=gas_limit | ||
| ) | ||
| execution_block = Block(txs=[benchmark_tx]) | ||
| return [execution_block] | ||
|
|
||
| def generate_blockchain_test(self) -> BlockchainTest: | ||
| """Create a BlockchainTest from this BenchmarkTest.""" | ||
| return BlockchainTest.from_test( | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.