ethereum.forks.arrow_glacier.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.tx_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.tx_env.state, contract_address
93
    ) or account_has_storage(evm.message.tx_env.state, contract_address):
94
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
95
        push(evm.stack, U256(0))
96
        return
97
98
    increment_nonce(evm.message.tx_env.state, evm.message.current_target)
99
100
    child_message = Message(
101
        block_env=evm.message.block_env,
102
        tx_env=evm.message.tx_env,
103
        caller=evm.message.current_target,
104
        target=Bytes0(),
105
        gas=create_message_gas,
106
        value=endowment,
107
        data=b"",
108
        code=call_data,
109
        current_target=contract_address,
110
        depth=evm.message.depth + Uint(1),
111
        code_address=None,
112
        should_transfer_value=True,
113
        is_static=False,
114
        accessed_addresses=evm.accessed_addresses.copy(),
115
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
116
        parent_evm=evm,
117
    )
118
    child_evm = process_create_message(child_message)
119
120
    if child_evm.error:
121
        incorporate_child_on_error(evm, child_evm)
122
        evm.return_data = child_evm.output
123
        push(evm.stack, U256(0))
124
    else:
125
        incorporate_child_on_success(evm, child_evm)
126
        evm.return_data = b""
127
        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:
131
    """
132
    Creates a new account with associated code.
133
134
    Parameters
135
    ----------
136
    evm :
137
        The current EVM frame.
138
139
    """
140
    # STACK
141
    endowment = pop(evm.stack)
142
    memory_start_position = pop(evm.stack)
143
    memory_size = pop(evm.stack)
144
145
    # GAS
146
    extend_memory = calculate_gas_extend_memory(
147
        evm.memory, [(memory_start_position, memory_size)]
148
    )
149
150
    charge_gas(evm, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost)
151
152
    # OPERATION
153
    evm.memory += b"\x00" * extend_memory.expand_by
154
    contract_address = compute_contract_address(
155
        evm.message.current_target,
156
        get_account(
157
            evm.message.tx_env.state, evm.message.current_target
158
        ).nonce,
159
    )
160
161
    generic_create(
162
        evm, endowment, contract_address, memory_start_position, memory_size
163
    )
164
165
    # PROGRAM COUNTER
166
    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:
170
    """
171
    Creates a new account with associated code.
172
173
    It's similar to the CREATE opcode except that the address of the new
174
    account depends on the init_code instead of the nonce of sender.
175
176
    Parameters
177
    ----------
178
    evm :
179
        The current EVM frame.
180
181
    """
182
    # STACK
183
    endowment = pop(evm.stack)
184
    memory_start_position = pop(evm.stack)
185
    memory_size = pop(evm.stack)
186
    salt = pop(evm.stack).to_be_bytes32()
187
188
    # GAS
189
    extend_memory = calculate_gas_extend_memory(
190
        evm.memory, [(memory_start_position, memory_size)]
191
    )
192
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
193
    charge_gas(
194
        evm,
195
        GasCosts.OPCODE_CREATE_BASE
196
        + GasCosts.OPCODE_KECCACK256_PER_WORD * call_data_words
197
        + extend_memory.cost,
198
    )
199
200
    # OPERATION
201
    evm.memory += b"\x00" * extend_memory.expand_by
202
    contract_address = compute_create2_contract_address(
203
        evm.message.current_target,
204
        salt,
205
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
206
    )
207
208
    generic_create(
209
        evm, endowment, contract_address, memory_start_position, memory_size
210
    )
211
212
    # PROGRAM COUNTER
213
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
471
    """
472
    Halt execution and register account for later deletion.
473
474
    Parameters
475
    ----------
476
    evm :
477
        The current EVM frame.
478
479
    """
480
    # STACK
481
    beneficiary = to_address_masked(pop(evm.stack))
482
483
    # GAS
484
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
485
    if beneficiary not in evm.accessed_addresses:
486
        evm.accessed_addresses.add(beneficiary)
487
        gas_cost += GasCosts.COLD_ACCOUNT_ACCESS
488
489
    if (
490
        not is_account_alive(evm.message.tx_env.state, beneficiary)
491
        and get_account(
492
            evm.message.tx_env.state, evm.message.current_target
493
        ).balance
494
        != 0
495
    ):
496
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_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.tx_env.state, beneficiary
505
    ).balance
506
    originator_balance = get_account(
507
        evm.message.tx_env.state, originator
508
    ).balance
509
510
    # First Transfer to beneficiary
511
    set_account_balance(
512
        evm.message.tx_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.tx_env.state, originator, U256(0))
520
521
    # register account for deletion
522
    evm.accounts_to_delete.add(originator)
523
524
    # mark beneficiary as touched
525
    if account_exists_and_is_empty(evm.message.tx_env.state, beneficiary):
526
        evm.touched_accounts.add(beneficiary)
527
528
    # HALT the execution
529
    evm.running = False
530
531
    # PROGRAM COUNTER
532
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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