Skip to content

Commit eb1fc44

Browse files
refactor(benchmark): update jump initcode analysis
1 parent d6ec9aa commit eb1fc44

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

tests/benchmark/test_worst_bytecode.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99

1010
import pytest
1111

12+
from ethereum_test_benchmark.benchmark_code_generator import JumpLoopGenerator
1213
from ethereum_test_forks import Fork
1314
from ethereum_test_tools import (
1415
Account,
1516
Alloc,
17+
BenchmarkTestFiller,
1618
Block,
1719
BlockchainTestFiller,
1820
Bytecode,
21+
Bytes,
1922
Environment,
2023
Hash,
2124
StateTestFiller,
@@ -245,7 +248,7 @@ def test_worst_bytecode_single_opcode(
245248
ids=lambda x: x.hex(),
246249
)
247250
def test_worst_initcode_jumpdest_analysis(
248-
state_test: StateTestFiller,
251+
benchmark_test: BenchmarkTestFiller,
249252
pre: Alloc,
250253
fork: Fork,
251254
pattern: Bytecode,
@@ -260,7 +263,6 @@ def test_worst_initcode_jumpdest_analysis(
260263
The initicode is modified by mixing-in the returned create address between CREATE invocations
261264
to prevent caching.
262265
"""
263-
max_code_size = fork.max_code_size()
264266
initcode_size = fork.max_initcode_size()
265267

266268
# Expand the initcode pattern to the transaction data so it can be used in CALLDATACOPY
@@ -290,32 +292,20 @@ def test_worst_initcode_jumpdest_analysis(
290292
# Make sure the last opcode in the initcode is JUMPDEST.
291293
code_prepare_initcode += Op.MSTORE(initcode_size - 32, Op.PUSH32[bytes(Op.JUMPDEST) * 32])
292294

293-
code_invoke_create = (
295+
attack_block = (
294296
Op.PUSH1[len(initcode_prefix)]
295297
+ Op.MSTORE
296298
+ Op.CREATE(value=Op.PUSH0, offset=Op.PUSH0, size=Op.MSIZE)
297299
)
298300

299-
initial_random = Op.PUSH0
300-
code_prefix = code_prepare_initcode + initial_random
301-
code_loop_header = Op.JUMPDEST
302-
code_loop_footer = Op.JUMP(len(code_prefix))
303-
code_loop_body_len = (
304-
max_code_size - len(code_prefix) - len(code_loop_header) - len(code_loop_footer)
305-
)
306-
307-
code_loop_body = (code_loop_body_len // len(code_invoke_create)) * bytes(code_invoke_create)
308-
code = code_prefix + code_loop_header + code_loop_body + code_loop_footer
309-
assert (max_code_size - len(code_invoke_create)) < len(code) <= max_code_size
301+
setup = code_prepare_initcode + Op.PUSH0
310302

311-
tx = Transaction(
312-
to=pre.deploy_contract(code=code),
313-
data=tx_data,
314-
gas_limit=gas_benchmark_value,
315-
sender=pre.fund_eoa(),
316-
)
303+
generator = JumpLoopGenerator(setup=setup, attack_block=attack_block, cleanup=Bytecode())
304+
generator.deploy_contracts(pre, fork)
305+
tx = generator.generate_transaction(pre, gas_benchmark_value, fork)
306+
tx.data = Bytes(tx_data)
317307

318-
state_test(
308+
benchmark_test(
319309
pre=pre,
320310
post={},
321311
tx=tx,

0 commit comments

Comments
 (0)