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:
60
    """
61
    Core logic used by the `CREATE*` family of opcodes.
62
    """
63
    # This import causes a circular import error
64
    # if it's not moved inside this method
65
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create_message
66
67
    call_data = memory_read_bytes(
68
        evm.memory, memory_start_position, memory_size
69
    )
70
71
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
72
    evm.gas_left -= create_message_gas
73
    if evm.message.is_static:
74
        raise WriteInStaticContext
75
    evm.return_data = b""
76
77
    sender_address = evm.message.current_target
78
    sender = get_account(evm.message.tx_env.state, sender_address)
79
80
    if (
81
        sender.balance < endowment
82
        or sender.nonce == Uint(2**64 - 1)
83
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
84
    ):
85
        evm.gas_left += create_message_gas
86
        push(evm.stack, U256(0))
87
        return
88
89
    evm.accessed_addresses.add(contract_address)
90
91
    if account_has_code_or_nonce(
92
        evm.message.tx_env.state, contract_address
93
    ) or account_has_storage(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
    """
132
    Creates a new account with associated code.
133
134
    Parameters
135
    ----------
136
    evm :
137
        The current EVM frame.
138
139
    """
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, endowment, contract_address, memory_start_position, memory_size
163
    )
164
165
    # PROGRAM COUNTER
166
    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:
170
    """
171
    Creates a new account with associated code.
172
173
    It's similar to the CREATE opcode except that the address of the new
174
    account depends on the init_code instead of the nonce of sender.
175
176
    Parameters
177
    ----------
178
    evm :
179
        The current EVM frame.
180
181
    """
182
    # STACK
183
    endowment = pop(evm.stack)
184
    memory_start_position = pop(evm.stack)
185
    memory_size = pop(evm.stack)
186
    salt = pop(evm.stack).to_be_bytes32()
187
188
    # GAS
189
    extend_memory = calculate_gas_extend_memory(
190
        evm.memory, [(memory_start_position, memory_size)]
191
    )
192
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
193
    charge_gas(
194
        evm,
195
        GasCosts.OPCODE_CREATE_BASE
196
        + GasCosts.OPCODE_KECCACK256_PER_WORD * call_data_words
197
        + extend_memory.cost,
198
    )
199
200
    # OPERATION
201
    evm.memory += b"\x00" * extend_memory.expand_by
202
    contract_address = compute_create2_contract_address(
203
        evm.message.current_target,
204
        salt,
205
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
206
    )
207
208
    generic_create(
209
        evm, endowment, contract_address, memory_start_position, memory_size
210
    )
211
212
    # PROGRAM COUNTER
213
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
217
    """
218
    Halts execution returning output data.
219
220
    Parameters
221
    ----------
222
    evm :
223
        The current EVM frame.
224
225
    """
226
    # STACK
227
    memory_start_position = pop(evm.stack)
228
    memory_size = pop(evm.stack)
229
230
    # GAS
231
    extend_memory = calculate_gas_extend_memory(
232
        evm.memory, [(memory_start_position, memory_size)]
233
    )
234
235
    charge_gas(evm, GasCosts.ZERO + extend_memory.cost)
236
237
    # OPERATION
238
    evm.memory += b"\x00" * extend_memory.expand_by
239
    evm.output = memory_read_bytes(
240
        evm.memory, memory_start_position, memory_size
241
    )
242
243
    evm.running = False
244
245
    # PROGRAM COUNTER
246
    pass

generic_call

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

def generic_call(evm: Evm, ​​gas: Uint, ​​value: U256, ​​caller: Address, ​​to: Address, ​​code_address: Address, ​​should_transfer_value: bool, ​​is_staticcall: bool, ​​memory_input_start_position: U256, ​​memory_input_size: U256, ​​memory_output_start_position: U256, ​​memory_output_size: U256) -> None:
263
    """
264
    Perform the core logic of the `CALL*` family of opcodes.
265
    """
266
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
267
268
    evm.return_data = b""
269
270
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
271
        evm.gas_left += gas
272
        push(evm.stack, U256(0))
273
        return
274
275
    call_data = memory_read_bytes(
276
        evm.memory, memory_input_start_position, memory_input_size
277
    )
278
    _code_account = get_account(evm.message.tx_env.state, code_address)
279
    code = get_code(evm.message.tx_env.state, _code_account.code_hash)
280
    child_message = Message(
281
        block_env=evm.message.block_env,
282
        tx_env=evm.message.tx_env,
283
        caller=caller,
284
        target=to,
285
        gas=gas,
286
        value=value,
287
        data=call_data,
288
        code=code,
289
        current_target=to,
290
        depth=evm.message.depth + Uint(1),
291
        code_address=code_address,
292
        should_transfer_value=should_transfer_value,
293
        is_static=True if is_staticcall else evm.message.is_static,
294
        accessed_addresses=evm.accessed_addresses.copy(),
295
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
296
        parent_evm=evm,
297
    )
298
    child_evm = process_message(child_message)
299
300
    if child_evm.error:
301
        incorporate_child_on_error(evm, child_evm)
302
        evm.return_data = child_evm.output
303
        push(evm.stack, U256(0))
304
    else:
305
        incorporate_child_on_success(evm, child_evm)
306
        evm.return_data = child_evm.output
307
        push(evm.stack, U256(1))
308
309
    actual_output_size = min(memory_output_size, U256(len(child_evm.output)))
310
    memory_write(
311
        evm.memory,
312
        memory_output_start_position,
313
        child_evm.output[:actual_output_size],
314
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
318
    """
319
    Message-call into an account.
320
321
    Parameters
322
    ----------
323
    evm :
324
        The current EVM frame.
325
326
    """
327
    # STACK
328
    gas = Uint(pop(evm.stack))
329
    to = to_address_masked(pop(evm.stack))
330
    value = pop(evm.stack)
331
    memory_input_start_position = pop(evm.stack)
332
    memory_input_size = pop(evm.stack)
333
    memory_output_start_position = pop(evm.stack)
334
    memory_output_size = pop(evm.stack)
335
336
    # GAS
337
    extend_memory = calculate_gas_extend_memory(
338
        evm.memory,
339
        [
340
            (memory_input_start_position, memory_input_size),
341
            (memory_output_start_position, memory_output_size),
342
        ],
343
    )
344
345
    if to in evm.accessed_addresses:
346
        access_gas_cost = GasCosts.WARM_ACCESS
347
    else:
348
        evm.accessed_addresses.add(to)
349
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
350
351
    code_address = to
352
353
    create_gas_cost = GasCosts.NEW_ACCOUNT
354
    if value == 0 or is_account_alive(evm.message.tx_env.state, to):
355
        create_gas_cost = Uint(0)
356
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
357
    message_call_gas = calculate_message_call_gas(
358
        value,
359
        gas,
360
        Uint(evm.gas_left),
361
        extend_memory.cost,
362
        access_gas_cost + create_gas_cost + transfer_gas_cost,
363
    )
364
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
365
    if evm.message.is_static and value != U256(0):
366
        raise WriteInStaticContext
367
    evm.memory += b"\x00" * extend_memory.expand_by
368
    sender_balance = get_account(
369
        evm.message.tx_env.state, evm.message.current_target
370
    ).balance
371
    if sender_balance < value:
372
        push(evm.stack, U256(0))
373
        evm.return_data = b""
374
        evm.gas_left += message_call_gas.sub_call
375
    else:
376
        generic_call(
377
            evm,
378
            message_call_gas.sub_call,
379
            value,
380
            evm.message.current_target,
381
            to,
382
            code_address,
383
            True,
384
            False,
385
            memory_input_start_position,
386
            memory_input_size,
387
            memory_output_start_position,
388
            memory_output_size,
389
        )
390
391
    # PROGRAM COUNTER
392
    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:
396
    """
397
    Message-call into this account with alternative account’s code.
398
399
    Parameters
400
    ----------
401
    evm :
402
        The current EVM frame.
403
404
    """
405
    # STACK
406
    gas = Uint(pop(evm.stack))
407
    code_address = to_address_masked(pop(evm.stack))
408
    value = pop(evm.stack)
409
    memory_input_start_position = pop(evm.stack)
410
    memory_input_size = pop(evm.stack)
411
    memory_output_start_position = pop(evm.stack)
412
    memory_output_size = pop(evm.stack)
413
414
    # GAS
415
    to = evm.message.current_target
416
417
    extend_memory = calculate_gas_extend_memory(
418
        evm.memory,
419
        [
420
            (memory_input_start_position, memory_input_size),
421
            (memory_output_start_position, memory_output_size),
422
        ],
423
    )
424
425
    if code_address in evm.accessed_addresses:
426
        access_gas_cost = GasCosts.WARM_ACCESS
427
    else:
428
        evm.accessed_addresses.add(code_address)
429
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
430
431
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
432
    message_call_gas = calculate_message_call_gas(
433
        value,
434
        gas,
435
        Uint(evm.gas_left),
436
        extend_memory.cost,
437
        access_gas_cost + transfer_gas_cost,
438
    )
439
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
440
441
    # OPERATION
442
    evm.memory += b"\x00" * extend_memory.expand_by
443
    sender_balance = get_account(
444
        evm.message.tx_env.state, evm.message.current_target
445
    ).balance
446
    if sender_balance < value:
447
        push(evm.stack, U256(0))
448
        evm.return_data = b""
449
        evm.gas_left += message_call_gas.sub_call
450
    else:
451
        generic_call(
452
            evm,
453
            message_call_gas.sub_call,
454
            value,
455
            evm.message.current_target,
456
            to,
457
            code_address,
458
            True,
459
            False,
460
            memory_input_start_position,
461
            memory_input_size,
462
            memory_output_start_position,
463
            memory_output_size,
464
        )
465
466
    # PROGRAM COUNTER
467
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
471
    """
472
    Halt execution and register account for later deletion.
473
474
    Parameters
475
    ----------
476
    evm :
477
        The current EVM frame.
478
479
    """
480
    # STACK
481
    beneficiary = to_address_masked(pop(evm.stack))
482
483
    # GAS
484
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
485
    if beneficiary not in evm.accessed_addresses:
486
        evm.accessed_addresses.add(beneficiary)
487
        gas_cost += GasCosts.COLD_ACCOUNT_ACCESS
488
489
    if (
490
        not is_account_alive(evm.message.tx_env.state, beneficiary)
491
        and get_account(
492
            evm.message.tx_env.state, evm.message.current_target
493
        ).balance
494
        != 0
495
    ):
496
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
497
498
    originator = evm.message.current_target
499
500
    refunded_accounts = evm.accounts_to_delete
501
    parent_evm = evm.message.parent_evm
502
    while parent_evm is not None:
503
        refunded_accounts.update(parent_evm.accounts_to_delete)
504
        parent_evm = parent_evm.message.parent_evm
505
506
    if originator not in refunded_accounts:
507
        evm.refund_counter += GasCosts.REFUND_SELF_DESTRUCT
508
509
    charge_gas(evm, gas_cost)
510
    if evm.message.is_static:
511
        raise WriteInStaticContext
512
513
    originator = evm.message.current_target
514
    beneficiary_balance = get_account(
515
        evm.message.tx_env.state, beneficiary
516
    ).balance
517
    originator_balance = get_account(
518
        evm.message.tx_env.state, originator
519
    ).balance
520
521
    # First Transfer to beneficiary
522
    set_account_balance(
523
        evm.message.tx_env.state,
524
        beneficiary,
525
        beneficiary_balance + originator_balance,
526
    )
527
    # Next, Zero the balance of the address being deleted (must come after
528
    # sending to beneficiary in case the contract named itself as the
529
    # beneficiary).
530
    set_account_balance(evm.message.tx_env.state, originator, U256(0))
531
532
    # register account for deletion
533
    evm.accounts_to_delete.add(originator)
534
535
    # mark beneficiary as touched
536
    if account_exists_and_is_empty(evm.message.tx_env.state, beneficiary):
537
        evm.touched_accounts.add(beneficiary)
538
539
    # HALT the execution
540
    evm.running = False
541
542
    # PROGRAM COUNTER
543
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
547
    """
548
    Message-call into an account.
549
550
    Parameters
551
    ----------
552
    evm :
553
        The current EVM frame.
554
555
    """
556
    # STACK
557
    gas = Uint(pop(evm.stack))
558
    code_address = to_address_masked(pop(evm.stack))
559
    memory_input_start_position = pop(evm.stack)
560
    memory_input_size = pop(evm.stack)
561
    memory_output_start_position = pop(evm.stack)
562
    memory_output_size = pop(evm.stack)
563
564
    # GAS
565
    extend_memory = calculate_gas_extend_memory(
566
        evm.memory,
567
        [
568
            (memory_input_start_position, memory_input_size),
569
            (memory_output_start_position, memory_output_size),
570
        ],
571
    )
572
573
    if code_address in evm.accessed_addresses:
574
        access_gas_cost = GasCosts.WARM_ACCESS
575
    else:
576
        evm.accessed_addresses.add(code_address)
577
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
578
579
    message_call_gas = calculate_message_call_gas(
580
        U256(0), gas, Uint(evm.gas_left), extend_memory.cost, access_gas_cost
581
    )
582
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
583
584
    # OPERATION
585
    evm.memory += b"\x00" * extend_memory.expand_by
586
    generic_call(
587
        evm,
588
        message_call_gas.sub_call,
589
        evm.message.value,
590
        evm.message.caller,
591
        evm.message.current_target,
592
        code_address,
593
        False,
594
        False,
595
        memory_input_start_position,
596
        memory_input_size,
597
        memory_output_start_position,
598
        memory_output_size,
599
    )
600
601
    # PROGRAM COUNTER
602
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(evm: Evm) -> None:
606
    """
607
    Message-call into an account.
608
609
    Parameters
610
    ----------
611
    evm :
612
        The current EVM frame.
613
614
    """
615
    # STACK
616
    gas = Uint(pop(evm.stack))
617
    to = to_address_masked(pop(evm.stack))
618
    memory_input_start_position = pop(evm.stack)
619
    memory_input_size = pop(evm.stack)
620
    memory_output_start_position = pop(evm.stack)
621
    memory_output_size = pop(evm.stack)
622
623
    # GAS
624
    extend_memory = calculate_gas_extend_memory(
625
        evm.memory,
626
        [
627
            (memory_input_start_position, memory_input_size),
628
            (memory_output_start_position, memory_output_size),
629
        ],
630
    )
631
632
    if to in evm.accessed_addresses:
633
        access_gas_cost = GasCosts.WARM_ACCESS
634
    else:
635
        evm.accessed_addresses.add(to)
636
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
637
638
    code_address = to
639
640
    message_call_gas = calculate_message_call_gas(
641
        U256(0),
642
        gas,
643
        Uint(evm.gas_left),
644
        extend_memory.cost,
645
        access_gas_cost,
646
    )
647
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
648
649
    # OPERATION
650
    evm.memory += b"\x00" * extend_memory.expand_by
651
    generic_call(
652
        evm,
653
        message_call_gas.sub_call,
654
        U256(0),
655
        evm.message.current_target,
656
        to,
657
        code_address,
658
        True,
659
        True,
660
        memory_input_start_position,
661
        memory_input_size,
662
        memory_output_start_position,
663
        memory_output_size,
664
    )
665
666
    # PROGRAM COUNTER
667
    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:
671
    """
672
    Stop execution and revert state changes, without consuming all provided gas
673
    and also has the ability to return a reason.
674
675
    Parameters
676
    ----------
677
    evm :
678
        The current EVM frame.
679
680
    """
681
    # STACK
682
    memory_start_index = pop(evm.stack)
683
    size = pop(evm.stack)
684
685
    # GAS
686
    extend_memory = calculate_gas_extend_memory(
687
        evm.memory, [(memory_start_index, size)]
688
    )
689
690
    charge_gas(evm, extend_memory.cost)
691
692
    # OPERATION
693
    evm.memory += b"\x00" * extend_memory.expand_by
694
    output = memory_read_bytes(evm.memory, memory_start_index, size)
695
    evm.output = Bytes(output)
696
    raise Revert
697
698
    # PROGRAM COUNTER
699
    # no-op