Skip to content

Test Eofcreate

Documentation for tests/prague/eip7692_eof_v1/eip7620_eof_create/test_eofcreate.py.

Generate fixtures for these test cases for Prague with:

Prague only:

fill -v tests/prague/eip7692_eof_v1/eip7620_eof_create/test_eofcreate.py --fork=Prague --evm-bin=/path/to/evm-tool-dev-version
For all forks up to and including Prague:
fill -v tests/prague/eip7692_eof_v1/eip7620_eof_create/test_eofcreate.py --until=Prague --evm-bin=/path/to/evm-tool-dev-version

Test good and bad EOFCREATE cases

test_simple_eofcreate(state_test, pre)

Verifies a simple EOFCREATE case

Source code in tests/prague/eip7692_eof_v1/eip7620_eof_create/test_eofcreate.py
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
def test_simple_eofcreate(
    state_test: StateTestFiller,
    pre: Alloc,
):
    """
    Verifies a simple EOFCREATE case
    """
    env = Environment()
    sender = pre.fund_eoa()
    contract_address = pre.deploy_contract(
        code=Container(
            sections=[
                Section.Code(
                    code=Op.SSTORE(0, Op.EOFCREATE[0](0, 0, 0, 0)) + Op.STOP,
                ),
                Section.Container(container=smallest_initcode_subcontainer),
            ],
            data=b"abcdef",
        ),
        storage={0: 0xB17D},  # a canary to be overwritten
    )
    # Storage in 0 should have the address,
    post = {
        contract_address: Account(
            storage={
                0: compute_eofcreate_address(contract_address, 0, smallest_initcode_subcontainer)
            }
        )
    }
    tx = Transaction(
        to=contract_address,
        gas_limit=10_000_000,
        gas_price=10,
        protected=False,
        sender=sender,
    )
    state_test(env=env, pre=pre, post=post, tx=tx)

test_eofcreate_then_call(state_test, pre)

Verifies a simple EOFCREATE case, and then calls the deployed contract

Source code in tests/prague/eip7692_eof_v1/eip7620_eof_create/test_eofcreate.py
 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
def test_eofcreate_then_call(
    state_test: StateTestFiller,
    pre: Alloc,
):
    """
    Verifies a simple EOFCREATE case, and then calls the deployed contract
    """
    env = Environment()
    callable_contract = Container(
        sections=[
            Section.Code(
                code=Op.SSTORE(slot_code_worked, value_code_worked) + Op.STOP,
            ),
        ]
    )
    callable_contract_initcode = Container(
        sections=[
            Section.Code(
                code=Op.RETURNCONTRACT[0](0, 0),
            ),
            Section.Container(container=callable_contract),
        ]
    )

    sender = pre.fund_eoa()
    contract_address = pre.deploy_contract(
        Container(
            sections=[
                Section.Code(
                    code=Op.SSTORE(slot_create_address, Op.EOFCREATE[0](0, 0, 0, 0))
                    + Op.EXTCALL(Op.SLOAD(slot_create_address), 0, 0, 0)
                    + Op.SSTORE(slot_code_worked, value_code_worked)
                    + Op.STOP,
                ),
                Section.Container(container=callable_contract_initcode),
            ],
        )
    )

    callable_address = compute_eofcreate_address(contract_address, 0, callable_contract_initcode)

    # Storage in 0 should have the address,
    #
    post = {
        contract_address: Account(
            storage={slot_create_address: callable_address, slot_code_worked: value_code_worked}
        ),
        callable_address: Account(storage={slot_code_worked: value_code_worked}),
    }

    tx = Transaction(
        to=contract_address,
        gas_limit=10_000_000,
        gas_price=10,
        protected=False,
        sender=sender,
    )
    state_test(env=env, pre=pre, post=post, tx=tx)

test_auxdata_variations(state_test, pre, auxdata_bytes)

Verifies that auxdata bytes are correctly handled in RETURNCONTRACT

Source code in tests/prague/eip7692_eof_v1/eip7620_eof_create/test_eofcreate.py
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
@pytest.mark.parametrize(
    "auxdata_bytes",
    [
        pytest.param(b"", id="zero"),
        pytest.param(b"aabbcc", id="short"),
        pytest.param(b"aabbccddeef", id="one_byte_short"),
        pytest.param(b"aabbccddeeff", id="exact"),
        pytest.param(b"aabbccddeeffg", id="one_byte_long"),
        pytest.param(b"aabbccddeeffgghhii", id="extra"),
    ],
)
def test_auxdata_variations(state_test: StateTestFiller, pre: Alloc, auxdata_bytes: bytes):
    """
    Verifies that auxdata bytes are correctly handled in RETURNCONTRACT
    """
    env = Environment()
    auxdata_size = len(auxdata_bytes)
    pre_deploy_header_data_size = 18
    pre_deploy_data = b"AABBCC"
    deploy_success = len(auxdata_bytes) + len(pre_deploy_data) >= pre_deploy_header_data_size

    runtime_subcontainer = Container(
        name="Runtime Subcontainer with truncated data",
        sections=[
            Section.Code(code=Op.STOP),
            Section.Data(data=pre_deploy_data, custom_size=pre_deploy_header_data_size),
        ],
    )

    initcode_subcontainer = Container(
        name="Initcode Subcontainer",
        sections=[
            Section.Code(
                code=Op.MSTORE(0, Op.PUSH32(auxdata_bytes.ljust(32, b"\0")))
                + Op.RETURNCONTRACT[0](0, auxdata_size),
            ),
            Section.Container(container=runtime_subcontainer),
        ],
    )

    sender = pre.fund_eoa()
    contract_address = pre.deploy_contract(
        code=Container(
            sections=[
                Section.Code(
                    code=Op.SSTORE(slot_create_address, Op.EOFCREATE[0](0, 0, 0, 0)) + Op.STOP,
                ),
                Section.Container(container=initcode_subcontainer),
            ]
        ),
        storage={slot_create_address: value_canary_to_be_overwritten},
    )

    # Storage in 0 should have the address,
    post = {
        contract_address: Account(
            storage={
                slot_create_address: compute_eofcreate_address(
                    contract_address, 0, initcode_subcontainer
                )
                if deploy_success
                else b"\0"
            }
        )
    }

    tx = Transaction(
        to=contract_address,
        gas_limit=10_000_000,
        gas_price=10,
        protected=False,
        sender=sender,
    )

    state_test(env=env, pre=pre, post=post, tx=tx)

test_calldata(state_test, pre)

Verifies CALLDATA passing through EOFCREATE

Source code in tests/prague/eip7692_eof_v1/eip7620_eof_create/test_eofcreate.py
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
def test_calldata(state_test: StateTestFiller, pre: Alloc):
    """
    Verifies CALLDATA passing through EOFCREATE
    """
    env = Environment()

    initcode_subcontainer = Container(
        name="Initcode Subcontainer",
        sections=[
            Section.Code(
                code=Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE)
                + Op.SSTORE(slot_calldata, Op.MLOAD(0))
                + Op.RETURNCONTRACT[0](0, Op.CALLDATASIZE),
            ),
            Section.Container(container=smallest_runtime_subcontainer),
        ],
    )

    calldata_size = 32
    calldata = b"\x45" * calldata_size
    sender = pre.fund_eoa()
    contract_address = pre.deploy_contract(
        code=Container(
            sections=[
                Section.Code(
                    code=Op.MSTORE(0, Op.PUSH32(calldata))
                    + Op.SSTORE(slot_create_address, Op.EOFCREATE[0](0, 0, 0, calldata_size))
                    + Op.STOP,
                ),
                Section.Container(container=initcode_subcontainer),
            ]
        )
    )

    # deployed contract is smallest plus data
    deployed_contract = Container(
        name="deployed contract",
        sections=[
            *smallest_runtime_subcontainer.sections,
            Section.Data(data=calldata),
        ],
    )
    # factory contract Storage in 0 should have the created address,
    # created contract storage in 0 should have the calldata
    created_address = compute_eofcreate_address(contract_address, 0, initcode_subcontainer)
    post = {
        contract_address: Account(storage={slot_create_address: created_address}),
        created_address: Account(code=deployed_contract, storage={slot_calldata: calldata}),
    }

    tx = Transaction(
        to=contract_address,
        gas_limit=10_000_000,
        gas_price=10,
        protected=False,
        sender=sender,
    )

    state_test(env=env, pre=pre, post=post, tx=tx)

test_eofcreate_in_initcode(state_test, pre)

Verifies an EOFCREATE occuring within initcode creates that contract

Source code in tests/prague/eip7692_eof_v1/eip7620_eof_create/test_eofcreate.py
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
def test_eofcreate_in_initcode(
    state_test: StateTestFiller,
    pre: Alloc,
):
    """
    Verifies an EOFCREATE occuring within initcode creates that contract
    """
    nested_initcode_subcontainer = Container(
        sections=[
            Section.Code(
                code=Op.SSTORE(slot_create_address, Op.EOFCREATE[0](0, 0, 0, 0))
                + Op.SSTORE(slot_code_worked, value_code_worked)
                + Op.RETURNCONTRACT[1](0, 0),
            ),
            Section.Container(container=smallest_initcode_subcontainer),
            Section.Container(container=smallest_runtime_subcontainer),
        ]
    )

    env = Environment()
    sender = pre.fund_eoa()
    contract_address = pre.deploy_contract(
        code=Container(
            sections=[
                Section.Code(
                    code=Op.SSTORE(slot_create_address, Op.EOFCREATE[0](0, 0, 0, 0))
                    + Op.SSTORE(slot_code_worked, value_code_worked)
                    + Op.STOP,
                ),
                Section.Container(container=nested_initcode_subcontainer),
            ]
        )
    )

    outer_address = compute_eofcreate_address(contract_address, 0, nested_initcode_subcontainer)
    inner_address = compute_eofcreate_address(outer_address, 0, smallest_initcode_subcontainer)
    post = {
        contract_address: Account(
            storage={slot_create_address: outer_address, slot_code_worked: value_code_worked}
        ),
        outer_address: Account(
            storage={slot_create_address: inner_address, slot_code_worked: value_code_worked}
        ),
    }

    tx = Transaction(
        to=contract_address,
        gas_limit=10_000_000,
        gas_price=10,
        protected=False,
        sender=sender,
    )

    state_test(env=env, pre=pre, post=post, tx=tx)

test_eofcreate_in_initcode_reverts(state_test, pre)

Verifies an EOFCREATE occuring in an initcode is rolled back when the initcode reverts

Source code in tests/prague/eip7692_eof_v1/eip7620_eof_create/test_eofcreate.py
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
def test_eofcreate_in_initcode_reverts(
    state_test: StateTestFiller,
    pre: Alloc,
):
    """
    Verifies an EOFCREATE occuring in an initcode is rolled back when the initcode reverts
    """
    nested_initcode_subcontainer = Container(
        sections=[
            Section.Code(
                code=Op.SSTORE(slot_create_address, Op.EOFCREATE[0](0, 0, 0, 0))
                + Op.SSTORE(slot_code_worked, value_code_worked)
                + Op.REVERT(0, 0),
            ),
            Section.Container(container=smallest_initcode_subcontainer),
        ]
    )

    env = Environment()
    sender = pre.fund_eoa()
    contract_address = pre.deploy_contract(
        code=Container(
            sections=[
                Section.Code(
                    code=Op.SSTORE(slot_create_address, Op.EOFCREATE[0](0, 0, 0, 0))
                    + Op.SSTORE(slot_code_worked, value_code_worked)
                    + Op.STOP,
                ),
                Section.Container(container=nested_initcode_subcontainer),
            ]
        ),
        storage={slot_create_address: value_canary_to_be_overwritten},
    )

    outer_address = compute_eofcreate_address(contract_address, 0, nested_initcode_subcontainer)
    inner_address = compute_eofcreate_address(outer_address, 0, smallest_initcode_subcontainer)
    post = {
        contract_address: Account(
            storage={
                slot_create_address: 0,
                slot_code_worked: value_code_worked,
            }
        ),
        outer_address: Account.NONEXISTENT,
        inner_address: Account.NONEXISTENT,
    }
    tx = Transaction(
        to=contract_address,
        gas_limit=10_000_000,
        gas_price=10,
        protected=False,
        sender=sender,
    )
    state_test(env=env, pre=pre, post=post, tx=tx)

test_return_data_cleared(state_test, pre)

Verifies the return data is not re-used from a extcall but is cleared upon eofcreate

Source code in tests/prague/eip7692_eof_v1/eip7620_eof_create/test_eofcreate.py
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
def test_return_data_cleared(
    state_test: StateTestFiller,
    pre: Alloc,
):
    """
    Verifies the return data is not re-used from a extcall but is cleared upon eofcreate
    """
    env = Environment()
    value_return_canary = 0x4158675309
    value_return_canary_size = 5
    callable_address = pre.deploy_contract(
        code=Container(
            sections=[
                Section.Code(
                    code=Op.MSTORE(0, value_return_canary)
                    + Op.RETURN(0, value_return_canary_size),
                )
            ]
        )
    )

    slot_returndata_size_2 = slot_last_slot * 2 + slot_returndata_size
    sender = pre.fund_eoa()
    contract_address = pre.deploy_contract(
        code=Container(
            sections=[
                Section.Code(
                    code=Op.SSTORE(slot_call_result, Op.EXTCALL(callable_address, 0, 0, 0))
                    + Op.SSTORE(slot_returndata_size, Op.RETURNDATASIZE)
                    + Op.SSTORE(slot_create_address, Op.EOFCREATE[0](0, 0, 0, 0))
                    + Op.SSTORE(slot_returndata_size_2, Op.RETURNDATASIZE)
                    + Op.SSTORE(slot_code_worked, value_code_worked)
                    + Op.STOP,
                ),
                Section.Container(container=smallest_initcode_subcontainer),
            ],
        )
    )

    new_contract_address = compute_eofcreate_address(
        contract_address, 0, smallest_initcode_subcontainer
    )
    post = {
        contract_address: Account(
            storage={
                slot_call_result: value_call_result_success,
                slot_returndata_size: value_return_canary_size,
                slot_create_address: new_contract_address,
                slot_returndata_size_2: 0,
                slot_code_worked: value_code_worked,
            },
            nonce=2,
        ),
        callable_address: Account(nonce=1),
        new_contract_address: Account(nonce=1),
    }
    tx = Transaction(
        to=contract_address,
        gas_limit=10_000_000,
        gas_price=10,
        protected=False,
        sender=sender,
    )

    state_test(env=env, pre=pre, post=post, tx=tx)

test_address_collision(state_test, pre)

Verifies a simple EOFCREATE case

Source code in tests/prague/eip7692_eof_v1/eip7620_eof_create/test_eofcreate.py
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
def test_address_collision(
    state_test: StateTestFiller,
    pre: Alloc,
):
    """
    Verifies a simple EOFCREATE case
    """
    env = Environment()

    slot_create_address_2 = slot_last_slot * 2 + slot_create_address
    slot_create_address_3 = slot_last_slot * 3 + slot_create_address
    sender = pre.fund_eoa()
    contract_address = pre.deploy_contract(
        code=Container(
            sections=[
                Section.Code(
                    code=Op.SSTORE(slot_create_address, Op.EOFCREATE[0](0, 0, 0, 0))
                    + Op.SSTORE(slot_create_address_2, Op.EOFCREATE[0](0, 0, 0, 0))
                    + Op.SSTORE(slot_create_address_3, Op.EOFCREATE[0](0, 1, 0, 0))
                    + Op.SSTORE(slot_code_worked, value_code_worked)
                    + Op.STOP,
                ),
                Section.Container(container=smallest_initcode_subcontainer),
            ],
        )
    )
    salt_zero_address = compute_eofcreate_address(
        contract_address, 0, smallest_initcode_subcontainer
    )
    salt_one_address = compute_eofcreate_address(
        contract_address, 1, smallest_initcode_subcontainer
    )

    # Hard-code address for collision, no other way to do this.
    # We should mark tests that do this, and fail on unmarked tests.
    pre[salt_one_address] = Account(balance=1, nonce=1)

    post = {
        contract_address: Account(
            storage={
                slot_create_address: salt_zero_address,
                slot_create_address_2: value_create_failed,  # had an in-transaction collision
                slot_create_address_3: value_create_failed,  # had a pre-existing collision
                slot_code_worked: value_code_worked,
            }
        )
    }

    # Multiple create fails is expensive, use an absurd amount of gas
    tx = Transaction(
        to=contract_address,
        gas_limit=300_000_000_000,
        gas_price=10,
        protected=False,
        sender=sender,
    )
    state_test(env=env, pre=pre, post=post, tx=tx)

test_eofcreate_revert_eof_returndata(state_test, pre)

Verifies the return data is not being deployed, even if happens to be valid EOF

Source code in tests/prague/eip7692_eof_v1/eip7620_eof_create/test_eofcreate.py
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
def test_eofcreate_revert_eof_returndata(
    state_test: StateTestFiller,
    pre: Alloc,
):
    """
    Verifies the return data is not being deployed, even if happens to be valid EOF
    """
    env = Environment()
    code_reverts_with_calldata = Container(
        name="Initcode Subcontainer reverting with its calldata",
        sections=[
            Section.Code(
                code=Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE) + Op.REVERT(0, Op.CALLDATASIZE),
            ),
        ],
    )

    sender = pre.fund_eoa()
    salt = 0
    contract_address = pre.deploy_contract(
        code=Container(
            sections=[
                Section.Code(
                    code=Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE)
                    + Op.SSTORE(slot_create_address, Op.EOFCREATE[0](0, salt, 0, Op.CALLDATASIZE))
                    + Op.SSTORE(slot_returndata_size, Op.RETURNDATASIZE)
                    + Op.STOP,
                ),
                Section.Container(container=code_reverts_with_calldata),
            ],
        ),
        storage={slot_create_address: value_canary_to_be_overwritten},
    )
    eof_create_address = compute_eofcreate_address(
        contract_address, salt, code_reverts_with_calldata
    )

    post = {
        contract_address: Account(
            storage={
                slot_create_address: 0,
                slot_returndata_size: len(smallest_runtime_subcontainer),
            },
        ),
        eof_create_address: Account.NONEXISTENT,
    }

    tx = Transaction(
        to=contract_address,
        gas_limit=1_000_000,
        sender=sender,
        # Simplest possible valid EOF container, which is going to be
        # revert-returned from initcode and must not end up being deployed.
        data=smallest_runtime_subcontainer,
    )

    state_test(env=env, pre=pre, post=post, tx=tx)