Skip to content

test_nested_failure_resets_to_tx_reservoir()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_reservoir.py::test_nested_failure_resets_to_tx_reservoir@5c024cbb.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_reservoir.py::test_nested_failure_resets_to_tx_reservoir --fork Amsterdam

Verify failure cascade refunds state-gas to the top reservoir.

Each frame runs its parametrized body, then calls or CREATEs the next frame, terminating with the failure mode. Every level fails so the cascade reaches the top.

Axes: - failure_mode: REVERT vs HALT. Top-level gas_left semantics differ, but state gas refund must agree per the updated EIP. - spill_mode: no_spill sizes the reservoir to cover all state gas charges. spill shrinks it so charges drain into gas_left, exercising the spill-refund-on-halt rule. - frame_op: call chains via CALL with no per-frame pre-charge. create chains via CREATE, where each level pre-charges STATE_BYTES_PER_NEW_ACCOUNT * cpsb and exercises credit-on-failure interleaved with the spill.

Refunds are LIFO. On REVERT every state gas charge (body charges, spilled portions, and CREATE pre-charges) is refilled, the spill landing back in gas_left, so the user pays only regular charges plus intrinsic. On HALT the LIFO refill returns spilled state gas to gas_left, which is then zeroed, so only the start reservoir survives and the user pays tx_gas - reservoir = gas_limit_cap, regardless of spill axis or CREATE pre-charges.

Two assertions cross-check the gas accounting: - cumulative_gas_used (receipt) pins tx.gas - gas_left - state_gas_left, catching bugs in the leftover split. - header.gas_used pins max(block_regular, block_state) via the block accumulators.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_reservoir.py
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
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
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
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
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
@pytest.mark.parametrize(
    "frame_bodies",
    [
        pytest.param(
            [
                Op.SSTORE(0, 1),
                Op.SSTORE(1, 1),
                Op.SSTORE(2, 1),
                Op.SSTORE(3, 1),
            ],
            id="depth_4_sstore_each",
        ),
        pytest.param(
            [
                Op.SSTORE(0, 1),
                Bytecode(),
                Op.SSTORE(2, 1),
                Bytecode(),
            ],
            id="depth_4_alternating_state",
        ),
        pytest.param(
            [Bytecode(), Bytecode(), Bytecode(), Bytecode()],
            id="depth_4_no_state",
        ),
        pytest.param(
            [
                Op.SSTORE(0, 1) + Op.SSTORE(1, 1),
                Op.SSTORE(2, 1) + Op.SSTORE(3, 1),
                Op.SSTORE(4, 1) + Op.SSTORE(5, 1),
            ],
            id="depth_3_two_sstores_each",
        ),
        pytest.param(
            [
                Bytecode(),
                Bytecode(),
                Op.SSTORE(0, 1)
                + Op.SSTORE(
                    0,
                    0,
                    key_warm=True,
                    original_value=0,
                    current_value=1,
                    new_value=0,
                ),
            ],
            id="depth_3_deepest_0_to_x_to_0",
        ),
        pytest.param(
            [
                Bytecode(),
                Bytecode(),
                Op.SSTORE(0, 1)
                + Op.SSTORE(
                    0,
                    2,
                    key_warm=True,
                    original_value=0,
                    current_value=1,
                    new_value=2,
                )
                + Op.SSTORE(
                    0,
                    0,
                    key_warm=True,
                    original_value=0,
                    current_value=2,
                    new_value=0,
                ),
            ],
            id="depth_3_deepest_0_to_x_to_y_to_0",
        ),
    ],
)
@pytest.mark.parametrize(
    "failure_mode",
    [
        pytest.param("revert", id="revert"),
        pytest.param("halt", id="halt"),
    ],
)
@pytest.mark.parametrize(
    "spill_mode",
    [
        pytest.param("no_spill", id="no_spill"),
        pytest.param("spill", id="spill"),
    ],
)
@pytest.mark.parametrize(
    "frame_op",
    [
        pytest.param("call", id="call_chain"),
        pytest.param("create", id="create_chain"),
    ],
)
@pytest.mark.valid_from("EIP8037")
def test_nested_failure_resets_to_tx_reservoir(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    failure_mode: str,
    frame_bodies: list[Bytecode],
    spill_mode: str,
    frame_op: str,
) -> None:
    """
    Verify failure cascade refunds state-gas to the top reservoir.

    Each frame runs its parametrized body, then calls or CREATEs the
    next frame, terminating with the failure mode. Every level fails
    so the cascade reaches the top.

    Axes:
    - `failure_mode`: REVERT vs HALT. Top-level gas_left semantics
      differ, but state gas refund must agree per the updated EIP.
    - `spill_mode`: `no_spill` sizes the reservoir to cover all state
      gas charges. `spill` shrinks it so charges drain into gas_left,
      exercising the spill-refund-on-halt rule.
    - `frame_op`: `call` chains via CALL with no per-frame pre-charge.
      `create` chains via CREATE, where each level pre-charges
      `STATE_BYTES_PER_NEW_ACCOUNT * cpsb` and exercises
      credit-on-failure interleaved with the spill.

    Refunds are LIFO. On REVERT every state gas charge (body charges,
    spilled portions, and CREATE pre-charges) is refilled, the spill
    landing back in `gas_left`, so the user pays only regular charges
    plus intrinsic. On HALT the LIFO refill returns spilled state gas
    to `gas_left`, which is then zeroed, so only the start reservoir
    survives and the user pays `tx_gas - reservoir = gas_limit_cap`,
    regardless of spill axis or CREATE pre-charges.

    Two assertions cross-check the gas accounting:
    - `cumulative_gas_used` (receipt) pins `tx.gas - gas_left -
      state_gas_left`, catching bugs in the leftover split.
    - `header.gas_used` pins `max(block_regular, block_state)` via
      the block accumulators.
    """
    gas_limit_cap = fork.transaction_gas_limit_cap()
    assert gas_limit_cap is not None
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)
    new_account_state_gas = fork.gas_costs().NEW_ACCOUNT
    intrinsic_cost = fork.transaction_intrinsic_cost_calculator()()

    body_state_total = sum(b.state_cost(fork) for b in frame_bodies)
    n_creates = (len(frame_bodies) - 1) if frame_op == "create" else 0
    total_state_charges = body_state_total + n_creates * new_account_state_gas

    if spill_mode == "no_spill":
        # Reservoir comfortably covers all state-gas charges.
        reservoir = max(
            total_state_charges + sstore_state_gas, sstore_state_gas
        )
    else:
        # Reservoir is small; charges spill into gas_left.
        reservoir = sstore_state_gas
    tx_gas = gas_limit_cap + reservoir

    terminator = Op.REVERT(0, 0) if failure_mode == "revert" else Op.INVALID

    if frame_op == "call":
        top, frame_codes = _build_call_chain(pre, frame_bodies, terminator)
    else:
        top, frame_codes = _build_create_chain(pre, frame_bodies, terminator)

    sum_regular = sum(code.regular_cost(fork) for code in frame_codes)
    if failure_mode == "halt":
        # LIFO refill returns spilled state gas (and spilled CREATE
        # pre-charges) to gas_left, which halt then zeros. Only the
        # start reservoir survives.
        expected_cumulative = tx_gas - reservoir
        assert expected_cumulative == gas_limit_cap
        # Header: all gas_left (including the refilled spill) is
        # consumed as regular. Block state gas is zero for plain
        # frames.
        expected_header_gas_used = gas_limit_cap
    elif failure_mode == "revert":
        # Revert preserves gas_left, full state gas refund, so the
        # user pays only regular costs plus intrinsic.
        expected_cumulative = intrinsic_cost + sum_regular
        # Header reflects the regular-vs-state attribution directly:
        # state_gas_used is zeroed by the tx error handler, so only
        # regular gas usage shows up.
        expected_header_gas_used = intrinsic_cost + sum_regular
    else:
        raise ValueError("Invariant, unreachable code.")

    tx = Transaction(
        to=top,
        state_gas_reservoir=reservoir,
        sender=pre.fund_eoa(),
        expected_receipt=TransactionReceipt(
            cumulative_gas_used=expected_cumulative,
        ),
    )

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=[tx],
                header_verify=Header(gas_used=expected_header_gas_used),
            )
        ],
        post={},
    )

Parametrized Test Cases

This test generates 48 parametrized test cases across 1 fork.