ethereum.forks.byzantium.vm.instructions.system

Ethereum Virtual Machine (EVM) System Instructions.

.. contents:: Table of Contents :backlinks: none :local:

Introduction

Implementations of the EVM system related instructions.

create

Creates a new account with associated code.

Parameters

evm : The current EVM frame.

def create(evm: Evm) -> None:
49
    """
50
    Creates a new account with associated code.
51
52
    Parameters
53
    ----------
54
    evm :
55
        The current EVM frame.
56
57
    """
58
    # This import causes a circular import error
59
    # if it's not moved inside this method
60
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create_message
61
62
    # STACK
63
    endowment = pop(evm.stack)
64
    memory_start_position = pop(evm.stack)
65
    memory_size = pop(evm.stack)
66
67
    # GAS
68
    extend_memory = calculate_gas_extend_memory(
69
        evm.memory, [(memory_start_position, memory_size)]
70
    )
71
72
    charge_gas(evm, GasCosts.OPCODE_CREATE_BASE + extend_memory.cost)
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.memory += b"\x00" * extend_memory.expand_by
79
    evm.return_data = b""
80
81
    sender_address = evm.message.current_target
82
    sender = get_account(evm.message.block_env.state, sender_address)
83
84
    contract_address = compute_contract_address(
85
        evm.message.current_target,
86
        get_account(
87
            evm.message.block_env.state, evm.message.current_target
88
        ).nonce,
89
    )
90
91
    if (
92
        sender.balance < endowment
93
        or sender.nonce == Uint(2**64 - 1)
94
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
95
    ):
96
        push(evm.stack, U256(0))
97
        evm.gas_left += create_message_gas
98
    elif account_has_code_or_nonce(
99
        evm.message.block_env.state, contract_address
100
    ) or account_has_storage(evm.message.block_env.state, contract_address):
101
        increment_nonce(
102
            evm.message.block_env.state, evm.message.current_target
103
        )
104
        push(evm.stack, U256(0))
105
    else:
106
        call_data = memory_read_bytes(
107
            evm.memory, memory_start_position, memory_size
108
        )
109
110
        increment_nonce(
111
            evm.message.block_env.state, evm.message.current_target
112
        )
113
114
        child_message = Message(
115
            block_env=evm.message.block_env,
116
            tx_env=evm.message.tx_env,
117
            caller=evm.message.current_target,
118
            target=Bytes0(),
119
            gas=create_message_gas,
120
            value=endowment,
121
            data=b"",
122
            code=call_data,
123
            current_target=contract_address,
124
            depth=evm.message.depth + Uint(1),
125
            code_address=None,
126
            should_transfer_value=True,
127
            is_static=False,
128
            parent_evm=evm,
129
        )
130
        child_evm = process_create_message(child_message)
131
132
        if child_evm.error:
133
            incorporate_child_on_error(evm, child_evm)
134
            evm.return_data = child_evm.output
135
            push(evm.stack, U256(0))
136
        else:
137
            incorporate_child_on_success(evm, child_evm)
138
            evm.return_data = b""
139
            push(
140
                evm.stack, U256.from_be_bytes(child_evm.message.current_target)
141
            )
142
143
    # PROGRAM COUNTER
144
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
148
    """
149
    Halts execution returning output data.
150
151
    Parameters
152
    ----------
153
    evm :
154
        The current EVM frame.
155
156
    """
157
    # STACK
158
    memory_start_position = pop(evm.stack)
159
    memory_size = pop(evm.stack)
160
161
    # GAS
162
    extend_memory = calculate_gas_extend_memory(
163
        evm.memory, [(memory_start_position, memory_size)]
164
    )
165
166
    charge_gas(evm, GasCosts.ZERO + extend_memory.cost)
167
168
    # OPERATION
169
    evm.memory += b"\x00" * extend_memory.expand_by
170
    evm.output = memory_read_bytes(
171
        evm.memory, memory_start_position, memory_size
172
    )
173
174
    evm.running = False
175
176
    # PROGRAM COUNTER
177
    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:
194
    """
195
    Perform the core logic of the `CALL*` family of opcodes.
196
    """
197
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
198
199
    evm.return_data = b""
200
201
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
202
        evm.gas_left += gas
203
        push(evm.stack, U256(0))
204
        return
205
206
    call_data = memory_read_bytes(
207
        evm.memory, memory_input_start_position, memory_input_size
208
    )
209
    account = get_account(evm.message.block_env.state, code_address)
210
    code = get_code(evm.message.block_env.state, account.code_hash)
211
    child_message = Message(
212
        block_env=evm.message.block_env,
213
        tx_env=evm.message.tx_env,
214
        caller=caller,
215
        target=to,
216
        gas=gas,
217
        value=value,
218
        data=call_data,
219
        code=code,
220
        current_target=to,
221
        depth=evm.message.depth + Uint(1),
222
        code_address=code_address,
223
        should_transfer_value=should_transfer_value,
224
        is_static=True if is_staticcall else evm.message.is_static,
225
        parent_evm=evm,
226
    )
227
    child_evm = process_message(child_message)
228
229
    if child_evm.error:
230
        incorporate_child_on_error(evm, child_evm)
231
        evm.return_data = child_evm.output
232
        push(evm.stack, U256(0))
233
    else:
234
        incorporate_child_on_success(evm, child_evm)
235
        evm.return_data = child_evm.output
236
        push(evm.stack, U256(1))
237
238
    actual_output_size = min(memory_output_size, U256(len(child_evm.output)))
239
    memory_write(
240
        evm.memory,
241
        memory_output_start_position,
242
        child_evm.output[:actual_output_size],
243
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
247
    """
248
    Message-call into an account.
249
250
    Parameters
251
    ----------
252
    evm :
253
        The current EVM frame.
254
255
    """
256
    # STACK
257
    gas = Uint(pop(evm.stack))
258
    to = to_address_masked(pop(evm.stack))
259
    value = pop(evm.stack)
260
    memory_input_start_position = pop(evm.stack)
261
    memory_input_size = pop(evm.stack)
262
    memory_output_start_position = pop(evm.stack)
263
    memory_output_size = pop(evm.stack)
264
265
    # GAS
266
    extend_memory = calculate_gas_extend_memory(
267
        evm.memory,
268
        [
269
            (memory_input_start_position, memory_input_size),
270
            (memory_output_start_position, memory_output_size),
271
        ],
272
    )
273
274
    code_address = to
275
276
    create_gas_cost = GasCosts.NEW_ACCOUNT
277
    if value == 0 or is_account_alive(evm.message.block_env.state, to):
278
        create_gas_cost = Uint(0)
279
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
280
    message_call_gas = calculate_message_call_gas(
281
        value,
282
        gas,
283
        Uint(evm.gas_left),
284
        extend_memory.cost,
285
        GasCosts.OPCODE_CALL_BASE + create_gas_cost + transfer_gas_cost,
286
    )
287
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
288
    if evm.message.is_static and value != U256(0):
289
        raise WriteInStaticContext
290
    evm.memory += b"\x00" * extend_memory.expand_by
291
    sender_balance = get_account(
292
        evm.message.block_env.state, evm.message.current_target
293
    ).balance
294
    if sender_balance < value:
295
        push(evm.stack, U256(0))
296
        evm.return_data = b""
297
        evm.gas_left += message_call_gas.sub_call
298
    else:
299
        generic_call(
300
            evm,
301
            message_call_gas.sub_call,
302
            value,
303
            evm.message.current_target,
304
            to,
305
            code_address,
306
            True,
307
            False,
308
            memory_input_start_position,
309
            memory_input_size,
310
            memory_output_start_position,
311
            memory_output_size,
312
        )
313
314
    # PROGRAM COUNTER
315
    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:
319
    """
320
    Message-call into this account with alternative account’s code.
321
322
    Parameters
323
    ----------
324
    evm :
325
        The current EVM frame.
326
327
    """
328
    # STACK
329
    gas = Uint(pop(evm.stack))
330
    code_address = to_address_masked(pop(evm.stack))
331
    value = pop(evm.stack)
332
    memory_input_start_position = pop(evm.stack)
333
    memory_input_size = pop(evm.stack)
334
    memory_output_start_position = pop(evm.stack)
335
    memory_output_size = pop(evm.stack)
336
337
    # GAS
338
    to = evm.message.current_target
339
340
    extend_memory = calculate_gas_extend_memory(
341
        evm.memory,
342
        [
343
            (memory_input_start_position, memory_input_size),
344
            (memory_output_start_position, memory_output_size),
345
        ],
346
    )
347
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
348
    message_call_gas = calculate_message_call_gas(
349
        value,
350
        gas,
351
        Uint(evm.gas_left),
352
        extend_memory.cost,
353
        GasCosts.OPCODE_CALL_BASE + transfer_gas_cost,
354
    )
355
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
356
357
    # OPERATION
358
    evm.memory += b"\x00" * extend_memory.expand_by
359
    sender_balance = get_account(
360
        evm.message.block_env.state, evm.message.current_target
361
    ).balance
362
    if sender_balance < value:
363
        push(evm.stack, U256(0))
364
        evm.return_data = b""
365
        evm.gas_left += message_call_gas.sub_call
366
    else:
367
        generic_call(
368
            evm,
369
            message_call_gas.sub_call,
370
            value,
371
            evm.message.current_target,
372
            to,
373
            code_address,
374
            True,
375
            False,
376
            memory_input_start_position,
377
            memory_input_size,
378
            memory_output_start_position,
379
            memory_output_size,
380
        )
381
382
    # PROGRAM COUNTER
383
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
387
    """
388
    Halt execution and register account for later deletion.
389
390
    Parameters
391
    ----------
392
    evm :
393
        The current EVM frame.
394
395
    """
396
    # STACK
397
    beneficiary = to_address_masked(pop(evm.stack))
398
399
    # GAS
400
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
401
    if (
402
        not is_account_alive(evm.message.block_env.state, beneficiary)
403
        and get_account(
404
            evm.message.block_env.state, evm.message.current_target
405
        ).balance
406
        != 0
407
    ):
408
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
409
410
    originator = evm.message.current_target
411
412
    refunded_accounts = evm.accounts_to_delete
413
    parent_evm = evm.message.parent_evm
414
    while parent_evm is not None:
415
        refunded_accounts.update(parent_evm.accounts_to_delete)
416
        parent_evm = parent_evm.message.parent_evm
417
418
    if originator not in refunded_accounts:
419
        evm.refund_counter += GasCosts.REFUND_SELF_DESTRUCT
420
421
    charge_gas(evm, gas_cost)
422
    if evm.message.is_static:
423
        raise WriteInStaticContext
424
425
    beneficiary_balance = get_account(
426
        evm.message.block_env.state, beneficiary
427
    ).balance
428
    originator_balance = get_account(
429
        evm.message.block_env.state, originator
430
    ).balance
431
432
    # First Transfer to beneficiary
433
    set_account_balance(
434
        evm.message.block_env.state,
435
        beneficiary,
436
        beneficiary_balance + originator_balance,
437
    )
438
    # Next, Zero the balance of the address being deleted (must come after
439
    # sending to beneficiary in case the contract named itself as the
440
    # beneficiary).
441
    set_account_balance(evm.message.block_env.state, originator, U256(0))
442
443
    # register account for deletion
444
    evm.accounts_to_delete.add(originator)
445
446
    # mark beneficiary as touched
447
    if account_exists_and_is_empty(evm.message.block_env.state, beneficiary):
448
        evm.touched_accounts.add(beneficiary)
449
450
    # HALT the execution
451
    evm.running = False
452
453
    # PROGRAM COUNTER
454
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
458
    """
459
    Message-call into an account.
460
461
    Parameters
462
    ----------
463
    evm :
464
        The current EVM frame.
465
466
    """
467
    # STACK
468
    gas = Uint(pop(evm.stack))
469
    code_address = to_address_masked(pop(evm.stack))
470
    memory_input_start_position = pop(evm.stack)
471
    memory_input_size = pop(evm.stack)
472
    memory_output_start_position = pop(evm.stack)
473
    memory_output_size = pop(evm.stack)
474
475
    # GAS
476
    extend_memory = calculate_gas_extend_memory(
477
        evm.memory,
478
        [
479
            (memory_input_start_position, memory_input_size),
480
            (memory_output_start_position, memory_output_size),
481
        ],
482
    )
483
    message_call_gas = calculate_message_call_gas(
484
        U256(0),
485
        gas,
486
        Uint(evm.gas_left),
487
        extend_memory.cost,
488
        GasCosts.OPCODE_CALL_BASE,
489
    )
490
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
491
492
    # OPERATION
493
    evm.memory += b"\x00" * extend_memory.expand_by
494
    generic_call(
495
        evm,
496
        message_call_gas.sub_call,
497
        evm.message.value,
498
        evm.message.caller,
499
        evm.message.current_target,
500
        code_address,
501
        False,
502
        False,
503
        memory_input_start_position,
504
        memory_input_size,
505
        memory_output_start_position,
506
        memory_output_size,
507
    )
508
509
    # PROGRAM COUNTER
510
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(evm: Evm) -> None:
514
    """
515
    Message-call into an account.
516
517
    Parameters
518
    ----------
519
    evm :
520
        The current EVM frame.
521
522
    """
523
    # STACK
524
    gas = Uint(pop(evm.stack))
525
    to = to_address_masked(pop(evm.stack))
526
    memory_input_start_position = pop(evm.stack)
527
    memory_input_size = pop(evm.stack)
528
    memory_output_start_position = pop(evm.stack)
529
    memory_output_size = pop(evm.stack)
530
531
    # GAS
532
    extend_memory = calculate_gas_extend_memory(
533
        evm.memory,
534
        [
535
            (memory_input_start_position, memory_input_size),
536
            (memory_output_start_position, memory_output_size),
537
        ],
538
    )
539
540
    code_address = to
541
542
    message_call_gas = calculate_message_call_gas(
543
        U256(0),
544
        gas,
545
        Uint(evm.gas_left),
546
        extend_memory.cost,
547
        GasCosts.OPCODE_CALL_BASE,
548
    )
549
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
550
551
    # OPERATION
552
    evm.memory += b"\x00" * extend_memory.expand_by
553
    generic_call(
554
        evm,
555
        message_call_gas.sub_call,
556
        U256(0),
557
        evm.message.current_target,
558
        to,
559
        code_address,
560
        True,
561
        True,
562
        memory_input_start_position,
563
        memory_input_size,
564
        memory_output_start_position,
565
        memory_output_size,
566
    )
567
568
    # PROGRAM COUNTER
569
    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:
573
    """
574
    Stop execution and revert state changes, without consuming all provided gas
575
    and also has the ability to return a reason.
576
577
    Parameters
578
    ----------
579
    evm :
580
        The current EVM frame.
581
582
    """
583
    # STACK
584
    memory_start_index = pop(evm.stack)
585
    size = pop(evm.stack)
586
587
    # GAS
588
    extend_memory = calculate_gas_extend_memory(
589
        evm.memory, [(memory_start_index, size)]
590
    )
591
592
    charge_gas(evm, extend_memory.cost)
593
594
    # OPERATION
595
    evm.memory += b"\x00" * extend_memory.expand_by
596
    output = memory_read_bytes(evm.memory, memory_start_index, size)
597
    evm.output = Bytes(output)
598
    raise Revert
599
600
    # PROGRAM COUNTER
601
    # no-op