Skip to content

test_extcodecopy_empty_account_composes_additively()

Documentation for tests/amsterdam/eip8038_state_access_gas_cost_increase/test_ext_code_opcodes_gas.py::test_extcodecopy_empty_account_composes_additively@2867859a.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8038_state_access_gas_cost_increase/test_ext_code_opcodes_gas.py::test_extcodecopy_empty_account_composes_additively --fork Amsterdam

Compose the surcharge additively when copying from an empty account.

EXTCODECOPY of a non-existent source copies zero bytes into memory, yet still charges the account-access cost, the EIP-8038 code-read WARM_ACCESS surcharge, the EIP-150 per-word copy cost (OPCODE_COPY_PER_WORD per word, driven by the requested size, not the source length), and the memory-expansion cost. The measured gas must equal the sum of all four components, confirming the surcharge composes additively even when there is no code to read.

Source code in tests/amsterdam/eip8038_state_access_gas_cost_increase/test_ext_code_opcodes_gas.py
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
@EIPChecklist.GasCostChanges.Test.GasUpdatesMeasurement()
@pytest.mark.parametrize("warm", [False, True], ids=["cold", "warm"])
@pytest.mark.parametrize(
    "copy_size", [32, 96], ids=["one_word", "three_words"]
)
def test_extcodecopy_empty_account_composes_additively(
    state_test: StateTestFiller,
    env: Environment,
    pre: Alloc,
    fork: Fork,
    warm: bool,
    copy_size: int,
) -> None:
    """
    Compose the surcharge additively when copying from an empty account.

    ``EXTCODECOPY`` of a non-existent source copies zero bytes into memory,
    yet still charges the account-access cost, the EIP-8038 code-read
    ``WARM_ACCESS`` surcharge, the EIP-150 per-word copy cost
    (``OPCODE_COPY_PER_WORD`` per word, driven by the requested size, not
    the source length), and the memory-expansion cost. The measured gas
    must equal the sum of all four components, confirming the surcharge
    composes additively even when there is no code to read.
    """
    # Empty source: never deployed, no code. The copy yields zeros, but the
    # cost is driven by the requested size, identical to a code-bearing
    # source of the same length.
    empty_addr = pre.nonexistent_account()

    oracle = Op.EXTCODECOPY.with_metadata(
        address_warm=warm,
        data_size=copy_size,
        new_memory_size=copy_size,
        old_memory_size=0,
    )
    measured_code = oracle(
        address=empty_addr, dest_offset=0, offset=0, size=copy_size
    )

    expected_gas = oracle.gas_cost(fork)

    code_gas_measure = CodeGasMeasure(
        code=measured_code,
        overhead_cost=measured_code.gas_cost(fork) - oracle.gas_cost(fork),
        extra_stack_items=0,
    )
    measure_address = pre.deploy_contract(code=code_gas_measure)

    tx = Transaction(
        to=measure_address,
        sender=pre.fund_eoa(),
        access_list=[AccessList(address=empty_addr, storage_keys=[])]
        if warm
        else None,
    )

    post = {measure_address: Account(storage={0: expected_gas})}
    state_test(env=env, pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 4 parametrized test cases across 1 fork.