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, 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
    originator_balance = get_account(
541
        evm.message.tx_env.state, originator
542
    ).balance
543
544
    move_ether(
545
        evm.message.tx_env.state,
546
        originator,
547
        beneficiary,
548
        originator_balance,
549
    )
550
551
    # register account for deletion only if it was created
552
    # in the same transaction
553
    if originator in evm.message.tx_env.state.created_accounts:
554
        # If beneficiary is the same as originator, then
555
        # the ether is burnt.
556
        set_account_balance(evm.message.tx_env.state, originator, U256(0))
557
        evm.accounts_to_delete.add(originator)
558
559
    # HALT the execution
560
    evm.running = False
561
562
    # PROGRAM COUNTER
563
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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