|
39 | 39 | from tests.prague.eip2537_bls_12_381_precompiles import spec as bls12381_spec
|
40 | 40 | from tests.prague.eip2537_bls_12_381_precompiles.spec import BytesConcatenation
|
41 | 41 |
|
| 42 | +from .helpers import code_loop_precompile_call |
| 43 | + |
42 | 44 | REFERENCE_SPEC_GIT_PATH = "TODO"
|
43 | 45 | REFERENCE_SPEC_VERSION = "TODO"
|
44 | 46 |
|
@@ -311,18 +313,20 @@ def test_worst_msize(
|
311 | 313 | The `mem_size` parameter indicates by how much the memory is expanded.
|
312 | 314 | """
|
313 | 315 | env = Environment()
|
314 |
| - max_code_size = fork.max_code_size() |
| 316 | + max_stack_height = fork.max_stack_height() |
315 | 317 |
|
316 |
| - # We use CALLVALUE for the parameter since is 1 gas cheaper than PUSHX. |
317 |
| - code_prefix = Op.MLOAD(Op.CALLVALUE) + Op.JUMPDEST |
318 |
| - iter_loop = Op.POP(Op.MSIZE) |
319 |
| - code_suffix = Op.JUMP(len(code_prefix) - 1) |
320 |
| - code_iter_len = (max_code_size - len(code_prefix) - len(code_suffix)) // len(iter_loop) |
321 |
| - code = code_prefix + iter_loop * code_iter_len + code_suffix |
322 |
| - assert len(code) <= max_code_size |
| 318 | + code_sequence = Op.MLOAD(Op.CALLVALUE) + Op.POP + Op.MSIZE * max_stack_height |
| 319 | + target_address = pre.deploy_contract(code=code_sequence) |
| 320 | + |
| 321 | + calldata = Bytecode() |
| 322 | + attack_block = Op.POP(Op.STATICCALL(Op.GAS, target_address, 0, 0, 0, 0)) |
| 323 | + code = code_loop_precompile_call(calldata, attack_block, fork) |
| 324 | + assert len(code) <= fork.max_code_size() |
| 325 | + |
| 326 | + code_address = pre.deploy_contract(code=code) |
323 | 327 |
|
324 | 328 | tx = Transaction(
|
325 |
| - to=pre.deploy_contract(code=bytes(code)), |
| 329 | + to=code_address, |
326 | 330 | gas_limit=env.gas_limit,
|
327 | 331 | sender=pre.fund_eoa(),
|
328 | 332 | value=mem_size,
|
@@ -823,24 +827,6 @@ def test_worst_precompile_fixed_cost(
|
823 | 827 | )
|
824 | 828 |
|
825 | 829 |
|
826 |
| -def code_loop_precompile_call(calldata: Bytecode, attack_block: Bytecode, fork: Fork): |
827 |
| - """Create a code loop that calls a precompile with the given calldata.""" |
828 |
| - max_code_size = fork.max_code_size() |
829 |
| - |
830 |
| - # The attack contract is: CALLDATA_PREP + #JUMPDEST + [attack_block]* + JUMP(#) |
831 |
| - jumpdest = Op.JUMPDEST |
832 |
| - jump_back = Op.JUMP(len(calldata)) |
833 |
| - max_iters_loop = (max_code_size - len(calldata) - len(jumpdest) - len(jump_back)) // len( |
834 |
| - attack_block |
835 |
| - ) |
836 |
| - code = calldata + jumpdest + sum([attack_block] * max_iters_loop) + jump_back |
837 |
| - if len(code) > max_code_size: |
838 |
| - # Must never happen, but keep it as a sanity check. |
839 |
| - raise ValueError(f"Code size {len(code)} exceeds maximum code size {max_code_size}") |
840 |
| - |
841 |
| - return code |
842 |
| - |
843 |
| - |
844 | 830 | @pytest.mark.zkevm
|
845 | 831 | @pytest.mark.valid_from("Cancun")
|
846 | 832 | @pytest.mark.slow
|
|
0 commit comments