ethereum.forks.bpo1.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,
161
        GasCosts.OPCODE_CREATE_BASE + extend_memory.cost + init_code_gas,
162
    )
163
164
    # OPERATION
165
    evm.memory += b"\x00" * extend_memory.expand_by
166
    contract_address = compute_contract_address(
167
        evm.message.current_target,
168
        get_account(
169
            evm.message.tx_env.state, evm.message.current_target
170
        ).nonce,
171
    )
172
173
    generic_create(
174
        evm,
175
        endowment,
176
        contract_address,
177
        memory_start_position,
178
        memory_size,
179
    )
180
181
    # PROGRAM COUNTER
182
    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:
186
    """
187
    Creates a new account with associated code.
188
189
    It's similar to the CREATE opcode except that the address of the new
190
    account depends on the init_code instead of the nonce of sender.
191
192
    Parameters
193
    ----------
194
    evm :
195
        The current EVM frame.
196
197
    """
198
    # STACK
199
    endowment = pop(evm.stack)
200
    memory_start_position = pop(evm.stack)
201
    memory_size = pop(evm.stack)
202
    salt = pop(evm.stack).to_be_bytes32()
203
204
    # GAS
205
    extend_memory = calculate_gas_extend_memory(
206
        evm.memory, [(memory_start_position, memory_size)]
207
    )
208
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
209
    init_code_gas = init_code_cost(Uint(memory_size))
210
    charge_gas(
211
        evm,
212
        GasCosts.OPCODE_CREATE_BASE
213
        + GasCosts.OPCODE_KECCACK256_PER_WORD * call_data_words
214
        + extend_memory.cost
215
        + init_code_gas,
216
    )
217
218
    # OPERATION
219
    evm.memory += b"\x00" * extend_memory.expand_by
220
    contract_address = compute_create2_contract_address(
221
        evm.message.current_target,
222
        salt,
223
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
224
    )
225
226
    generic_create(
227
        evm,
228
        endowment,
229
        contract_address,
230
        memory_start_position,
231
        memory_size,
232
    )
233
234
    # PROGRAM COUNTER
235
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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