Skip to content

test_code_deposit_state_gas_scales_with_size()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_code_deposit_state_gas_scales_with_size@5c024cbb.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_code_deposit_state_gas_scales_with_size --fork Amsterdam

Test code deposit state gas scales linearly with code size.

The code deposit charges len(code) * cost_per_state_byte of state gas. Larger deployed code requires proportionally more state gas. When code exceeds MAX_CODE_SIZE, the size check rejects before any gas is charged and the contract is not deployed.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
@pytest.mark.parametrize(
    "code_size",
    [
        pytest.param(1, id="tiny_code"),
        pytest.param(32, id="one_word"),
        pytest.param(256, id="small_contract"),
        pytest.param(1024, id="medium_contract"),
        pytest.param("max", id="max_code_size"),
        pytest.param("max+1", id="over_max_code_size"),
    ],
)
@pytest.mark.valid_from("EIP8037")
def test_code_deposit_state_gas_scales_with_size(
    state_test: StateTestFiller,
    pre: Alloc,
    code_size: Union[int, str],
    fork: Fork,
) -> None:
    """
    Test code deposit state gas scales linearly with code size.

    The code deposit charges len(code) * cost_per_state_byte of state
    gas. Larger deployed code requires proportionally more state gas.
    When code exceeds MAX_CODE_SIZE, the size check rejects before
    any gas is charged and the contract is not deployed.
    """
    if code_size == "max":
        code_size = fork.max_code_size()
    elif code_size == "max+1":
        code_size = fork.max_code_size() + 1
    assert isinstance(code_size, int)

    # State gas: new account + code deposit
    total_state_gas = fork.create_state_gas(code_size=code_size)

    # Build init code that returns `code_size` bytes of 0x00
    # PUSH2 code_size, PUSH1 0, RETURN
    init_code = Op.RETURN(0, code_size)

    sender = pre.fund_eoa()
    tx = Transaction(
        to=None,
        data=init_code,
        state_gas_reservoir=total_state_gas,
        sender=sender,
    )

    if code_size > fork.max_code_size():
        create_address = compute_create_address(address=sender, nonce=0)
        post = {create_address: Account.NONEXISTENT}
    else:
        post = {}

    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 6 parametrized test cases across 1 fork.