ethereum.forks.paris.vm.instructions.systemethereum.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:
63
    <snip>
66
    # This import causes a circular import error
67
    # if it's not moved inside this method
67
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create_message
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 account_has_code_or_nonce(
101
        evm.message.tx_env.state, contract_address
102
    ) or account_has_storage(evm.message.tx_env.state, contract_address):
103
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
104
        push(evm.stack, U256(0))
105
        return
106
107
    increment_nonce(evm.message.tx_env.state, evm.message.current_target)
108
109
    child_message = Message(
110
        block_env=evm.message.block_env,
111
        tx_env=evm.message.tx_env,
112
        caller=evm.message.current_target,
113
        target=Bytes0(),
114
        gas=create_message_gas,
115
        value=endowment,
116
        data=b"",
117
        code=call_data,
118
        current_target=contract_address,
119
        depth=evm.message.depth + Uint(1),
120
        code_address=None,
121
        should_transfer_value=True,
122
        is_static=False,
123
        accessed_addresses=evm.accessed_addresses.copy(),
124
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
125
        parent_evm=evm,
126
    )
127
    child_evm = process_create_message(child_message)
128
129
    if child_evm.error:
130
        incorporate_child_on_error(evm, child_evm)
131
        evm.return_data = child_evm.output
132
        push(evm.stack, U256(0))
133
    else:
134
        incorporate_child_on_success(evm, child_evm)
135
        evm.return_data = b""
136
        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:
140
    <snip>
149
    # STACK
150
    endowment = pop(evm.stack)
151
    memory_start_position = pop(evm.stack)
152
    memory_size = pop(evm.stack)
153
154
    # GAS
155
    extend_memory = calculate_gas_extend_memory(
156
        evm.memory, [(memory_start_position, memory_size)]
157
    )
158
    init_code_gas = init_code_cost(Uint(memory_size))
159
152
    charge_gas(evm, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost)
160
    charge_gas(
161
        evm, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost + init_code_gas
162
    )
163
164
    # OPERATION
165
    evm.memory += b"\x00" * extend_memory.expand_by
166
    contract_address = compute_contract_address(
167
        evm.message.current_target,
168
        get_account(
169
            evm.message.tx_env.state, evm.message.current_target
170
        ).nonce,
171
    )
172
173
    generic_create(
164
        evm, endowment, contract_address, memory_start_position, memory_size
174
        evm,
175
        endowment,
176
        contract_address,
177
        memory_start_position,
178
        memory_size,
179
    )
180
181
    # PROGRAM COUNTER
182
    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:
186
    <snip>
198
    # STACK
199
    endowment = pop(evm.stack)
200
    memory_start_position = pop(evm.stack)
201
    memory_size = pop(evm.stack)
202
    salt = pop(evm.stack).to_be_bytes32()
203
204
    # GAS
205
    extend_memory = calculate_gas_extend_memory(
206
        evm.memory, [(memory_start_position, memory_size)]
207
    )
208
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
209
    init_code_gas = init_code_cost(Uint(memory_size))
210
    charge_gas(
211
        evm,
197
        GasCosts.OPCODE_CREATE_BASE
198
        + GasCosts.OPCODE_KECCAK256_PER_WORD * call_data_words
199
        + extend_memory.cost,
212
        GasCosts.OPCODE_CREATE_BASE
213
        + GasCosts.OPCODE_KECCAK256_PER_WORD * call_data_words
214
        + extend_memory.cost
215
        + init_code_gas,
216
    )
217
218
    # OPERATION
219
    evm.memory += b"\x00" * extend_memory.expand_by
220
    contract_address = compute_create2_contract_address(
221
        evm.message.current_target,
222
        salt,
223
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
224
    )
225
226
    generic_create(
211
        evm, endowment, contract_address, memory_start_position, memory_size
227
        evm,
228
        endowment,
229
        contract_address,
230
        memory_start_position,
231
        memory_size,
232
    )
233
234
    # PROGRAM COUNTER
235
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

GenericCall

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

271
@final
272
@dataclass
class GenericCall:

gas

278
    gas: Uint

value

279
    value: U256

caller

280
    caller: Address

to

281
    to: Address

code_address

282
    code_address: Address

should_transfer_value

283
    should_transfer_value: bool

is_staticcall

284
    is_staticcall: bool

memory_input_start_position

285
    memory_input_start_position: U256

memory_input_size

286
    memory_input_size: U256

memory_output_start_position

287
    memory_output_start_position: U256

memory_output_size

288
    memory_output_size: U256

generic_call

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

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
510
    <snip>
519
    # STACK
520
    beneficiary = to_address_masked(pop(evm.stack))
521
522
    # GAS
523
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
524
    if beneficiary not in evm.accessed_addresses:
525
        evm.accessed_addresses.add(beneficiary)
526
        gas_cost += GasCosts.COLD_ACCOUNT_ACCESS
527
528
    if (
529
        not is_account_alive(evm.message.tx_env.state, beneficiary)
530
        and get_account(
531
            evm.message.tx_env.state, evm.message.current_target
532
        ).balance
533
        != 0
534
    ):
535
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
536
537
    charge_gas(evm, gas_cost)
538
    if evm.message.is_static:
539
        raise WriteInStaticContext
540
541
    originator = evm.message.current_target
542
    beneficiary_balance = get_account(
543
        evm.message.tx_env.state, beneficiary
544
    ).balance
545
    originator_balance = get_account(
546
        evm.message.tx_env.state, originator
547
    ).balance
548
549
    # First Transfer to beneficiary
550
    set_account_balance(
551
        evm.message.tx_env.state,
552
        beneficiary,
553
        beneficiary_balance + originator_balance,
554
    )
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))
559
560
    # register account for deletion
561
    evm.accounts_to_delete.add(originator)
562
563
    # HALT the execution
564
    evm.running = False
565
566
    # PROGRAM COUNTER
567
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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