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, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost + init_code_gas
159
    )
160
161
    # OPERATION
162
    evm.memory += b"\x00" * extend_memory.expand_by
163
    contract_address = compute_contract_address(
164
        evm.message.current_target,
165
        get_account(
166
            evm.message.tx_env.state, evm.message.current_target
167
        ).nonce,
168
    )
169
170
    generic_create(
171
        evm,
172
        endowment,
173
        contract_address,
174
        memory_start_position,
175
        memory_size,
176
    )
177
178
    # PROGRAM COUNTER
179
    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:
183
    <snip>
195
    # STACK
196
    endowment = pop(evm.stack)
197
    memory_start_position = pop(evm.stack)
198
    memory_size = pop(evm.stack)
199
    salt = pop(evm.stack).to_be_bytes32()
200
201
    # GAS
202
    extend_memory = calculate_gas_extend_memory(
203
        evm.memory, [(memory_start_position, memory_size)]
204
    )
205
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
206
    init_code_gas = init_code_cost(Uint(memory_size))
207
    charge_gas(
208
        evm,
209
        GasCosts.OPCODE_CREATE_BASE
210
        + GasCosts.OPCODE_KECCAK256_PER_WORD * call_data_words
211
        + extend_memory.cost
212
        + init_code_gas,
213
    )
214
215
    # OPERATION
216
    evm.memory += b"\x00" * extend_memory.expand_by
217
    contract_address = compute_create2_contract_address(
218
        evm.message.current_target,
219
        salt,
220
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
221
    )
222
223
    generic_create(
224
        evm,
225
        endowment,
226
        contract_address,
227
        memory_start_position,
228
        memory_size,
229
    )
230
231
    # PROGRAM COUNTER
232
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

GenericCall

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

268
@final
269
@dataclass
class GenericCall:

gas

275
    gas: Uint

value

276
    value: U256

caller

277
    caller: Address

to

278
    to: Address

code_address

279
    code_address: Address

should_transfer_value

280
    should_transfer_value: bool

is_staticcall

281
    is_staticcall: bool

memory_input_start_position

282
    memory_input_start_position: U256

memory_input_size

283
    memory_input_size: U256

memory_output_start_position

284
    memory_output_start_position: U256

memory_output_size

285
    memory_output_size: U256

generic_call

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

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
507
    <snip>
516
    # STACK
517
    beneficiary = to_address_masked(pop(evm.stack))
518
519
    # GAS
520
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
521
    if beneficiary not in evm.accessed_addresses:
522
        evm.accessed_addresses.add(beneficiary)
523
        gas_cost += GasCosts.COLD_ACCOUNT_ACCESS
524
525
    if (
526
        not is_account_alive(evm.message.tx_env.state, beneficiary)
527
        and get_account(
528
            evm.message.tx_env.state, evm.message.current_target
529
        ).balance
530
        != 0
531
    ):
532
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
533
534
    charge_gas(evm, gas_cost)
535
    if evm.message.is_static:
536
        raise WriteInStaticContext
537
538
    originator = evm.message.current_target
539
    beneficiary_balance = get_account(
540
        evm.message.tx_env.state, beneficiary
541
    ).balance
542
    originator_balance = get_account(
543
        evm.message.tx_env.state, originator
544
    ).balance
545
546
    # First Transfer to beneficiary
547
    set_account_balance(
548
        evm.message.tx_env.state,
549
        beneficiary,
550
        beneficiary_balance + originator_balance,
551
    )
552
    # Next, Zero the balance of the address being deleted (must come after
553
    # sending to beneficiary in case the contract named itself as the
554
    # beneficiary).
555
    set_account_balance(evm.message.tx_env.state, originator, U256(0))
556
557
    # register account for deletion
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), gas, Uint(evm.gas_left), extend_memory.cost, access_gas_cost
602
    )
603
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
604
605
    # OPERATION
606
    evm.memory += b"\x00" * extend_memory.expand_by
607
    generic_call(
608
        evm,
609
        GenericCall(
610
            gas=message_call_gas.sub_call,
611
            value=evm.message.value,
612
            caller=evm.message.caller,
613
            to=evm.message.current_target,
614
            code_address=code_address,
615
            should_transfer_value=False,
616
            is_staticcall=False,
617
            memory_input_start_position=memory_input_start_position,
618
            memory_input_size=memory_input_size,
619
            memory_output_start_position=memory_output_start_position,
620
            memory_output_size=memory_output_size,
621
        ),
622
    )
623
624
    # PROGRAM COUNTER
625
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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