Skip to content

test_call_memory_expands_on_early_revert()

Documentation for tests/frontier/opcodes/test_call.py::test_call_memory_expands_on_early_revert@88e9fb8f.

Generate fixtures for these test cases for Osaka with:

fill -v tests/frontier/opcodes/test_call.py::test_call_memory_expands_on_early_revert --fork Osaka

When CALL reverts early (e.g. because of not enough balance by the sender), memory should be expanded anyway. We check this with an MSTORE.

This is for a bug in an EVM implementation where memory is expanded after executing a CALL, but not when an early revert happens.

Source code in tests/frontier/opcodes/test_call.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
@pytest.mark.valid_from("Berlin")
def test_call_memory_expands_on_early_revert(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """
    When CALL reverts early (e.g. because of not enough balance by the sender),
    memory should be expanded anyway. We check this with an MSTORE.

    This is for a bug in an EVM implementation where memory is expanded after
    executing a CALL, but not when an early revert happens.
    """
    sender = pre.fund_eoa()

    gsc = fork.gas_costs()
    # arbitrary number, greater than memory size to trigger an expansion
    ret_size = 128

    call_measure = CodeGasMeasure(
        # CALL with value
        code=Op.CALL(gas=0, value=100, ret_size=ret_size),
        # Cost of pushing CALL args
        overhead_cost=gsc.G_VERY_LOW * len(Op.CALL.kwargs),
        # Because CALL pushes 1 item to the stack
        extra_stack_items=1,
        sstore_key=0,
        # Because it's the first CodeGasMeasure
        stop=False,
    )
    mstore_measure = CodeGasMeasure(
        # Low offset for not expanding memory
        code=Op.MSTORE(offset=ret_size // 2, value=1),
        # Cost of pushing MSTORE args
        overhead_cost=gsc.G_VERY_LOW * len(Op.MSTORE.kwargs),
        extra_stack_items=0,
        sstore_key=1,
    )

    # Contract without enough balance to send value transfer
    contract = pre.deploy_contract(code=call_measure + mstore_measure, balance=0)

    tx = Transaction(
        gas_limit=500_000,
        to=contract,
        value=0,
        sender=sender,
    )

    memory_expansion_gas_calc = fork.memory_expansion_gas_calculator()
    # call cost:
    #   address_access_cost+new_acc_cost+memory_expansion_cost+value-stipend
    call_cost = (
        gsc.G_COLD_ACCOUNT_ACCESS
        + gsc.G_NEW_ACCOUNT
        + memory_expansion_gas_calc(new_bytes=ret_size)
        + gsc.G_CALL_VALUE
        - gsc.G_CALL_STIPEND
    )

    # mstore cost: base cost. No memory expansion cost needed, it was expanded
    # on CALL.
    mstore_cost = gsc.G_MEMORY
    state_test(
        env=Environment(),
        pre=pre,
        tx=tx,
        post={
            contract: Account(
                storage={
                    0: call_cost,
                    1: mstore_cost,
                },
            )
        },
    )

Parametrized Test Cases

This test case is only parametrized by fork and fixture format.

Test ID (Abbreviated)
...fork_Berlin-state_test
...fork_Berlin-blockchain_test_from_state_test
...fork_London-state_test
...fork_London-blockchain_test_from_state_test
...fork_Paris-state_test
...fork_Paris-blockchain_test_from_state_test
...fork_Shanghai-state_test
...fork_Shanghai-blockchain_test_from_state_test
...fork_Cancun-state_test
...fork_Cancun-blockchain_test_from_state_test
...fork_Prague-state_test
...fork_Prague-blockchain_test_from_state_test
...fork_Osaka-state_test
...fork_Osaka-blockchain_test_from_state_test