2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127 | @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 a pre-existing balance-only leaf, the EIP-8037
success-refund path that the old conditional charge skipped.
"""
new_account = fork.gas_costs().NEW_ACCOUNT
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` regular 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)
# Regular gas the factory spends before the NEW_ACCOUNT charge: the
# initcode setup MSTORE plus the create opcode regular portion
# (`gas_cost` folds NEW_ACCOUNT into the create op, so strip it).
setup = Op.MSTORE(0, mstore_value)
pre_charge_regular = (
setup.gas_cost(fork) + create_call.gas_cost(fork) - new_account
)
forwarded_gas = gas_at_charge + pre_charge_regular
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)
|