Skip to content

test_recursive_revert_returns_reservoir()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py::test_recursive_revert_returns_reservoir@622620d8.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py::test_recursive_revert_returns_reservoir --fork Amsterdam

Test a deep self-recursing call chain returns the reservoir.

Each frame recurses while it has the gas to, and the bottom one instead draws the whole reservoir for an SSTORE and reverts, restoring it. A probe in the top frame, handed only its SSTORE's execution cost, then succeeds only if that restore travelled back up the entire unwind.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
@pytest.mark.valid_from("EIP8037")
def test_recursive_revert_returns_reservoir(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """
    Test a deep self-recursing call chain returns the reservoir.

    Each frame recurses while it has the gas to, and the bottom one
    instead draws the whole reservoir for an SSTORE and reverts,
    restoring it. A probe in the top frame, handed only its SSTORE's
    execution cost, then succeeds only if that restore travelled back
    up the entire unwind.
    """
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)

    # Recurse while there is comfortably more gas than the bottom frame
    # needs. Gas shrinks by only 1/64 per level, so the frame that first
    # falls below the threshold still holds well over `bottom_gas`.
    bottom = Op.SSTORE(0, 1) + Op.REVERT(0, 0)
    bottom_gas = bottom.execution_cost(fork)
    driver_code = Conditional(
        condition=Op.GT(Op.GAS, 2 * bottom_gas),
        if_true=Op.POP(Op.CALL(Op.GAS, Op.ADDRESS, 0, 0, 0, 0, 0)),
        if_false=bottom,
    )
    driver = pre.deploy_contract(code=driver_code)

    probe_code = Op.SSTORE(0, 1)
    probe = pre.deploy_contract(code=probe_code)
    probe_gas = probe_code.execution_cost(fork)

    storage = Storage()
    probe_slot = storage.store_next(1, "probe_succeeds")
    caller_code = Op.POP(Op.CALL(gas=Op.GAS, address=driver)) + Op.SSTORE(
        probe_slot,
        Op.CALL(gas=probe_gas, address=probe),
        # gas accounting
        original_value=1,
        current_value=1,
        new_value=1,
        key_warm=False,
    )
    caller = pre.deploy_contract(code=caller_code, storage={probe_slot: 1})

    tx = Transaction(
        to=caller,
        state_gas_reservoir=sstore_state_gas,
        sender=pre.fund_eoa(),
    )

    post = {
        caller: Account(storage=storage),
        probe: Account(storage={0: 1}),
    }
    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.