@pytest.mark.valid_from("Frontier")
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,
)
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(
to=contract,
value=0,
sender=sender,
protected=fork.supports_protected_txs(),
)
# Empty callee + value → NEW_ACCOUNT on the schedule. CodeGasMeasure
# reads execution gas only (`execution_cost`); EIP-8037 parks
# NEW_ACCOUNT in state gas.
gsc = fork.gas_costs()
call_cost = (
Op.CALL(
address_warm=False,
value_transfer=True,
account_new=True,
new_memory_size=ret_size,
).execution_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(
pre=pre,
tx=tx,
post={
contract: Account(
storage={
0: call_cost,
1: mstore_cost,
},
)
},
)