ethereum.forks.byzantium.vm.instructions.systemethereum.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:
63
    <snip>
66
    # This import causes a circular import error
67
    # if it's not moved inside this method
68
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create_message
69
70
    call_data = memory_read_bytes(
71
        evm.memory, memory_start_position, memory_size
72
    )
73
74
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
75
    evm.gas_left -= create_message_gas
76
    if evm.message.is_static:
77
        raise WriteInStaticContext
78
    evm.return_data = b""
79
80
    sender_address = evm.message.current_target
81
    sender = get_account(evm.message.tx_env.state, sender_address)
82
83
    if (
84
        sender.balance < endowment
85
        or sender.nonce == Uint(2**64 - 1)
86
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
87
    ):
88
        evm.gas_left += create_message_gas
89
        push(evm.stack, U256(0))
90
        return
91
92
    if account_has_code_or_nonce(
93
        evm.message.tx_env.state, contract_address
94
    ) or account_has_storage(evm.message.tx_env.state, contract_address):
95
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
96
        push(evm.stack, U256(0))
97
        return
98
99
    call_data = memory_read_bytes(
100
        evm.memory, memory_start_position, memory_size
101
    )
102
103
    increment_nonce(evm.message.tx_env.state, evm.message.current_target)
104
105
    child_message = Message(
106
        block_env=evm.message.block_env,
107
        tx_env=evm.message.tx_env,
108
        caller=evm.message.current_target,
109
        target=Bytes0(),
110
        gas=create_message_gas,
111
        value=endowment,
112
        data=b"",
113
        code=call_data,
114
        current_target=contract_address,
115
        depth=evm.message.depth + Uint(1),
116
        code_address=None,
117
        should_transfer_value=True,
118
        is_static=False,
119
        parent_evm=evm,
120
    )
121
    child_evm = process_create_message(child_message)
122
123
    if child_evm.error:
124
        incorporate_child_on_error(evm, child_evm)
125
        evm.return_data = child_evm.output
126
        push(evm.stack, U256(0))
127
    else:
128
        incorporate_child_on_success(evm, child_evm)
129
        evm.return_data = b""
130
        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:
134
    <snip>
61
    # This import causes a circular import error
62
    # if it's not moved inside this method
63
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create_message
64
143
    # STACK
144
    endowment = pop(evm.stack)
145
    memory_start_position = pop(evm.stack)
146
    memory_size = pop(evm.stack)
147
148
    # GAS
149
    extend_memory = calculate_gas_extend_memory(
150
        evm.memory, [(memory_start_position, memory_size)]
151
    )
152
153
    charge_gas(evm, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost)
154
77
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
78
    evm.gas_left -= create_message_gas
79
    if evm.message.is_static:
80
        raise WriteInStaticContext
81
    evm.memory += b"\x00" * extend_memory.expand_by
82
    evm.return_data = b""
83
84
    sender_address = evm.message.current_target
85
    sender = get_account(evm.message.tx_env.state, sender_address)
86
155
    # OPERATION
156
    evm.memory += b"\x00" * extend_memory.expand_by
157
    contract_address = compute_contract_address(
158
        evm.message.current_target,
159
        get_account(
160
            evm.message.tx_env.state, evm.message.current_target
161
        ).nonce,
162
    )
163
94
    if (
95
        sender.balance < endowment
96
        or sender.nonce == Uint(2**64 - 1)
97
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
98
    ):
99
        push(evm.stack, U256(0))
100
        evm.gas_left += create_message_gas
101
    elif 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
    else:
107
        call_data = memory_read_bytes(
108
            evm.memory, memory_start_position, memory_size
109
        )
110
111
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
112
113
        child_message = Message(
114
            block_env=evm.message.block_env,
115
            tx_env=evm.message.tx_env,
116
            caller=evm.message.current_target,
117
            target=Bytes0(),
118
            gas=create_message_gas,
119
            value=endowment,
120
            data=b"",
121
            code=call_data,
122
            current_target=contract_address,
123
            depth=evm.message.depth + Uint(1),
124
            code_address=None,
125
            should_transfer_value=True,
126
            is_static=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(
139
                evm.stack, U256.from_be_bytes(child_evm.message.current_target)
140
            )
164
    generic_create(
165
        evm, endowment, contract_address, memory_start_position, memory_size
166
    )
167
168
    # PROGRAM COUNTER
169
    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:
173
    <snip>
185
    # STACK
186
    endowment = pop(evm.stack)
187
    memory_start_position = pop(evm.stack)
188
    memory_size = pop(evm.stack)
189
    salt = pop(evm.stack).to_be_bytes32()
190
191
    # GAS
192
    extend_memory = calculate_gas_extend_memory(
193
        evm.memory, [(memory_start_position, memory_size)]
194
    )
195
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
196
    charge_gas(
197
        evm,
198
        GasCosts.OPCODE_CREATE_BASE
199
        + GasCosts.OPCODE_KECCAK256_PER_WORD * call_data_words
200
        + extend_memory.cost,
201
    )
202
203
    # OPERATION
204
    evm.memory += b"\x00" * extend_memory.expand_by
205
    contract_address = compute_create2_contract_address(
206
        evm.message.current_target,
207
        salt,
208
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
209
    )
210
211
    generic_create(
212
        evm, endowment, contract_address, memory_start_position, memory_size
213
    )
214
215
    # PROGRAM COUNTER
216
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
220
    <snip>
229
    # STACK
230
    memory_start_position = pop(evm.stack)
231
    memory_size = pop(evm.stack)
232
233
    # GAS
234
    extend_memory = calculate_gas_extend_memory(
235
        evm.memory, [(memory_start_position, memory_size)]
236
    )
237
238
    charge_gas(evm, GasCosts.ZERO + extend_memory.cost)
239
240
    # OPERATION
241
    evm.memory += b"\x00" * extend_memory.expand_by
242
    evm.output = memory_read_bytes(
243
        evm.memory, memory_start_position, memory_size
244
    )
245
246
    evm.running = False
247
248
    # PROGRAM COUNTER
249
    pass

GenericCall

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

252
@final
253
@dataclass
class GenericCall:

gas

259
    gas: Uint

value

260
    value: U256

caller

261
    caller: Address

to

262
    to: Address

code_address

263
    code_address: Address

should_transfer_value

264
    should_transfer_value: bool

is_staticcall

265
    is_staticcall: bool

memory_input_start_position

266
    memory_input_start_position: U256

memory_input_size

267
    memory_input_size: U256

memory_output_start_position

268
    memory_output_start_position: U256

memory_output_size

269
    memory_output_size: U256

generic_call

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

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
330
    <snip>
339
    # STACK
340
    gas = Uint(pop(evm.stack))
341
    to = to_address_masked(pop(evm.stack))
342
    value = pop(evm.stack)
343
    memory_input_start_position = pop(evm.stack)
344
    memory_input_size = pop(evm.stack)
345
    memory_output_start_position = pop(evm.stack)
346
    memory_output_size = pop(evm.stack)
347
348
    # GAS
349
    extend_memory = calculate_gas_extend_memory(
350
        evm.memory,
351
        [
352
            (memory_input_start_position, memory_input_size),
353
            (memory_output_start_position, memory_output_size),
354
        ],
355
    )
356
357
    code_address = to
358
359
    create_gas_cost = GasCosts.NEW_ACCOUNT
360
    if value == 0 or is_account_alive(evm.message.tx_env.state, to):
361
        create_gas_cost = Uint(0)
362
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
363
    message_call_gas = calculate_message_call_gas(
364
        value,
365
        gas,
366
        Uint(evm.gas_left),
367
        memory_cost=extend_memory.cost,
368
        extra_gas=GasCosts.OPCODE_CALL_BASE
369
        + create_gas_cost
370
        + transfer_gas_cost,
371
    )
372
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
373
    if evm.message.is_static and value != U256(0):
374
        raise WriteInStaticContext
375
    evm.memory += b"\x00" * extend_memory.expand_by
376
    sender_balance = get_account(
377
        evm.message.tx_env.state, evm.message.current_target
378
    ).balance
379
    if sender_balance < value:
380
        push(evm.stack, U256(0))
381
        evm.return_data = b""
382
        evm.gas_left += message_call_gas.sub_call
383
    else:
384
        generic_call(
385
            evm,
386
            GenericCall(
387
                gas=message_call_gas.sub_call,
388
                value=value,
389
                caller=evm.message.current_target,
390
                to=to,
391
                code_address=code_address,
392
                should_transfer_value=True,
393
                is_staticcall=False,
394
                memory_input_start_position=memory_input_start_position,
395
                memory_input_size=memory_input_size,
396
                memory_output_start_position=memory_output_start_position,
397
                memory_output_size=memory_output_size,
398
            ),
399
        )
400
401
    # PROGRAM COUNTER
402
    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:
406
    <snip>
415
    # STACK
416
    gas = Uint(pop(evm.stack))
417
    code_address = to_address_masked(pop(evm.stack))
418
    value = pop(evm.stack)
419
    memory_input_start_position = pop(evm.stack)
420
    memory_input_size = pop(evm.stack)
421
    memory_output_start_position = pop(evm.stack)
422
    memory_output_size = pop(evm.stack)
423
424
    # GAS
425
    to = evm.message.current_target
426
427
    extend_memory = calculate_gas_extend_memory(
428
        evm.memory,
429
        [
430
            (memory_input_start_position, memory_input_size),
431
            (memory_output_start_position, memory_output_size),
432
        ],
433
    )
434
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
435
    message_call_gas = calculate_message_call_gas(
436
        value,
437
        gas,
438
        Uint(evm.gas_left),
439
        extend_memory.cost,
440
        GasCosts.OPCODE_CALL_BASE + transfer_gas_cost,
441
    )
442
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
443
444
    # OPERATION
445
    evm.memory += b"\x00" * extend_memory.expand_by
446
    sender_balance = get_account(
447
        evm.message.tx_env.state, evm.message.current_target
448
    ).balance
449
    if sender_balance < value:
450
        push(evm.stack, U256(0))
451
        evm.return_data = b""
452
        evm.gas_left += message_call_gas.sub_call
453
    else:
454
        generic_call(
455
            evm,
456
            GenericCall(
457
                gas=message_call_gas.sub_call,
458
                value=value,
459
                caller=evm.message.current_target,
460
                to=to,
461
                code_address=code_address,
462
                should_transfer_value=True,
463
                is_staticcall=False,
464
                memory_input_start_position=memory_input_start_position,
465
                memory_input_size=memory_input_size,
466
                memory_output_start_position=memory_output_start_position,
467
                memory_output_size=memory_output_size,
468
            ),
469
        )
470
471
    # PROGRAM COUNTER
472
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
476
    <snip>
485
    # STACK
486
    beneficiary = to_address_masked(pop(evm.stack))
487
488
    # GAS
489
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
490
    if (
491
        not is_account_alive(evm.message.tx_env.state, beneficiary)
492
        and get_account(
493
            evm.message.tx_env.state, evm.message.current_target
494
        ).balance
495
        != 0
496
    ):
497
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
498
499
    originator = evm.message.current_target
500
501
    refunded_accounts = evm.accounts_to_delete
502
    parent_evm = evm.message.parent_evm
503
    while parent_evm is not None:
504
        refunded_accounts.update(parent_evm.accounts_to_delete)
505
        parent_evm = parent_evm.message.parent_evm
506
507
    if originator not in refunded_accounts:
508
        evm.refund_counter += GasCosts.REFUND_SELF_DESTRUCT
509
510
    charge_gas(evm, gas_cost)
511
    if evm.message.is_static:
512
        raise WriteInStaticContext
513
514
    originator = evm.message.current_target
515
    beneficiary_balance = get_account(
516
        evm.message.tx_env.state, beneficiary
517
    ).balance
518
    originator_balance = get_account(
519
        evm.message.tx_env.state, originator
520
    ).balance
521
522
    # First Transfer to beneficiary
523
    set_account_balance(
524
        evm.message.tx_env.state,
525
        beneficiary,
526
        beneficiary_balance + originator_balance,
527
    )
528
    # Next, Zero the balance of the address being deleted (must come after
529
    # sending to beneficiary in case the contract named itself as the
530
    # beneficiary).
531
    set_account_balance(evm.message.tx_env.state, originator, U256(0))
532
533
    # register account for deletion
534
    evm.accounts_to_delete.add(originator)
535
536
    # mark beneficiary as touched
537
    if account_exists_and_is_empty(evm.message.tx_env.state, beneficiary):
538
        evm.touched_accounts.add(beneficiary)
539
540
    # HALT the execution
541
    evm.running = False
542
543
    # PROGRAM COUNTER
544
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(evm: Evm) -> None:
606
    <snip>
615
    # STACK
616
    gas = Uint(pop(evm.stack))
617
    to = to_address_masked(pop(evm.stack))
618
    memory_input_start_position = pop(evm.stack)
619
    memory_input_size = pop(evm.stack)
620
    memory_output_start_position = pop(evm.stack)
621
    memory_output_size = pop(evm.stack)
622
623
    # GAS
624
    extend_memory = calculate_gas_extend_memory(
625
        evm.memory,
626
        [
627
            (memory_input_start_position, memory_input_size),
628
            (memory_output_start_position, memory_output_size),
629
        ],
630
    )
631
632
    code_address = to
633
634
    message_call_gas = calculate_message_call_gas(
635
        U256(0),
636
        gas,
637
        Uint(evm.gas_left),
638
        extend_memory.cost,
639
        GasCosts.OPCODE_CALL_BASE,
640
    )
641
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
642
643
    # OPERATION
644
    evm.memory += b"\x00" * extend_memory.expand_by
645
    generic_call(
646
        evm,
647
        GenericCall(
648
            gas=message_call_gas.sub_call,
649
            value=U256(0),
650
            caller=evm.message.current_target,
651
            to=to,
652
            code_address=code_address,
653
            should_transfer_value=True,
654
            is_staticcall=True,
655
            memory_input_start_position=memory_input_start_position,
656
            memory_input_size=memory_input_size,
657
            memory_output_start_position=memory_output_start_position,
658
            memory_output_size=memory_output_size,
659
        ),
660
    )
661
662
    # PROGRAM COUNTER
663
    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:
667
    <snip>
677
    # STACK
678
    memory_start_index = pop(evm.stack)
679
    size = pop(evm.stack)
680
681
    # GAS
682
    extend_memory = calculate_gas_extend_memory(
683
        evm.memory, [(memory_start_index, size)]
684
    )
685
686
    charge_gas(evm, extend_memory.cost)
687
688
    # OPERATION
689
    evm.memory += b"\x00" * extend_memory.expand_by
690
    output = memory_read_bytes(evm.memory, memory_start_index, size)
691
    evm.output = Bytes(output)
692
    raise Revert
693
694
    # PROGRAM COUNTER
695
    # no-op