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

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

GenericCall

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

271
@final
272
@dataclass
class GenericCall:

gas

278
    gas: Uint

value

279
    value: U256

caller

280
    caller: Address

to

281
    to: Address

code_address

282
    code_address: Address

should_transfer_value

283
    should_transfer_value: bool

is_staticcall

284
    is_staticcall: bool

memory_input_start_position

285
    memory_input_start_position: U256

memory_input_size

286
    memory_input_size: U256

memory_output_start_position

287
    memory_output_start_position: U256

memory_output_size

288
    memory_output_size: U256

code

289
    code: Bytes

disable_precompiles

290
    disable_precompiles: bool

generic_call

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

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
588
    <snip>
597
    # STACK
598
    gas = Uint(pop(evm.stack))
599
    code_address = to_address_masked(pop(evm.stack))
600
    memory_input_start_position = pop(evm.stack)
601
    memory_input_size = pop(evm.stack)
602
    memory_output_start_position = pop(evm.stack)
603
    memory_output_size = pop(evm.stack)
604
605
    # GAS
606
    extend_memory = calculate_gas_extend_memory(
607
        evm.memory,
608
        [
609
            (memory_input_start_position, memory_input_size),
610
            (memory_output_start_position, memory_output_size),
611
        ],
612
    )
613
614
    if code_address in evm.accessed_addresses:
615
        access_gas_cost = GasCosts.WARM_ACCESS
616
    else:
617
        evm.accessed_addresses.add(code_address)
618
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
619
620
    (
621
        disable_precompiles,
622
        code_address,
623
        code,
624
        delegated_access_gas_cost,
625
    ) = access_delegation(evm, code_address)
626
    access_gas_cost += delegated_access_gas_cost
627
628
    message_call_gas = calculate_message_call_gas(
629
        U256(0), gas, Uint(evm.gas_left), extend_memory.cost, 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
        GenericCall(
638
            gas=message_call_gas.sub_call,
639
            value=evm.message.value,
640
            caller=evm.message.caller,
641
            to=evm.message.current_target,
642
            code_address=code_address,
643
            should_transfer_value=False,
644
            is_staticcall=False,
645
            memory_input_start_position=memory_input_start_position,
646
            memory_input_size=memory_input_size,
647
            memory_output_start_position=memory_output_start_position,
648
            memory_output_size=memory_output_size,
649
            code=code,
650
            disable_precompiles=disable_precompiles,
651
        ),
652
    )
653
654
    # PROGRAM COUNTER
655
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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