ethereum.forks.shanghai.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:
62
    <snip>
65
    # This import causes a circular import error
66
    # if it's not moved inside this method
67
    from ...vm.interpreter import (
68
        MAX_INIT_CODE_SIZE,
69
        STACK_DEPTH_LIMIT,
70
        process_create_message,
71
    )
72
73
    call_data = memory_read_bytes(
74
        evm.memory, memory_start_position, memory_size
75
    )
76
    if len(call_data) > MAX_INIT_CODE_SIZE:
77
        raise OutOfGasError
78
79
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
80
    evm.gas_left -= create_message_gas
81
    if evm.message.is_static:
82
        raise WriteInStaticContext
83
    evm.return_data = b""
84
85
    sender_address = evm.message.current_target
86
    sender = get_account(evm.message.tx_env.state, sender_address)
87
88
    if (
89
        sender.balance < endowment
90
        or sender.nonce == Uint(2**64 - 1)
91
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
92
    ):
93
        evm.gas_left += create_message_gas
94
        push(evm.stack, U256(0))
95
        return
96
97
    evm.accessed_addresses.add(contract_address)
98
99
    if not account_deployable(evm.message.tx_env.state, contract_address):
100
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
101
        push(evm.stack, U256(0))
102
        return
103
104
    increment_nonce(evm.message.tx_env.state, evm.message.current_target)
105
106
    child_message = Message(
107
        block_env=evm.message.block_env,
108
        tx_env=evm.message.tx_env,
109
        caller=evm.message.current_target,
110
        target=Bytes0(),
111
        gas=create_message_gas,
112
        value=endowment,
113
        data=b"",
114
        code=call_data,
115
        current_target=contract_address,
116
        depth=evm.message.depth + Uint(1),
117
        code_address=None,
118
        should_transfer_value=True,
119
        is_static=False,
120
        accessed_addresses=evm.accessed_addresses.copy(),
121
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
122
        parent_evm=evm,
123
    )
124
    child_evm = process_create_message(child_message)
125
126
    if child_evm.error:
127
        incorporate_child_on_error(evm, child_evm)
128
        evm.return_data = child_evm.output
129
        push(evm.stack, U256(0))
130
    else:
131
        incorporate_child_on_success(evm, child_evm)
132
        evm.return_data = b""
133
        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:
137
    <snip>
146
    # STACK
147
    endowment = pop(evm.stack)
148
    memory_start_position = pop(evm.stack)
149
    memory_size = pop(evm.stack)
150
151
    # GAS
152
    extend_memory = calculate_gas_extend_memory(
153
        evm.memory, [(memory_start_position, memory_size)]
154
    )
155
    init_code_gas = init_code_cost(Uint(memory_size))
156
157
    charge_gas(
158
        evm,
159
        GasCosts.OPCODE_CREATE_BASE + extend_memory.cost + init_code_gas,
160
    )
161
162
    # OPERATION
163
    evm.memory += b"\x00" * extend_memory.expand_by
164
    contract_address = compute_contract_address(
165
        evm.message.current_target,
166
        get_account(
167
            evm.message.tx_env.state, evm.message.current_target
168
        ).nonce,
169
    )
170
171
    generic_create(
172
        evm,
173
        endowment,
174
        contract_address,
175
        memory_start_position,
176
        memory_size,
177
    )
178
179
    # PROGRAM COUNTER
180
    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:
184
    <snip>
196
    # STACK
197
    endowment = pop(evm.stack)
198
    memory_start_position = pop(evm.stack)
199
    memory_size = pop(evm.stack)
200
    salt = pop(evm.stack).to_be_bytes32()
201
202
    # GAS
203
    extend_memory = calculate_gas_extend_memory(
204
        evm.memory, [(memory_start_position, memory_size)]
205
    )
206
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
207
    init_code_gas = init_code_cost(Uint(memory_size))
208
    charge_gas(
209
        evm,
210
        GasCosts.OPCODE_CREATE_BASE
211
        + GasCosts.OPCODE_KECCAK256_PER_WORD * call_data_words
212
        + extend_memory.cost
213
        + init_code_gas,
214
    )
215
216
    # OPERATION
217
    evm.memory += b"\x00" * extend_memory.expand_by
218
    contract_address = compute_create2_contract_address(
219
        evm.message.current_target,
220
        salt,
221
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
222
    )
223
224
    generic_create(
225
        evm,
226
        endowment,
227
        contract_address,
228
        memory_start_position,
229
        memory_size,
230
    )
231
232
    # PROGRAM COUNTER
233
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

GenericCall

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

269
@final
270
@dataclass
class GenericCall:

gas

276
    gas: Uint

value

277
    value: U256

caller

278
    caller: Address

to

279
    to: Address

code_address

280
    code_address: Address

should_transfer_value

281
    should_transfer_value: bool

is_staticcall

282
    is_staticcall: bool

memory_input_start_position

283
    memory_input_start_position: U256

memory_input_size

284
    memory_input_size: U256

memory_output_start_position

285
    memory_output_start_position: U256

memory_output_size

286
    memory_output_size: U256

generic_call

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

def generic_call(​evm: Evm, ​​params: GenericCall​) -> None:
290
    <snip>
293
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
294
295
    evm.return_data = b""
296
297
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
298
        evm.gas_left += params.gas
299
        push(evm.stack, U256(0))
300
        return
301
302
    call_data = memory_read_bytes(
303
        evm.memory,
304
        params.memory_input_start_position,
305
        params.memory_input_size,
306
    )
307
    tx_state = evm.message.tx_env.state
308
    code = get_code(
309
        tx_state, get_account(tx_state, params.code_address).code_hash
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=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
        parent_evm=evm,
328
    )
329
    child_evm = process_message(child_message)
330
331
    if child_evm.error:
332
        incorporate_child_on_error(evm, child_evm)
333
        evm.return_data = child_evm.output
334
        push(evm.stack, U256(0))
335
    else:
336
        incorporate_child_on_success(evm, child_evm)
337
        evm.return_data = child_evm.output
338
        push(evm.stack, U256(1))
339
340
    actual_output_size = min(
341
        params.memory_output_size, U256(len(child_evm.output))
342
    )
343
    memory_write(
344
        evm.memory,
345
        params.memory_output_start_position,
346
        child_evm.output[:actual_output_size],
347
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(​evm: Evm​) -> None:
351
    <snip>
360
    # STACK
361
    gas = Uint(pop(evm.stack))
362
    to = to_address_masked(pop(evm.stack))
363
    value = pop(evm.stack)
364
    memory_input_start_position = pop(evm.stack)
365
    memory_input_size = pop(evm.stack)
366
    memory_output_start_position = pop(evm.stack)
367
    memory_output_size = pop(evm.stack)
368
369
    # GAS
370
    extend_memory = calculate_gas_extend_memory(
371
        evm.memory,
372
        [
373
            (memory_input_start_position, memory_input_size),
374
            (memory_output_start_position, memory_output_size),
375
        ],
376
    )
377
378
    if to in evm.accessed_addresses:
379
        access_gas_cost = GasCosts.WARM_ACCESS
380
    else:
381
        evm.accessed_addresses.add(to)
382
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
383
384
    code_address = to
385
386
    create_gas_cost = GasCosts.NEW_ACCOUNT
387
    if value == 0 or is_account_alive(evm.message.tx_env.state, to):
388
        create_gas_cost = Uint(0)
389
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
390
    message_call_gas = calculate_message_call_gas(
391
        value,
392
        gas,
393
        Uint(evm.gas_left),
394
        memory_cost=extend_memory.cost,
395
        extra_gas=access_gas_cost + create_gas_cost + transfer_gas_cost,
396
    )
397
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
398
    if evm.message.is_static and value != U256(0):
399
        raise WriteInStaticContext
400
    evm.memory += b"\x00" * extend_memory.expand_by
401
    sender_balance = get_account(
402
        evm.message.tx_env.state, evm.message.current_target
403
    ).balance
404
    if sender_balance < value:
405
        push(evm.stack, U256(0))
406
        evm.return_data = b""
407
        evm.gas_left += message_call_gas.sub_call
408
    else:
409
        generic_call(
410
            evm,
411
            GenericCall(
412
                gas=message_call_gas.sub_call,
413
                value=value,
414
                caller=evm.message.current_target,
415
                to=to,
416
                code_address=code_address,
417
                should_transfer_value=True,
418
                is_staticcall=False,
419
                memory_input_start_position=memory_input_start_position,
420
                memory_input_size=memory_input_size,
421
                memory_output_start_position=memory_output_start_position,
422
                memory_output_size=memory_output_size,
423
            ),
424
        )
425
426
    # PROGRAM COUNTER
427
    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:
431
    <snip>
440
    # STACK
441
    gas = Uint(pop(evm.stack))
442
    code_address = to_address_masked(pop(evm.stack))
443
    value = pop(evm.stack)
444
    memory_input_start_position = pop(evm.stack)
445
    memory_input_size = pop(evm.stack)
446
    memory_output_start_position = pop(evm.stack)
447
    memory_output_size = pop(evm.stack)
448
449
    # GAS
450
    to = evm.message.current_target
451
452
    extend_memory = calculate_gas_extend_memory(
453
        evm.memory,
454
        [
455
            (memory_input_start_position, memory_input_size),
456
            (memory_output_start_position, memory_output_size),
457
        ],
458
    )
459
460
    if code_address in evm.accessed_addresses:
461
        access_gas_cost = GasCosts.WARM_ACCESS
462
    else:
463
        evm.accessed_addresses.add(code_address)
464
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
465
466
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
467
    message_call_gas = calculate_message_call_gas(
468
        value,
469
        gas,
470
        Uint(evm.gas_left),
471
        extend_memory.cost,
472
        access_gas_cost + transfer_gas_cost,
473
    )
474
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
475
476
    # OPERATION
477
    evm.memory += b"\x00" * extend_memory.expand_by
478
    sender_balance = get_account(
479
        evm.message.tx_env.state, evm.message.current_target
480
    ).balance
481
    if sender_balance < value:
482
        push(evm.stack, U256(0))
483
        evm.return_data = b""
484
        evm.gas_left += message_call_gas.sub_call
485
    else:
486
        generic_call(
487
            evm,
488
            GenericCall(
489
                gas=message_call_gas.sub_call,
490
                value=value,
491
                caller=evm.message.current_target,
492
                to=to,
493
                code_address=code_address,
494
                should_transfer_value=True,
495
                is_staticcall=False,
496
                memory_input_start_position=memory_input_start_position,
497
                memory_input_size=memory_input_size,
498
                memory_output_start_position=memory_output_start_position,
499
                memory_output_size=memory_output_size,
500
            ),
501
        )
502
503
    # PROGRAM COUNTER
504
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(​evm: Evm​) -> None:
508
    <snip>
517
    # STACK
518
    beneficiary = to_address_masked(pop(evm.stack))
519
520
    # GAS
521
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
522
    if beneficiary not in evm.accessed_addresses:
523
        evm.accessed_addresses.add(beneficiary)
524
        gas_cost += GasCosts.COLD_ACCOUNT_ACCESS
525
526
    if (
527
        not is_account_alive(evm.message.tx_env.state, beneficiary)
528
        and get_account(
529
            evm.message.tx_env.state, evm.message.current_target
530
        ).balance
531
        != 0
532
    ):
533
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
534
535
    charge_gas(evm, gas_cost)
536
    if evm.message.is_static:
537
        raise WriteInStaticContext
538
539
    originator = evm.message.current_target
540
    beneficiary_balance = get_account(
541
        evm.message.tx_env.state, beneficiary
542
    ).balance
543
    originator_balance = get_account(
544
        evm.message.tx_env.state, originator
545
    ).balance
546
547
    # First Transfer to beneficiary
548
    set_account_balance(
549
        evm.message.tx_env.state,
550
        beneficiary,
551
        beneficiary_balance + originator_balance,
552
    )
553
    # Next, Zero the balance of the address being deleted (must come after
554
    # sending to beneficiary in case the contract named itself as the
555
    # beneficiary).
556
    set_account_balance(evm.message.tx_env.state, originator, U256(0))
557
558
    # register account for deletion
559
    evm.accounts_to_delete.add(originator)
560
561
    # HALT the execution
562
    evm.running = False
563
564
    # PROGRAM COUNTER
565
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(​evm: Evm​) -> None:
569
    <snip>
578
    # STACK
579
    gas = Uint(pop(evm.stack))
580
    code_address = to_address_masked(pop(evm.stack))
581
    memory_input_start_position = pop(evm.stack)
582
    memory_input_size = pop(evm.stack)
583
    memory_output_start_position = pop(evm.stack)
584
    memory_output_size = pop(evm.stack)
585
586
    # GAS
587
    extend_memory = calculate_gas_extend_memory(
588
        evm.memory,
589
        [
590
            (memory_input_start_position, memory_input_size),
591
            (memory_output_start_position, memory_output_size),
592
        ],
593
    )
594
595
    if code_address in evm.accessed_addresses:
596
        access_gas_cost = GasCosts.WARM_ACCESS
597
    else:
598
        evm.accessed_addresses.add(code_address)
599
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
600
601
    message_call_gas = calculate_message_call_gas(
602
        U256(0),
603
        gas,
604
        Uint(evm.gas_left),
605
        extend_memory.cost,
606
        access_gas_cost,
607
    )
608
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
609
610
    # OPERATION
611
    evm.memory += b"\x00" * extend_memory.expand_by
612
    generic_call(
613
        evm,
614
        GenericCall(
615
            gas=message_call_gas.sub_call,
616
            value=evm.message.value,
617
            caller=evm.message.caller,
618
            to=evm.message.current_target,
619
            code_address=code_address,
620
            should_transfer_value=False,
621
            is_staticcall=False,
622
            memory_input_start_position=memory_input_start_position,
623
            memory_input_size=memory_input_size,
624
            memory_output_start_position=memory_output_start_position,
625
            memory_output_size=memory_output_size,
626
        ),
627
    )
628
629
    # PROGRAM COUNTER
630
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(​evm: Evm​) -> None:
634
    <snip>
643
    # STACK
644
    gas = Uint(pop(evm.stack))
645
    to = to_address_masked(pop(evm.stack))
646
    memory_input_start_position = pop(evm.stack)
647
    memory_input_size = pop(evm.stack)
648
    memory_output_start_position = pop(evm.stack)
649
    memory_output_size = pop(evm.stack)
650
651
    # GAS
652
    extend_memory = calculate_gas_extend_memory(
653
        evm.memory,
654
        [
655
            (memory_input_start_position, memory_input_size),
656
            (memory_output_start_position, memory_output_size),
657
        ],
658
    )
659
660
    if to in evm.accessed_addresses:
661
        access_gas_cost = GasCosts.WARM_ACCESS
662
    else:
663
        evm.accessed_addresses.add(to)
664
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
665
666
    code_address = to
667
668
    message_call_gas = calculate_message_call_gas(
669
        U256(0),
670
        gas,
671
        Uint(evm.gas_left),
672
        extend_memory.cost,
673
        access_gas_cost,
674
    )
675
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
676
677
    # OPERATION
678
    evm.memory += b"\x00" * extend_memory.expand_by
679
    generic_call(
680
        evm,
681
        GenericCall(
682
            gas=message_call_gas.sub_call,
683
            value=U256(0),
684
            caller=evm.message.current_target,
685
            to=to,
686
            code_address=code_address,
687
            should_transfer_value=True,
688
            is_staticcall=True,
689
            memory_input_start_position=memory_input_start_position,
690
            memory_input_size=memory_input_size,
691
            memory_output_start_position=memory_output_start_position,
692
            memory_output_size=memory_output_size,
693
        ),
694
    )
695
696
    # PROGRAM COUNTER
697
    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:
701
    <snip>
711
    # STACK
712
    memory_start_index = pop(evm.stack)
713
    size = pop(evm.stack)
714
715
    # GAS
716
    extend_memory = calculate_gas_extend_memory(
717
        evm.memory, [(memory_start_index, size)]
718
    )
719
720
    charge_gas(evm, extend_memory.cost)
721
722
    # OPERATION
723
    evm.memory += b"\x00" * extend_memory.expand_by
724
    output = memory_read_bytes(evm.memory, memory_start_index, size)
725
    evm.output = Bytes(output)
726
    raise Revert
727
728
    # PROGRAM COUNTER
729
    # no-op