Skip to content

test_calldataload()

Documentation for tests/frontier/opcodes/test_calldataload.py::test_calldataload@b47f0253.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/frontier/opcodes/test_calldataload.py::test_calldataload --fork Amsterdam

Test CALLDATALOAD opcode.

Tests two scenarios: - calldata_source is "contract": CALLDATALOAD reads from calldata passed by another contract - calldata_source is "tx": CALLDATALOAD reads directly from transaction calldata

Based on https://github.com/ethereum/tests/blob/ ae47910/src/ GeneralStateTestsFiller/VMTests/vmTests/calldatacopyFiller.yml

Source code in tests/frontier/opcodes/test_calldataload.py
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
@pytest.mark.ported_from(
    [
        "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/VMTests/vmTests/calldataloadFiller.yml",
    ],
    pr=["https://github.com/ethereum/execution-spec-tests/pull/1236"],
)
@pytest.mark.parametrize(
    "calldata,calldata_offset,expected_storage",
    [
        (
            b"\x25\x60",
            0x0,
            0x2560000000000000000000000000000000000000000000000000000000000000,
        ),
        (
            b"\xff" * 32 + b"\x23",
            0x1,
            0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF23,
        ),
        (
            bytes.fromhex(
                "123456789ABCDEF00000000000000000000000000000000000000000000000000024"
            ),
            0x5,
            0xBCDEF00000000000000000000000000000000000000000000000000024000000,
        ),
    ],
    ids=[
        "two_bytes",
        "word_n_byte",
        "34_bytes",
    ],
)
@pytest.mark.parametrize("calldata_source", ["contract", "tx"])
def test_calldataload(
    state_test: StateTestFiller,
    calldata: bytes,
    calldata_offset: int,
    fork: Fork,
    pre: Alloc,
    expected_storage: Account,
    calldata_source: str,
) -> None:
    """
    Test `CALLDATALOAD` opcode.

    Tests two scenarios:
    - calldata_source is "contract": CALLDATALOAD reads from calldata
                                     passed by another contract
    - calldata_source is "tx": CALLDATALOAD reads directly from
                               transaction calldata

    Based on
    https://github.com/ethereum/tests/blob/
    ae4791077e8fcf716136e70fe8392f1a1f1495fb/src/
    GeneralStateTestsFiller/VMTests/vmTests/calldatacopyFiller.yml
    """
    contract_address = pre.deploy_contract(
        Op.SSTORE(0, Op.CALLDATALOAD(offset=calldata_offset)) + Op.STOP,
    )

    if calldata_source == "contract":
        to = pre.deploy_contract(
            Om.MSTORE(calldata, 0x0)
            + Op.CALL(
                gas=Op.SUB(Op.GAS(), 0x100),
                address=contract_address,
                value=0x0,
                args_offset=0x0,
                args_size=len(calldata),
                ret_offset=0x0,
                ret_size=0x0,
            )
            + Op.STOP
        )

        tx = Transaction(
            data=calldata,
            gas_limit=100_000,
            protected=fork.supports_protected_txs(),
            sender=pre.fund_eoa(),
            to=to,
        )

    else:
        tx = Transaction(
            data=calldata,
            gas_limit=100_000,
            protected=fork.supports_protected_txs(),
            sender=pre.fund_eoa(),
            to=contract_address,
        )

    post = {contract_address: Account(storage={0x00: expected_storage})}
    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 6 parametrized test cases across 14 forks.