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

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

GenericCall

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

257
@final
258
@dataclass
class GenericCall:

gas

264
    gas: Uint

value

265
    value: U256

caller

266
    caller: Address

to

267
    to: Address

code_address

268
    code_address: Address

should_transfer_value

269
    should_transfer_value: bool

is_staticcall

270
    is_staticcall: bool

memory_input_start_position

271
    memory_input_start_position: U256

memory_input_size

272
    memory_input_size: U256

memory_output_start_position

273
    memory_output_start_position: U256

memory_output_size

274
    memory_output_size: U256

generic_call

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

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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