|
| 1 | +"""test `CALLDATALOAD` opcode.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from ethereum_test_forks import Byzantium, Fork |
| 6 | +from ethereum_test_tools import Account, Alloc, StateTestFiller, Transaction |
| 7 | +from ethereum_test_tools import Macros as Om |
| 8 | +from ethereum_test_tools.vm.opcode import Opcodes as Op |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize( |
| 12 | + "calldata,calldata_offset,expected_storage", |
| 13 | + [ |
| 14 | + ( |
| 15 | + b"\x25\x60", |
| 16 | + 0x0, |
| 17 | + 0x2560000000000000000000000000000000000000000000000000000000000000, |
| 18 | + ), |
| 19 | + ( |
| 20 | + b"\xff" * 32 + b"\x23", |
| 21 | + 0x1, |
| 22 | + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF23, |
| 23 | + ), |
| 24 | + ( |
| 25 | + bytes.fromhex("123456789ABCDEF00000000000000000000000000000000000000000000000000024"), |
| 26 | + 0x5, |
| 27 | + 0xBCDEF00000000000000000000000000000000000000000000000000024000000, |
| 28 | + ), |
| 29 | + ], |
| 30 | + ids=[ |
| 31 | + "two_bytes", |
| 32 | + "word_n_byte", |
| 33 | + "34_bytes", |
| 34 | + ], |
| 35 | +) |
| 36 | +@pytest.mark.parametrize("calldata_source", ["contract", "tx"]) |
| 37 | +def test_calldataload( |
| 38 | + state_test: StateTestFiller, |
| 39 | + calldata: bytes, |
| 40 | + calldata_offset: int, |
| 41 | + fork: Fork, |
| 42 | + pre: Alloc, |
| 43 | + expected_storage: Account, |
| 44 | + calldata_source: str, |
| 45 | +): |
| 46 | + """ |
| 47 | + Test `CALLDATALOAD` opcode. |
| 48 | +
|
| 49 | + Tests two scenarios: |
| 50 | + - calldata_source is "contract": CALLDATALOAD reads from calldata passed by another contract |
| 51 | + - calldata_source is "tx": CALLDATALOAD reads directly from transaction calldata |
| 52 | +
|
| 53 | + Based on https://github.com/ethereum/tests/blob/ae4791077e8fcf716136e70fe8392f1a1f1495fb/src/GeneralStateTestsFiller/VMTests/vmTests/calldatacopyFiller.yml |
| 54 | + """ |
| 55 | + contract_address = pre.deploy_contract( |
| 56 | + Op.SSTORE(0, Op.CALLDATALOAD(offset=calldata_offset)) + Op.STOP, |
| 57 | + ) |
| 58 | + |
| 59 | + if calldata_source == "contract": |
| 60 | + to = pre.deploy_contract( |
| 61 | + Om.MSTORE(calldata, 0x0) |
| 62 | + + Op.CALL( |
| 63 | + gas=Op.SUB(Op.GAS(), 0x100), |
| 64 | + address=contract_address, |
| 65 | + value=0x0, |
| 66 | + args_offset=0x0, |
| 67 | + args_size=len(calldata), |
| 68 | + ret_offset=0x0, |
| 69 | + ret_size=0x0, |
| 70 | + ) |
| 71 | + + Op.STOP |
| 72 | + ) |
| 73 | + |
| 74 | + tx = Transaction( |
| 75 | + data=calldata, |
| 76 | + gas_limit=100_000, |
| 77 | + protected=fork >= Byzantium, |
| 78 | + sender=pre.fund_eoa(), |
| 79 | + to=to, |
| 80 | + ) |
| 81 | + |
| 82 | + else: |
| 83 | + tx = Transaction( |
| 84 | + data=calldata, |
| 85 | + gas_limit=100_000, |
| 86 | + protected=fork >= Byzantium, |
| 87 | + sender=pre.fund_eoa(), |
| 88 | + to=contract_address, |
| 89 | + ) |
| 90 | + |
| 91 | + post = {contract_address: Account(storage={0x00: expected_storage})} |
| 92 | + state_test(pre=pre, post=post, tx=tx) |
0 commit comments