ethereum.forks.bpo1.vm.instructions.systemethereum.forks.bpo2.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:
64
    <snip>
67
    # This import causes a circular import error
68
    # if it's not moved inside this method
69
    from ...vm.interpreter import (
70
        MAX_INIT_CODE_SIZE,
71
        STACK_DEPTH_LIMIT,
72
        process_create_message,
73
    )
74
75
    call_data = memory_read_bytes(
76
        evm.memory, memory_start_position, memory_size
77
    )
78
    if len(call_data) > MAX_INIT_CODE_SIZE:
79
        raise OutOfGasError
80
81
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
82
    evm.gas_left -= create_message_gas
83
    if evm.message.is_static:
84
        raise WriteInStaticContext
85
    evm.return_data = b""
86
87
    sender_address = evm.message.current_target
88
    sender = get_account(evm.message.tx_env.state, sender_address)
89
90
    if (
91
        sender.balance < endowment
92
        or sender.nonce == Uint(2**64 - 1)
93
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
94
    ):
95
        evm.gas_left += create_message_gas
96
        push(evm.stack, U256(0))
97
        return
98
99
    evm.accessed_addresses.add(contract_address)
100
101
    if account_has_code_or_nonce(
102
        evm.message.tx_env.state, contract_address
103
    ) or account_has_storage(evm.message.tx_env.state, contract_address):
104
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
105
        push(evm.stack, U256(0))
106
        return
107
108
    increment_nonce(evm.message.tx_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
        disable_precompiles=False,
127
        parent_evm=evm,
128
    )
129
    child_evm = process_create_message(child_message)
130
131
    if child_evm.error:
132
        incorporate_child_on_error(evm, child_evm)
133
        evm.return_data = child_evm.output
134
        push(evm.stack, U256(0))
135
    else:
136
        incorporate_child_on_success(evm, child_evm)
137
        evm.return_data = b""
138
        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:
142
    <snip>
151
    # STACK
152
    endowment = pop(evm.stack)
153
    memory_start_position = pop(evm.stack)
154
    memory_size = pop(evm.stack)
155
156
    # GAS
157
    extend_memory = calculate_gas_extend_memory(
158
        evm.memory, [(memory_start_position, memory_size)]
159
    )
160
    init_code_gas = init_code_cost(Uint(memory_size))
161
162
    charge_gas(
163
        evm,
164
        GasCosts.OPCODE_CREATE_BASE + extend_memory.cost + init_code_gas,
163
        evm, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost + init_code_gas
164
    )
165
166
    # OPERATION
167
    evm.memory += b"\x00" * extend_memory.expand_by
168
    contract_address = compute_contract_address(
169
        evm.message.current_target,
170
        get_account(
171
            evm.message.tx_env.state, evm.message.current_target
172
        ).nonce,
173
    )
174
175
    generic_create(
176
        evm,
177
        endowment,
178
        contract_address,
179
        memory_start_position,
180
        memory_size,
181
    )
182
183
    # PROGRAM COUNTER
184
    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:
188
    <snip>
200
    # STACK
201
    endowment = pop(evm.stack)
202
    memory_start_position = pop(evm.stack)
203
    memory_size = pop(evm.stack)
204
    salt = pop(evm.stack).to_be_bytes32()
205
206
    # GAS
207
    extend_memory = calculate_gas_extend_memory(
208
        evm.memory, [(memory_start_position, memory_size)]
209
    )
210
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
211
    init_code_gas = init_code_cost(Uint(memory_size))
212
    charge_gas(
213
        evm,
214
        GasCosts.OPCODE_CREATE_BASE
215
        + GasCosts.OPCODE_KECCAK256_PER_WORD * call_data_words
216
        + extend_memory.cost
217
        + init_code_gas,
218
    )
219
220
    # OPERATION
221
    evm.memory += b"\x00" * extend_memory.expand_by
222
    contract_address = compute_create2_contract_address(
223
        evm.message.current_target,
224
        salt,
225
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
226
    )
227
228
    generic_create(
229
        evm,
230
        endowment,
231
        contract_address,
232
        memory_start_position,
233
        memory_size,
234
    )
235
236
    # PROGRAM COUNTER
237
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
241
    <snip>
250
    # STACK
251
    memory_start_position = pop(evm.stack)
252
    memory_size = pop(evm.stack)
253
254
    # GAS
255
    extend_memory = calculate_gas_extend_memory(
256
        evm.memory, [(memory_start_position, memory_size)]
257
    )
258
259
    charge_gas(evm, GasCosts.ZERO + extend_memory.cost)
260
261
    # OPERATION
262
    evm.memory += b"\x00" * extend_memory.expand_by
263
    evm.output = memory_read_bytes(
264
        evm.memory, memory_start_position, memory_size
265
    )
266
267
    evm.running = False
268
269
    # PROGRAM COUNTER
270
    pass

GenericCall

Parameters for the core logic of the CALL* family of opcodes.

273
@final
274
@dataclass
class GenericCall:

gas

280
    gas: Uint

value

281
    value: U256

caller

282
    caller: Address

to

283
    to: Address

code_address

284
    code_address: Address

should_transfer_value

285
    should_transfer_value: bool

is_staticcall

286
    is_staticcall: bool

memory_input_start_position

287
    memory_input_start_position: U256

memory_input_size

288
    memory_input_size: U256

memory_output_start_position

289
    memory_output_start_position: U256

memory_output_size

290
    memory_output_size: U256

code

291
    code: Bytes

disable_precompiles

292
    disable_precompiles: bool

generic_call

Perform the core logic of the CALL* family of opcodes.

def generic_call(evm: Evm, ​​params: GenericCall) -> None:
296
    <snip>
299
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
300
301
    evm.return_data = b""
302
303
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
304
        evm.gas_left += params.gas
305
        push(evm.stack, U256(0))
306
        return
307
308
    call_data = memory_read_bytes(
309
        evm.memory,
310
        params.memory_input_start_position,
311
        params.memory_input_size,
312
    )
313
314
    child_message = Message(
315
        block_env=evm.message.block_env,
316
        tx_env=evm.message.tx_env,
317
        caller=params.caller,
318
        target=params.to,
319
        gas=params.gas,
320
        value=params.value,
321
        data=call_data,
322
        code=params.code,
323
        current_target=params.to,
324
        depth=evm.message.depth + Uint(1),
325
        code_address=params.code_address,
326
        should_transfer_value=params.should_transfer_value,
327
        is_static=params.is_staticcall or evm.message.is_static,
328
        accessed_addresses=evm.accessed_addresses.copy(),
329
        accessed_storage_keys=evm.accessed_storage_keys.copy(),
330
        disable_precompiles=params.disable_precompiles,
331
        parent_evm=evm,
332
    )
333
    child_evm = process_message(child_message)
334
335
    if child_evm.error:
336
        incorporate_child_on_error(evm, child_evm)
337
        evm.return_data = child_evm.output
338
        push(evm.stack, U256(0))
339
    else:
340
        incorporate_child_on_success(evm, child_evm)
341
        evm.return_data = child_evm.output
342
        push(evm.stack, U256(1))
343
344
    actual_output_size = min(
345
        params.memory_output_size, U256(len(child_evm.output))
346
    )
347
    memory_write(
348
        evm.memory,
349
        params.memory_output_start_position,
350
        child_evm.output[:actual_output_size],
351
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
355
    <snip>
364
    # STACK
365
    gas = Uint(pop(evm.stack))
366
    to = to_address_masked(pop(evm.stack))
367
    value = pop(evm.stack)
368
    memory_input_start_position = pop(evm.stack)
369
    memory_input_size = pop(evm.stack)
370
    memory_output_start_position = pop(evm.stack)
371
    memory_output_size = pop(evm.stack)
372
373
    # GAS
374
    extend_memory = calculate_gas_extend_memory(
375
        evm.memory,
376
        [
377
            (memory_input_start_position, memory_input_size),
378
            (memory_output_start_position, memory_output_size),
379
        ],
380
    )
381
382
    if to in evm.accessed_addresses:
383
        access_gas_cost = GasCosts.WARM_ACCESS
384
    else:
385
        evm.accessed_addresses.add(to)
386
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
387
388
    code_address = to
389
    (
390
        disable_precompiles,
391
        code_address,
392
        code,
393
        delegated_access_gas_cost,
394
    ) = access_delegation(evm, code_address)
395
    access_gas_cost += delegated_access_gas_cost
396
397
    create_gas_cost = GasCosts.NEW_ACCOUNT
398
    if value == 0 or is_account_alive(evm.message.tx_env.state, to):
399
        create_gas_cost = Uint(0)
400
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
401
    message_call_gas = calculate_message_call_gas(
402
        value,
403
        gas,
404
        Uint(evm.gas_left),
405
        memory_cost=extend_memory.cost,
406
        extra_gas=access_gas_cost + create_gas_cost + transfer_gas_cost,
407
    )
408
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
409
    if evm.message.is_static and value != U256(0):
410
        raise WriteInStaticContext
411
    evm.memory += b"\x00" * extend_memory.expand_by
412
    sender_balance = get_account(
413
        evm.message.tx_env.state, evm.message.current_target
414
    ).balance
415
    if sender_balance < value:
416
        push(evm.stack, U256(0))
417
        evm.return_data = b""
418
        evm.gas_left += message_call_gas.sub_call
419
    else:
420
        generic_call(
421
            evm,
422
            GenericCall(
423
                gas=message_call_gas.sub_call,
424
                value=value,
425
                caller=evm.message.current_target,
426
                to=to,
427
                code_address=code_address,
428
                should_transfer_value=True,
429
                is_staticcall=False,
430
                memory_input_start_position=memory_input_start_position,
431
                memory_input_size=memory_input_size,
432
                memory_output_start_position=memory_output_start_position,
433
                memory_output_size=memory_output_size,
434
                code=code,
435
                disable_precompiles=disable_precompiles,
436
            ),
437
        )
438
439
    # PROGRAM COUNTER
440
    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:
444
    <snip>
453
    # STACK
454
    gas = Uint(pop(evm.stack))
455
    code_address = to_address_masked(pop(evm.stack))
456
    value = pop(evm.stack)
457
    memory_input_start_position = pop(evm.stack)
458
    memory_input_size = pop(evm.stack)
459
    memory_output_start_position = pop(evm.stack)
460
    memory_output_size = pop(evm.stack)
461
462
    # GAS
463
    to = evm.message.current_target
464
465
    extend_memory = calculate_gas_extend_memory(
466
        evm.memory,
467
        [
468
            (memory_input_start_position, memory_input_size),
469
            (memory_output_start_position, memory_output_size),
470
        ],
471
    )
472
473
    if code_address in evm.accessed_addresses:
474
        access_gas_cost = GasCosts.WARM_ACCESS
475
    else:
476
        evm.accessed_addresses.add(code_address)
477
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
478
479
    (
480
        disable_precompiles,
481
        code_address,
482
        code,
483
        delegated_access_gas_cost,
484
    ) = access_delegation(evm, code_address)
485
    access_gas_cost += delegated_access_gas_cost
486
487
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
488
    message_call_gas = calculate_message_call_gas(
489
        value,
490
        gas,
491
        Uint(evm.gas_left),
492
        extend_memory.cost,
493
        access_gas_cost + transfer_gas_cost,
494
    )
495
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
496
497
    # OPERATION
498
    evm.memory += b"\x00" * extend_memory.expand_by
499
    sender_balance = get_account(
500
        evm.message.tx_env.state, evm.message.current_target
501
    ).balance
502
    if sender_balance < value:
503
        push(evm.stack, U256(0))
504
        evm.return_data = b""
505
        evm.gas_left += message_call_gas.sub_call
506
    else:
507
        generic_call(
508
            evm,
509
            GenericCall(
510
                gas=message_call_gas.sub_call,
511
                value=value,
512
                caller=evm.message.current_target,
513
                to=to,
514
                code_address=code_address,
515
                should_transfer_value=True,
516
                is_staticcall=False,
517
                memory_input_start_position=memory_input_start_position,
518
                memory_input_size=memory_input_size,
519
                memory_output_start_position=memory_output_start_position,
520
                memory_output_size=memory_output_size,
521
                code=code,
522
                disable_precompiles=disable_precompiles,
523
            ),
524
        )
525
526
    # PROGRAM COUNTER
527
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
531
    <snip>
540
    # STACK
541
    beneficiary = to_address_masked(pop(evm.stack))
542
543
    # GAS
544
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
545
    if beneficiary not in evm.accessed_addresses:
546
        evm.accessed_addresses.add(beneficiary)
547
        gas_cost += GasCosts.COLD_ACCOUNT_ACCESS
548
549
    if (
550
        not is_account_alive(evm.message.tx_env.state, beneficiary)
551
        and get_account(
552
            evm.message.tx_env.state, evm.message.current_target
553
        ).balance
554
        != 0
555
    ):
556
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
557
558
    charge_gas(evm, gas_cost)
559
    if evm.message.is_static:
560
        raise WriteInStaticContext
561
562
    originator = evm.message.current_target
563
    originator_balance = get_account(
564
        evm.message.tx_env.state, originator
565
    ).balance
566
567
    move_ether(
568
        evm.message.tx_env.state,
569
        originator,
570
        beneficiary,
571
        originator_balance,
572
    )
573
574
    # register account for deletion only if it was created
575
    # in the same transaction
576
    if originator in evm.message.tx_env.state.created_accounts:
577
        # If beneficiary is the same as originator, then
578
        # the ether is burnt.
579
        set_account_balance(evm.message.tx_env.state, originator, U256(0))
580
        evm.accounts_to_delete.add(originator)
581
582
    # HALT the execution
583
    evm.running = False
584
585
    # PROGRAM COUNTER
586
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
590
    <snip>
599
    # STACK
600
    gas = Uint(pop(evm.stack))
601
    code_address = 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 code_address in evm.accessed_addresses:
617
        access_gas_cost = GasCosts.WARM_ACCESS
618
    else:
619
        evm.accessed_addresses.add(code_address)
620
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
621
622
    (
623
        disable_precompiles,
624
        code_address,
625
        code,
626
        delegated_access_gas_cost,
627
    ) = access_delegation(evm, code_address)
628
    access_gas_cost += delegated_access_gas_cost
629
630
    message_call_gas = calculate_message_call_gas(
631
        U256(0), gas, Uint(evm.gas_left), extend_memory.cost, access_gas_cost
632
    )
633
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
634
635
    # OPERATION
636
    evm.memory += b"\x00" * extend_memory.expand_by
637
    generic_call(
638
        evm,
639
        GenericCall(
640
            gas=message_call_gas.sub_call,
641
            value=evm.message.value,
642
            caller=evm.message.caller,
643
            to=evm.message.current_target,
644
            code_address=code_address,
645
            should_transfer_value=False,
646
            is_staticcall=False,
647
            memory_input_start_position=memory_input_start_position,
648
            memory_input_size=memory_input_size,
649
            memory_output_start_position=memory_output_start_position,
650
            memory_output_size=memory_output_size,
651
            code=code,
652
            disable_precompiles=disable_precompiles,
653
        ),
654
    )
655
656
    # PROGRAM COUNTER
657
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(evm: Evm) -> None:
661
    <snip>
670
    # STACK
671
    gas = Uint(pop(evm.stack))
672
    to = to_address_masked(pop(evm.stack))
673
    memory_input_start_position = pop(evm.stack)
674
    memory_input_size = pop(evm.stack)
675
    memory_output_start_position = pop(evm.stack)
676
    memory_output_size = pop(evm.stack)
677
678
    # GAS
679
    extend_memory = calculate_gas_extend_memory(
680
        evm.memory,
681
        [
682
            (memory_input_start_position, memory_input_size),
683
            (memory_output_start_position, memory_output_size),
684
        ],
685
    )
686
687
    if to in evm.accessed_addresses:
688
        access_gas_cost = GasCosts.WARM_ACCESS
689
    else:
690
        evm.accessed_addresses.add(to)
691
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
692
693
    code_address = to
694
    (
695
        disable_precompiles,
696
        code_address,
697
        code,
698
        delegated_access_gas_cost,
699
    ) = access_delegation(evm, code_address)
700
    access_gas_cost += delegated_access_gas_cost
701
702
    message_call_gas = calculate_message_call_gas(
703
        U256(0),
704
        gas,
705
        Uint(evm.gas_left),
706
        extend_memory.cost,
707
        access_gas_cost,
708
    )
709
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
710
711
    # OPERATION
712
    evm.memory += b"\x00" * extend_memory.expand_by
713
    generic_call(
714
        evm,
715
        GenericCall(
716
            gas=message_call_gas.sub_call,
717
            value=U256(0),
718
            caller=evm.message.current_target,
719
            to=to,
720
            code_address=code_address,
721
            should_transfer_value=True,
722
            is_staticcall=True,
723
            memory_input_start_position=memory_input_start_position,
724
            memory_input_size=memory_input_size,
725
            memory_output_start_position=memory_output_start_position,
726
            memory_output_size=memory_output_size,
727
            code=code,
728
            disable_precompiles=disable_precompiles,
729
        ),
730
    )
731
732
    # PROGRAM COUNTER
733
    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:
737
    <snip>
747
    # STACK
748
    memory_start_index = pop(evm.stack)
749
    size = pop(evm.stack)
750
751
    # GAS
752
    extend_memory = calculate_gas_extend_memory(
753
        evm.memory, [(memory_start_index, size)]
754
    )
755
756
    charge_gas(evm, extend_memory.cost)
757
758
    # OPERATION
759
    evm.memory += b"\x00" * extend_memory.expand_by
760
    output = memory_read_bytes(evm.memory, memory_start_index, size)
761
    evm.output = Bytes(output)
762
    raise Revert
763
764
    # PROGRAM COUNTER
765
    # no-op