ethereum.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:
67
    """
68
    Core logic used by the `CREATE*` family of opcodes.
69
    """
70
    # This import causes a circular import error
71
    # if it's not moved inside this method
72
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create_message
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.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.env.state, contract_address
94
    ) or account_has_storage(evm.env.state, contract_address):
95
        increment_nonce(evm.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.env.state, evm.message.current_target)
104
105
    child_message = Message(
106
        caller=evm.message.current_target,
107
        target=Bytes0(),
108
        gas=create_message_gas,
109
        value=endowment,
110
        data=b"",
111
        code=call_data,
112
        current_target=contract_address,
113
        depth=evm.message.depth + Uint(1),
114
        code_address=None,
115
        should_transfer_value=True,
116
        is_static=False,
117
        parent_evm=evm,
118
    )
119
    child_evm = process_create_message(child_message, evm.env)
120
121
    if child_evm.error:
122
        incorporate_child_on_error(evm, child_evm)
123
        evm.return_data = child_evm.output
124
        push(evm.stack, U256(0))
125
    else:
126
        incorporate_child_on_success(evm, child_evm)
127
        evm.return_data = b""
128
        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:
132
    """
133
    Creates a new account with associated code.
134
135
    Parameters
136
    ----------
137
    evm :
138
        The current EVM frame.
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, GAS_CREATE + 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(evm.env.state, evm.message.current_target).nonce,
157
    )
158
159
    generic_create(
160
        evm, endowment, contract_address, memory_start_position, memory_size
161
    )
162
163
    # PROGRAM COUNTER
164
    evm.pc += Uint(1)

create2

Creates a new account with associated code.

It's similar to CREATE opcode except that the address of new account depends on the init_code instead of the nonce of sender.

Parameters

evm : The current EVM frame.

def create2(evm: Evm) -> None:
168
    """
169
    Creates a new account with associated code.
170
171
    It's similar to CREATE opcode except that the address of new account
172
    depends on the init_code instead of the nonce of sender.
173
174
    Parameters
175
    ----------
176
    evm :
177
        The current EVM frame.
178
    """
179
    # STACK
180
    endowment = pop(evm.stack)
181
    memory_start_position = pop(evm.stack)
182
    memory_size = pop(evm.stack)
183
    salt = pop(evm.stack).to_be_bytes32()
184
185
    # GAS
186
    extend_memory = calculate_gas_extend_memory(
187
        evm.memory, [(memory_start_position, memory_size)]
188
    )
189
    call_data_words = ceil32(Uint(memory_size)) // Uint(32)
190
    charge_gas(
191
        evm,
192
        GAS_CREATE + GAS_KECCAK256_WORD * call_data_words + extend_memory.cost,
193
    )
194
195
    # OPERATION
196
    evm.memory += b"\x00" * extend_memory.expand_by
197
    contract_address = compute_create2_contract_address(
198
        evm.message.current_target,
199
        salt,
200
        memory_read_bytes(evm.memory, memory_start_position, memory_size),
201
    )
202
203
    generic_create(
204
        evm, endowment, contract_address, memory_start_position, memory_size
205
    )
206
207
    # PROGRAM COUNTER
208
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
212
    """
213
    Halts execution returning output data.
214
215
    Parameters
216
    ----------
217
    evm :
218
        The current EVM frame.
219
    """
220
    # STACK
221
    memory_start_position = pop(evm.stack)
222
    memory_size = pop(evm.stack)
223
224
    # GAS
225
    extend_memory = calculate_gas_extend_memory(
226
        evm.memory, [(memory_start_position, memory_size)]
227
    )
228
229
    charge_gas(evm, GAS_ZERO + extend_memory.cost)
230
231
    # OPERATION
232
    evm.memory += b"\x00" * extend_memory.expand_by
233
    evm.output = memory_read_bytes(
234
        evm.memory, memory_start_position, memory_size
235
    )
236
237
    evm.running = False
238
239
    # PROGRAM COUNTER
240
    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:
257
    """
258
    Perform the core logic of the `CALL*` family of opcodes.
259
    """
260
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
261
262
    evm.return_data = b""
263
264
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
265
        evm.gas_left += gas
266
        push(evm.stack, U256(0))
267
        return
268
269
    call_data = memory_read_bytes(
270
        evm.memory, memory_input_start_position, memory_input_size
271
    )
272
    code = get_account(evm.env.state, code_address).code
273
    child_message = Message(
274
        caller=caller,
275
        target=to,
276
        gas=gas,
277
        value=value,
278
        data=call_data,
279
        code=code,
280
        current_target=to,
281
        depth=evm.message.depth + Uint(1),
282
        code_address=code_address,
283
        should_transfer_value=should_transfer_value,
284
        is_static=True if is_staticcall else evm.message.is_static,
285
        parent_evm=evm,
286
    )
287
    child_evm = process_message(child_message, evm.env)
288
289
    if child_evm.error:
290
        incorporate_child_on_error(evm, child_evm)
291
        evm.return_data = child_evm.output
292
        push(evm.stack, U256(0))
293
    else:
294
        incorporate_child_on_success(evm, child_evm)
295
        evm.return_data = child_evm.output
296
        push(evm.stack, U256(1))
297
298
    actual_output_size = min(memory_output_size, U256(len(child_evm.output)))
299
    memory_write(
300
        evm.memory,
301
        memory_output_start_position,
302
        child_evm.output[:actual_output_size],
303
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
444
    """
445
    Halt execution and register account for later deletion.
446
447
    Parameters
448
    ----------
449
    evm :
450
        The current EVM frame.
451
    """
452
    # STACK
453
    beneficiary = to_address(pop(evm.stack))
454
455
    # GAS
456
    gas_cost = GAS_SELF_DESTRUCT
457
    if (
458
        not is_account_alive(evm.env.state, beneficiary)
459
        and get_account(evm.env.state, evm.message.current_target).balance != 0
460
    ):
461
        gas_cost += GAS_SELF_DESTRUCT_NEW_ACCOUNT
462
463
    originator = evm.message.current_target
464
465
    refunded_accounts = evm.accounts_to_delete
466
    parent_evm = evm.message.parent_evm
467
    while parent_evm is not None:
468
        refunded_accounts.update(parent_evm.accounts_to_delete)
469
        parent_evm = parent_evm.message.parent_evm
470
471
    if originator not in refunded_accounts:
472
        evm.refund_counter += REFUND_SELF_DESTRUCT
473
474
    charge_gas(evm, gas_cost)
475
    if evm.message.is_static:
476
        raise WriteInStaticContext
477
478
    beneficiary_balance = get_account(evm.env.state, beneficiary).balance
479
    originator_balance = get_account(evm.env.state, originator).balance
480
481
    # First Transfer to beneficiary
482
    set_account_balance(
483
        evm.env.state, beneficiary, beneficiary_balance + originator_balance
484
    )
485
    # Next, Zero the balance of the address being deleted (must come after
486
    # sending to beneficiary in case the contract named itself as the
487
    # beneficiary).
488
    set_account_balance(evm.env.state, originator, U256(0))
489
490
    # register account for deletion
491
    evm.accounts_to_delete.add(originator)
492
493
    # mark beneficiary as touched
494
    if account_exists_and_is_empty(evm.env.state, beneficiary):
495
        evm.touched_accounts.add(beneficiary)
496
497
    # HALT the execution
498
    evm.running = False
499
500
    # PROGRAM COUNTER
501
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
505
    """
506
    Message-call into an account.
507
508
    Parameters
509
    ----------
510
    evm :
511
        The current EVM frame.
512
    """
513
    # STACK
514
    gas = Uint(pop(evm.stack))
515
    code_address = to_address(pop(evm.stack))
516
    memory_input_start_position = pop(evm.stack)
517
    memory_input_size = pop(evm.stack)
518
    memory_output_start_position = pop(evm.stack)
519
    memory_output_size = pop(evm.stack)
520
521
    # GAS
522
    extend_memory = calculate_gas_extend_memory(
523
        evm.memory,
524
        [
525
            (memory_input_start_position, memory_input_size),
526
            (memory_output_start_position, memory_output_size),
527
        ],
528
    )
529
    message_call_gas = calculate_message_call_gas(
530
        U256(0), gas, Uint(evm.gas_left), extend_memory.cost, GAS_CALL
531
    )
532
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
533
534
    # OPERATION
535
    evm.memory += b"\x00" * extend_memory.expand_by
536
    generic_call(
537
        evm,
538
        message_call_gas.stipend,
539
        evm.message.value,
540
        evm.message.caller,
541
        evm.message.current_target,
542
        code_address,
543
        False,
544
        False,
545
        memory_input_start_position,
546
        memory_input_size,
547
        memory_output_start_position,
548
        memory_output_size,
549
    )
550
551
    # PROGRAM COUNTER
552
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(evm: Evm) -> None:
556
    """
557
    Message-call into an account.
558
559
    Parameters
560
    ----------
561
    evm :
562
        The current EVM frame.
563
    """
564
    # STACK
565
    gas = Uint(pop(evm.stack))
566
    to = to_address(pop(evm.stack))
567
    memory_input_start_position = pop(evm.stack)
568
    memory_input_size = pop(evm.stack)
569
    memory_output_start_position = pop(evm.stack)
570
    memory_output_size = pop(evm.stack)
571
572
    # GAS
573
    extend_memory = calculate_gas_extend_memory(
574
        evm.memory,
575
        [
576
            (memory_input_start_position, memory_input_size),
577
            (memory_output_start_position, memory_output_size),
578
        ],
579
    )
580
    message_call_gas = calculate_message_call_gas(
581
        U256(0),
582
        gas,
583
        Uint(evm.gas_left),
584
        extend_memory.cost,
585
        GAS_CALL,
586
    )
587
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
588
589
    # OPERATION
590
    evm.memory += b"\x00" * extend_memory.expand_by
591
    generic_call(
592
        evm,
593
        message_call_gas.stipend,
594
        U256(0),
595
        evm.message.current_target,
596
        to,
597
        to,
598
        True,
599
        True,
600
        memory_input_start_position,
601
        memory_input_size,
602
        memory_output_start_position,
603
        memory_output_size,
604
    )
605
606
    # PROGRAM COUNTER
607
    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:
611
    """
612
    Stop execution and revert state changes, without consuming all provided gas
613
    and also has the ability to return a reason
614
    Parameters
615
    ----------
616
    evm :
617
        The current EVM frame.
618
    """
619
    # STACK
620
    memory_start_index = pop(evm.stack)
621
    size = pop(evm.stack)
622
623
    # GAS
624
    extend_memory = calculate_gas_extend_memory(
625
        evm.memory, [(memory_start_index, size)]
626
    )
627
628
    charge_gas(evm, extend_memory.cost)
629
630
    # OPERATION
631
    evm.memory += b"\x00" * extend_memory.expand_by
632
    output = memory_read_bytes(evm.memory, memory_start_index, size)
633
    evm.output = bytes(output)
634
    raise Revert
635
636
    # PROGRAM COUNTER
637
    pass