130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277 | @pytest.mark.parametrize(
"case_id",
[
"a_to_a",
"a_to_b",
"diff_acc_to_b",
"a_to_diff_acc",
"diff_acc_to_diff_acc",
],
)
@pytest.mark.parametrize("transfer_amount", [0, 1])
@pytest.mark.parametrize(
"receiver_account_type",
[
pytest.param(
ReceiverAccountType(balance=0, delegated=False),
id="empty_account",
),
pytest.param(
ReceiverAccountType(balance=1, delegated=False),
id="non_empty_account",
),
pytest.param(
ReceiverAccountType(balance=0, delegated=True),
id="delegated_account",
),
],
)
@pytest.mark.parametrize("warm_access", [False, True])
def test_ether_transfers(
benchmark_test: BenchmarkTestFiller,
pre: Alloc,
case_id: str,
receiver_account_type: ReceiverAccountType,
transfer_amount: int,
fork: Fork,
gas_benchmark_value: int,
warm_access: bool,
ether_transfer_case: Tuple[
Generator[Address, None, None], Generator[Address, None, None]
],
) -> None:
"""
Single test for ether transfer scenarios.
Scenarios:
- a_to_a: one sender → one sender
- a_to_b: one sender → one receiver
- diff_acc_to_b: multiple senders → one receiver
- a_to_diff_acc: one sender → multiple receivers
- diff_acc_to_diff_acc: multiple senders → multiple receivers
When warm_access is True, each transaction includes an access list
entry for the receiver to warm the account before the transfer.
"""
senders, receivers = ether_transfer_case
balance = receiver_account_type.balance
sends_value = transfer_amount > 0
distinct_receivers = case_id in ("a_to_diff_acc", "diff_acc_to_diff_acc")
if case_id == "a_to_a":
recipient_type = RecipientType.SELF
elif receiver_account_type.delegated:
recipient_type = RecipientType.DELEGATION_7702
else:
recipient_type = RecipientType.CONTRACT
warm_list = (
[AccessList(address=Address(0x100), storage_keys=[])]
if warm_access
else None
)
transfer_cost = fork.transaction_intrinsic_cost_calculator()(
access_list=warm_list,
sends_value=sends_value,
recipient_type=recipient_type,
) + fork.transaction_top_frame_execution_gas(
sends_value=sends_value,
recipient_type=recipient_type,
)
creates_account = (
sends_value
and balance == 0
and not receiver_account_type.delegated
and recipient_type != RecipientType.SELF
)
new_account_cost = (
fork.transaction_top_frame_state_gas(
sends_value=True,
recipient_type=RecipientType.EMPTY_ACCOUNT,
)
if creates_account
else 0
)
txs = []
token_transfers: dict[Address, int] = {}
gas_used = 0
tx_index = 0
while True:
creating = creates_account and (distinct_receivers or tx_index == 0)
tx_cost = transfer_cost + (new_account_cost if creating else 0)
if gas_used + tx_cost > gas_benchmark_value:
break
gas_used += tx_cost
receiver = next(receivers)
token_transfers[receiver] = (
token_transfers.get(receiver, 0) + transfer_amount
)
txs.append(
Transaction(
to=receiver,
value=transfer_amount,
gas_limit=tx_cost,
sender=next(senders),
access_list=(
[AccessList(address=receiver, storage_keys=[])]
if warm_access
else None
),
)
)
tx_index += 1
post_state = (
{}
if recipient_type == RecipientType.SELF
else {
receiver: Account(balance=balance + transferred_amount)
for receiver, transferred_amount in token_transfers.items()
if balance + transferred_amount > 0
}
)
benchmark_test(
pre=pre,
post=post_state,
blocks=[Block(txs=txs)],
expected_benchmark_gas_used=gas_used,
)
|