Skip to content

test_gas_cost()

Documentation for tests/prague/eip7702_set_code_tx/test_gas.py::test_gas_cost@verkle@v0.0.6.

Generate fixtures for these test cases for Prague with:

Prague only:

fill -v tests/prague/eip7702_set_code_tx/test_gas.py::test_gas_cost --fork=Prague --evm-bin=/path/to/evm-tool-dev-version

For all forks up to and including Prague:

fill -v tests/prague/eip7702_set_code_tx/test_gas.py::test_gas_cost --until=Prague

Test gas at the execution start of a set-code transaction in multiple scenarios.

Source code in tests/prague/eip7702_set_code_tx/test_gas.py
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
@pytest.mark.parametrize(**gas_test_parameter_args(include_pre_authorized=False))
def test_gas_cost(
    state_test: StateTestFiller,
    pre: Alloc,
    authorization_list_with_properties: List[AuthorizationWithProperties],
    authorization_list: List[AuthorizationTuple],
    data: bytes,
    access_list: List[AccessList],
    sender: EOA,
):
    """
    Test gas at the execution start of a set-code transaction in multiple scenarios.
    """
    intrinsic_gas = (
        21_000
        + eip_2028_transaction_data_cost(data)
        + 1900 * sum(len(al.storage_keys) for al in access_list)
        + 2400 * len(access_list)
    )
    # Calculate the intrinsic gas cost of the authorizations, by default the
    # full empty account cost is charged for each authorization.
    intrinsic_gas += Spec.PER_EMPTY_ACCOUNT_COST * len(authorization_list_with_properties)

    discounted_authorizations = 0
    seen_authority = set()
    for authorization_with_properties in authorization_list_with_properties:
        if authorization_with_properties.invalidity_type is None:
            authority = authorization_with_properties.tuple.signer
            if not authorization_with_properties.empty:
                seen_authority.add(authority)
            if authority in seen_authority:
                discounted_authorizations += 1
            else:
                seen_authority.add(authority)

    discount_gas = (
        Spec.PER_EMPTY_ACCOUNT_COST - Spec.PER_AUTH_BASE_COST
    ) * discounted_authorizations

    # We calculate the exact gas required to execute the test code.
    # We add SSTORE opcodes in order to make sure that the refund is less than one fifth (EIP-3529)
    # of the total gas used, so we can see the full discount being reflected in most of the tests.
    gas_opcode_cost = 2
    sstore_opcode_count = 10
    push_opcode_count = (2 * (sstore_opcode_count)) - 1
    push_opcode_cost = 3 * push_opcode_count
    sstore_opcode_cost = 20_000 * sstore_opcode_count
    cold_storage_cost = 2_100 * sstore_opcode_count

    execution_gas = gas_opcode_cost + push_opcode_cost + sstore_opcode_cost + cold_storage_cost

    # The first opcode that executes in the code is the GAS opcode, which costs 2 gas, so we
    # subtract that from the expected gas measure.
    expected_gas_measure = execution_gas - gas_opcode_cost

    test_code_storage = Storage()
    test_code = (
        Op.SSTORE(test_code_storage.store_next(expected_gas_measure), Op.GAS)
        + sum(
            Op.SSTORE(test_code_storage.store_next(1), 1) for _ in range(sstore_opcode_count - 1)
        )
        + Op.STOP
    )
    test_code_address = pre.deploy_contract(test_code)

    tx_gas_limit = intrinsic_gas + execution_gas
    tx_max_fee_per_gas = 7
    tx_exact_cost = tx_gas_limit * tx_max_fee_per_gas

    # EIP-3529
    max_discount = tx_gas_limit // 5

    if discount_gas > max_discount:
        # Only one test hits this condition, but it's ok to also test this case.
        discount_gas = max_discount

    discount_cost = discount_gas * tx_max_fee_per_gas

    sender_account = pre[sender]
    assert sender_account is not None

    tx = Transaction(
        gas_limit=tx_gas_limit,
        max_fee_per_gas=tx_max_fee_per_gas,
        to=test_code_address,
        value=0,
        data=data,
        authorization_list=authorization_list,
        access_list=access_list,
        sender=sender,
    )

    state_test(
        env=Environment(gas_limit=max(tx_gas_limit, 30_000_000)),
        pre=pre,
        tx=tx,
        post={
            test_code_address: Account(storage=test_code_storage),
            sender: Account(balance=sender_account.balance - tx_exact_cost + discount_cost),
        },
    )

Parametrized Test Cases

The interactive table below is also available as a standalone page.

Skipped Parameters

For more concise readability, the table below does not list the following parameter values: fork, blockchain_test, state_test, state_test_only, eof_test, eof_state_test.

Test ID authorize_to_address signer_type authorization_invalidity_type authorizations_count chain_id_type access_list_case self_sponsored re_authorize authority_type data
single_valid_authorization_single_signer AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
single_valid_chain_specific_authorization_single_signer AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.CHAIN_SPECIFIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
multiple_valid_authorizations_single_signer AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 2 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
single_invalid_nonce_authorization_single_signer AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER AuthorizationInvalidityType.INVALID_NONCE 1 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
single_invalid_authorization_invalid_chain_id_single_signer AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER AuthorizationInvalidityType.INVALID_CHAIN_ID 1 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
multiple_invalid_nonce_authorizations_single_signer AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER AuthorizationInvalidityType.INVALID_NONCE 2 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
multiple_invalid_nonce_authorizations_multiple_signers AddressType.EMPTY_ACCOUNT SignerType.MULTIPLE_SIGNERS AuthorizationInvalidityType.INVALID_NONCE 2 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
multiple_invalid_chain_id_authorizations_single_signer AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER AuthorizationInvalidityType.INVALID_CHAIN_ID 2 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
multiple_valid_authorizations_multiple_signers AddressType.EMPTY_ACCOUNT SignerType.MULTIPLE_SIGNERS None 2 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
first_valid_then_single_repeated_nonce_authorization AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER AuthorizationInvalidityType.REPEATED_NONCE 2 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
first_valid_then_single_repeated_nonce_authorizations_multiple_signers AddressType.EMPTY_ACCOUNT SignerType.MULTIPLE_SIGNERS AuthorizationInvalidityType.REPEATED_NONCE 4 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
single_valid_authorization_to_eoa AddressType.EOA SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
single_valid_authorization_to_contract AddressType.CONTRACT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
single_valid_authorization_with_authority_in_access_list AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.CONTAINS_AUTHORITY False False AddressType.EMPTY_ACCOUNT
single_valid_authorization_with_set_code_address_in_access_list AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.CONTAINS_SET_CODE_ADDRESS False False AddressType.EMPTY_ACCOUNT
single_valid_authorization_with_authority_and_set_code_address_in_access_list AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.CONTAINS_AUTHORITY_AND_SET_CODE_ADDRESS False False AddressType.EMPTY_ACCOUNT
single_valid_authorization_eoa_authority AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EOA
single_valid_re_authorization_eoa_authority AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.EMPTY False True AddressType.EOA_WITH_SET_CODE
multiple_valid_authorizations_eoa_authority AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 2 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EOA
single_valid_authorization_eoa_self_sponsored_authority AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.EMPTY True False AddressType.EOA
multiple_valid_authorizations_eoa_self_sponsored_authority AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 2 ChainIDType.GENERIC AccessListType.EMPTY True False AddressType.EOA
single_valid_authorization_invalid_contract_authority AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.CONTRACT
multiple_authorizations_empty_account_then_contract_authority AddressType.EMPTY_ACCOUNT SignerType.MULTIPLE_SIGNERS None 2 ChainIDType.GENERIC AccessListType.EMPTY False False [, ]
multiple_authorizations_eoa_then_contract_authority AddressType.EMPTY_ACCOUNT SignerType.MULTIPLE_SIGNERS None 2 ChainIDType.GENERIC AccessListType.EMPTY False False [, ]
multiple_authorizations_eoa_self_sponsored_then_contract_authority AddressType.EMPTY_ACCOUNT SignerType.MULTIPLE_SIGNERS None 2 ChainIDType.GENERIC AccessListType.EMPTY True False [, ]
single_valid_authorization_with_single_non_zero_byte_data AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT 01
single_valid_authorization_with_single_zero_byte_data AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT 00
many_valid_authorizations_single_signer AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 5000 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
many_valid_authorizations_multiple_signers AddressType.EMPTY_ACCOUNT SignerType.MULTIPLE_SIGNERS None 5000 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
first_valid_then_many_duplicate_authorizations AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER AuthorizationInvalidityType.REPEATED_NONCE 5000 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
single_valid_authorization_single_signer AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
single_valid_chain_specific_authorization_single_signer AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.CHAIN_SPECIFIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
multiple_valid_authorizations_single_signer AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 2 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
single_invalid_nonce_authorization_single_signer AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER AuthorizationInvalidityType.INVALID_NONCE 1 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
single_invalid_authorization_invalid_chain_id_single_signer AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER AuthorizationInvalidityType.INVALID_CHAIN_ID 1 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
multiple_invalid_nonce_authorizations_single_signer AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER AuthorizationInvalidityType.INVALID_NONCE 2 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
multiple_invalid_nonce_authorizations_multiple_signers AddressType.EMPTY_ACCOUNT SignerType.MULTIPLE_SIGNERS AuthorizationInvalidityType.INVALID_NONCE 2 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
multiple_invalid_chain_id_authorizations_single_signer AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER AuthorizationInvalidityType.INVALID_CHAIN_ID 2 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
multiple_valid_authorizations_multiple_signers AddressType.EMPTY_ACCOUNT SignerType.MULTIPLE_SIGNERS None 2 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
first_valid_then_single_repeated_nonce_authorization AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER AuthorizationInvalidityType.REPEATED_NONCE 2 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
first_valid_then_single_repeated_nonce_authorizations_multiple_signers AddressType.EMPTY_ACCOUNT SignerType.MULTIPLE_SIGNERS AuthorizationInvalidityType.REPEATED_NONCE 4 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
single_valid_authorization_to_eoa AddressType.EOA SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
single_valid_authorization_to_contract AddressType.CONTRACT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
single_valid_authorization_with_authority_in_access_list AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.CONTAINS_AUTHORITY False False AddressType.EMPTY_ACCOUNT
single_valid_authorization_with_set_code_address_in_access_list AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.CONTAINS_SET_CODE_ADDRESS False False AddressType.EMPTY_ACCOUNT
single_valid_authorization_with_authority_and_set_code_address_in_access_list AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.CONTAINS_AUTHORITY_AND_SET_CODE_ADDRESS False False AddressType.EMPTY_ACCOUNT
single_valid_authorization_eoa_authority AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EOA
single_valid_re_authorization_eoa_authority AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.EMPTY False True AddressType.EOA_WITH_SET_CODE
multiple_valid_authorizations_eoa_authority AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 2 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EOA
single_valid_authorization_eoa_self_sponsored_authority AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.EMPTY True False AddressType.EOA
multiple_valid_authorizations_eoa_self_sponsored_authority AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 2 ChainIDType.GENERIC AccessListType.EMPTY True False AddressType.EOA
single_valid_authorization_invalid_contract_authority AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.CONTRACT
multiple_authorizations_empty_account_then_contract_authority AddressType.EMPTY_ACCOUNT SignerType.MULTIPLE_SIGNERS None 2 ChainIDType.GENERIC AccessListType.EMPTY False False [, ]
multiple_authorizations_eoa_then_contract_authority AddressType.EMPTY_ACCOUNT SignerType.MULTIPLE_SIGNERS None 2 ChainIDType.GENERIC AccessListType.EMPTY False False [, ]
multiple_authorizations_eoa_self_sponsored_then_contract_authority AddressType.EMPTY_ACCOUNT SignerType.MULTIPLE_SIGNERS None 2 ChainIDType.GENERIC AccessListType.EMPTY True False [, ]
single_valid_authorization_with_single_non_zero_byte_data AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT 01
single_valid_authorization_with_single_zero_byte_data AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 1 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT 00
many_valid_authorizations_single_signer AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER None 5000 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
many_valid_authorizations_multiple_signers AddressType.EMPTY_ACCOUNT SignerType.MULTIPLE_SIGNERS None 5000 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT
first_valid_then_many_duplicate_authorizations AddressType.EMPTY_ACCOUNT SignerType.SINGLE_SIGNER AuthorizationInvalidityType.REPEATED_NONCE 5000 ChainIDType.GENERIC AccessListType.EMPTY False False AddressType.EMPTY_ACCOUNT