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

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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