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

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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