Skip to content

test_sstore_restoration_create_init_revert()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py::test_sstore_restoration_create_init_revert@2119b382.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py::test_sstore_restoration_create_init_revert --fork Amsterdam

Verify a reverting CREATE sub-frame does not inflate the caller's reservoir under source-based (LIFO) refills.

The init code spills its state gas from gas_left, does 0 to x to 0 and REVERTs. The CREATE is wrapped in an outer frame that also REVERTs. Each refill returns the spilled state gas to gas_left, and the reverts refill it to the caller's gas_left, not the reservoir. The reservoir stays at 0, so a probe sized to OOG by 1 fails, since its fixed forwarded gas cannot reach the gas_left refund.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
@pytest.mark.with_all_create_opcodes
@pytest.mark.valid_from("EIP8037")
def test_sstore_restoration_create_init_revert(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    create_opcode: Op,
) -> None:
    """
    Verify a reverting CREATE sub-frame does not inflate the caller's
    reservoir under source-based (LIFO) refills.

    The init code spills its state gas from `gas_left`, does 0 to x to 0
    and REVERTs. The CREATE is wrapped in an outer frame that also
    REVERTs. Each refill returns the spilled state gas to `gas_left`,
    and the reverts refill it to the caller's `gas_left`, not the
    reservoir. The reservoir stays at 0, so a probe sized to OOG by 1
    fails, since its fixed forwarded gas cannot reach the `gas_left`
    refund.
    """
    gas_costs = fork.gas_costs()
    # Probe SSTORE(0, 1): 2 pushes + cold write + state gas - 1. OOGs by
    # 1 when the reservoir is 0, as forwarded gas misses gas_left.
    probe_gas = (
        2 * gas_costs.VERY_LOW
        + gas_costs.COLD_STORAGE_WRITE
        + Op.SSTORE(new_value=1).state_cost(fork)
        - 1
    )

    init_code = Op.SSTORE(0, 1) + Op.SSTORE(0, 0) + Op.REVERT(0, 0)
    probe = pre.deploy_contract(code=Op.SSTORE(0, 1))

    if create_opcode == Op.CREATE:
        create_call = Op.CREATE(0, 0, len(init_code))
    else:
        create_call = Op.CREATE2(0, 0, len(init_code), 0)

    # Inner contract performs the CREATE then REVERTs.
    inner = pre.deploy_contract(
        code=(
            Op.MSTORE(
                0,
                int.from_bytes(bytes(init_code), "big")
                << (256 - 8 * len(init_code)),
            )
            + Op.POP(create_call)
            + Op.REVERT(0, 0)
        ),
    )

    caller_storage = Storage()
    caller = pre.deploy_contract(
        code=(
            Op.POP(Op.CALL(gas=Op.GAS, address=inner))
            + Op.SSTORE(
                caller_storage.store_next(0, "probe_must_fail"),
                Op.CALL(gas=probe_gas, address=probe),
            )
        ),
    )

    # gas_limit at the cap means the caller's reservoir starts at 0.
    tx = Transaction(
        to=caller,
        state_gas_reservoir=0,
        sender=pre.fund_eoa(),
    )

    post = {caller: Account(storage=caller_storage)}
    state_test(pre=pre, tx=tx, post=post)

Parametrized Test Cases

This test generates 2 parametrized test cases across 1 fork.