|
| 1 | +""" |
| 2 | +The test calls CREATE in a loop deploying 1-byte contracts with all possible byte values, |
| 3 | +records in storage the values that failed to deploy. |
| 4 | +""" |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from ethereum_test_forks import Byzantium, Fork, London |
| 9 | +from ethereum_test_tools import ( |
| 10 | + Account, |
| 11 | + Address, |
| 12 | + Alloc, |
| 13 | + Bytecode, |
| 14 | + Environment, |
| 15 | + StateTestFiller, |
| 16 | + Storage, |
| 17 | + Transaction, |
| 18 | +) |
| 19 | +from ethereum_test_tools import Opcodes as Op |
| 20 | +from ethereum_test_types import compute_create_address |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.valid_from("Frontier") |
| 24 | +@pytest.mark.with_all_create_opcodes |
| 25 | +def test_create_one_byte( |
| 26 | + state_test: StateTestFiller, |
| 27 | + fork: Fork, |
| 28 | + pre: Alloc, |
| 29 | + create_opcode: Op, |
| 30 | +): |
| 31 | + """Run create deploys with single bytes for each byte.""" |
| 32 | + initcode: dict[int, Bytecode] = {} |
| 33 | + for byte in range(256): |
| 34 | + initcode[byte] = Op.MSTORE8(0, byte) + Op.RETURN(0, 1) |
| 35 | + initcode_length = 10 |
| 36 | + |
| 37 | + sender = pre.fund_eoa() |
| 38 | + expect_post = Storage() |
| 39 | + |
| 40 | + # make a subcontract that deploys code, because deploy 0xef eats ALL gas |
| 41 | + create_contract = pre.deploy_contract( |
| 42 | + code=Op.MSTORE(0, Op.CALLDATALOAD(0)) |
| 43 | + + Op.MSTORE(32, create_opcode(offset=32 - initcode_length, salt=0, size=initcode_length)) |
| 44 | + + Op.RETURN(32, 32) |
| 45 | + ) |
| 46 | + code = pre.deploy_contract( |
| 47 | + nonce=1, |
| 48 | + code=Op.MSTORE(0, Op.PUSH32(bytes(initcode[0]))) |
| 49 | + + sum( |
| 50 | + [ |
| 51 | + Op.MSTORE8(23, opcode) # correct the deploy byte |
| 52 | + + Op.CALL( |
| 53 | + gas=50_000, |
| 54 | + address=create_contract, |
| 55 | + args_size=32, |
| 56 | + ret_offset=32, |
| 57 | + ret_size=32, |
| 58 | + ) |
| 59 | + + Op.POP # remove call result from stack for vm trace files |
| 60 | + + Op.SSTORE( |
| 61 | + opcode, |
| 62 | + Op.MLOAD(32), |
| 63 | + ) |
| 64 | + for opcode, _ in initcode.items() |
| 65 | + ], |
| 66 | + ) |
| 67 | + + Op.SSTORE(256, 1), |
| 68 | + ) |
| 69 | + |
| 70 | + created_accounts: dict[int, Address] = {} |
| 71 | + for opcode, opcode_init in initcode.items(): |
| 72 | + ef_exception = opcode == 239 and fork >= London |
| 73 | + created_accounts[opcode] = compute_create_address( |
| 74 | + address=create_contract, |
| 75 | + salt=0, |
| 76 | + nonce=opcode + 1, |
| 77 | + initcode=opcode_init, |
| 78 | + opcode=create_opcode, |
| 79 | + ) |
| 80 | + if not ef_exception: |
| 81 | + expect_post[opcode] = created_accounts[opcode] |
| 82 | + expect_post[256] = 1 |
| 83 | + |
| 84 | + tx = Transaction( |
| 85 | + gas_limit=14_000_000, |
| 86 | + to=code, |
| 87 | + data=b"", |
| 88 | + nonce=0, |
| 89 | + sender=sender, |
| 90 | + protected=fork >= Byzantium, |
| 91 | + ) |
| 92 | + |
| 93 | + post = { |
| 94 | + code: Account(storage=expect_post), |
| 95 | + } |
| 96 | + for opcode, _ in initcode.items(): |
| 97 | + ef_exception = opcode == 239 and fork >= London |
| 98 | + if not ef_exception: |
| 99 | + post[created_accounts[opcode]] = Account(code=bytes.fromhex(f"{opcode:02x}")) |
| 100 | + |
| 101 | + state_test(env=Environment(), pre=pre, post=post, tx=tx) |
0 commit comments