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

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

def return_(evm: Evm) -> None:
144
    <snip>
153
    # STACK
154
    memory_start_position = pop(evm.stack)
155
    memory_size = pop(evm.stack)
156
157
    # GAS
158
    extend_memory = calculate_gas_extend_memory(
159
        evm.memory, [(memory_start_position, memory_size)]
160
    )
161
162
    charge_gas(evm, GasCosts.ZERO + extend_memory.cost)
163
164
    # OPERATION
165
    evm.memory += b"\x00" * extend_memory.expand_by
166
    evm.output = memory_read_bytes(
167
        evm.memory, memory_start_position, memory_size
168
    )
169
170
    evm.running = False
171
172
    # PROGRAM COUNTER
173
    pass

GenericCall

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

176
@final
177
@dataclass
class GenericCall:

gas

183
    gas: Uint

value

184
    value: U256

caller

185
    caller: Address

to

186
    to: Address

code_address

187
    code_address: Address

should_transfer_value

188
    should_transfer_value: bool

is_staticcall

189
    is_staticcall: bool

memory_input_start_position

190
    memory_input_start_position: U256

memory_input_size

191
    memory_input_size: U256

memory_output_start_position

192
    memory_output_start_position: U256

memory_output_size

193
    memory_output_size: U256

generic_call

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

def generic_call(evm: Evm, ​​params: GenericCall) -> None:
197
    <snip>
200
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
201
202
    evm.return_data = b""
203
204
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
205
        evm.gas_left += params.gas
206
        push(evm.stack, U256(0))
207
        return
208
209
    call_data = memory_read_bytes(
210
        evm.memory,
211
        params.memory_input_start_position,
212
        params.memory_input_size,
213
    )
214
    account = get_account(evm.message.tx_env.state, params.code_address)
215
    code = get_code(evm.message.tx_env.state, account.code_hash)
216
    child_message = Message(
217
        block_env=evm.message.block_env,
218
        tx_env=evm.message.tx_env,
219
        caller=params.caller,
220
        target=params.to,
221
        gas=params.gas,
222
        value=params.value,
223
        data=call_data,
224
        code=code,
225
        current_target=params.to,
226
        depth=evm.message.depth + Uint(1),
227
        code_address=params.code_address,
228
        should_transfer_value=params.should_transfer_value,
229
        is_static=params.is_staticcall or evm.message.is_static,
230
        parent_evm=evm,
231
    )
232
    child_evm = process_message(child_message)
233
234
    if child_evm.error:
235
        incorporate_child_on_error(evm, child_evm)
236
        evm.return_data = child_evm.output
237
        push(evm.stack, U256(0))
238
    else:
239
        incorporate_child_on_success(evm, child_evm)
240
        evm.return_data = child_evm.output
241
        push(evm.stack, U256(1))
242
243
    actual_output_size = min(
244
        params.memory_output_size, U256(len(child_evm.output))
245
    )
246
    memory_write(
247
        evm.memory,
248
        params.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
    <snip>
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 = GasCosts.NEW_ACCOUNT
284
    if value == 0 or is_account_alive(evm.message.tx_env.state, to):
285
        create_gas_cost = Uint(0)
286
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
287
    message_call_gas = calculate_message_call_gas(
288
        value,
289
        gas,
290
        Uint(evm.gas_left),
291
        memory_cost=extend_memory.cost,
292
        extra_gas=GasCosts.OPCODE_CALL_BASE
293
        + create_gas_cost
294
        + transfer_gas_cost,
295
    )
296
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
297
    if evm.message.is_static and value != U256(0):
298
        raise WriteInStaticContext
299
    evm.memory += b"\x00" * extend_memory.expand_by
300
    sender_balance = get_account(
301
        evm.message.tx_env.state, evm.message.current_target
302
    ).balance
303
    if sender_balance < value:
304
        push(evm.stack, U256(0))
305
        evm.return_data = b""
306
        evm.gas_left += message_call_gas.sub_call
307
    else:
308
        generic_call(
309
            evm,
310
            GenericCall(
311
                gas=message_call_gas.sub_call,
312
                value=value,
313
                caller=evm.message.current_target,
314
                to=to,
315
                code_address=code_address,
316
                should_transfer_value=True,
317
                is_staticcall=False,
318
                memory_input_start_position=memory_input_start_position,
319
                memory_input_size=memory_input_size,
320
                memory_output_start_position=memory_output_start_position,
321
                memory_output_size=memory_output_size,
322
            ),
323
        )
324
325
    # PROGRAM COUNTER
326
    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:
330
    <snip>
339
    # STACK
340
    gas = Uint(pop(evm.stack))
341
    code_address = 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
    to = evm.message.current_target
350
351
    extend_memory = calculate_gas_extend_memory(
352
        evm.memory,
353
        [
354
            (memory_input_start_position, memory_input_size),
355
            (memory_output_start_position, memory_output_size),
356
        ],
357
    )
358
    transfer_gas_cost = Uint(0) if value == 0 else GasCosts.CALL_VALUE
359
    message_call_gas = calculate_message_call_gas(
360
        value,
361
        gas,
362
        Uint(evm.gas_left),
363
        extend_memory.cost,
364
        GasCosts.OPCODE_CALL_BASE + transfer_gas_cost,
365
    )
366
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
367
368
    # OPERATION
369
    evm.memory += b"\x00" * extend_memory.expand_by
370
    sender_balance = get_account(
371
        evm.message.tx_env.state, evm.message.current_target
372
    ).balance
373
    if sender_balance < value:
374
        push(evm.stack, U256(0))
375
        evm.return_data = b""
376
        evm.gas_left += message_call_gas.sub_call
377
    else:
378
        generic_call(
379
            evm,
380
            GenericCall(
381
                gas=message_call_gas.sub_call,
382
                value=value,
383
                caller=evm.message.current_target,
384
                to=to,
385
                code_address=code_address,
386
                should_transfer_value=True,
387
                is_staticcall=False,
388
                memory_input_start_position=memory_input_start_position,
389
                memory_input_size=memory_input_size,
390
                memory_output_start_position=memory_output_start_position,
391
                memory_output_size=memory_output_size,
392
            ),
393
        )
394
395
    # PROGRAM COUNTER
396
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
400
    <snip>
409
    # STACK
410
    beneficiary = to_address_masked(pop(evm.stack))
411
412
    # GAS
413
    gas_cost = GasCosts.OPCODE_SELFDESTRUCT_BASE
414
    if (
415
        not is_account_alive(evm.message.tx_env.state, beneficiary)
416
        and get_account(
417
            evm.message.tx_env.state, evm.message.current_target
418
        ).balance
419
        != 0
420
    ):
421
        gas_cost += GasCosts.OPCODE_SELFDESTRUCT_NEW_ACCOUNT
422
423
    originator = evm.message.current_target
424
425
    refunded_accounts = evm.accounts_to_delete
426
    parent_evm = evm.message.parent_evm
427
    while parent_evm is not None:
428
        refunded_accounts.update(parent_evm.accounts_to_delete)
429
        parent_evm = parent_evm.message.parent_evm
430
431
    if originator not in refunded_accounts:
432
        evm.refund_counter += GasCosts.REFUND_SELF_DESTRUCT
433
434
    charge_gas(evm, gas_cost)
435
    if evm.message.is_static:
436
        raise WriteInStaticContext
437
438
    beneficiary_balance = get_account(
439
        evm.message.tx_env.state, beneficiary
440
    ).balance
441
    originator_balance = get_account(
442
        evm.message.tx_env.state, originator
443
    ).balance
444
445
    # First Transfer to beneficiary
446
    set_account_balance(
447
        evm.message.tx_env.state,
448
        beneficiary,
449
        beneficiary_balance + originator_balance,
450
    )
451
    # Next, Zero the balance of the address being deleted (must come after
452
    # sending to beneficiary in case the contract named itself as the
453
    # beneficiary).
454
    set_account_balance(evm.message.tx_env.state, originator, U256(0))
455
456
    # register account for deletion
457
    evm.accounts_to_delete.add(originator)
458
459
    # mark beneficiary as touched
460
    if account_exists_and_is_empty(evm.message.tx_env.state, beneficiary):
461
        evm.touched_accounts.add(beneficiary)
462
463
    # HALT the execution
464
    evm.running = False
465
466
    # PROGRAM COUNTER
467
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
471
    <snip>
480
    # STACK
481
    gas = Uint(pop(evm.stack))
482
    code_address = to_address_masked(pop(evm.stack))
483
    memory_input_start_position = pop(evm.stack)
484
    memory_input_size = pop(evm.stack)
485
    memory_output_start_position = pop(evm.stack)
486
    memory_output_size = pop(evm.stack)
487
488
    # GAS
489
    extend_memory = calculate_gas_extend_memory(
490
        evm.memory,
491
        [
492
            (memory_input_start_position, memory_input_size),
493
            (memory_output_start_position, memory_output_size),
494
        ],
495
    )
496
    message_call_gas = calculate_message_call_gas(
497
        U256(0),
498
        gas,
499
        Uint(evm.gas_left),
500
        extend_memory.cost,
501
        GasCosts.OPCODE_CALL_BASE,
502
    )
503
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
504
505
    # OPERATION
506
    evm.memory += b"\x00" * extend_memory.expand_by
507
    generic_call(
508
        evm,
509
        GenericCall(
510
            gas=message_call_gas.sub_call,
511
            value=evm.message.value,
512
            caller=evm.message.caller,
513
            to=evm.message.current_target,
514
            code_address=code_address,
515
            should_transfer_value=False,
516
            is_staticcall=False,
517
            memory_input_start_position=memory_input_start_position,
518
            memory_input_size=memory_input_size,
519
            memory_output_start_position=memory_output_start_position,
520
            memory_output_size=memory_output_size,
521
        ),
522
    )
523
524
    # PROGRAM COUNTER
525
    evm.pc += Uint(1)

staticcall

Message-call into an account.

Parameters

evm : The current EVM frame.

def staticcall(evm: Evm) -> None:
529
    <snip>
538
    # STACK
539
    gas = Uint(pop(evm.stack))
540
    to = to_address_masked(pop(evm.stack))
541
    memory_input_start_position = pop(evm.stack)
542
    memory_input_size = pop(evm.stack)
543
    memory_output_start_position = pop(evm.stack)
544
    memory_output_size = pop(evm.stack)
545
546
    # GAS
547
    extend_memory = calculate_gas_extend_memory(
548
        evm.memory,
549
        [
550
            (memory_input_start_position, memory_input_size),
551
            (memory_output_start_position, memory_output_size),
552
        ],
553
    )
554
555
    code_address = to
556
557
    message_call_gas = calculate_message_call_gas(
558
        U256(0),
559
        gas,
560
        Uint(evm.gas_left),
561
        extend_memory.cost,
562
        GasCosts.OPCODE_CALL_BASE,
563
    )
564
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
565
566
    # OPERATION
567
    evm.memory += b"\x00" * extend_memory.expand_by
568
    generic_call(
569
        evm,
570
        GenericCall(
571
            gas=message_call_gas.sub_call,
572
            value=U256(0),
573
            caller=evm.message.current_target,
574
            to=to,
575
            code_address=code_address,
576
            should_transfer_value=True,
577
            is_staticcall=True,
578
            memory_input_start_position=memory_input_start_position,
579
            memory_input_size=memory_input_size,
580
            memory_output_start_position=memory_output_start_position,
581
            memory_output_size=memory_output_size,
582
        ),
583
    )
584
585
    # PROGRAM COUNTER
586
    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:
590
    <snip>
600
    # STACK
601
    memory_start_index = pop(evm.stack)
602
    size = pop(evm.stack)
603
604
    # GAS
605
    extend_memory = calculate_gas_extend_memory(
606
        evm.memory, [(memory_start_index, size)]
607
    )
608
609
    charge_gas(evm, extend_memory.cost)
610
611
    # OPERATION
612
    evm.memory += b"\x00" * extend_memory.expand_by
613
    output = memory_read_bytes(evm.memory, memory_start_index, size)
614
    evm.output = Bytes(output)
615
    raise Revert
616
617
    # PROGRAM COUNTER
618
    # no-op