44"""
55
66import math
7+ from typing import Dict
78
89import pytest
910from execution_testing import (
2425 While ,
2526 compute_create2_address ,
2627)
28+ from execution_testing .base_types .conversions import NumberConvertible
2729
2830from tests .benchmark .compute .helpers import (
2931 XOR_TABLE ,
@@ -72,7 +74,6 @@ def test_codesize(
7274)
7375def test_codecopy (
7476 benchmark_test : BenchmarkTestFiller ,
75- pre : Alloc ,
7677 fork : Fork ,
7778 max_code_size_ratio : float ,
7879 fixed_src_dst : bool ,
@@ -86,26 +87,12 @@ def test_codecopy(
8687 src_dst = 0 if fixed_src_dst else Op .MOD (Op .GAS , 7 )
8788 attack_block = Op .CODECOPY (src_dst , src_dst , Op .DUP1 ) # DUP1 copies size.
8889
89- code = JumpLoopGenerator (
90- setup = setup , attack_block = attack_block
91- ).generate_repeated_code (
92- repeated_code = attack_block , setup = setup , fork = fork
93- )
94-
95- # Pad the generated code to ensure the contract size matches the maximum
96- # The content of the padding bytes is arbitrary.
97- code += Op .INVALID * (max_code_size - len (code ))
98- assert len (code ) == max_code_size , (
99- f"Code size { len (code )} is not equal to max code size { max_code_size } ."
100- )
101-
102- tx = Transaction (
103- to = pre .deploy_contract (code = code ),
104- sender = pre .fund_eoa (),
90+ benchmark_test (
91+ code_generator = JumpLoopGenerator (
92+ setup = setup , attack_block = attack_block
93+ )
10594 )
10695
107- benchmark_test (tx = tx )
108-
10996
11097@pytest .mark .parametrize (
11198 "opcode" ,
@@ -361,7 +348,21 @@ def test_extcodecopy_warm(
361348 ],
362349)
363350@pytest .mark .parametrize (
364- "absent_target" ,
351+ "empty_account" ,
352+ [
353+ True ,
354+ False ,
355+ ],
356+ )
357+ @pytest .mark .parametrize (
358+ "initial_balance" ,
359+ [
360+ True ,
361+ False ,
362+ ],
363+ )
364+ @pytest .mark .parametrize (
365+ "initial_storage" ,
365366 [
366367 True ,
367368 False ,
@@ -371,27 +372,40 @@ def test_ext_account_query_warm(
371372 benchmark_test : BenchmarkTestFiller ,
372373 pre : Alloc ,
373374 opcode : Op ,
374- absent_target : bool ,
375+ empty_account : bool ,
376+ initial_balance : bool ,
377+ initial_storage : bool ,
375378) -> None :
376379 """
377380 Test running a block with as many stateful opcodes doing warm access
378381 for an account.
379382 """
380383 # Setup
381- target_addr = pre .empty_account ()
384+ target_addr = pre .empty_account () if initial_balance else pre . fund_eoa ()
382385 post = {}
383- if not absent_target :
386+ if not empty_account :
384387 code = Op .STOP + Op .JUMPDEST * 100
385- target_addr = pre .deploy_contract (balance = 100 , code = code )
386- post [target_addr ] = Account (balance = 100 , code = code )
387388
388- # Execution
389- setup = Op .MSTORE (0 , target_addr )
390- attack_block = Op .POP (opcode (address = Op .MLOAD (0 )))
389+ storage : Dict [NumberConvertible , NumberConvertible ] = (
390+ {0 : 0x1337 } if initial_storage else {0 : 0 }
391+ )
392+ target_addr = pre .deploy_contract (
393+ balance = initial_balance ,
394+ code = code ,
395+ storage = storage ,
396+ )
397+
398+ post [target_addr ] = Account (
399+ balance = initial_balance ,
400+ code = code ,
401+ storage = storage ,
402+ )
403+
391404 benchmark_test (
392405 post = post ,
393406 code_generator = JumpLoopGenerator (
394- setup = setup , attack_block = attack_block
407+ setup = Op .MSTORE (0 , target_addr ),
408+ attack_block = Op .POP (opcode (address = Op .MLOAD (0 ))),
395409 ),
396410 )
397411
0 commit comments