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

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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