|
| 1 | +"""Tests supported precompiled contracts.""" |
| 2 | + |
| 3 | +from typing import Iterator, Tuple |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from ethereum_test_forks import Fork |
| 8 | +from ethereum_test_tools import ( |
| 9 | + Account, |
| 10 | + Alloc, |
| 11 | + Environment, |
| 12 | + StateTestFiller, |
| 13 | + Transaction, |
| 14 | +) |
| 15 | +from ethereum_test_tools.code.generators import Conditional |
| 16 | +from ethereum_test_tools.vm.opcode import Opcodes as Op |
| 17 | + |
| 18 | + |
| 19 | +def precompile_addresses(fork: Fork) -> Iterator[Tuple[str, bool]]: |
| 20 | + """ |
| 21 | + Yield the addresses of precompiled contracts and their support status for a given fork. |
| 22 | +
|
| 23 | + Args: |
| 24 | + fork (Fork): The fork instance containing precompiled contract information. |
| 25 | +
|
| 26 | + Yields: |
| 27 | + Iterator[Tuple[str, bool]]: A tuple containing the address in hexadecimal format and a |
| 28 | + boolean indicating whether the address is a supported precompile. |
| 29 | +
|
| 30 | + """ |
| 31 | + supported_precompiles = fork.precompiles() |
| 32 | + |
| 33 | + for address in range(1, len(supported_precompiles) + 2): |
| 34 | + yield (hex(address), address in supported_precompiles) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.valid_from("Berlin") |
| 38 | +@pytest.mark.parametrize_by_fork("address,precompile_exists", precompile_addresses) |
| 39 | +def test_precompiles( |
| 40 | + state_test: StateTestFiller, address: str, precompile_exists: bool, pre: Alloc |
| 41 | +): |
| 42 | + """ |
| 43 | + Tests the behavior of precompiled contracts in the Ethereum state test. |
| 44 | +
|
| 45 | + Args: |
| 46 | + state_test (StateTestFiller): The state test filler object used to run the test. |
| 47 | + address (str): The address of the precompiled contract to test. |
| 48 | + precompile_exists (bool): A flag indicating whether the precompiled contract exists at the |
| 49 | + given address. |
| 50 | + pre (Alloc): The allocation object used to deploy the contract and set up the initial state. |
| 51 | +
|
| 52 | + This test deploys a contract that performs two CALL operations to the specified address and a |
| 53 | + fixed address (0x10000), measuring the gas used for each call. It then stores the difference |
| 54 | + in gas usage in storage slot 0. The test verifies the expected storage value based on |
| 55 | + whether the precompiled contract exists at the given address. |
| 56 | +
|
| 57 | + """ |
| 58 | + env = Environment() |
| 59 | + |
| 60 | + args_offset = 0x1000 |
| 61 | + args_size = 0x20 |
| 62 | + output_offset = 0x2000 |
| 63 | + output_size = 0x20 |
| 64 | + |
| 65 | + gas_test = 0x00 |
| 66 | + gas_10000 = 0x20 |
| 67 | + |
| 68 | + account = pre.deploy_contract( |
| 69 | + Op.MSTORE(gas_test, Op.GAS) |
| 70 | + + Op.CALL( |
| 71 | + address=address, |
| 72 | + args_offset=args_offset, |
| 73 | + args_size=args_size, |
| 74 | + output_offset=output_offset, |
| 75 | + output_size=output_size, |
| 76 | + ) |
| 77 | + + Op.MSTORE(gas_test, Op.SUB(Op.GAS, Op.MLOAD(gas_test))) |
| 78 | + + Op.MSTORE(gas_10000, Op.GAS) |
| 79 | + + Op.CALL( |
| 80 | + address=0x10000, |
| 81 | + args_offset=args_offset, |
| 82 | + args_size=args_size, |
| 83 | + output_offset=output_offset, |
| 84 | + output_size=output_size, |
| 85 | + ) |
| 86 | + + Op.MSTORE(gas_10000, Op.SUB(Op.GAS, Op.MLOAD(gas_10000))) |
| 87 | + + Op.SSTORE( |
| 88 | + 0, |
| 89 | + Op.LT( |
| 90 | + Conditional( |
| 91 | + condition=Op.GT(Op.MLOAD(gas_test), Op.MLOAD(gas_10000)), |
| 92 | + if_true=Op.SUB(Op.MLOAD(gas_test), Op.MLOAD(gas_10000)), |
| 93 | + if_false=Op.SUB(Op.MLOAD(gas_10000), Op.MLOAD(gas_test)), |
| 94 | + ), |
| 95 | + 0x1A4, |
| 96 | + ), |
| 97 | + ) |
| 98 | + + Op.STOP, |
| 99 | + storage={0: 0xDEADBEEF}, |
| 100 | + ) |
| 101 | + |
| 102 | + tx = Transaction( |
| 103 | + to=account, |
| 104 | + sender=pre.fund_eoa(), |
| 105 | + gas_limit=1_000_000, |
| 106 | + protected=True, |
| 107 | + ) |
| 108 | + |
| 109 | + # A high gas cost will result from calling a precompile |
| 110 | + # Expect 0x00 when a precompile exists at the address, 0x01 otherwise |
| 111 | + post = {account: Account(storage={0: "0x00" if precompile_exists else "0x01"})} |
| 112 | + |
| 113 | + state_test(env=env, pre=pre, post=post, tx=tx) |
0 commit comments