964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102 | @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(
"memory_expansion", [False, True], ids=["no_memory", "with_memory"]
)
def test_bal_delegatecall_7702_delegation_and_oog(
pre: Alloc,
blockchain_test: BlockchainTestFiller,
fork: Fork,
oog_boundary: OutOfGasBoundary,
target_is_warm: bool,
delegation_is_warm: bool,
memory_expansion: bool,
) -> None:
"""
DELEGATECALL 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.
For 7702 delegation, there's ALWAYS a gap between static gas and
second check (delegation_cost) - all 3 scenarios produce distinct
behaviors.
"""
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
ret_offset = 0
# Full gas metadata: includes delegation cost
delegatecall_code = Op.DELEGATECALL(
gas=0,
address=target,
ret_size=ret_size,
ret_offset=ret_offset,
address_warm=target_is_warm,
new_memory_size=ret_size,
delegated_address=True,
delegated_address_warm=delegation_is_warm,
)
caller = pre.deploy_contract(code=delegatecall_code)
# 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
delegatecall_static = Op.DELEGATECALL(
gas=0,
address=target,
ret_size=ret_size,
ret_offset=ret_offset,
address_warm=target_is_warm,
new_memory_size=ret_size,
)
if oog_boundary == OutOfGasBoundary.OOG_BEFORE_TARGET_ACCESS:
gas_limit = intrinsic_cost + delegatecall_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 + delegatecall_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 + delegatecall_code.gas_cost(fork) - 1
else:
gas_limit = intrinsic_cost + delegatecall_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
account_expectations: Dict[Address, BalAccountExpectation | None] = {
caller: BalAccountExpectation.empty(),
delegation_target: (
BalAccountExpectation.empty() if delegation_in_bal else None
),
}
if target_in_bal:
account_expectations[target] = BalAccountExpectation.empty()
else:
account_expectations[target] = None
blockchain_test(
pre=pre,
blocks=[
Block(
txs=[tx],
expected_block_access_list=BlockAccessListExpectation(
account_expectations=account_expectations
),
)
],
post={alice: Account(nonce=1)},
)
|