ethereum.forks.prague.vm.instructions.system

Ethereum Virtual Machine (EVM) System Instructions.

.. contents:: Table of Contents :backlinks: none :local:

Introduction

Implementations of the EVM system related instructions.

generic_create

Core logic used by the CREATE* family of opcodes.

def generic_create(evm: Evm, ​​endowment: U256, ​​contract_address: Address, ​​memory_start_position: U256, ​​memory_size: U256) -> None:
63
    <snip>
66
    # This import causes a circular import error
67
    # if it's not moved inside this method
68
    from ...vm.interpreter import (
69
        MAX_INIT_CODE_SIZE,
70
        STACK_DEPTH_LIMIT,
71
        process_create_message,
72
    )
73
74
    call_data = memory_read_bytes(
75
        evm.memory, memory_start_position, memory_size
76
    )
77
    if len(call_data) > MAX_INIT_CODE_SIZE:
78
        raise OutOfGasError
79
80
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
81
    evm.gas_left -= create_message_gas
82
    if evm.message.is_static:
83
        raise WriteInStaticContext
84
    evm.return_data = b""
85
86
    sender_address = evm.message.current_target
87
    sender = get_account(evm.message.tx_env.state, sender_address)
88
89
    if (
90
        sender.balance < endowment
91
        or sender.nonce == Uint(2**64 - 1)
92
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
93
    ):
94
        evm.gas_left += create_message_gas
95
        push(evm.stack, U256(0))
96
        return
97
98
    evm.accessed_addresses.add(contract_address)
99
100
    if not account_deployable(evm.message.tx_env.state, contract_address):
101
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
102
        push(evm.stack, U256(0))
103
        return
104
105
    increment_nonce(evm.message.tx_env.state, evm.message.current_target)
106
107
    child_message = Message(
108
        block_env=evm.message.block_env,
109
        tx_env=evm.message.tx_env,
110
        caller=evm.message.current_target,
111
        target=Bytes0(),
112
        gas=create_message_gas,
113
        value=endowment,
114
        data=b"",
115
        code=call_data,
116
        current_target=contract_address,
117
        depth=evm.message.depth + Uint(1),
118
        code_address=None,
119
        should_transfer_value=True,
120
        is_static=False,
121
        accessed_addresses=evm.accessed_addresses.copy(),
122
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
123
        disable_precompiles=False,
124
        parent_evm=evm,
125
    )
126
    child_evm = process_create_message(child_message)
127
128
    if child_evm.error:
129
        incorporate_child_on_error(evm, child_evm)
130
        evm.return_data = child_evm.output
131
        push(evm.stack, U256(0))
132
    else:
133
        incorporate_child_on_success(evm, child_evm)
134
        evm.return_data = b""
135
        push(evm.stack, U256.from_be_bytes(child_evm.message.current_target))

create

Creates a new account with associated code.

Parameters

evm : The current EVM frame.

def create(evm: Evm) -> None:
139
    <snip>
148
    # STACK
149
    endowment = pop(evm.stack)
150
    memory_start_position = pop(evm.stack)
151
    memory_size = pop(evm.stack)
152
153
    # GAS
154
    extend_memory = calculate_gas_extend_memory(
155
        evm.memory, [(memory_start_position, memory_size)]
156
    )
157
    init_code_gas = init_code_cost(Uint(memory_size))
158
159
    charge_gas(
160
        evm, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost + init_code_gas
161
    )
162
163
    # OPERATION
164
    evm.memory += b"\x00" * extend_memory.expand_by
165
    contract_address = compute_contract_address(
166
        evm.message.current_target,
167
        get_account(
168
            evm.message.tx_env.state, evm.message.current_target
169
        ).nonce,
170
    )
171
172
    generic_create(
173
        evm,
174
        endowment,
175
        contract_address,
176
        memory_start_position,
177
        memory_size,
178
    )
179
180
    # PROGRAM COUNTER
181
    evm.pc += Uint(1)

create2

Creates a new account with associated code.

It's similar to the CREATE opcode except that the address of the new account depends on the init_code instead of the nonce of sender.

Parameters

evm : The current EVM frame.

def create2(evm: Evm) -> None:
185
    <snip>
197
    # STACK
198
    endowment = pop(evm.stack)
199
    memory_start_position = pop(evm.stack)
200
    memory_size = pop(evm.stack)
201
    salt = pop(evm.stack).to_be_bytes32()
202
203
    # GAS
204
    extend_memory = calculate_gas_extend_memory(
205
        evm.memory, [(memory_start_position, memory_size)]
206
    )
207
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
208
    init_code_gas = init_code_cost(Uint(memory_size))
209
    charge_gas(
210
        evm,
211
        GasCosts.OPCODE_CREATE_BASE
212
        + GasCosts.OPCODE_KECCAK256_PER_WORD * call_data_words
213
        + extend_memory.cost
214
        + init_code_gas,
215
    )
216
217
    # OPERATION
218
    evm.memory += b"\x00" * extend_memory.expand_by
219
    contract_address = compute_create2_contract_address(
220
        evm.message.current_target,
221
        salt,
222
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
223
    )
224
225
    generic_create(
226
        evm,
227
        endowment,
228
        contract_address,
229
        memory_start_position,
230
        memory_size,
231
    )
232
233
    # PROGRAM COUNTER
234
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
238
    <snip>
247
    # STACK
248
    memory_start_position = pop(evm.stack)
249
    memory_size = pop(evm.stack)
250
251
    # GAS
252
    extend_memory = calculate_gas_extend_memory(
253
        evm.memory, [(memory_start_position, memory_size)]
254
    )
255
256
    charge_gas(evm, GasCosts.ZERO + extend_memory.cost)
257
258
    # OPERATION
259
    evm.memory += b"\x00" * extend_memory.expand_by
260
    evm.output = memory_read_bytes(
261
        evm.memory, memory_start_position, memory_size
262
    )
263
264
    evm.running = False
265
266
    # PROGRAM COUNTER
267
    pass

GenericCall

Parameters for the core logic of the CALL* family of opcodes.

270
@final
271
@dataclass
class GenericCall:

gas

277
    gas: Uint

value

278
    value: U256

caller

279
    caller: Address

to

280
    to: Address

code_address

281
    code_address: Address

should_transfer_value

282
    should_transfer_value: bool

is_staticcall

283
    is_staticcall: bool

memory_input_start_position

284
    memory_input_start_position: U256

memory_input_size

285
    memory_input_size: U256

memory_output_start_position

286
    memory_output_start_position: U256

memory_output_size

287
    memory_output_size: U256

code

288
    code: Bytes

disable_precompiles

289
    disable_precompiles: bool

generic_call

Perform the core logic of the CALL* family of opcodes.

def generic_call(evm: Evm, ​​params: GenericCall) -> None:
293
    <snip>
296
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
297
298
    evm.return_data = b""
299
300
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
301
        evm.gas_left += params.gas
302
        push(evm.stack, U256(0))
303
        return
304
305
    call_data = memory_read_bytes(
306
        evm.memory,
307
        params.memory_input_start_position,
308
        params.memory_input_size,
309
    )
310
311
    child_message = Message(
312
        block_env=evm.message.block_env,
313
        tx_env=evm.message.tx_env,
314
        caller=params.caller,
315
        target=params.to,
316
        gas=params.gas,
317
        value=params.value,
318
        data=call_data,
319
        code=params.code,
320
        current_target=params.to,
321
        depth=evm.message.depth + Uint(1),
322
        code_address=params.code_address,
323
        should_transfer_value=params.should_transfer_value,
324
        is_static=params.is_staticcall or evm.message.is_static,
325
        accessed_addresses=evm.accessed_addresses.copy(),
326
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
327
        disable_precompiles=params.disable_precompiles,
328
        parent_evm=evm,
329
    )
330
    child_evm = process_message(child_message)
331
332
    if child_evm.error:
333
        incorporate_child_on_error(evm, child_evm)
334
        evm.return_data = child_evm.output
335
        push(evm.stack, U256(0))
336
    else:
337
        incorporate_child_on_success(evm, child_evm)
338
        evm.return_data = child_evm.output
339
        push(evm.stack, U256(1))
340
341
    actual_output_size = min(
342
        params.memory_output_size, U256(len(child_evm.output))
343
    )
344
    memory_write(
345
        evm.memory,
346
        params.memory_output_start_position,
347
        child_evm.output[:actual_output_size],
348
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
352
    <snip>
361
    # STACK
362
    gas = Uint(pop(evm.stack))
363
    to = to_address_masked(pop(evm.stack))
364
    value = pop(evm.stack)
365
    memory_input_start_position = pop(evm.stack)
366
    memory_input_size = pop(evm.stack)
367
    memory_output_start_position = pop(evm.stack)
368
    memory_output_size = pop(evm.stack)
369
370
    # GAS
371
    extend_memory = calculate_gas_extend_memory(
372
        evm.memory,
373
        [
374
            (memory_input_start_position, memory_input_size),
375
            (memory_output_start_position, memory_output_size),
376
        ],
377
    )
378
379
    if to in evm.accessed_addresses:
380
        access_gas_cost = GasCosts.WARM_ACCESS
381
    else:
382
        evm.accessed_addresses.add(to)
383
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
384
385
    code_address = to
386
    (
387
        disable_precompiles,
388
        code_address,
389
        code,
390
        delegated_access_gas_cost,
391
    ) = access_delegation(evm, code_address)
392
    access_gas_cost += delegated_access_gas_cost
393
394
    create_gas_cost = GasCosts.NEW_ACCOUNT
395
    if value == 0 or is_account_alive(evm.message.tx_env.state, to):
396
        create_gas_cost = Uint(0)
397
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
398
    message_call_gas = calculate_message_call_gas(
399
        value,
400
        gas,
401
        Uint(evm.gas_left),
402
        memory_cost=extend_memory.cost,
403
        extra_gas=access_gas_cost + create_gas_cost + transfer_gas_cost,
404
    )
405
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
406
    if evm.message.is_static and value != U256(0):
407
        raise WriteInStaticContext
408
    evm.memory += b"\x00" * extend_memory.expand_by
409
    sender_balance = get_account(
410
        evm.message.tx_env.state, evm.message.current_target
411
    ).balance
412
    if sender_balance < value:
413
        push(evm.stack, U256(0))
414
        evm.return_data = b""
415
        evm.gas_left += message_call_gas.sub_call
416
    else:
417
        generic_call(
418
            evm,
419
            GenericCall(
420
                gas=message_call_gas.sub_call,
421
                value=value,
422
                caller=evm.message.current_target,
423
                to=to,
424
                code_address=code_address,
425
                should_transfer_value=True,
426
                is_staticcall=False,
427
                memory_input_start_position=memory_input_start_position,
428
                memory_input_size=memory_input_size,
429
                memory_output_start_position=memory_output_start_position,
430
                memory_output_size=memory_output_size,
431
                code=code,
432
                disable_precompiles=disable_precompiles,
433
            ),
434
        )
435
436
    # PROGRAM COUNTER
437
    evm.pc += Uint(1)

callcode

Message-call into this account with alternative account’s code.

Parameters

evm : The current EVM frame.

def callcode(evm: Evm) -> None:
441
    <snip>
450
    # STACK
451
    gas = Uint(pop(evm.stack))
452
    code_address = to_address_masked(pop(evm.stack))
453
    value = pop(evm.stack)
454
    memory_input_start_position = pop(evm.stack)
455
    memory_input_size = pop(evm.stack)
456
    memory_output_start_position = pop(evm.stack)
457
    memory_output_size = pop(evm.stack)
458
459
    # GAS
460
    to = evm.message.current_target
461
462
    extend_memory = calculate_gas_extend_memory(
463
        evm.memory,
464
        [
465
            (memory_input_start_position, memory_input_size),
466
            (memory_output_start_position, memory_output_size),
467
        ],
468
    )
469
470
    if code_address in evm.accessed_addresses:
471
        access_gas_cost = GasCosts.WARM_ACCESS
472
    else:
473
        evm.accessed_addresses.add(code_address)
474
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
475
476
    (
477
        disable_precompiles,
478
        code_address,
479
        code,
480
        delegated_access_gas_cost,
481
    ) = access_delegation(evm, code_address)
482
    access_gas_cost += delegated_access_gas_cost
483
484
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
485
    message_call_gas = calculate_message_call_gas(
486
        value,
487
        gas,
488
        Uint(evm.gas_left),
489
        extend_memory.cost,
490
        access_gas_cost + transfer_gas_cost,
491
    )
492
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
493
494
    # OPERATION
495
    evm.memory += b"\x00" * extend_memory.expand_by
496
    sender_balance = get_account(
497
        evm.message.tx_env.state, evm.message.current_target
498
    ).balance
499
    if sender_balance < value:
500
        push(evm.stack, U256(0))
501
        evm.return_data = b""
502
        evm.gas_left += message_call_gas.sub_call
503
    else:
504
        generic_call(
505
            evm,
506
            GenericCall(
507
                gas=message_call_gas.sub_call,
508
                value=value,
509
                caller=evm.message.current_target,
510
                to=to,
511
                code_address=code_address,
512
                should_transfer_value=True,
513
                is_staticcall=False,
514
                memory_input_start_position=memory_input_start_position,
515
                memory_input_size=memory_input_size,
516
                memory_output_start_position=memory_output_start_position,
517
                memory_output_size=memory_output_size,
518
                code=code,
519
                disable_precompiles=disable_precompiles,
520
            ),
521
        )
522
523
    # PROGRAM COUNTER
524
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
528
    <snip>
537
    # STACK
538
    beneficiary = to_address_masked(pop(evm.stack))
539
540
    # GAS
541
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
542
    if beneficiary not in evm.accessed_addresses:
543
        evm.accessed_addresses.add(beneficiary)
544
        gas_cost += GasCosts.COLD_ACCOUNT_ACCESS
545
546
    if (
547
        not is_account_alive(evm.message.tx_env.state, beneficiary)
548
        and get_account(
549
            evm.message.tx_env.state, evm.message.current_target
550
        ).balance
551
        != 0
552
    ):
553
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
554
555
    charge_gas(evm, gas_cost)
556
    if evm.message.is_static:
557
        raise WriteInStaticContext
558
559
    originator = evm.message.current_target
560
    originator_balance = get_account(
561
        evm.message.tx_env.state, originator
562
    ).balance
563
564
    move_ether(
565
        evm.message.tx_env.state,
566
        originator,
567
        beneficiary,
568
        originator_balance,
569
    )
570
571
    # register account for deletion only if it was created
572
    # in the same transaction
573
    if originator in evm.message.tx_env.state.created_accounts:
574
        # If beneficiary is the same as originator, then
575
        # the ether is burnt.
576
        set_account_balance(evm.message.tx_env.state, originator, U256(0))
577
        evm.accounts_to_delete.add(originator)
578
579
    # HALT the execution
580
    evm.running = False
581
582
    # PROGRAM COUNTER
583
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
587
    <snip>
596
    # STACK
597
    gas = Uint(pop(evm.stack))
598
    code_address = to_address_masked(pop(evm.stack))
599
    memory_input_start_position = pop(evm.stack)
600
    memory_input_size = pop(evm.stack)
601
    memory_output_start_position = pop(evm.stack)
602
    memory_output_size = pop(evm.stack)
603
604
    # GAS
605
    extend_memory = calculate_gas_extend_memory(
606
        evm.memory,
607
        [
608
            (memory_input_start_position, memory_input_size),
609
            (memory_output_start_position, memory_output_size),
610
        ],
611
    )
612
613
    if code_address in evm.accessed_addresses:
614
        access_gas_cost = GasCosts.WARM_ACCESS
615
    else:
616
        evm.accessed_addresses.add(code_address)
617
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
618
619
    (
620
        disable_precompiles,
621
        code_address,
622
        code,
623
        delegated_access_gas_cost,
624
    ) = access_delegation(evm, code_address)
625
    access_gas_cost += delegated_access_gas_cost
626
627
    message_call_gas = calculate_message_call_gas(
628
        U256(0), gas, Uint(evm.gas_left), extend_memory.cost, access_gas_cost
629
    )
630
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
631
632
    # OPERATION
633
    evm.memory += b"\x00" * extend_memory.expand_by
634
    generic_call(
635
        evm,
636
        GenericCall(
637
            gas=message_call_gas.sub_call,
638
            value=evm.message.value,
639
            caller=evm.message.caller,
640
            to=evm.message.current_target,
641
            code_address=code_address,
642
            should_transfer_value=False,
643
            is_staticcall=False,
644
            memory_input_start_position=memory_input_start_position,
645
            memory_input_size=memory_input_size,
646
            memory_output_start_position=memory_output_start_position,
647
            memory_output_size=memory_output_size,
648
            code=code,
649
            disable_precompiles=disable_precompiles,
650
        ),
651
    )
652
653
    # PROGRAM COUNTER
654
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(evm: Evm) -> None:
658
    <snip>
667
    # STACK
668
    gas = Uint(pop(evm.stack))
669
    to = to_address_masked(pop(evm.stack))
670
    memory_input_start_position = pop(evm.stack)
671
    memory_input_size = pop(evm.stack)
672
    memory_output_start_position = pop(evm.stack)
673
    memory_output_size = pop(evm.stack)
674
675
    # GAS
676
    extend_memory = calculate_gas_extend_memory(
677
        evm.memory,
678
        [
679
            (memory_input_start_position, memory_input_size),
680
            (memory_output_start_position, memory_output_size),
681
        ],
682
    )
683
684
    if to in evm.accessed_addresses:
685
        access_gas_cost = GasCosts.WARM_ACCESS
686
    else:
687
        evm.accessed_addresses.add(to)
688
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
689
690
    code_address = to
691
    (
692
        disable_precompiles,
693
        code_address,
694
        code,
695
        delegated_access_gas_cost,
696
    ) = access_delegation(evm, code_address)
697
    access_gas_cost += delegated_access_gas_cost
698
699
    message_call_gas = calculate_message_call_gas(
700
        U256(0),
701
        gas,
702
        Uint(evm.gas_left),
703
        extend_memory.cost,
704
        access_gas_cost,
705
    )
706
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
707
708
    # OPERATION
709
    evm.memory += b"\x00" * extend_memory.expand_by
710
    generic_call(
711
        evm,
712
        GenericCall(
713
            gas=message_call_gas.sub_call,
714
            value=U256(0),
715
            caller=evm.message.current_target,
716
            to=to,
717
            code_address=code_address,
718
            should_transfer_value=True,
719
            is_staticcall=True,
720
            memory_input_start_position=memory_input_start_position,
721
            memory_input_size=memory_input_size,
722
            memory_output_start_position=memory_output_start_position,
723
            memory_output_size=memory_output_size,
724
            code=code,
725
            disable_precompiles=disable_precompiles,
726
        ),
727
    )
728
729
    # PROGRAM COUNTER
730
    evm.pc += Uint(1)

revert

Stop execution and revert state changes, without consuming all provided gas and also has the ability to return a reason.

Parameters

evm : The current EVM frame.

def revert(evm: Evm) -> None:
734
    <snip>
744
    # STACK
745
    memory_start_index = pop(evm.stack)
746
    size = pop(evm.stack)
747
748
    # GAS
749
    extend_memory = calculate_gas_extend_memory(
750
        evm.memory, [(memory_start_index, size)]
751
    )
752
753
    charge_gas(evm, extend_memory.cost)
754
755
    # OPERATION
756
    evm.memory += b"\x00" * extend_memory.expand_by
757
    output = memory_read_bytes(evm.memory, memory_start_index, size)
758
    evm.output = Bytes(output)
759
    raise Revert
760
761
    # PROGRAM COUNTER
762
    # no-op