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

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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