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

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

call

Message-call into an account.

Parameters

evm : The current EVM frame.

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

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

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

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

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