ethereum.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:
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
        parent_evm=evm,
124
    )
125
    child_evm = process_create_message(child_message)
126
127
    if child_evm.error:
128
        incorporate_child_on_error(evm, child_evm)
129
        evm.return_data = child_evm.output
130
        push(evm.stack, U256(0))
131
    else:
132
        incorporate_child_on_success(evm, child_evm)
133
        evm.return_data = b""
134
        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:
138
    <snip>
147
    # STACK
148
    endowment = pop(evm.stack)
149
    memory_start_position = pop(evm.stack)
150
    memory_size = pop(evm.stack)
151
152
    # GAS
153
    extend_memory = calculate_gas_extend_memory(
154
        evm.memory, [(memory_start_position, memory_size)]
155
    )
156
    init_code_gas = init_code_cost(Uint(memory_size))
157
158
    charge_gas(
159
        evm,
160
        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

generic_call

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

def generic_call(​evm: Evm, ​​params: GenericCall​) -> None:
291
    <snip>
294
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
295
296
    evm.return_data = b""
297
298
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
299
        evm.gas_left += params.gas
300
        push(evm.stack, U256(0))
301
        return
302
303
    call_data = memory_read_bytes(
304
        evm.memory,
305
        params.memory_input_start_position,
306
        params.memory_input_size,
307
    )
308
    tx_state = evm.message.tx_env.state
309
    code = get_code(
310
        tx_state, get_account(tx_state, params.code_address).code_hash
311
    )
312
    child_message = Message(
313
        block_env=evm.message.block_env,
314
        tx_env=evm.message.tx_env,
315
        caller=params.caller,
316
        target=params.to,
317
        gas=params.gas,
318
        value=params.value,
319
        data=call_data,
320
        code=code,
321
        current_target=params.to,
322
        depth=evm.message.depth + Uint(1),
323
        code_address=params.code_address,
324
        should_transfer_value=params.should_transfer_value,
325
        is_static=params.is_staticcall or evm.message.is_static,
326
        accessed_addresses=evm.accessed_addresses.copy(),
327
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
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
    create_gas_cost = GasCosts.NEW_ACCOUNT
388
    if value == 0 or is_account_alive(evm.message.tx_env.state, to):
389
        create_gas_cost = Uint(0)
390
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
391
    message_call_gas = calculate_message_call_gas(
392
        value,
393
        gas,
394
        Uint(evm.gas_left),
395
        memory_cost=extend_memory.cost,
396
        extra_gas=access_gas_cost + create_gas_cost + transfer_gas_cost,
397
    )
398
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
399
    if evm.message.is_static and value != U256(0):
400
        raise WriteInStaticContext
401
    evm.memory += b"\x00" * extend_memory.expand_by
402
    sender_balance = get_account(
403
        evm.message.tx_env.state, evm.message.current_target
404
    ).balance
405
    if sender_balance < value:
406
        push(evm.stack, U256(0))
407
        evm.return_data = b""
408
        evm.gas_left += message_call_gas.sub_call
409
    else:
410
        generic_call(
411
            evm,
412
            GenericCall(
413
                gas=message_call_gas.sub_call,
414
                value=value,
415
                caller=evm.message.current_target,
416
                to=to,
417
                code_address=code_address,
418
                should_transfer_value=True,
419
                is_staticcall=False,
420
                memory_input_start_position=memory_input_start_position,
421
                memory_input_size=memory_input_size,
422
                memory_output_start_position=memory_output_start_position,
423
                memory_output_size=memory_output_size,
424
            ),
425
        )
426
427
    # PROGRAM COUNTER
428
    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:
432
    <snip>
441
    # STACK
442
    gas = Uint(pop(evm.stack))
443
    code_address = to_address_masked(pop(evm.stack))
444
    value = pop(evm.stack)
445
    memory_input_start_position = pop(evm.stack)
446
    memory_input_size = pop(evm.stack)
447
    memory_output_start_position = pop(evm.stack)
448
    memory_output_size = pop(evm.stack)
449
450
    # GAS
451
    to = evm.message.current_target
452
453
    extend_memory = calculate_gas_extend_memory(
454
        evm.memory,
455
        [
456
            (memory_input_start_position, memory_input_size),
457
            (memory_output_start_position, memory_output_size),
458
        ],
459
    )
460
461
    if code_address in evm.accessed_addresses:
462
        access_gas_cost = GasCosts.WARM_ACCESS
463
    else:
464
        evm.accessed_addresses.add(code_address)
465
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
466
467
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
468
    message_call_gas = calculate_message_call_gas(
469
        value,
470
        gas,
471
        Uint(evm.gas_left),
472
        extend_memory.cost,
473
        access_gas_cost + transfer_gas_cost,
474
    )
475
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
476
477
    # OPERATION
478
    evm.memory += b"\x00" * extend_memory.expand_by
479
    sender_balance = get_account(
480
        evm.message.tx_env.state, evm.message.current_target
481
    ).balance
482
    if sender_balance < value:
483
        push(evm.stack, U256(0))
484
        evm.return_data = b""
485
        evm.gas_left += message_call_gas.sub_call
486
    else:
487
        generic_call(
488
            evm,
489
            GenericCall(
490
                gas=message_call_gas.sub_call,
491
                value=value,
492
                caller=evm.message.current_target,
493
                to=to,
494
                code_address=code_address,
495
                should_transfer_value=True,
496
                is_staticcall=False,
497
                memory_input_start_position=memory_input_start_position,
498
                memory_input_size=memory_input_size,
499
                memory_output_start_position=memory_output_start_position,
500
                memory_output_size=memory_output_size,
501
            ),
502
        )
503
504
    # PROGRAM COUNTER
505
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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