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

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

GenericCall

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

272
@final
273
@dataclass
class GenericCall:

gas

279
    gas: Uint

value

280
    value: U256

caller

281
    caller: Address

to

282
    to: Address

code_address

283
    code_address: Address

should_transfer_value

284
    should_transfer_value: bool

is_staticcall

285
    is_staticcall: bool

memory_input_start_position

286
    memory_input_start_position: U256

memory_input_size

287
    memory_input_size: U256

memory_output_start_position

288
    memory_output_start_position: U256

memory_output_size

289
    memory_output_size: U256

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
    tx_state = evm.message.tx_env.state
311
    code = get_code(
312
        tx_state, get_account(tx_state, params.code_address).code_hash
313
    )
314
    child_message = Message(
315
        block_env=evm.message.block_env,
316
        tx_env=evm.message.tx_env,
317
        caller=params.caller,
318
        target=params.to,
319
        gas=params.gas,
320
        value=params.value,
321
        data=call_data,
322
        code=code,
323
        current_target=params.to,
324
        depth=evm.message.depth + Uint(1),
325
        code_address=params.code_address,
326
        should_transfer_value=params.should_transfer_value,
327
        is_static=params.is_staticcall or evm.message.is_static,
328
        accessed_addresses=evm.accessed_addresses.copy(),
329
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
330
        parent_evm=evm,
331
    )
332
    child_evm = process_message(child_message)
333
334
    if child_evm.error:
335
        incorporate_child_on_error(evm, child_evm)
336
        evm.return_data = child_evm.output
337
        push(evm.stack, U256(0))
338
    else:
339
        incorporate_child_on_success(evm, child_evm)
340
        evm.return_data = child_evm.output
341
        push(evm.stack, U256(1))
342
343
    actual_output_size = min(
344
        params.memory_output_size, U256(len(child_evm.output))
345
    )
346
    memory_write(
347
        evm.memory,
348
        params.memory_output_start_position,
349
        child_evm.output[:actual_output_size],
350
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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