ethereum.forks.constantinople.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:
62
    <snip>
65
    # This import causes a circular import error
66
    # if it's not moved inside this method
67
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create_message
68
69
    call_data = memory_read_bytes(
70
        evm.memory, memory_start_position, memory_size
71
    )
72
73
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
74
    evm.gas_left -= create_message_gas
75
    if evm.message.is_static:
76
        raise WriteInStaticContext
77
    evm.return_data = b""
78
79
    sender_address = evm.message.current_target
80
    sender = get_account(evm.message.tx_env.state, sender_address)
81
82
    if (
83
        sender.balance < endowment
84
        or sender.nonce == Uint(2**64 - 1)
85
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
86
    ):
87
        evm.gas_left += create_message_gas
88
        push(evm.stack, U256(0))
89
        return
90
91
    if not account_deployable(evm.message.tx_env.state, contract_address):
92
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
93
        push(evm.stack, U256(0))
94
        return
95
96
    call_data = memory_read_bytes(
97
        evm.memory, memory_start_position, memory_size
98
    )
99
100
    increment_nonce(evm.message.tx_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
        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
    <snip>
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
    <snip>
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_KECCAK256_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
    <snip>
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

GenericCall

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

249
@final
250
@dataclass
class GenericCall:

gas

256
    gas: Uint

value

257
    value: U256

caller

258
    caller: Address

to

259
    to: Address

code_address

260
    code_address: Address

should_transfer_value

261
    should_transfer_value: bool

is_staticcall

262
    is_staticcall: bool

memory_input_start_position

263
    memory_input_start_position: U256

memory_input_size

264
    memory_input_size: U256

memory_output_start_position

265
    memory_output_start_position: U256

memory_output_size

266
    memory_output_size: U256

generic_call

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

def generic_call(evm: Evm, ​​params: GenericCall) -> None:
270
    <snip>
273
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
274
275
    evm.return_data = b""
276
277
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
278
        evm.gas_left += params.gas
279
        push(evm.stack, U256(0))
280
        return
281
282
    call_data = memory_read_bytes(
283
        evm.memory,
284
        params.memory_input_start_position,
285
        params.memory_input_size,
286
    )
287
    account = get_account(evm.message.tx_env.state, params.code_address)
288
    code = get_code(evm.message.tx_env.state, account.code_hash)
289
    child_message = Message(
290
        block_env=evm.message.block_env,
291
        tx_env=evm.message.tx_env,
292
        caller=params.caller,
293
        target=params.to,
294
        gas=params.gas,
295
        value=params.value,
296
        data=call_data,
297
        code=code,
298
        current_target=params.to,
299
        depth=evm.message.depth + Uint(1),
300
        code_address=params.code_address,
301
        should_transfer_value=params.should_transfer_value,
302
        is_static=params.is_staticcall or evm.message.is_static,
303
        parent_evm=evm,
304
    )
305
    child_evm = process_message(child_message)
306
307
    if child_evm.error:
308
        incorporate_child_on_error(evm, child_evm)
309
        evm.return_data = child_evm.output
310
        push(evm.stack, U256(0))
311
    else:
312
        incorporate_child_on_success(evm, child_evm)
313
        evm.return_data = child_evm.output
314
        push(evm.stack, U256(1))
315
316
    actual_output_size = min(
317
        params.memory_output_size, U256(len(child_evm.output))
318
    )
319
    memory_write(
320
        evm.memory,
321
        params.memory_output_start_position,
322
        child_evm.output[:actual_output_size],
323
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
327
    <snip>
336
    # STACK
337
    gas = Uint(pop(evm.stack))
338
    to = to_address_masked(pop(evm.stack))
339
    value = pop(evm.stack)
340
    memory_input_start_position = pop(evm.stack)
341
    memory_input_size = pop(evm.stack)
342
    memory_output_start_position = pop(evm.stack)
343
    memory_output_size = pop(evm.stack)
344
345
    # GAS
346
    extend_memory = calculate_gas_extend_memory(
347
        evm.memory,
348
        [
349
            (memory_input_start_position, memory_input_size),
350
            (memory_output_start_position, memory_output_size),
351
        ],
352
    )
353
354
    code_address = to
355
356
    create_gas_cost = GasCosts.NEW_ACCOUNT
357
    if value == 0 or is_account_alive(evm.message.tx_env.state, to):
358
        create_gas_cost = Uint(0)
359
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
360
    message_call_gas = calculate_message_call_gas(
361
        value,
362
        gas,
363
        Uint(evm.gas_left),
364
        memory_cost=extend_memory.cost,
365
        extra_gas=GasCosts.OPCODE_CALL_BASE
366
        + create_gas_cost
367
        + transfer_gas_cost,
368
    )
369
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
370
    if evm.message.is_static and value != U256(0):
371
        raise WriteInStaticContext
372
    evm.memory += b"\x00" * extend_memory.expand_by
373
    sender_balance = get_account(
374
        evm.message.tx_env.state, evm.message.current_target
375
    ).balance
376
    if sender_balance < value:
377
        push(evm.stack, U256(0))
378
        evm.return_data = b""
379
        evm.gas_left += message_call_gas.sub_call
380
    else:
381
        generic_call(
382
            evm,
383
            GenericCall(
384
                gas=message_call_gas.sub_call,
385
                value=value,
386
                caller=evm.message.current_target,
387
                to=to,
388
                code_address=code_address,
389
                should_transfer_value=True,
390
                is_staticcall=False,
391
                memory_input_start_position=memory_input_start_position,
392
                memory_input_size=memory_input_size,
393
                memory_output_start_position=memory_output_start_position,
394
                memory_output_size=memory_output_size,
395
            ),
396
        )
397
398
    # PROGRAM COUNTER
399
    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:
403
    <snip>
412
    # STACK
413
    gas = Uint(pop(evm.stack))
414
    code_address = to_address_masked(pop(evm.stack))
415
    value = pop(evm.stack)
416
    memory_input_start_position = pop(evm.stack)
417
    memory_input_size = pop(evm.stack)
418
    memory_output_start_position = pop(evm.stack)
419
    memory_output_size = pop(evm.stack)
420
421
    # GAS
422
    to = evm.message.current_target
423
424
    extend_memory = calculate_gas_extend_memory(
425
        evm.memory,
426
        [
427
            (memory_input_start_position, memory_input_size),
428
            (memory_output_start_position, memory_output_size),
429
        ],
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
        GasCosts.OPCODE_CALL_BASE + 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
            GenericCall(
454
                gas=message_call_gas.sub_call,
455
                value=value,
456
                caller=evm.message.current_target,
457
                to=to,
458
                code_address=code_address,
459
                should_transfer_value=True,
460
                is_staticcall=False,
461
                memory_input_start_position=memory_input_start_position,
462
                memory_input_size=memory_input_size,
463
                memory_output_start_position=memory_output_start_position,
464
                memory_output_size=memory_output_size,
465
            ),
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
    <snip>
482
    # STACK
483
    beneficiary = to_address_masked(pop(evm.stack))
484
485
    # GAS
486
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
487
    if (
488
        not is_account_alive(evm.message.tx_env.state, beneficiary)
489
        and get_account(
490
            evm.message.tx_env.state, evm.message.current_target
491
        ).balance
492
        != 0
493
    ):
494
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
495
496
    originator = evm.message.current_target
497
498
    refunded_accounts = evm.accounts_to_delete
499
    parent_evm = evm.message.parent_evm
500
    while parent_evm is not None:
501
        refunded_accounts.update(parent_evm.accounts_to_delete)
502
        parent_evm = parent_evm.message.parent_evm
503
504
    if originator not in refunded_accounts:
505
        evm.refund_counter += GasCosts.REFUND_SELF_DESTRUCT
506
507
    charge_gas(evm, gas_cost)
508
    if evm.message.is_static:
509
        raise WriteInStaticContext
510
511
    originator = evm.message.current_target
512
    beneficiary_balance = get_account(
513
        evm.message.tx_env.state, beneficiary
514
    ).balance
515
    originator_balance = get_account(
516
        evm.message.tx_env.state, originator
517
    ).balance
518
519
    # First Transfer to beneficiary
520
    set_account_balance(
521
        evm.message.tx_env.state,
522
        beneficiary,
523
        beneficiary_balance + originator_balance,
524
    )
525
    # Next, Zero the balance of the address being deleted (must come after
526
    # sending to beneficiary in case the contract named itself as the
527
    # beneficiary).
528
    set_account_balance(evm.message.tx_env.state, originator, U256(0))
529
530
    # register account for deletion
531
    evm.accounts_to_delete.add(originator)
532
533
    # mark beneficiary as touched
534
    if account_exists_and_is_empty(evm.message.tx_env.state, beneficiary):
535
        evm.touched_accounts.add(beneficiary)
536
537
    # HALT the execution
538
    evm.running = False
539
540
    # PROGRAM COUNTER
541
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
545
    <snip>
554
    # STACK
555
    gas = Uint(pop(evm.stack))
556
    code_address = to_address_masked(pop(evm.stack))
557
    memory_input_start_position = pop(evm.stack)
558
    memory_input_size = pop(evm.stack)
559
    memory_output_start_position = pop(evm.stack)
560
    memory_output_size = pop(evm.stack)
561
562
    # GAS
563
    extend_memory = calculate_gas_extend_memory(
564
        evm.memory,
565
        [
566
            (memory_input_start_position, memory_input_size),
567
            (memory_output_start_position, memory_output_size),
568
        ],
569
    )
570
    message_call_gas = calculate_message_call_gas(
571
        U256(0),
572
        gas,
573
        Uint(evm.gas_left),
574
        extend_memory.cost,
575
        GasCosts.OPCODE_CALL_BASE,
576
    )
577
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
578
579
    # OPERATION
580
    evm.memory += b"\x00" * extend_memory.expand_by
581
    generic_call(
582
        evm,
583
        GenericCall(
584
            gas=message_call_gas.sub_call,
585
            value=evm.message.value,
586
            caller=evm.message.caller,
587
            to=evm.message.current_target,
588
            code_address=code_address,
589
            should_transfer_value=False,
590
            is_staticcall=False,
591
            memory_input_start_position=memory_input_start_position,
592
            memory_input_size=memory_input_size,
593
            memory_output_start_position=memory_output_start_position,
594
            memory_output_size=memory_output_size,
595
        ),
596
    )
597
598
    # PROGRAM COUNTER
599
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(evm: Evm) -> None:
603
    <snip>
612
    # STACK
613
    gas = Uint(pop(evm.stack))
614
    to = to_address_masked(pop(evm.stack))
615
    memory_input_start_position = pop(evm.stack)
616
    memory_input_size = pop(evm.stack)
617
    memory_output_start_position = pop(evm.stack)
618
    memory_output_size = pop(evm.stack)
619
620
    # GAS
621
    extend_memory = calculate_gas_extend_memory(
622
        evm.memory,
623
        [
624
            (memory_input_start_position, memory_input_size),
625
            (memory_output_start_position, memory_output_size),
626
        ],
627
    )
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
        GasCosts.OPCODE_CALL_BASE,
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
        GenericCall(
645
            gas=message_call_gas.sub_call,
646
            value=U256(0),
647
            caller=evm.message.current_target,
648
            to=to,
649
            code_address=code_address,
650
            should_transfer_value=True,
651
            is_staticcall=True,
652
            memory_input_start_position=memory_input_start_position,
653
            memory_input_size=memory_input_size,
654
            memory_output_start_position=memory_output_start_position,
655
            memory_output_size=memory_output_size,
656
        ),
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
    <snip>
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