|
18 | 18 | Bytecode,
|
19 | 19 | Environment,
|
20 | 20 | Transaction,
|
| 21 | + While, |
21 | 22 | )
|
22 | 23 | from ethereum_test_tools.vm.opcode import Opcodes as Op
|
23 | 24 |
|
@@ -282,3 +283,79 @@ def code_loop_precompile_call(calldata: Bytecode, attack_block: Bytecode):
|
282 | 283 | raise ValueError(f"Code size {len(code)} exceeds maximum code size {MAX_CODE_SIZE}")
|
283 | 284 |
|
284 | 285 | 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