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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
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 | @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(
"memory_expansion", [False, True], ids=["no_memory", "with_memory"]
)
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,
memory_expansion: bool,
) -> 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.
"""
alice = pre.fund_eoa()
delegation_target = pre.deploy_contract(code=Op.STOP)
target = pre.fund_eoa(amount=0, delegation=delegation_target)
# memory expansion / no expansion
ret_size = 32 if memory_expansion else 0
# Full gas metadata: includes delegation cost
call_code = Op.CALL(
gas=0,
address=target,
value=value,
ret_size=ret_size,
ret_offset=0,
address_warm=target_is_warm,
value_transfer=value > 0,
account_new=False,
new_memory_size=ret_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,
ret_size=ret_size,
ret_offset=0,
address_warm=target_is_warm,
value_transfer=value > 0,
account_new=False,
new_memory_size=ret_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,
)
|