ethereum.forks.muir_glacier.vm.instructions.systemethereum.forks.berlin.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 STACK_DEPTH_LIMIT, process_create_message
69
70
    call_data = memory_read_bytes(
71
        evm.memory, memory_start_position, memory_size
72
    )
73
74
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
75
    evm.gas_left -= create_message_gas
76
    if evm.message.is_static:
77
        raise WriteInStaticContext
78
    evm.return_data = b""
79
80
    sender_address = evm.message.current_target
81
    sender = get_account(evm.message.tx_env.state, sender_address)
82
83
    if (
84
        sender.balance < endowment
85
        or sender.nonce == Uint(2**64 - 1)
86
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
87
    ):
88
        evm.gas_left += create_message_gas
89
        push(evm.stack, U256(0))
90
        return
91
92
    evm.accessed_addresses.add(contract_address)
93
94
    if account_has_code_or_nonce(
95
        evm.message.tx_env.state, contract_address
96
    ) or account_has_storage(evm.message.tx_env.state, contract_address):
97
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
98
        push(evm.stack, U256(0))
99
        return
98
99
    call_data = memory_read_bytes(
100
        evm.memory, memory_start_position, memory_size
101
    )
100
101
    increment_nonce(evm.message.tx_env.state, evm.message.current_target)
102
103
    child_message = Message(
104
        block_env=evm.message.block_env,
105
        tx_env=evm.message.tx_env,
106
        caller=evm.message.current_target,
107
        target=Bytes0(),
108
        gas=create_message_gas,
109
        value=endowment,
110
        data=b"",
111
        code=call_data,
112
        current_target=contract_address,
113
        depth=evm.message.depth + Uint(1),
114
        code_address=None,
115
        should_transfer_value=True,
116
        is_static=False,
117
        accessed_addresses=evm.accessed_addresses.copy(),
118
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
119
        parent_evm=evm,
120
    )
121
    child_evm = process_create_message(child_message)
122
123
    if child_evm.error:
124
        incorporate_child_on_error(evm, child_evm)
125
        evm.return_data = child_evm.output
126
        push(evm.stack, U256(0))
127
    else:
128
        incorporate_child_on_success(evm, child_evm)
129
        evm.return_data = b""
130
        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:
134
    <snip>
143
    # STACK
144
    endowment = pop(evm.stack)
145
    memory_start_position = pop(evm.stack)
146
    memory_size = pop(evm.stack)
147
148
    # GAS
149
    extend_memory = calculate_gas_extend_memory(
150
        evm.memory, [(memory_start_position, memory_size)]
151
    )
152
153
    charge_gas(evm, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost)
154
155
    # OPERATION
156
    evm.memory += b"\x00" * extend_memory.expand_by
157
    contract_address = compute_contract_address(
158
        evm.message.current_target,
159
        get_account(
160
            evm.message.tx_env.state, evm.message.current_target
161
        ).nonce,
162
    )
163
164
    generic_create(
165
        evm, endowment, contract_address, memory_start_position, memory_size
166
    )
167
168
    # PROGRAM COUNTER
169
    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:
173
    <snip>
185
    # STACK
186
    endowment = pop(evm.stack)
187
    memory_start_position = pop(evm.stack)
188
    memory_size = pop(evm.stack)
189
    salt = pop(evm.stack).to_be_bytes32()
190
191
    # GAS
192
    extend_memory = calculate_gas_extend_memory(
193
        evm.memory, [(memory_start_position, memory_size)]
194
    )
195
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
196
    charge_gas(
197
        evm,
198
        GasCosts.OPCODE_CREATE_BASE
199
        + GasCosts.OPCODE_KECCAK256_PER_WORD * call_data_words
200
        + extend_memory.cost,
201
    )
202
203
    # OPERATION
204
    evm.memory += b"\x00" * extend_memory.expand_by
205
    contract_address = compute_create2_contract_address(
206
        evm.message.current_target,
207
        salt,
208
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
209
    )
210
211
    generic_create(
212
        evm, endowment, contract_address, memory_start_position, memory_size
213
    )
214
215
    # PROGRAM COUNTER
216
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
220
    <snip>
229
    # STACK
230
    memory_start_position = pop(evm.stack)
231
    memory_size = pop(evm.stack)
232
233
    # GAS
234
    extend_memory = calculate_gas_extend_memory(
235
        evm.memory, [(memory_start_position, memory_size)]
236
    )
237
238
    charge_gas(evm, GasCosts.ZERO + extend_memory.cost)
239
240
    # OPERATION
241
    evm.memory += b"\x00" * extend_memory.expand_by
242
    evm.output = memory_read_bytes(
243
        evm.memory, memory_start_position, memory_size
244
    )
245
246
    evm.running = False
247
248
    # PROGRAM COUNTER
249
    pass

GenericCall

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

252
@final
253
@dataclass
class GenericCall:

gas

259
    gas: Uint

value

260
    value: U256

caller

261
    caller: Address

to

262
    to: Address

code_address

263
    code_address: Address

should_transfer_value

264
    should_transfer_value: bool

is_staticcall

265
    is_staticcall: bool

memory_input_start_position

266
    memory_input_start_position: U256

memory_input_size

267
    memory_input_size: U256

memory_output_start_position

268
    memory_output_start_position: U256

memory_output_size

269
    memory_output_size: U256

generic_call

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

def generic_call(evm: Evm, ​​params: GenericCall) -> None:
273
    <snip>
276
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
277
278
    evm.return_data = b""
279
280
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
281
        evm.gas_left += params.gas
282
        push(evm.stack, U256(0))
283
        return
284
285
    call_data = memory_read_bytes(
286
        evm.memory,
287
        params.memory_input_start_position,
288
        params.memory_input_size,
289
    )
290
    code_account = get_account(evm.message.tx_env.state, params.code_address)
291
    code = get_code(evm.message.tx_env.state, code_account.code_hash)
290
    _code_account = get_account(evm.message.tx_env.state, params.code_address)
291
    code = get_code(evm.message.tx_env.state, _code_account.code_hash)
292
    child_message = Message(
293
        block_env=evm.message.block_env,
294
        tx_env=evm.message.tx_env,
295
        caller=params.caller,
296
        target=params.to,
297
        gas=params.gas,
298
        value=params.value,
299
        data=call_data,
300
        code=code,
301
        current_target=params.to,
302
        depth=evm.message.depth + Uint(1),
303
        code_address=params.code_address,
304
        should_transfer_value=params.should_transfer_value,
305
        is_static=params.is_staticcall or evm.message.is_static,
306
        accessed_addresses=evm.accessed_addresses.copy(),
307
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
308
        parent_evm=evm,
309
    )
310
    child_evm = process_message(child_message)
311
312
    if child_evm.error:
313
        incorporate_child_on_error(evm, child_evm)
314
        evm.return_data = child_evm.output
315
        push(evm.stack, U256(0))
316
    else:
317
        incorporate_child_on_success(evm, child_evm)
318
        evm.return_data = child_evm.output
319
        push(evm.stack, U256(1))
320
321
    actual_output_size = min(
322
        params.memory_output_size, U256(len(child_evm.output))
323
    )
324
    memory_write(
325
        evm.memory,
326
        params.memory_output_start_position,
327
        child_evm.output[:actual_output_size],
328
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
332
    <snip>
341
    # STACK
342
    gas = Uint(pop(evm.stack))
343
    to = to_address_masked(pop(evm.stack))
344
    value = pop(evm.stack)
345
    memory_input_start_position = pop(evm.stack)
346
    memory_input_size = pop(evm.stack)
347
    memory_output_start_position = pop(evm.stack)
348
    memory_output_size = pop(evm.stack)
349
350
    # GAS
351
    extend_memory = calculate_gas_extend_memory(
352
        evm.memory,
353
        [
354
            (memory_input_start_position, memory_input_size),
355
            (memory_output_start_position, memory_output_size),
356
        ],
357
    )
358
359
    if to in evm.accessed_addresses:
360
        access_gas_cost = GasCosts.WARM_ACCESS
361
    else:
362
        evm.accessed_addresses.add(to)
363
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
364
365
    code_address = to
366
367
    create_gas_cost = GasCosts.NEW_ACCOUNT
368
    if value == 0 or is_account_alive(evm.message.tx_env.state, to):
369
        create_gas_cost = Uint(0)
370
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
371
    message_call_gas = calculate_message_call_gas(
372
        value,
373
        gas,
374
        Uint(evm.gas_left),
375
        memory_cost=extend_memory.cost,
368
        extra_gas=GasCosts.OPCODE_CALL_BASE
369
        + create_gas_cost
370
        + transfer_gas_cost,
376
        extra_gas=access_gas_cost + create_gas_cost + transfer_gas_cost,
377
    )
378
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
379
    if evm.message.is_static and value != U256(0):
380
        raise WriteInStaticContext
381
    evm.memory += b"\x00" * extend_memory.expand_by
382
    sender_balance = get_account(
383
        evm.message.tx_env.state, evm.message.current_target
384
    ).balance
385
    if sender_balance < value:
386
        push(evm.stack, U256(0))
387
        evm.return_data = b""
388
        evm.gas_left += message_call_gas.sub_call
389
    else:
390
        generic_call(
391
            evm,
392
            GenericCall(
393
                gas=message_call_gas.sub_call,
394
                value=value,
395
                caller=evm.message.current_target,
396
                to=to,
397
                code_address=code_address,
398
                should_transfer_value=True,
399
                is_staticcall=False,
400
                memory_input_start_position=memory_input_start_position,
401
                memory_input_size=memory_input_size,
402
                memory_output_start_position=memory_output_start_position,
403
                memory_output_size=memory_output_size,
404
            ),
405
        )
406
407
    # PROGRAM COUNTER
408
    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:
412
    <snip>
421
    # STACK
422
    gas = Uint(pop(evm.stack))
423
    code_address = to_address_masked(pop(evm.stack))
424
    value = pop(evm.stack)
425
    memory_input_start_position = pop(evm.stack)
426
    memory_input_size = pop(evm.stack)
427
    memory_output_start_position = pop(evm.stack)
428
    memory_output_size = pop(evm.stack)
429
430
    # GAS
431
    to = evm.message.current_target
432
433
    extend_memory = calculate_gas_extend_memory(
434
        evm.memory,
435
        [
436
            (memory_input_start_position, memory_input_size),
437
            (memory_output_start_position, memory_output_size),
438
        ],
439
    )
440
441
    if code_address in evm.accessed_addresses:
442
        access_gas_cost = GasCosts.WARM_ACCESS
443
    else:
444
        evm.accessed_addresses.add(code_address)
445
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
446
447
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
448
    message_call_gas = calculate_message_call_gas(
449
        value,
450
        gas,
451
        Uint(evm.gas_left),
452
        extend_memory.cost,
440
        GasCosts.OPCODE_CALL_BASE + transfer_gas_cost,
453
        access_gas_cost + transfer_gas_cost,
454
    )
455
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
456
457
    # OPERATION
458
    evm.memory += b"\x00" * extend_memory.expand_by
459
    sender_balance = get_account(
460
        evm.message.tx_env.state, evm.message.current_target
461
    ).balance
462
    if sender_balance < value:
463
        push(evm.stack, U256(0))
464
        evm.return_data = b""
465
        evm.gas_left += message_call_gas.sub_call
466
    else:
467
        generic_call(
468
            evm,
469
            GenericCall(
470
                gas=message_call_gas.sub_call,
471
                value=value,
472
                caller=evm.message.current_target,
473
                to=to,
474
                code_address=code_address,
475
                should_transfer_value=True,
476
                is_staticcall=False,
477
                memory_input_start_position=memory_input_start_position,
478
                memory_input_size=memory_input_size,
479
                memory_output_start_position=memory_output_start_position,
480
                memory_output_size=memory_output_size,
481
            ),
482
        )
483
484
    # PROGRAM COUNTER
485
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
489
    <snip>
498
    # STACK
499
    beneficiary = to_address_masked(pop(evm.stack))
500
501
    # GAS
502
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
503
    if beneficiary not in evm.accessed_addresses:
504
        evm.accessed_addresses.add(beneficiary)
505
        gas_cost += GasCosts.COLD_ACCOUNT_ACCESS
506
507
    if (
508
        not is_account_alive(evm.message.tx_env.state, beneficiary)
509
        and get_account(
510
            evm.message.tx_env.state, evm.message.current_target
511
        ).balance
512
        != 0
513
    ):
514
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
515
516
    originator = evm.message.current_target
517
518
    refunded_accounts = evm.accounts_to_delete
519
    parent_evm = evm.message.parent_evm
520
    while parent_evm is not None:
521
        refunded_accounts.update(parent_evm.accounts_to_delete)
522
        parent_evm = parent_evm.message.parent_evm
523
524
    if originator not in refunded_accounts:
525
        evm.refund_counter += GasCosts.REFUND_SELF_DESTRUCT
526
527
    charge_gas(evm, gas_cost)
528
    if evm.message.is_static:
529
        raise WriteInStaticContext
530
531
    originator = evm.message.current_target
532
    beneficiary_balance = get_account(
533
        evm.message.tx_env.state, beneficiary
534
    ).balance
535
    originator_balance = get_account(
536
        evm.message.tx_env.state, originator
537
    ).balance
538
539
    # First Transfer to beneficiary
540
    set_account_balance(
541
        evm.message.tx_env.state,
542
        beneficiary,
543
        beneficiary_balance + originator_balance,
544
    )
545
    # Next, Zero the balance of the address being deleted (must come after
546
    # sending to beneficiary in case the contract named itself as the
547
    # beneficiary).
548
    set_account_balance(evm.message.tx_env.state, originator, U256(0))
549
550
    # register account for deletion
551
    evm.accounts_to_delete.add(originator)
552
553
    # mark beneficiary as touched
554
    if account_exists_and_is_empty(evm.message.tx_env.state, beneficiary):
555
        evm.touched_accounts.add(beneficiary)
556
557
    # HALT the execution
558
    evm.running = False
559
560
    # PROGRAM COUNTER
561
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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