Skip to content

test_calldatasize()

Documentation for tests/frontier/opcodes/test_calldatasize.py::test_calldatasize@5f132e7c.

Generate fixtures for these test cases for Amsterdam with:

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

Test CALLDATASIZE opcode.

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

Based on https://github.com/ethereum/tests/blob/ 81862e4/src/ GeneralStateTestsFiller/VMTests/vmTests/calldatasizeFiller.yml

Source code in tests/frontier/opcodes/test_calldatasize.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
@pytest.mark.ported_from(
    [
        "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/VMTests/vmTests/calldatasizeFiller.yml",
    ],
    pr=["https://github.com/ethereum/execution-spec-tests/pull/1236"],
)
@pytest.mark.parametrize(
    "args_size",
    [0, 2, 16, 33, 257],
)
@pytest.mark.parametrize("calldata_source", ["contract", "tx"])
@pytest.mark.slow()
def test_calldatasize(
    state_test: StateTestFiller,
    fork: Fork,
    args_size: int,
    pre: Alloc,
    calldata_source: str,
) -> None:
    """
    Test `CALLDATASIZE` opcode.

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

    Based on
    https://github.com/ethereum/tests/blob/
    81862e4848585a438d64f911a19b3825f0f4cd95/src/
    GeneralStateTestsFiller/VMTests/vmTests/calldatasizeFiller.yml
    """
    contract_code = Op.SSTORE(key=0x0, value=Op.CALLDATASIZE)
    contract_address = pre.deploy_contract(contract_code)
    calldata = b"\x01" * args_size

    intrinsic = fork.transaction_intrinsic_cost_calculator()
    # EIP-1706 sentry: SSTORE fails if gas_left <= CALL_STIPEND (2300)
    # before its base cost is deducted, so the inner frame needs that
    # much headroom on top of the SSTORE cost.
    sstore_sentry_slack = fork.gas_costs().CALL_STIPEND + 1
    # Outer's CALL reserves this many gas units (`Op.SUB(Op.GAS(), N)`)
    # before forwarding the rest to the inner frame.
    outer_call_reserve = 256
    if calldata_source == "contract":
        outer_code = Om.MSTORE(calldata, 0x0) + Op.CALL(
            gas=Op.SUB(Op.GAS(), outer_call_reserve),
            address=contract_address,
            value=0x0,
            args_offset=0x0,
            args_size=args_size,
            ret_offset=0x0,
            ret_size=0x0,
        )
        to = pre.deploy_contract(code=outer_code)

        tx = Transaction(
            gas_limit=(
                intrinsic()
                + outer_code.gas_cost(fork)
                + outer_call_reserve
                + contract_code.gas_cost(fork)
                + sstore_sentry_slack
                + Op.SSTORE(new_value=1).state_cost(fork)
            ),
            protected=fork.supports_protected_txs(),
            sender=pre.fund_eoa(),
            to=to,
        )

    else:
        tx = Transaction(
            data=calldata,
            gas_limit=(
                intrinsic(calldata=calldata)
                + contract_code.gas_cost(fork)
                + sstore_sentry_slack
                + Op.SSTORE(new_value=1).state_cost(fork)
            ),
            protected=fork.supports_protected_txs(),
            sender=pre.fund_eoa(),
            to=contract_address,
        )

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

Parametrized Test Cases

This test generates 10 parametrized test cases across 14 forks.