ethereum.forks.spurious_dragon.vm.instructions.systemethereum.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:
56
    """
57
    Creates a new account with associated code.
58
59
    Parameters
60
    ----------
61
    evm :
62
        The current EVM frame.
63
64
    """
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
    # STACK
70
    endowment = pop(evm.stack)
71
    memory_start_position = pop(evm.stack)
72
    memory_size = pop(evm.stack)
73
74
    # GAS
75
    extend_memory = calculate_gas_extend_memory(
76
        evm.memory, [(memory_start_position, memory_size)]
77
    )
78
79
    charge_gas(evm, GAS_CREATE + extend_memory.cost)
80
81
    create_message_gas = max_message_call_gas(Uint(evm.gas_left))
82
    evm.gas_left -= create_message_gas
83
    if evm.message.is_static:
84
        raise WriteInStaticContext
85
    evm.memory += b"\x00" * extend_memory.expand_by
86
    evm.return_data = b""
87
83
    # OPERATION
84
    evm.memory += b"\x00" * extend_memory.expand_by
88
    sender_address = evm.message.current_target
89
    sender = get_account(evm.message.block_env.state, sender_address)
90
91
    contract_address = compute_contract_address(
92
        evm.message.current_target,
93
        get_account(
94
            evm.message.block_env.state, evm.message.current_target
95
        ).nonce,
96
    )
97
98
    if (
99
        sender.balance < endowment
100
        or sender.nonce == Uint(2**64 - 1)
101
        or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT
102
    ):
103
        push(evm.stack, U256(0))
104
        evm.gas_left += create_message_gas
105
    elif account_has_code_or_nonce(
106
        evm.message.block_env.state, contract_address
107
    ) or account_has_storage(evm.message.block_env.state, contract_address):
108
        increment_nonce(
109
            evm.message.block_env.state, evm.message.current_target
110
        )
111
        push(evm.stack, U256(0))
112
    else:
113
        call_data = memory_read_bytes(
114
            evm.memory, memory_start_position, memory_size
115
        )
116
117
        increment_nonce(
118
            evm.message.block_env.state, evm.message.current_target
119
        )
120
121
        child_message = Message(
122
            block_env=evm.message.block_env,
123
            tx_env=evm.message.tx_env,
124
            caller=evm.message.current_target,
125
            target=Bytes0(),
126
            gas=create_message_gas,
127
            value=endowment,
128
            data=b"",
129
            code=call_data,
130
            current_target=contract_address,
131
            depth=evm.message.depth + Uint(1),
132
            code_address=None,
133
            should_transfer_value=True,
134
            is_static=False,
135
            parent_evm=evm,
136
        )
137
        child_evm = process_create_message(child_message)
138
139
        if child_evm.error:
140
            incorporate_child_on_error(evm, child_evm)
141
            evm.return_data = child_evm.output
142
            push(evm.stack, U256(0))
143
        else:
144
            incorporate_child_on_success(evm, child_evm)
145
            evm.return_data = b""
146
            push(
147
                evm.stack, U256.from_be_bytes(child_evm.message.current_target)
148
            )
149
150
    # PROGRAM COUNTER
151
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

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