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

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
320
    """
321
    Message-call into an account.
322
323
    Parameters
324
    ----------
325
    evm :
326
        The current EVM frame.
327
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 = GasCosts.WARM_ACCESS
349
    else:
350
        evm.accessed_addresses.add(to)
351
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
352
353
    code_address = to
354
355
    create_gas_cost = GasCosts.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 GasCosts.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
    """
407
    # STACK
408
    gas = Uint(pop(evm.stack))
409
    code_address = to_address_masked(pop(evm.stack))
410
    value = pop(evm.stack)
411
    memory_input_start_position = pop(evm.stack)
412
    memory_input_size = pop(evm.stack)
413
    memory_output_start_position = pop(evm.stack)
414
    memory_output_size = pop(evm.stack)
415
416
    # GAS
417
    to = evm.message.current_target
418
419
    extend_memory = calculate_gas_extend_memory(
420
        evm.memory,
421
        [
422
            (memory_input_start_position, memory_input_size),
423
            (memory_output_start_position, memory_output_size),
424
        ],
425
    )
426
427
    if code_address in evm.accessed_addresses:
428
        access_gas_cost = GasCosts.WARM_ACCESS
429
    else:
430
        evm.accessed_addresses.add(code_address)
431
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
432
433
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
434
    message_call_gas = calculate_message_call_gas(
435
        value,
436
        gas,
437
        Uint(evm.gas_left),
438
        extend_memory.cost,
439
        access_gas_cost + transfer_gas_cost,
440
    )
441
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
442
443
    # OPERATION
444
    evm.memory += b"\x00" * extend_memory.expand_by
445
    sender_balance = get_account(
446
        evm.message.block_env.state, evm.message.current_target
447
    ).balance
448
    if sender_balance < value:
449
        push(evm.stack, U256(0))
450
        evm.return_data = b""
451
        evm.gas_left += message_call_gas.sub_call
452
    else:
453
        generic_call(
454
            evm,
455
            message_call_gas.sub_call,
456
            value,
457
            evm.message.current_target,
458
            to,
459
            code_address,
460
            True,
461
            False,
462
            memory_input_start_position,
463
            memory_input_size,
464
            memory_output_start_position,
465
            memory_output_size,
466
        )
467
468
    # PROGRAM COUNTER
469
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
473
    """
474
    Halt execution and register account for later deletion.
475
476
    Parameters
477
    ----------
478
    evm :
479
        The current EVM frame.
480
481
    """
482
    # STACK
483
    beneficiary = to_address_masked(pop(evm.stack))
484
485
    # GAS
486
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
487
    if beneficiary not in evm.accessed_addresses:
488
        evm.accessed_addresses.add(beneficiary)
489
        gas_cost += GasCosts.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 += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
499
500
    charge_gas(evm, gas_cost)
501
    if evm.message.is_static:
502
        raise WriteInStaticContext
503
504
    originator = evm.message.current_target
505
    beneficiary_balance = get_account(
506
        evm.message.block_env.state, beneficiary
507
    ).balance
508
    originator_balance = get_account(
509
        evm.message.block_env.state, originator
510
    ).balance
511
512
    # First Transfer to beneficiary
513
    set_account_balance(
514
        evm.message.block_env.state,
515
        beneficiary,
516
        beneficiary_balance + originator_balance,
517
    )
518
    # Next, Zero the balance of the address being deleted (must come after
519
    # sending to beneficiary in case the contract named itself as the
520
    # beneficiary).
521
    set_account_balance(evm.message.block_env.state, originator, U256(0))
522
523
    # register account for deletion
524
    evm.accounts_to_delete.add(originator)
525
526
    # mark beneficiary as touched
527
    if account_exists_and_is_empty(evm.message.block_env.state, beneficiary):
528
        evm.touched_accounts.add(beneficiary)
529
530
    # HALT the execution
531
    evm.running = False
532
533
    # PROGRAM COUNTER
534
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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