ethereum.forks.bpo3.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:
61
    """
62
    Core logic used by the `CREATE*` family of opcodes.
63
    """
64
    # This import causes a circular import error
65
    # if it's not moved inside this method
66
    from ...vm.interpreter import (
67
        MAX_INIT_CODE_SIZE,
68
        STACK_DEPTH_LIMIT,
69
        process_create_message,
70
    )
71
72
    call_data = memory_read_bytes(
73
        evm.memory, memory_start_position, memory_size
74
    )
75
    if len(call_data) > MAX_INIT_CODE_SIZE:
76
        raise OutOfGasError
77
78
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
79
    evm.gas_left -= create_message_gas
80
    if evm.message.is_static:
81
        raise WriteInStaticContext
82
    evm.return_data = b""
83
84
    sender_address = evm.message.current_target
85
    sender = get_account(evm.message.tx_env.state, sender_address)
86
87
    if (
88
        sender.balance < endowment
89
        or sender.nonce == Uint(2**64 - 1)
90
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
91
    ):
92
        evm.gas_left += create_message_gas
93
        push(evm.stack, U256(0))
94
        return
95
96
    evm.accessed_addresses.add(contract_address)
97
98
    if account_has_code_or_nonce(
99
        evm.message.tx_env.state, contract_address
100
    ) or account_has_storage(evm.message.tx_env.state, contract_address):
101
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
102
        push(evm.stack, U256(0))
103
        return
104
105
    increment_nonce(evm.message.tx_env.state, evm.message.current_target)
106
107
    child_message = Message(
108
        block_env=evm.message.block_env,
109
        tx_env=evm.message.tx_env,
110
        caller=evm.message.current_target,
111
        target=Bytes0(),
112
        gas=create_message_gas,
113
        value=endowment,
114
        data=b"",
115
        code=call_data,
116
        current_target=contract_address,
117
        depth=evm.message.depth + Uint(1),
118
        code_address=None,
119
        should_transfer_value=True,
120
        is_static=False,
121
        accessed_addresses=evm.accessed_addresses.copy(),
122
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
123
        disable_precompiles=False,
124
        parent_evm=evm,
125
    )
126
    child_evm = process_create_message(child_message)
127
128
    if child_evm.error:
129
        incorporate_child_on_error(evm, child_evm)
130
        evm.return_data = child_evm.output
131
        push(evm.stack, U256(0))
132
    else:
133
        incorporate_child_on_success(evm, child_evm)
134
        evm.return_data = b""
135
        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:
139
    """
140
    Creates a new account with associated code.
141
142
    Parameters
143
    ----------
144
    evm :
145
        The current EVM frame.
146
147
    """
148
    # STACK
149
    endowment = pop(evm.stack)
150
    memory_start_position = pop(evm.stack)
151
    memory_size = pop(evm.stack)
152
153
    # GAS
154
    extend_memory = calculate_gas_extend_memory(
155
        evm.memory, [(memory_start_position, memory_size)]
156
    )
157
    init_code_gas = init_code_cost(Uint(memory_size))
158
159
    charge_gas(
160
        evm, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost + init_code_gas
161
    )
162
163
    # OPERATION
164
    evm.memory += b"\x00" * extend_memory.expand_by
165
    contract_address = compute_contract_address(
166
        evm.message.current_target,
167
        get_account(
168
            evm.message.tx_env.state, evm.message.current_target
169
        ).nonce,
170
    )
171
172
    generic_create(
173
        evm,
174
        endowment,
175
        contract_address,
176
        memory_start_position,
177
        memory_size,
178
    )
179
180
    # PROGRAM COUNTER
181
    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:
185
    """
186
    Creates a new account with associated code.
187
188
    It's similar to the CREATE opcode except that the address of the new
189
    account depends on the init_code instead of the nonce of sender.
190
191
    Parameters
192
    ----------
193
    evm :
194
        The current EVM frame.
195
196
    """
197
    # STACK
198
    endowment = pop(evm.stack)
199
    memory_start_position = pop(evm.stack)
200
    memory_size = pop(evm.stack)
201
    salt = pop(evm.stack).to_be_bytes32()
202
203
    # GAS
204
    extend_memory = calculate_gas_extend_memory(
205
        evm.memory, [(memory_start_position, memory_size)]
206
    )
207
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
208
    init_code_gas = init_code_cost(Uint(memory_size))
209
    charge_gas(
210
        evm,
211
        GasCosts.OPCODE_CREATE_BASE
212
        + GasCosts.OPCODE_KECCACK256_PER_WORD * call_data_words
213
        + extend_memory.cost
214
        + init_code_gas,
215
    )
216
217
    # OPERATION
218
    evm.memory += b"\x00" * extend_memory.expand_by
219
    contract_address = compute_create2_contract_address(
220
        evm.message.current_target,
221
        salt,
222
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
223
    )
224
225
    generic_create(
226
        evm,
227
        endowment,
228
        contract_address,
229
        memory_start_position,
230
        memory_size,
231
    )
232
233
    # PROGRAM COUNTER
234
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
238
    """
239
    Halts execution returning output data.
240
241
    Parameters
242
    ----------
243
    evm :
244
        The current EVM frame.
245
246
    """
247
    # STACK
248
    memory_start_position = pop(evm.stack)
249
    memory_size = pop(evm.stack)
250
251
    # GAS
252
    extend_memory = calculate_gas_extend_memory(
253
        evm.memory, [(memory_start_position, memory_size)]
254
    )
255
256
    charge_gas(evm, GasCosts.ZERO + extend_memory.cost)
257
258
    # OPERATION
259
    evm.memory += b"\x00" * extend_memory.expand_by
260
    evm.output = memory_read_bytes(
261
        evm.memory, memory_start_position, memory_size
262
    )
263
264
    evm.running = False
265
266
    # PROGRAM COUNTER
267
    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, ​​code: Bytes, ​​disable_precompiles: bool) -> None:
286
    """
287
    Perform the core logic of the `CALL*` family of opcodes.
288
    """
289
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
290
291
    evm.return_data = b""
292
293
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
294
        evm.gas_left += gas
295
        push(evm.stack, U256(0))
296
        return
297
298
    call_data = memory_read_bytes(
299
        evm.memory, memory_input_start_position, memory_input_size
300
    )
301
302
    child_message = Message(
303
        block_env=evm.message.block_env,
304
        tx_env=evm.message.tx_env,
305
        caller=caller,
306
        target=to,
307
        gas=gas,
308
        value=value,
309
        data=call_data,
310
        code=code,
311
        current_target=to,
312
        depth=evm.message.depth + Uint(1),
313
        code_address=code_address,
314
        should_transfer_value=should_transfer_value,
315
        is_static=True if is_staticcall else evm.message.is_static,
316
        accessed_addresses=evm.accessed_addresses.copy(),
317
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
318
        disable_precompiles=disable_precompiles,
319
        parent_evm=evm,
320
    )
321
    child_evm = process_message(child_message)
322
323
    if child_evm.error:
324
        incorporate_child_on_error(evm, child_evm)
325
        evm.return_data = child_evm.output
326
        push(evm.stack, U256(0))
327
    else:
328
        incorporate_child_on_success(evm, child_evm)
329
        evm.return_data = child_evm.output
330
        push(evm.stack, U256(1))
331
332
    actual_output_size = min(memory_output_size, U256(len(child_evm.output)))
333
    memory_write(
334
        evm.memory,
335
        memory_output_start_position,
336
        child_evm.output[:actual_output_size],
337
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
341
    """
342
    Message-call into an account.
343
344
    Parameters
345
    ----------
346
    evm :
347
        The current EVM frame.
348
349
    """
350
    # STACK
351
    gas = Uint(pop(evm.stack))
352
    to = to_address_masked(pop(evm.stack))
353
    value = pop(evm.stack)
354
    memory_input_start_position = pop(evm.stack)
355
    memory_input_size = pop(evm.stack)
356
    memory_output_start_position = pop(evm.stack)
357
    memory_output_size = pop(evm.stack)
358
359
    # GAS
360
    extend_memory = calculate_gas_extend_memory(
361
        evm.memory,
362
        [
363
            (memory_input_start_position, memory_input_size),
364
            (memory_output_start_position, memory_output_size),
365
        ],
366
    )
367
368
    if to in evm.accessed_addresses:
369
        access_gas_cost = GasCosts.WARM_ACCESS
370
    else:
371
        evm.accessed_addresses.add(to)
372
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
373
374
    code_address = to
375
    (
376
        disable_precompiles,
377
        code_address,
378
        code,
379
        delegated_access_gas_cost,
380
    ) = access_delegation(evm, code_address)
381
    access_gas_cost += delegated_access_gas_cost
382
383
    create_gas_cost = GasCosts.NEW_ACCOUNT
384
    if value == 0 or is_account_alive(evm.message.tx_env.state, to):
385
        create_gas_cost = Uint(0)
386
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
387
    message_call_gas = calculate_message_call_gas(
388
        value,
389
        gas,
390
        Uint(evm.gas_left),
391
        extend_memory.cost,
392
        access_gas_cost + create_gas_cost + transfer_gas_cost,
393
    )
394
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
395
    if evm.message.is_static and value != U256(0):
396
        raise WriteInStaticContext
397
    evm.memory += b"\x00" * extend_memory.expand_by
398
    sender_balance = get_account(
399
        evm.message.tx_env.state, evm.message.current_target
400
    ).balance
401
    if sender_balance < value:
402
        push(evm.stack, U256(0))
403
        evm.return_data = b""
404
        evm.gas_left += message_call_gas.sub_call
405
    else:
406
        generic_call(
407
            evm,
408
            message_call_gas.sub_call,
409
            value,
410
            evm.message.current_target,
411
            to,
412
            code_address,
413
            True,
414
            False,
415
            memory_input_start_position,
416
            memory_input_size,
417
            memory_output_start_position,
418
            memory_output_size,
419
            code,
420
            disable_precompiles,
421
        )
422
423
    # PROGRAM COUNTER
424
    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:
428
    """
429
    Message-call into this account with alternative account’s code.
430
431
    Parameters
432
    ----------
433
    evm :
434
        The current EVM frame.
435
436
    """
437
    # STACK
438
    gas = Uint(pop(evm.stack))
439
    code_address = to_address_masked(pop(evm.stack))
440
    value = pop(evm.stack)
441
    memory_input_start_position = pop(evm.stack)
442
    memory_input_size = pop(evm.stack)
443
    memory_output_start_position = pop(evm.stack)
444
    memory_output_size = pop(evm.stack)
445
446
    # GAS
447
    to = evm.message.current_target
448
449
    extend_memory = calculate_gas_extend_memory(
450
        evm.memory,
451
        [
452
            (memory_input_start_position, memory_input_size),
453
            (memory_output_start_position, memory_output_size),
454
        ],
455
    )
456
457
    if code_address in evm.accessed_addresses:
458
        access_gas_cost = GasCosts.WARM_ACCESS
459
    else:
460
        evm.accessed_addresses.add(code_address)
461
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
462
463
    (
464
        disable_precompiles,
465
        code_address,
466
        code,
467
        delegated_access_gas_cost,
468
    ) = access_delegation(evm, code_address)
469
    access_gas_cost += delegated_access_gas_cost
470
471
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
472
    message_call_gas = calculate_message_call_gas(
473
        value,
474
        gas,
475
        Uint(evm.gas_left),
476
        extend_memory.cost,
477
        access_gas_cost + transfer_gas_cost,
478
    )
479
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
480
481
    # OPERATION
482
    evm.memory += b"\x00" * extend_memory.expand_by
483
    sender_balance = get_account(
484
        evm.message.tx_env.state, evm.message.current_target
485
    ).balance
486
    if sender_balance < value:
487
        push(evm.stack, U256(0))
488
        evm.return_data = b""
489
        evm.gas_left += message_call_gas.sub_call
490
    else:
491
        generic_call(
492
            evm,
493
            message_call_gas.sub_call,
494
            value,
495
            evm.message.current_target,
496
            to,
497
            code_address,
498
            True,
499
            False,
500
            memory_input_start_position,
501
            memory_input_size,
502
            memory_output_start_position,
503
            memory_output_size,
504
            code,
505
            disable_precompiles,
506
        )
507
508
    # PROGRAM COUNTER
509
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
513
    """
514
    Halt execution and register account for later deletion.
515
516
    Parameters
517
    ----------
518
    evm :
519
        The current EVM frame.
520
521
    """
522
    # STACK
523
    beneficiary = to_address_masked(pop(evm.stack))
524
525
    # GAS
526
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
527
    if beneficiary not in evm.accessed_addresses:
528
        evm.accessed_addresses.add(beneficiary)
529
        gas_cost += GasCosts.COLD_ACCOUNT_ACCESS
530
531
    if (
532
        not is_account_alive(evm.message.tx_env.state, beneficiary)
533
        and get_account(
534
            evm.message.tx_env.state, evm.message.current_target
535
        ).balance
536
        != 0
537
    ):
538
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
539
540
    charge_gas(evm, gas_cost)
541
    if evm.message.is_static:
542
        raise WriteInStaticContext
543
544
    originator = evm.message.current_target
545
    originator_balance = get_account(
546
        evm.message.tx_env.state, originator
547
    ).balance
548
549
    move_ether(
550
        evm.message.tx_env.state,
551
        originator,
552
        beneficiary,
553
        originator_balance,
554
    )
555
556
    # register account for deletion only if it was created
557
    # in the same transaction
558
    if originator in evm.message.tx_env.state.created_accounts:
559
        # If beneficiary is the same as originator, then
560
        # the ether is burnt.
561
        set_account_balance(evm.message.tx_env.state, originator, U256(0))
562
        evm.accounts_to_delete.add(originator)
563
564
    # HALT the execution
565
    evm.running = False
566
567
    # PROGRAM COUNTER
568
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
572
    """
573
    Message-call into an account.
574
575
    Parameters
576
    ----------
577
    evm :
578
        The current EVM frame.
579
580
    """
581
    # STACK
582
    gas = Uint(pop(evm.stack))
583
    code_address = to_address_masked(pop(evm.stack))
584
    memory_input_start_position = pop(evm.stack)
585
    memory_input_size = pop(evm.stack)
586
    memory_output_start_position = pop(evm.stack)
587
    memory_output_size = pop(evm.stack)
588
589
    # GAS
590
    extend_memory = calculate_gas_extend_memory(
591
        evm.memory,
592
        [
593
            (memory_input_start_position, memory_input_size),
594
            (memory_output_start_position, memory_output_size),
595
        ],
596
    )
597
598
    if code_address in evm.accessed_addresses:
599
        access_gas_cost = GasCosts.WARM_ACCESS
600
    else:
601
        evm.accessed_addresses.add(code_address)
602
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
603
604
    (
605
        disable_precompiles,
606
        code_address,
607
        code,
608
        delegated_access_gas_cost,
609
    ) = access_delegation(evm, code_address)
610
    access_gas_cost += delegated_access_gas_cost
611
612
    message_call_gas = calculate_message_call_gas(
613
        U256(0), gas, Uint(evm.gas_left), extend_memory.cost, access_gas_cost
614
    )
615
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
616
617
    # OPERATION
618
    evm.memory += b"\x00" * extend_memory.expand_by
619
    generic_call(
620
        evm,
621
        message_call_gas.sub_call,
622
        evm.message.value,
623
        evm.message.caller,
624
        evm.message.current_target,
625
        code_address,
626
        False,
627
        False,
628
        memory_input_start_position,
629
        memory_input_size,
630
        memory_output_start_position,
631
        memory_output_size,
632
        code,
633
        disable_precompiles,
634
    )
635
636
    # PROGRAM COUNTER
637
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(evm: Evm) -> None:
641
    """
642
    Message-call into an account.
643
644
    Parameters
645
    ----------
646
    evm :
647
        The current EVM frame.
648
649
    """
650
    # STACK
651
    gas = Uint(pop(evm.stack))
652
    to = to_address_masked(pop(evm.stack))
653
    memory_input_start_position = pop(evm.stack)
654
    memory_input_size = pop(evm.stack)
655
    memory_output_start_position = pop(evm.stack)
656
    memory_output_size = pop(evm.stack)
657
658
    # GAS
659
    extend_memory = calculate_gas_extend_memory(
660
        evm.memory,
661
        [
662
            (memory_input_start_position, memory_input_size),
663
            (memory_output_start_position, memory_output_size),
664
        ],
665
    )
666
667
    if to in evm.accessed_addresses:
668
        access_gas_cost = GasCosts.WARM_ACCESS
669
    else:
670
        evm.accessed_addresses.add(to)
671
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
672
673
    code_address = to
674
    (
675
        disable_precompiles,
676
        code_address,
677
        code,
678
        delegated_access_gas_cost,
679
    ) = access_delegation(evm, code_address)
680
    access_gas_cost += delegated_access_gas_cost
681
682
    message_call_gas = calculate_message_call_gas(
683
        U256(0),
684
        gas,
685
        Uint(evm.gas_left),
686
        extend_memory.cost,
687
        access_gas_cost,
688
    )
689
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
690
691
    # OPERATION
692
    evm.memory += b"\x00" * extend_memory.expand_by
693
    generic_call(
694
        evm,
695
        message_call_gas.sub_call,
696
        U256(0),
697
        evm.message.current_target,
698
        to,
699
        code_address,
700
        True,
701
        True,
702
        memory_input_start_position,
703
        memory_input_size,
704
        memory_output_start_position,
705
        memory_output_size,
706
        code,
707
        disable_precompiles,
708
    )
709
710
    # PROGRAM COUNTER
711
    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:
715
    """
716
    Stop execution and revert state changes, without consuming all provided gas
717
    and also has the ability to return a reason.
718
719
    Parameters
720
    ----------
721
    evm :
722
        The current EVM frame.
723
724
    """
725
    # STACK
726
    memory_start_index = pop(evm.stack)
727
    size = pop(evm.stack)
728
729
    # GAS
730
    extend_memory = calculate_gas_extend_memory(
731
        evm.memory, [(memory_start_index, size)]
732
    )
733
734
    charge_gas(evm, extend_memory.cost)
735
736
    # OPERATION
737
    evm.memory += b"\x00" * extend_memory.expand_by
738
    output = memory_read_bytes(evm.memory, memory_start_index, size)
739
    evm.output = Bytes(output)
740
    raise Revert
741
742
    # PROGRAM COUNTER
743
    # no-op