forked from ethereum/execution-specs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate_test.py.j2
More file actions
412 lines (381 loc) · 12.4 KB
/
state_test.py.j2
File metadata and controls
412 lines (381 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
{#-
Jinja2 template for generating Python test files from static fillers.
Parametrize approach: always (d, g, v).
Transaction fields are looked up from module-level arrays:
tx_data[d], tx_gas[g], tx_value[v]
Post-state and exceptions are resolved at runtime:
resolve_expect_post(expect_entries_, d, g, v, fork)
Context variables (from IntermediateTestModel via render.py):
docstring str Module docstring (from _info.comment)
filler_path str Relative path to filler file
test_name str Python function name (test_xxx)
short_docstring str One-line docstring for the function
valid_from str Fork name (e.g., "Cancun")
valid_until str|None Fork name or None
is_slow bool Mark @pytest.mark.slow
is_multi_case bool len(parameters) > 1
has_exceptions bool Any case has an exception
needs_op bool Import Op
needs_tx_exception bool Import TransactionException
needs_access_list bool Import AccessList
needs_hash bool Import Hash
env EnvironmentIR
accounts list[AccountIR]
transaction TransactionIR
-- Module-level arrays (from filler transaction) --
tx_data list[str] Compiled data hex per d index
tx_gas list[int] Gas limit per g index
tx_value list[int] Value per v index
-- Expect entries (from filler expect sections) --
expect_entries list[ExpectEntryIR]
Each has: .indexes (dict), .network (list[str]),
.result (dict: var_name -> Account assertion),
.expect_exception (dict | None)
Resolved from expect.result.resolve(tags).
Used at runtime by resolve_expect_post().
-- Parametrize (d, g, v) combos --
parameters list[ParameterCaseIR]
Each has: .d, .g, .v, .has_exception, .label
Built from data x gasLimit x value matrix.
-- Single-case data (when not is_multi_case) --
single_post str|None Formatted post dict, or None if empty
single_error str|None Formatted error, or None
-- Sender --
sender SenderIR .is_tagged, .key, .balance
address_constants list Non-tagged address variables
[{var_name, hex}]
Custom filters:
format_int(v) Hex vs decimal heuristic
format_storage(d) Format {slot: value} dict
format_account(a) Format Account(storage=..., code=...)
format_post(post) Format post dict {addr: Account(...)}
wrap_op_chain(s, ind) Split Op chain at + for 79-char lines
-#}
"""
{{ docstring }}
Ported from:
{{ filler_path }}
"""
import pytest
from execution_testing import (
EOA,
Account,
Address,
Alloc,
{% if needs_bytes %}
Bytes,
{% endif %}
Environment,
StateTestFiller,
Transaction,
{% if needs_tx_exception %}
TransactionException,
{% endif %}
{% if needs_access_list %}
AccessList,
{% endif %}
{% if needs_hash %}
Hash,
{% endif %}
{% if needs_storage_any %}
Storage,
{% endif %}
{% if needs_compute_create_address %}
compute_create_address,
{% endif %}
)
{% if needs_op %}
from execution_testing.vm import Op
{% endif %}
{% if is_multi_case %}
from execution_testing.forks import Fork
from execution_testing.specs.static_state.expect_section import (
resolve_expect_post,
)
{% elif is_fork_dependent %}
from execution_testing.forks import Fork
from execution_testing.specs.static_state.expect_section import (
resolve_expect_post_fork,
)
{% endif %}
REFERENCE_SPEC_GIT_PATH = "N/A"
REFERENCE_SPEC_VERSION = "N/A"
{% if needs_storage_any %}
def _storage_with_any(base: dict, any_keys: list) -> Storage:
"""Create Storage with set_expect_any for specified keys."""
s = Storage(base)
for k in any_keys:
s.set_expect_any(k)
return s
{% endif %}
@pytest.mark.ported_from(
["{{ filler_path }}"],
)
@pytest.mark.valid_from("{{ valid_from }}")
{% if valid_until %}
@pytest.mark.valid_until("{{ valid_until }}")
{% endif %}
{% if is_slow %}
@pytest.mark.slow
{% endif %}
{% if is_multi_case %}
@pytest.mark.parametrize(
"d, g, v",
[
{% for case in parameters %}
pytest.param(
{{ case.d }}, {{ case.g }}, {{ case.v }},
id="{{ case.id }}",
{% if case.marks %}
marks={{ case.marks }},
{% endif %}
),
{% endfor %}
],
)
{% endif %}
{% if has_exceptions and not is_multi_case %}
@pytest.mark.exception_test
{% endif %}
{% if needs_mutable_pre %}
@pytest.mark.pre_alloc_mutable
{% endif %}
def {{ test_name }}(
state_test: StateTestFiller,
pre: Alloc,
{% if is_multi_case %}
fork: Fork,
d: int,
g: int,
v: int,
{% elif is_fork_dependent %}
fork: Fork,
{% endif %}
) -> None:
"""{{ short_docstring }}"""
{% for addr in address_constants %}
{{ addr.var_name }} = Address({{ addr.hex }})
{% endfor %}
{% if sender.use_dynamic %}
{% if sender.nonce %}
sender = pre.fund_eoa(amount={{ sender.balance | format_int }}, nonce={{ sender.nonce }})
{% else %}
sender = pre.fund_eoa(amount={{ sender.balance | format_int }})
{% endif %}
{% elif sender.not_in_pre %}
sender = pre.fund_eoa(amount=0)
{% else %}
sender = EOA(
key={{ sender.key | format_int }}
)
{% endif %}
env = Environment(
fee_recipient={{ env.coinbase_var }},
number={{ env.number }},
timestamp={{ env.timestamp }},
{% if env.prev_randao is not none %}
prev_randao={{ env.prev_randao | format_int }},
{% endif %}
{% if env.difficulty is not none and env.prev_randao is none %}
difficulty={{ env.difficulty | format_int }},
{% endif %}
{% if env.base_fee_per_gas is not none %}
base_fee_per_gas={{ env.base_fee_per_gas }},
{% endif %}
{% if env.excess_blob_gas is not none %}
excess_blob_gas={{ env.excess_blob_gas | format_int }},
{% endif %}
gas_limit={{ env.gas_limit }},
)
{# Pre-state account setup #}
{% for account in accounts %}
{% if account.is_sender and sender.use_dynamic %}
{# Sender balance already set via pre.fund_eoa() above #}
{% elif account.is_sender %}
pre[sender] = Account(balance={{ account.balance | format_int }}{{ ", nonce=%d" | format(account.nonce) if account.nonce }}{{ ", storage=%s" | format(account.storage | format_storage) if account.storage }}{{ ", code=%s" | format(account.code_expr | wrap_op_chain(indent=8)) if account.code_expr }})
{% elif account.is_eoa and account.use_dynamic %}
{{ account.var_name }} = pre.fund_eoa(amount={{ account.balance | format_int }}) # noqa: F841
{% elif account.is_eoa %}
pre[{{ account.var_name }}] = Account(balance={{ account.balance | format_int }}{{ ", nonce=%d" | format(account.nonce) if account.nonce }}{{ ", storage=%s" | format(account.storage | format_storage) if account.storage }}{{ ", code=%s" | format(account.code_expr | wrap_op_chain(indent=8)) if account.code_expr }})
{% else %}
{# Contract: source comment + deploy #}
{{ account.source_comment }}
{% if account.oversized_code %}
{{ account.var_name }} = Address({{ account.address }}) # oversized contract
pre[{{ account.var_name }}] = Account(
code={{ account.code_expr | wrap_op_chain(indent=8) }},
{% if account.storage %}
storage={{ account.storage | format_storage }},
{% endif %}
{% if account.balance %}
balance={{ account.balance | format_int }},
{% endif %}
{% if account.nonce is not none %}
nonce={{ account.nonce }},
{% endif %}
)
{% else %}
{{ account.var_name }} = pre.deploy_contract(
code={{ account.code_expr | wrap_op_chain(indent=8) }},
{% if account.storage %}
storage={{ account.storage | format_storage }},
{% endif %}
{% if account.balance %}
balance={{ account.balance | format_int }},
{% endif %}
{% if account.nonce is not none %}
nonce={{ account.nonce }},
{% endif %}
{% if not account.use_dynamic %}
address=Address({{ account.address }}), # noqa: E501
{% endif %}
)
{% endif %}
{% endif %}
{% endfor %}
{# Expect entries (used by resolve_expect_post at runtime) #}
{% if single_post %}
{# Post will be assigned below #}
{% elif is_multi_case or is_fork_dependent %}
expect_entries_: list[dict] = [
{% for entry in expect_entries %}
{
{% if is_multi_case %}
"indexes": {{ entry.indexes }},
{% endif %}
"network": {{ entry.network }},
"result": {{ entry.result | format_post }},
{% if entry.expect_exception %}
"expect_exception": {{ entry.expect_exception | format_expect_exception }},
{% endif %}
},
{% endfor %}
]
{% if is_multi_case %}
post, _exc = resolve_expect_post(expect_entries_, d, g, v, fork)
{% else %}
post, _exc = resolve_expect_post_fork(expect_entries_, fork)
{% endif %}
{% endif %}
{% if is_multi_case %}
{# Case value arrays: tx fields indexed by d, g, v #}
tx_data = [
{% for d in tx_data %}
{{ d }},
{% endfor %}
]
tx_gas = [{{ tx_gas | join(", ") }}]
{% if tx_value != [0] %}
tx_value = [{{ tx_value | join(", ") }}]
{% endif %}
{% if tx.per_data_access_lists %}
tx_access_lists: dict[int, list] = {
{% for d_idx, al_entries in tx.per_data_access_lists.items() %}
{{ d_idx }}: [
{% for al in al_entries %}
AccessList(
{% if al.use_dynamic %}
address={{ al.address }},
{% else %}
address=Address({{ al.address }}),
{% endif %}
storage_keys=[
{% for sk in al.storage_keys %}
Hash("{{ sk }}"), # noqa: E501
{% endfor %}
],
),
{% endfor %}
],
{% endfor %}
}
{% endif %}
{% endif %}
{# Transaction #}
tx = Transaction(
sender=sender,
{% if tx.to_var is not none %}
to={{ tx.to_var }},
{% elif tx.to_is_none %}
to=None,
{% endif %}
{% if is_multi_case %}
data=tx_data[d],
gas_limit=tx_gas[g],
{% if tx_value != [0] %}
value=tx_value[v],
{% endif %}
{% else %}
{% if tx.data_inline and tx.data_inline != "b''" %}
data={{ tx.data_inline }},
{% endif %}
{% if tx.gas_limit is not none and tx.gas_limit != 21000 %}
gas_limit={{ tx.gas_limit }},
{% endif %}
{% if tx.value %}
value={{ tx.value | format_int }},
{% endif %}
{% endif %}
{% if tx.max_fee_per_gas is not none %}
max_fee_per_gas={{ tx.max_fee_per_gas }},
{% endif %}
{% if tx.max_priority_fee_per_gas is not none %}
max_priority_fee_per_gas={{ tx.max_priority_fee_per_gas }},
{% endif %}
{% if tx.nonce is not none and tx.nonce != 0 %}
nonce={{ tx.nonce }},
{% endif %}
{% if tx.gas_price is not none and tx.gas_price != 10 %}
gas_price={{ tx.gas_price }},
{% endif %}
{% if tx.max_fee_per_blob_gas is not none %}
max_fee_per_blob_gas={{ tx.max_fee_per_blob_gas | format_int }},
{% endif %}
{% if tx.blob_versioned_hashes is not none %}
blob_versioned_hashes=[
{% for h in tx.blob_versioned_hashes %}
Hash(
"{{ h }}" # noqa: E501
),
{% endfor %}
],
{% endif %}
{% if tx.per_data_access_lists and is_multi_case %}
access_list=tx_access_lists.get(d),
{% elif tx.access_list is not none %}
access_list=[
{% for al in tx.access_list %}
AccessList(
{% if al.use_dynamic %}
address={{ al.address }},
{% else %}
address=Address({{ al.address }}),
{% endif %}
storage_keys=[
{% for sk in al.storage_keys %}
Hash(
"{{ sk }}" # noqa: E501
),
{% endfor %}
],
),
{% endfor %}
],
{% endif %}
{% if single_error %}
error={{ single_error }},
{% elif single_post %}
{# Single post without error #}
{% elif is_multi_case or is_fork_dependent %}
error=_exc,
{% endif %}
)
{# Post-state #}
{% if single_post %}
post = {{ single_post | format_post }}
{% elif is_multi_case or is_fork_dependent %}
{# Post resolved above via resolve_expect_post / resolve_expect_post_fork #}
{% else %}
post: dict = {}
{% endif %}
state_test(env=env, pre=pre, post=post, tx=tx)