1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211 | @pytest.mark.valid_from("Prague")
def test_pointer_reentry(state_test: StateTestFiller, pre: Alloc):
"""
Check operations when reenter the pointer again
TODO: feel free to extend the code checks under given scenarios in switch case.
"""
env = Environment()
arg_contract = 0
arg_action = 32
storage_b = Storage()
storage_b.store_next(1, "contract_calls")
storage_b.store_next(1, "tstore_slot")
slot_reentry_address = storage_b.store_next(1, "address")
storage_pointer_b = Storage()
slot_calls = storage_pointer_b.store_next(2, "pointer_calls")
slot_tstore = storage_pointer_b.store_next(2, "tstore_slot")
sender = pre.fund_eoa()
pointer_b = pre.fund_eoa(amount=1000)
proxy = pre.deploy_contract(
code=Op.MSTORE(arg_contract, Op.CALLDATALOAD(arg_contract))
+ Op.MSTORE(arg_action, Op.CALLDATALOAD(arg_action))
+ Op.CALL(gas=400_000, address=pointer_b, args_offset=0, args_size=32 * 2)
)
contract_b = pre.deploy_contract(
balance=100,
code=Op.MSTORE(arg_contract, Op.CALLDATALOAD(arg_contract))
+ Op.MSTORE(arg_action, Op.CALLDATALOAD(arg_action))
+ Op.SSTORE(slot_calls, Op.ADD(Op.SLOAD(slot_calls), 1))
+ Op.TSTORE(slot_tstore, Op.ADD(Op.TLOAD(slot_tstore), 1))
+ Op.SSTORE(slot_tstore, Op.TLOAD(slot_tstore))
+ Switch(
cases=[
Case(
condition=Op.EQ(Op.MLOAD(arg_action), ReentryAction.CALL_PROXY),
action=Op.MSTORE(arg_action, ReentryAction.MEASURE_VALUES)
+ Op.CALL(gas=500_000, address=proxy, args_offset=0, args_size=32 * 2)
+ Op.STOP(),
),
Case(
# This code is executed under pointer -> proxy -> pointer context
condition=Op.EQ(Op.MLOAD(arg_action), ReentryAction.MEASURE_VALUES),
action=Op.SSTORE(storage_pointer_b.store_next(sender, "origin"), Op.ORIGIN())
+ Op.SSTORE(storage_pointer_b.store_next(pointer_b, "address"), Op.ADDRESS())
+ Op.SSTORE(
storage_pointer_b.store_next(1000, "selfbalance"), Op.SELFBALANCE()
)
+ Op.SSTORE(storage_pointer_b.store_next(proxy, "caller"), Op.CALLER())
# now call contract which is pointer dest directly
+ Op.MSTORE(arg_action, ReentryAction.MEASURE_VALUES_CONTRACT)
+ Op.CALL(
gas=500_000,
address=Op.MLOAD(arg_contract),
args_offset=0,
args_size=32 * 2,
),
),
Case(
# This code is executed under
# pointer -> proxy -> pointer -> contract
# so pointer calling the code of it's dest after reentry to itself
condition=Op.EQ(Op.MLOAD(arg_action), ReentryAction.MEASURE_VALUES_CONTRACT),
action=Op.SSTORE(storage_b.store_next(sender, "origin"), Op.ORIGIN())
+ Op.SSTORE(slot_reentry_address, Op.ADDRESS())
+ Op.SSTORE(storage_b.store_next(100, "selfbalance"), Op.SELFBALANCE())
+ Op.SSTORE(storage_b.store_next(pointer_b, "caller"), Op.CALLER()),
),
],
default_action=None,
),
)
storage_b[slot_reentry_address] = contract_b
tx = Transaction(
to=pointer_b,
gas_limit=2_000_000,
data=Hash(contract_b, left_padding=True)
+ Hash(ReentryAction.CALL_PROXY, left_padding=True),
value=0,
sender=sender,
authorization_list=[
AuthorizationTuple(
address=contract_b,
nonce=0,
signer=pointer_b,
)
],
)
post = {
contract_b: Account(storage=storage_b),
pointer_b: Account(storage=storage_pointer_b),
}
state_test(
env=env,
pre=pre,
post=post,
tx=tx,
)
|