Skip to content

test_cross_frame_refund_repays_spill_at_a_create()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_cross_frame_refund.py::test_cross_frame_refund_repays_spill_at_a_create@622620d8.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_cross_frame_refund.py::test_cross_frame_refund_repays_spill_at_a_create --fork Amsterdam

Test a create merging a refund repays the spill.

The initcode reaches a holder contract that clears its own slot. The refund repays the spill at the create merge and the next window's account creation spills afresh, so the clearing window costs one slot's state gas less than the no-op window.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_cross_frame_refund.py
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
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
@pytest.mark.with_all_create_opcodes
@pytest.mark.valid_from("EIP8037")
def test_cross_frame_refund_repays_spill_at_a_create(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    create_opcode: Opcode,
) -> None:
    """
    Test a create merging a refund repays the spill.

    The initcode reaches a holder contract that clears its own slot.
    The refund repays the spill at the create merge and the next
    window's account creation spills afresh, so the clearing window
    costs one slot's state gas less than the no-op window.
    """
    holder = deploy_slot_holder(pre)
    set_slot = Op.POP(Op.CALL(address=holder, args_size=1))
    # Pushing the zero arguments with PUSH0 keeps the initcode inside a
    # single memory word.
    initcode = Op.CALL(
        Op.GAS, holder, Op.PUSH0, Op.PUSH0, Op.PUSH0, Op.PUSH0, Op.PUSH0
    )
    mstore_value, size = init_code_at_high_bytes(initcode)

    # The probe measures with memory below 96, so the initcode sits
    # above it.
    code_offset = 96

    def window(salt: int) -> Bytecode:
        # A repeated CREATE2 salt would collide with the account the
        # previous window created.
        if create_opcode == Op.CREATE2:
            return Op.POP(Op.CREATE2(0, code_offset, size, salt))
        return Op.POP(Op.CREATE(0, code_offset, size))

    contract = pre.deploy_contract(
        code=Op.MSTORE(code_offset, mstore_value)
        + clearing_probe_code(set_slot, [window(salt) for salt in range(3)])
    )

    tx = Transaction(
        to=contract,
        state_gas_reservoir=0,
        sender=pre.fund_eoa(),
    )

    cleared = Op.SSTORE.with_metadata(
        key_warm=True,
        original_value=0,
        current_value=1,
        new_value=0,
    )
    # The account creation still outweighs the repayment, so `gas_left`
    # falls across the clearing window.
    post = {
        holder: Account(storage={SLOT_X: 0}),
        contract: Account(
            storage={
                SLOT_MARKER: 1,
                SLOT_INCREASED: 0,
                SLOT_RESULT: -cleared.state_refund(fork),
            }
        ),
    }
    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 2 parametrized test cases across 1 fork.