|
| 1 | +"""abstract: Test Calling Precompile Range (close to zero).""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from ethereum_test_forks import Fork |
| 6 | +from ethereum_test_tools import ( |
| 7 | + Account, |
| 8 | + Address, |
| 9 | + Alloc, |
| 10 | + Bytecode, |
| 11 | + StateTestFiller, |
| 12 | + Storage, |
| 13 | + Transaction, |
| 14 | +) |
| 15 | +from ethereum_test_tools import Opcodes as Op |
| 16 | + |
| 17 | +UPPER_BOUND = 0x101 |
| 18 | +RETURNDATASIZE_OFFSET = 0x10000000000000000 # Must be greater than UPPER_BOUND |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.parametrize( |
| 22 | + "calldata_size", |
| 23 | + [ |
| 24 | + pytest.param(0, id="empty_calldata"), |
| 25 | + pytest.param(31, id="31_bytes"), |
| 26 | + pytest.param(32, id="32_bytes"), |
| 27 | + ], |
| 28 | +) |
| 29 | +@pytest.mark.valid_from("Byzantium") |
| 30 | +def test_precompile_absence( |
| 31 | + state_test: StateTestFiller, |
| 32 | + pre: Alloc, |
| 33 | + fork: Fork, |
| 34 | + calldata_size: int, |
| 35 | +): |
| 36 | + """Test that addresses close to zero are not precompiles unless active in the fork.""" |
| 37 | + active_precompiles = fork.precompiles() |
| 38 | + storage = Storage() |
| 39 | + call_code = Bytecode() |
| 40 | + for address in range(1, UPPER_BOUND + 1): |
| 41 | + if Address(address) in active_precompiles: |
| 42 | + continue |
| 43 | + call_code += Op.SSTORE( |
| 44 | + address, |
| 45 | + Op.CALL(gas=0, address=address, args_size=calldata_size), |
| 46 | + ) |
| 47 | + storage[address] = 1 |
| 48 | + if Op.RETURNDATASIZE in fork.valid_opcodes(): |
| 49 | + call_code += Op.SSTORE( |
| 50 | + address + RETURNDATASIZE_OFFSET, |
| 51 | + Op.RETURNDATASIZE, |
| 52 | + ) |
| 53 | + storage[address + RETURNDATASIZE_OFFSET] = 0 |
| 54 | + |
| 55 | + call_code += Op.STOP |
| 56 | + |
| 57 | + entry_point_address = pre.deploy_contract(call_code, storage=storage.canary()) |
| 58 | + |
| 59 | + tx = Transaction( |
| 60 | + to=entry_point_address, |
| 61 | + gas_limit=10_000_000, |
| 62 | + sender=pre.fund_eoa(), |
| 63 | + protected=True, |
| 64 | + ) |
| 65 | + |
| 66 | + state_test( |
| 67 | + pre=pre, |
| 68 | + tx=tx, |
| 69 | + post={ |
| 70 | + entry_point_address: Account( |
| 71 | + storage=storage, |
| 72 | + ) |
| 73 | + }, |
| 74 | + ) |
0 commit comments