Skip to content

Commit 15700a2

Browse files
committed
Test jump and jumpdest
1 parent 25f0012 commit 15700a2

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

tests/zkevm/test_worst_compute.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
Bytecode,
1919
Environment,
2020
Transaction,
21+
While,
2122
)
2223
from ethereum_test_tools.vm.opcode import Opcodes as Op
2324

@@ -282,3 +283,79 @@ def code_loop_precompile_call(calldata: Bytecode, attack_block: Bytecode):
282283
raise ValueError(f"Code size {len(code)} exceeds maximum code size {MAX_CODE_SIZE}")
283284

284285
return code
286+
287+
288+
@pytest.mark.zkevm
289+
@pytest.mark.valid_from("Cancun")
290+
@pytest.mark.slow
291+
def test_worst_jumps(
292+
blockchain_test: BlockchainTestFiller,
293+
pre: Alloc,
294+
fork: Fork,
295+
):
296+
"""Test running a JUMP-intensive contract."""
297+
env = Environment()
298+
299+
def jump_seq():
300+
return Op.JUMP(Op.ADD(Op.PC, 1)) + Op.JUMPDEST
301+
302+
bytes_per_seq = len(jump_seq())
303+
seqs_per_call = MAX_CODE_SIZE // bytes_per_seq
304+
305+
# Create and deploy the jump-intensive contract
306+
jumps_code = sum([jump_seq() for _ in range(seqs_per_call)])
307+
jumps_address = pre.deploy_contract(code=jumps_code)
308+
309+
# Call the contract repeatedly until gas runs out.
310+
caller_code = While(body=Op.POP(Op.CALL(address=jumps_address)))
311+
caller_address = pre.deploy_contract(caller_code)
312+
313+
txs = [
314+
Transaction(
315+
to=caller_address,
316+
gas_limit=env.gas_limit,
317+
sender=pre.fund_eoa(),
318+
)
319+
]
320+
321+
blockchain_test(
322+
genesis_environment=env,
323+
pre=pre,
324+
post={},
325+
blocks=[Block(txs=txs)],
326+
)
327+
328+
329+
@pytest.mark.zkevm
330+
@pytest.mark.valid_from("Cancun")
331+
@pytest.mark.slow
332+
def test_worst_jumpdests(
333+
blockchain_test: BlockchainTestFiller,
334+
pre: Alloc,
335+
fork: Fork,
336+
):
337+
"""Test running a JUMPDEST-intensive contract."""
338+
env = Environment()
339+
340+
# Create and deploy a contract with many JUMPDESTs
341+
jumpdests_code = sum([Op.JUMPDEST] * MAX_CODE_SIZE)
342+
jumpdests_address = pre.deploy_contract(code=jumpdests_code)
343+
344+
# Call the contract repeatedly until gas runs out.
345+
caller_code = While(body=Op.POP(Op.CALL(address=jumpdests_address)))
346+
caller_address = pre.deploy_contract(caller_code)
347+
348+
txs = [
349+
Transaction(
350+
to=caller_address,
351+
gas_limit=env.gas_limit,
352+
sender=pre.fund_eoa(),
353+
)
354+
]
355+
356+
blockchain_test(
357+
genesis_environment=env,
358+
pre=pre,
359+
post={},
360+
blocks=[Block(txs=txs)],
361+
)

0 commit comments

Comments
 (0)