ethereum.muir_glacier.vm.instructions.systemethereum.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:
69
    """
70
    Core logic used by the `CREATE*` family of opcodes.
71
    """
72
    # This import causes a circular import error
73
    # if it's not moved inside this method
74
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create_message
75
76
    call_data = memory_read_bytes(
77
        evm.memory, memory_start_position, memory_size
78
    )
79
80
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
81
    evm.gas_left -= create_message_gas
82
    if evm.message.is_static:
83
        raise WriteInStaticContext
84
    evm.return_data = b""
85
86
    sender_address = evm.message.current_target
87
    sender = get_account(evm.message.block_env.state, sender_address)
88
89
    if (
90
        sender.balance < endowment
91
        or sender.nonce == Uint(2**64 - 1)
92
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
93
    ):
94
        evm.gas_left += create_message_gas
95
        push(evm.stack, U256(0))
96
        return
97
98
    evm.accessed_addresses.add(contract_address)
99
100
    if account_has_code_or_nonce(
101
        evm.message.block_env.state, contract_address
102
    ) or account_has_storage(evm.message.block_env.state, contract_address):
103
        increment_nonce(
104
            evm.message.block_env.state, evm.message.current_target
105
        )
106
        push(evm.stack, U256(0))
107
        return
104
105
    call_data = memory_read_bytes(
106
        evm.memory, memory_start_position, memory_size
107
    )
108
109
    increment_nonce(evm.message.block_env.state, evm.message.current_target)
110
111
    child_message = Message(
112
        block_env=evm.message.block_env,
113
        tx_env=evm.message.tx_env,
114
        caller=evm.message.current_target,
115
        target=Bytes0(),
116
        gas=create_message_gas,
117
        value=endowment,
118
        data=b"",
119
        code=call_data,
120
        current_target=contract_address,
121
        depth=evm.message.depth + Uint(1),
122
        code_address=None,
123
        should_transfer_value=True,
124
        is_static=False,
125
        accessed_addresses=evm.accessed_addresses.copy(),
126
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
127
        parent_evm=evm,
128
    )
129
    child_evm = process_create_message(child_message)
130
131
    if child_evm.error:
132
        incorporate_child_on_error(evm, child_evm)
133
        evm.return_data = child_evm.output
134
        push(evm.stack, U256(0))
135
    else:
136
        incorporate_child_on_success(evm, child_evm)
137
        evm.return_data = b""
138
        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:
142
    """
143
    Creates a new account with associated code.
144
145
    Parameters
146
    ----------
147
    evm :
148
        The current EVM frame.
149
    """
150
    # STACK
151
    endowment = pop(evm.stack)
152
    memory_start_position = pop(evm.stack)
153
    memory_size = pop(evm.stack)
154
155
    # GAS
156
    extend_memory = calculate_gas_extend_memory(
157
        evm.memory, [(memory_start_position, memory_size)]
158
    )
159
160
    charge_gas(evm, GAS_CREATE + extend_memory.cost)
161
162
    # OPERATION
163
    evm.memory += b"\x00" * extend_memory.expand_by
164
    contract_address = compute_contract_address(
165
        evm.message.current_target,
166
        get_account(
167
            evm.message.block_env.state, evm.message.current_target
168
        ).nonce,
169
    )
170
171
    generic_create(
172
        evm, endowment, contract_address, memory_start_position, memory_size
173
    )
174
175
    # PROGRAM COUNTER
176
    evm.pc += Uint(1)

create2

Creates a new account with associated code.

It's similar to CREATE opcode except that the address of new account depends on the init_code instead of the nonce of sender.

Parameters

evm : The current EVM frame.

def create2(evm: Evm) -> None:
180
    """
181
    Creates a new account with associated code.
182
183
    It's similar to CREATE opcode except that the address of new account
184
    depends on the init_code instead of the nonce of sender.
185
186
    Parameters
187
    ----------
188
    evm :
189
        The current EVM frame.
190
    """
191
    # STACK
192
    endowment = pop(evm.stack)
193
    memory_start_position = pop(evm.stack)
194
    memory_size = pop(evm.stack)
195
    salt = pop(evm.stack).to_be_bytes32()
196
197
    # GAS
198
    extend_memory = calculate_gas_extend_memory(
199
        evm.memory, [(memory_start_position, memory_size)]
200
    )
201
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
202
    charge_gas(
203
        evm,
204
        GAS_CREATE + GAS_KECCAK256_WORD * call_data_words + extend_memory.cost,
205
    )
206
207
    # OPERATION
208
    evm.memory += b"\x00" * extend_memory.expand_by
209
    contract_address = compute_create2_contract_address(
210
        evm.message.current_target,
211
        salt,
212
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
213
    )
214
215
    generic_create(
216
        evm, endowment, contract_address, memory_start_position, memory_size
217
    )
218
219
    # PROGRAM COUNTER
220
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
224
    """
225
    Halts execution returning output data.
226
227
    Parameters
228
    ----------
229
    evm :
230
        The current EVM frame.
231
    """
232
    # STACK
233
    memory_start_position = pop(evm.stack)
234
    memory_size = pop(evm.stack)
235
236
    # GAS
237
    extend_memory = calculate_gas_extend_memory(
238
        evm.memory, [(memory_start_position, memory_size)]
239
    )
240
241
    charge_gas(evm, GAS_ZERO + extend_memory.cost)
242
243
    # OPERATION
244
    evm.memory += b"\x00" * extend_memory.expand_by
245
    evm.output = memory_read_bytes(
246
        evm.memory, memory_start_position, memory_size
247
    )
248
249
    evm.running = False
250
251
    # PROGRAM COUNTER
252
    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:
269
    """
270
    Perform the core logic of the `CALL*` family of opcodes.
271
    """
272
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
273
274
    evm.return_data = b""
275
276
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
277
        evm.gas_left += gas
278
        push(evm.stack, U256(0))
279
        return
280
281
    call_data = memory_read_bytes(
282
        evm.memory, memory_input_start_position, memory_input_size
283
    )
284
    code = get_account(evm.message.block_env.state, code_address).code
285
    child_message = Message(
286
        block_env=evm.message.block_env,
287
        tx_env=evm.message.tx_env,
288
        caller=caller,
289
        target=to,
290
        gas=gas,
291
        value=value,
292
        data=call_data,
293
        code=code,
294
        current_target=to,
295
        depth=evm.message.depth + Uint(1),
296
        code_address=code_address,
297
        should_transfer_value=should_transfer_value,
298
        is_static=True if is_staticcall else evm.message.is_static,
299
        accessed_addresses=evm.accessed_addresses.copy(),
300
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
301
        parent_evm=evm,
302
    )
303
    child_evm = process_message(child_message)
304
305
    if child_evm.error:
306
        incorporate_child_on_error(evm, child_evm)
307
        evm.return_data = child_evm.output
308
        push(evm.stack, U256(0))
309
    else:
310
        incorporate_child_on_success(evm, child_evm)
311
        evm.return_data = child_evm.output
312
        push(evm.stack, U256(1))
313
314
    actual_output_size = min(memory_output_size, U256(len(child_evm.output)))
315
    memory_write(
316
        evm.memory,
317
        memory_output_start_position,
318
        child_evm.output[:actual_output_size],
319
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(evm: Evm) -> None:
607
    """
608
    Message-call into an account.
609
610
    Parameters
611
    ----------
612
    evm :
613
        The current EVM frame.
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 = GAS_WARM_ACCESS
634
    else:
635
        evm.accessed_addresses.add(to)
636
        access_gas_cost = GAS_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,
611
        GAS_CALL,
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
    Parameters
675
    ----------
676
    evm :
677
        The current EVM frame.
678
    """
679
    # STACK
680
    memory_start_index = pop(evm.stack)
681
    size = pop(evm.stack)
682
683
    # GAS
684
    extend_memory = calculate_gas_extend_memory(
685
        evm.memory, [(memory_start_index, size)]
686
    )
687
688
    charge_gas(evm, extend_memory.cost)
689
690
    # OPERATION
691
    evm.memory += b"\x00" * extend_memory.expand_by
692
    output = memory_read_bytes(evm.memory, memory_start_index, size)
693
    evm.output = Bytes(output)
694
    raise Revert