Skip to content

test_bal_call_7702_delegation_and_oog()

Documentation for tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists_opcodes.py::test_bal_call_7702_delegation_and_oog@c74f1a67.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists_opcodes.py::test_bal_call_7702_delegation_and_oog --fork Amsterdam

CALL with 7702 delegation - test all OOG boundaries.

When target_is_warm or delegation_is_warm, we use EIP-2930 tx access list. Access list warming does NOT add targets to BAL - only EVM access does.

Memory expansion is parametrized independently for args and ret per #1910.

Source code in tests/amsterdam/eip7928_block_level_access_lists/test_block_access_lists_opcodes.py
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
@pytest.mark.parametrize(
    "oog_boundary",
    list(OutOfGasBoundary),
    ids=lambda x: x.value,
)
@pytest.mark.parametrize(
    "target_is_warm", [False, True], ids=["cold_target", "warm_target"]
)
@pytest.mark.parametrize(
    "delegation_is_warm",
    [False, True],
    ids=["cold_delegation", "warm_delegation"],
)
@pytest.mark.parametrize("value", [0, 1], ids=["no_value", "with_value"])
@pytest.mark.parametrize(
    "args_size,ret_size",
    [
        pytest.param(0, 0, id="no_memory"),
        pytest.param(4096, 0, id="args_large"),
        pytest.param(0, 4096, id="ret_large"),
        pytest.param(32, 32, id="both_small"),
    ],
)
def test_bal_call_7702_delegation_and_oog(
    pre: Alloc,
    blockchain_test: BlockchainTestFiller,
    fork: Fork,
    oog_boundary: OutOfGasBoundary,
    target_is_warm: bool,
    delegation_is_warm: bool,
    value: int,
    args_size: int,
    ret_size: int,
) -> None:
    """
    CALL with 7702 delegation - test all OOG boundaries.

    When target_is_warm or delegation_is_warm, we use EIP-2930 tx access list.
    Access list warming does NOT add targets to BAL - only EVM access does.

    Memory expansion is parametrized independently for args and ret per #1910.
    """
    alice = pre.fund_eoa()

    delegation_target = pre.deploy_contract(code=Op.STOP)
    target = pre.fund_eoa(amount=0, delegation=delegation_target)

    new_memory_size = max(args_size, ret_size)

    # Full gas metadata: includes delegation cost
    call_code = Op.CALL(
        gas=0,
        address=target,
        value=value,
        args_size=args_size,
        args_offset=0,
        ret_size=ret_size,
        ret_offset=0,
        address_warm=target_is_warm,
        value_transfer=value > 0,
        account_new=False,
        new_memory_size=new_memory_size,
        delegated_address=True,
        delegated_address_warm=delegation_is_warm,
    )
    caller = pre.deploy_contract(code=call_code, balance=value)

    # Build access list for warming
    access_list: list[AccessList] = []
    if target_is_warm:
        access_list.append(AccessList(address=target, storage_keys=[]))
    if delegation_is_warm:
        access_list.append(
            AccessList(address=delegation_target, storage_keys=[])
        )

    intrinsic_cost = fork.transaction_intrinsic_cost_calculator()(
        access_list=access_list
    )

    # Static gas (before state access): no delegation
    call_static = Op.CALL(
        gas=0,
        address=target,
        value=value,
        args_size=args_size,
        args_offset=0,
        ret_size=ret_size,
        ret_offset=0,
        address_warm=target_is_warm,
        value_transfer=value > 0,
        account_new=False,
        new_memory_size=new_memory_size,
    )

    if oog_boundary == OutOfGasBoundary.OOG_BEFORE_TARGET_ACCESS:
        gas_limit = intrinsic_cost + call_static.gas_cost(fork) - 1
    elif oog_boundary == OutOfGasBoundary.OOG_AFTER_TARGET_ACCESS:
        # Enough for static_gas only - not enough for delegation_cost
        gas_limit = intrinsic_cost + call_static.gas_cost(fork)
    elif oog_boundary == OutOfGasBoundary.OOG_SUCCESS_MINUS_1:
        # One less than full cost - not enough for full call
        gas_limit = intrinsic_cost + call_code.gas_cost(fork) - 1
    else:
        gas_limit = intrinsic_cost + call_code.gas_cost(fork)

    tx = Transaction(
        sender=alice,
        to=caller,
        gas_limit=gas_limit,
        access_list=access_list,
    )

    # Access list warming does NOT add to BAL - only EVM execution does
    if oog_boundary == OutOfGasBoundary.OOG_BEFORE_TARGET_ACCESS:
        target_in_bal = False
        delegation_in_bal = False
    elif oog_boundary in (
        OutOfGasBoundary.OOG_AFTER_TARGET_ACCESS,
        OutOfGasBoundary.OOG_SUCCESS_MINUS_1,
    ):
        # Both cases: target accessed but not enough gas for full call
        # so delegation is NOT read (static check optimization)
        target_in_bal = True
        delegation_in_bal = False
    else:
        target_in_bal = True
        delegation_in_bal = True

    value_transferred = value > 0 and oog_boundary == OutOfGasBoundary.SUCCESS

    account_expectations: Dict[Address, BalAccountExpectation | None] = {
        caller: (
            BalAccountExpectation(
                balance_changes=[
                    BalBalanceChange(block_access_index=1, post_balance=0)
                ]
            )
            if value_transferred
            else BalAccountExpectation.empty()
        ),
        delegation_target: (
            BalAccountExpectation.empty() if delegation_in_bal else None
        ),
    }

    if target_in_bal:
        if value_transferred:
            account_expectations[target] = BalAccountExpectation(
                balance_changes=[
                    BalBalanceChange(block_access_index=1, post_balance=value)
                ]
            )
        else:
            account_expectations[target] = BalAccountExpectation.empty()
    else:
        account_expectations[target] = None

    # Post-state balance checks verify value transfer only happened on success
    post_state: Dict[Address, Account] = {alice: Account(nonce=1)}
    if value > 0:
        post_state[target] = Account(balance=value if value_transferred else 0)
        post_state[caller] = Account(balance=0 if value_transferred else value)

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=[tx],
                expected_block_access_list=BlockAccessListExpectation(
                    account_expectations=account_expectations
                ),
            )
        ],
        post=post_state,
    )

Parametrized Test Cases

This test generates 128 parametrized test cases across 1 fork.