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

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
235
    """
236
    Message-call into an account.
237
238
    Parameters
239
    ----------
240
    evm :
241
        The current EVM frame.
242
243
    """
244
    # STACK
245
    gas = Uint(pop(evm.stack))
246
    to = to_address_masked(pop(evm.stack))
247
    value = pop(evm.stack)
248
    memory_input_start_position = pop(evm.stack)
249
    memory_input_size = pop(evm.stack)
250
    memory_output_start_position = pop(evm.stack)
251
    memory_output_size = pop(evm.stack)
252
253
    # GAS
254
    extend_memory = calculate_gas_extend_memory(
255
        evm.memory,
256
        [
257
            (memory_input_start_position, memory_input_size),
258
            (memory_output_start_position, memory_output_size),
259
        ],
260
    )
261
262
    code_address = to
263
264
    message_call_gas = calculate_message_call_gas(
265
        evm.message.block_env.state, gas, to, value
266
    )
267
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
268
269
    # OPERATION
270
    evm.memory += b"\x00" * extend_memory.expand_by
271
    sender_balance = get_account(
272
        evm.message.block_env.state, evm.message.current_target
273
    ).balance
274
    if sender_balance < value:
275
        push(evm.stack, U256(0))
276
        evm.gas_left += message_call_gas.sub_call
277
    else:
278
        generic_call(
279
            evm,
280
            message_call_gas.sub_call,
281
            value,
282
            evm.message.current_target,
283
            to,
284
            code_address,
285
            True,
286
            memory_input_start_position,
287
            memory_input_size,
288
            memory_output_start_position,
289
            memory_output_size,
290
        )
291
292
    # PROGRAM COUNTER
293
    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:
297
    """
298
    Message-call into this account with alternative account’s code.
299
300
    Parameters
301
    ----------
302
    evm :
303
        The current EVM frame.
304
305
    """
306
    # STACK
307
    gas = Uint(pop(evm.stack))
308
    code_address = to_address_masked(pop(evm.stack))
309
    value = pop(evm.stack)
310
    memory_input_start_position = pop(evm.stack)
311
    memory_input_size = pop(evm.stack)
312
    memory_output_start_position = pop(evm.stack)
313
    memory_output_size = pop(evm.stack)
314
315
    # GAS
316
    to = evm.message.current_target
317
318
    extend_memory = calculate_gas_extend_memory(
319
        evm.memory,
320
        [
321
            (memory_input_start_position, memory_input_size),
322
            (memory_output_start_position, memory_output_size),
323
        ],
324
    )
325
    message_call_gas = calculate_message_call_gas(
326
        evm.message.block_env.state, gas, to, value
327
    )
328
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
329
330
    # OPERATION
331
    evm.memory += b"\x00" * extend_memory.expand_by
332
    sender_balance = get_account(
333
        evm.message.block_env.state, evm.message.current_target
334
    ).balance
335
    if sender_balance < value:
336
        push(evm.stack, U256(0))
337
        evm.gas_left += message_call_gas.sub_call
338
    else:
339
        generic_call(
340
            evm,
341
            message_call_gas.sub_call,
342
            value,
343
            evm.message.current_target,
344
            to,
345
            code_address,
346
            True,
347
            memory_input_start_position,
348
            memory_input_size,
349
            memory_output_start_position,
350
            memory_output_size,
351
        )
352
353
    # PROGRAM COUNTER
354
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
358
    """
359
    Halt execution and register account for later deletion.
360
361
    Parameters
362
    ----------
363
    evm :
364
        The current EVM frame.
365
366
    """
367
    # STACK
368
    beneficiary = to_address_masked(pop(evm.stack))
369
370
    # GAS
371
    gas_cost = GAS_ZERO
372
373
    originator = evm.message.current_target
374
375
    refunded_accounts = evm.accounts_to_delete
376
    parent_evm = evm.message.parent_evm
377
    while parent_evm is not None:
378
        refunded_accounts.update(parent_evm.accounts_to_delete)
379
        parent_evm = parent_evm.message.parent_evm
380
381
    if originator not in refunded_accounts:
382
        evm.refund_counter += int(REFUND_SELF_DESTRUCT)
383
384
    charge_gas(evm, gas_cost)
385
386
    # OPERATION
387
    beneficiary_balance = get_account(
388
        evm.message.block_env.state, beneficiary
389
    ).balance
390
    originator_balance = get_account(
391
        evm.message.block_env.state, originator
392
    ).balance
393
394
    # First Transfer to beneficiary
395
    set_account_balance(
396
        evm.message.block_env.state,
397
        beneficiary,
398
        beneficiary_balance + originator_balance,
399
    )
400
    # Next, Zero the balance of the address being deleted (must come after
401
    # sending to beneficiary in case the contract named itself as the
402
    # beneficiary).
403
    set_account_balance(evm.message.block_env.state, originator, U256(0))
404
405
    # register account for deletion
406
    evm.accounts_to_delete.add(originator)
407
408
    # HALT the execution
409
    evm.running = False
410
411
    # PROGRAM COUNTER
412
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
416
    """
417
    Message-call into an account.
418
419
    Parameters
420
    ----------
421
    evm :
422
        The current EVM frame.
423
424
    """
425
    # STACK
426
    gas = Uint(pop(evm.stack))
427
    code_address = to_address_masked(pop(evm.stack))
428
    memory_input_start_position = pop(evm.stack)
429
    memory_input_size = pop(evm.stack)
430
    memory_output_start_position = pop(evm.stack)
431
    memory_output_size = pop(evm.stack)
432
433
    # GAS
434
    extend_memory = calculate_gas_extend_memory(
435
        evm.memory,
436
        [
437
            (memory_input_start_position, memory_input_size),
438
            (memory_output_start_position, memory_output_size),
439
        ],
440
    )
441
    charge_gas(evm, GAS_CALL + gas + extend_memory.cost)
442
443
    # OPERATION
444
    evm.memory += b"\x00" * extend_memory.expand_by
445
    generic_call(
446
        evm,
447
        gas,
448
        evm.message.value,
449
        evm.message.caller,
450
        evm.message.current_target,
451
        code_address,
452
        False,
453
        memory_input_start_position,
454
        memory_input_size,
455
        memory_output_start_position,
456
        memory_output_size,
457
    )
458
459
    # PROGRAM COUNTER
460
    evm.pc += Uint(1)