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@9c2813ee.

Generate fixtures for these test cases for Amsterdam with:

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

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
 86
 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
163
164
165
166
167
@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()

    # arbitrary number, greater than memory size to trigger an expansion
    ret_size = 128

    # Cost of pushing args onto the stack (each PUSH costs VERY_LOW)
    call_push_cost = (Op.PUSH1(0) * len(Op.CALL.kwargs)).gas_cost(fork)
    mstore_push_cost = (Op.PUSH1(0) * len(Op.MSTORE.kwargs)).gas_cost(fork)

    call_measure = CodeGasMeasure(
        # CALL with value
        code=Op.CALL(gas=0, value=100, ret_size=ret_size),
        overhead_cost=call_push_cost,
        # 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),
        overhead_cost=mstore_push_cost,
        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,
    )

    # call cost:
    #   address_access_cost+new_acc_cost+memory_expansion_cost+value-stipend
    # CALL_STIPEND is a threshold check, not a gas cost — keep from gas_costs
    gsc = fork.gas_costs()
    call_cost = (
        Op.CALL(
            address_warm=False,
            value_transfer=True,
            account_new=True,
            new_memory_size=ret_size,
        ).gas_cost(fork)
        - gsc.CALL_STIPEND
    )

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

Parametrized Test Cases

This test generates 1 parametrized test case across 8 forks.