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:
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
    if account_has_code_or_nonce(
90
        evm.message.tx_env.state, contract_address
91
    ) or account_has_storage(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
    """
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
        parent_evm=evm,
295
    )
296
    child_evm = process_message(child_message)
297
298
    if child_evm.error:
299
        incorporate_child_on_error(evm, child_evm)
300
        evm.return_data = child_evm.output
301
        push(evm.stack, U256(0))
302
    else:
303
        incorporate_child_on_success(evm, child_evm)
304
        evm.return_data = child_evm.output
305
        push(evm.stack, U256(1))
306
307
    actual_output_size = min(memory_output_size, U256(len(child_evm.output)))
308
    memory_write(
309
        evm.memory,
310
        memory_output_start_position,
311
        child_evm.output[:actual_output_size],
312
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
456
    """
457
    Halt execution and register account for later deletion.
458
459
    Parameters
460
    ----------
461
    evm :
462
        The current EVM frame.
463
464
    """
465
    # STACK
466
    beneficiary = to_address_masked(pop(evm.stack))
467
468
    # GAS
469
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
470
    if (
471
        not is_account_alive(evm.message.tx_env.state, beneficiary)
472
        and get_account(
473
            evm.message.tx_env.state, evm.message.current_target
474
        ).balance
475
        != 0
476
    ):
477
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
478
479
    originator = evm.message.current_target
480
481
    refunded_accounts = evm.accounts_to_delete
482
    parent_evm = evm.message.parent_evm
483
    while parent_evm is not None:
484
        refunded_accounts.update(parent_evm.accounts_to_delete)
485
        parent_evm = parent_evm.message.parent_evm
486
487
    if originator not in refunded_accounts:
488
        evm.refund_counter += GasCosts.REFUND_SELF_DESTRUCT
489
490
    charge_gas(evm, gas_cost)
491
    if evm.message.is_static:
492
        raise WriteInStaticContext
493
494
    originator = evm.message.current_target
495
    beneficiary_balance = get_account(
496
        evm.message.tx_env.state, beneficiary
497
    ).balance
498
    originator_balance = get_account(
499
        evm.message.tx_env.state, originator
500
    ).balance
501
502
    # First Transfer to beneficiary
503
    set_account_balance(
504
        evm.message.tx_env.state,
505
        beneficiary,
506
        beneficiary_balance + originator_balance,
507
    )
508
    # Next, Zero the balance of the address being deleted (must come after
509
    # sending to beneficiary in case the contract named itself as the
510
    # beneficiary).
511
    set_account_balance(evm.message.tx_env.state, originator, U256(0))
512
513
    # register account for deletion
514
    evm.accounts_to_delete.add(originator)
515
516
    # mark beneficiary as touched
517
    if account_exists_and_is_empty(evm.message.tx_env.state, beneficiary):
518
        evm.touched_accounts.add(beneficiary)
519
520
    # HALT the execution
521
    evm.running = False
522
523
    # PROGRAM COUNTER
524
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
528
    """
529
    Message-call into an account.
530
531
    Parameters
532
    ----------
533
    evm :
534
        The current EVM frame.
535
536
    """
537
    # STACK
538
    gas = Uint(pop(evm.stack))
539
    code_address = to_address_masked(pop(evm.stack))
540
    memory_input_start_position = pop(evm.stack)
541
    memory_input_size = pop(evm.stack)
542
    memory_output_start_position = pop(evm.stack)
543
    memory_output_size = pop(evm.stack)
544
545
    # GAS
546
    extend_memory = calculate_gas_extend_memory(
547
        evm.memory,
548
        [
549
            (memory_input_start_position, memory_input_size),
550
            (memory_output_start_position, memory_output_size),
551
        ],
552
    )
553
    message_call_gas = calculate_message_call_gas(
554
        U256(0),
555
        gas,
556
        Uint(evm.gas_left),
557
        extend_memory.cost,
558
        GasCosts.OPCODE_CALL_BASE,
559
    )
560
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
561
562
    # OPERATION
563
    evm.memory += b"\x00" * extend_memory.expand_by
564
    generic_call(
565
        evm,
566
        message_call_gas.sub_call,
567
        evm.message.value,
568
        evm.message.caller,
569
        evm.message.current_target,
570
        code_address,
571
        False,
572
        False,
573
        memory_input_start_position,
574
        memory_input_size,
575
        memory_output_start_position,
576
        memory_output_size,
577
    )
578
579
    # PROGRAM COUNTER
580
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(evm: Evm) -> None:
584
    """
585
    Message-call into an account.
586
587
    Parameters
588
    ----------
589
    evm :
590
        The current EVM frame.
591
592
    """
593
    # STACK
594
    gas = Uint(pop(evm.stack))
595
    to = to_address_masked(pop(evm.stack))
596
    memory_input_start_position = pop(evm.stack)
597
    memory_input_size = pop(evm.stack)
598
    memory_output_start_position = pop(evm.stack)
599
    memory_output_size = pop(evm.stack)
600
601
    # GAS
602
    extend_memory = calculate_gas_extend_memory(
603
        evm.memory,
604
        [
605
            (memory_input_start_position, memory_input_size),
606
            (memory_output_start_position, memory_output_size),
607
        ],
608
    )
609
610
    code_address = to
611
612
    message_call_gas = calculate_message_call_gas(
613
        U256(0),
614
        gas,
615
        Uint(evm.gas_left),
616
        extend_memory.cost,
617
        GasCosts.OPCODE_CALL_BASE,
618
    )
619
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
620
621
    # OPERATION
622
    evm.memory += b"\x00" * extend_memory.expand_by
623
    generic_call(
624
        evm,
625
        message_call_gas.sub_call,
626
        U256(0),
627
        evm.message.current_target,
628
        to,
629
        code_address,
630
        True,
631
        True,
632
        memory_input_start_position,
633
        memory_input_size,
634
        memory_output_start_position,
635
        memory_output_size,
636
    )
637
638
    # PROGRAM COUNTER
639
    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:
643
    """
644
    Stop execution and revert state changes, without consuming all provided gas
645
    and also has the ability to return a reason.
646
647
    Parameters
648
    ----------
649
    evm :
650
        The current EVM frame.
651
652
    """
653
    # STACK
654
    memory_start_index = pop(evm.stack)
655
    size = pop(evm.stack)
656
657
    # GAS
658
    extend_memory = calculate_gas_extend_memory(
659
        evm.memory, [(memory_start_index, size)]
660
    )
661
662
    charge_gas(evm, extend_memory.cost)
663
664
    # OPERATION
665
    evm.memory += b"\x00" * extend_memory.expand_by
666
    output = memory_read_bytes(evm.memory, memory_start_index, size)
667
    evm.output = Bytes(output)
668
    raise Revert
669
670
    # PROGRAM COUNTER
671
    # no-op