2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513 | @pytest.mark.parametrize(
"reservoir_covers",
[
pytest.param(True, id="charge_from_reservoir"),
pytest.param(False, id="charge_spills_from_gas_left"),
],
)
@pytest.mark.with_all_create_opcodes()
@pytest.mark.valid_from("EIP8037")
def test_create_account_charge_reduces_child_gas(
state_test: StateTestFiller,
pre: Alloc,
fork: Fork,
create_opcode: Op,
reservoir_covers: bool,
) -> None:
"""
Verify the early NEW_ACCOUNT charge reduces forwarded child gas.
`generic_create` charges NEW_ACCOUNT before computing the child's
63/64 share. When the reservoir covers the charge `gas_left` is
untouched and the child receives the full share. When the reservoir
is empty the charge spills NEW_ACCOUNT from `gas_left` first, so the
child receives `NEW_ACCOUNT * 63 / 64` less. The init code burns a
fixed amount sized between the two shares, so it deploys when the
charge comes from the reservoir and runs out of gas when it spills.
The target is fresh, so NEW_ACCOUNT is required. The created account is
checked directly, avoiding a post-CREATE SSTORE with an unrelated state
gas requirement.
"""
new_account = create_opcode(account_new=True).state_cost(fork)
memory_gas = fork.memory_expansion_gas_calculator()
# Factory `gas_left` at the NEW_ACCOUNT charge. Three times
# NEW_ACCOUNT gives a wide discrimination window and a large
# absolute child share.
gas_at_charge = 3 * new_account
full_share = gas_at_charge - gas_at_charge // 64
spilled = gas_at_charge - new_account
reduced_share = spilled - spilled // 64
# Burn the middle of `(reduced_share, full_share]` for robustness.
target_burn = (full_share + reduced_share) // 2
# Init code burns `target_burn` execution gas via one MSTORE memory
# expansion, then deploys empty code (zero code deposit). Invert
# `words * MEMORY_PER_WORD + words ** 2 // 512 = target_mem` to size
# the sink offset from gas rather than a magic number.
init_static = (Op.MSTORE(0, 0) + Op.RETURN(0, 0)).gas_cost(fork)
target_mem = target_burn - init_static
# Memory cost is monotonic in word count, so binary search the
# largest word count whose expansion stays within `target_mem`.
low, high = 1, target_mem
while low < high:
mid = (low + high + 1) // 2
if int(memory_gas(new_bytes=mid * 32)) <= target_mem:
low = mid
else:
high = mid - 1
words = low
sink_offset = (words - 1) * 32
child_burn = init_static + int(memory_gas(new_bytes=words * 32))
assert reduced_share < child_burn <= full_share
init_code = Op.MSTORE(sink_offset, 0) + Op.RETURN(0, 0)
mstore_value, size = init_code_at_high_bytes(init_code)
create_call = (
create_opcode(value=0, offset=0, size=size, salt=0)
if create_opcode == Op.CREATE2
else create_opcode(value=0, offset=0, size=size)
)
storage = Storage()
expected = 1 if reservoir_covers else 0
factory = pre.deploy_contract(
code=Op.MSTORE(0, mstore_value)
+ Op.SSTORE(
storage.store_next(expected, "child_succeeds"),
Op.GT(create_call, 0),
),
)
# Pre-existing balance-only target: the success-refund path. Under
# the old conditional approach this alive target skips NEW_ACCOUNT,
# so the child gets the full share and fits in both cases.
if create_opcode == Op.CREATE2:
create_address = compute_create2_address(
address=factory, salt=0, initcode=bytes(init_code)
)
else:
create_address = compute_create_address(address=factory, nonce=1)
pre.fund_address(create_address, amount=1)
# Execution gas the factory spends before the NEW_ACCOUNT charge: the
# initcode setup MSTORE plus the create opcode's execution portion.
setup = Op.MSTORE(0, mstore_value)
pre_charge_execution = setup.gas_cost(fork) + create_call.execution_cost(
fork
)
forwarded_gas = gas_at_charge + pre_charge_execution
caller = pre.deploy_contract(
code=Op.CALL(gas=forwarded_gas, address=factory)
)
tx = Transaction(
to=caller,
state_gas_reservoir=new_account if reservoir_covers else 0,
sender=pre.fund_eoa(),
)
state_test(pre=pre, post={factory: Account(storage=storage)}, tx=tx)
|