ethereum.forks.frontier.vm.instructions.systemethereum.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:
48
    <snip>
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, GasCosts.OPCODE_CREATE_BASE + 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.tx_env.state, sender_address)
80
81
    contract_address = compute_contract_address(
82
        evm.message.current_target,
83
        get_account(
84
            evm.message.tx_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.tx_env.state, contract_address
97
    ) or account_has_storage(evm.message.tx_env.state, contract_address):
98
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
99
        push(evm.stack, U256(0))
100
    else:
101
        call_data = memory_read_bytes(
102
            evm.memory, memory_start_position, memory_size
103
        )
104
105
        increment_nonce(evm.message.tx_env.state, evm.message.current_target)
106
107
        child_message = Message(
108
            block_env=evm.message.block_env,
109
            tx_env=evm.message.tx_env,
110
            caller=evm.message.current_target,
111
            target=Bytes0(),
112
            gas=create_message_gas,
113
            value=endowment,
114
            data=b"",
115
            code=call_data,
116
            current_target=contract_address,
117
            depth=evm.message.depth + Uint(1),
118
            code_address=None,
119
            should_transfer_value=True,
120
            parent_evm=evm,
121
        )
122
        child_evm = process_create_message(child_message)
123
124
        if child_evm.error:
125
            incorporate_child_on_error(evm, child_evm)
126
            push(evm.stack, U256(0))
127
        else:
128
            incorporate_child_on_success(evm, child_evm)
129
            push(
130
                evm.stack, U256.from_be_bytes(child_evm.message.current_target)
131
            )
132
133
    # PROGRAM COUNTER
134
    evm.pc += Uint(1)

return_

Halts execution returning output data.

Parameters

evm : The current EVM frame.

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

GenericCall

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

170
@final
171
@dataclass
class GenericCall:

gas

177
    gas: Uint

value

178
    value: U256

caller

179
    caller: Address

to

180
    to: Address

code_address

181
    code_address: Address

should_transfer_value

182
    should_transfer_value: bool

memory_input_start_position

183
    memory_input_start_position: U256

memory_input_size

184
    memory_input_size: U256

memory_output_start_position

185
    memory_output_start_position: U256

memory_output_size

186
    memory_output_size: U256

generic_call

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

def generic_call(evm: Evm, ​​params: GenericCall) -> None:
190
    <snip>
193
    from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message
194
195
    if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT:
196
        evm.gas_left += params.gas
197
        push(evm.stack, U256(0))
198
        return
199
200
    call_data = memory_read_bytes(
201
        evm.memory,
202
        params.memory_input_start_position,
203
        params.memory_input_size,
204
    )
205
    account_to_call = get_account(
206
        evm.message.tx_env.state, params.code_address
207
    )
208
    code = get_code(evm.message.tx_env.state, account_to_call.code_hash)
209
    child_message = Message(
210
        block_env=evm.message.block_env,
211
        tx_env=evm.message.tx_env,
212
        caller=params.caller,
213
        target=params.to,
214
        gas=params.gas,
215
        value=params.value,
216
        data=call_data,
217
        code=code,
218
        current_target=params.to,
219
        depth=evm.message.depth + Uint(1),
220
        code_address=params.code_address,
221
        should_transfer_value=params.should_transfer_value,
222
        parent_evm=evm,
223
    )
224
    child_evm = process_message(child_message)
225
226
    if child_evm.error:
227
        incorporate_child_on_error(evm, child_evm)
228
        push(evm.stack, U256(0))
229
    else:
230
        incorporate_child_on_success(evm, child_evm)
231
        push(evm.stack, U256(1))
232
233
    actual_output_size = min(
234
        params.memory_output_size, U256(len(child_evm.output))
235
    )
236
    memory_write(
237
        evm.memory,
238
        params.memory_output_start_position,
239
        child_evm.output[:actual_output_size],
240
    )

call

Message-call into an account.

Parameters

evm : The current EVM frame.

def call(evm: Evm) -> None:
244
    <snip>
253
    # STACK
254
    gas = Uint(pop(evm.stack))
255
    to = to_address_masked(pop(evm.stack))
256
    value = pop(evm.stack)
257
    memory_input_start_position = pop(evm.stack)
258
    memory_input_size = pop(evm.stack)
259
    memory_output_start_position = pop(evm.stack)
260
    memory_output_size = pop(evm.stack)
261
262
    # GAS
263
    extend_memory = calculate_gas_extend_memory(
264
        evm.memory,
265
        [
266
            (memory_input_start_position, memory_input_size),
267
            (memory_output_start_position, memory_output_size),
268
        ],
269
    )
270
271
    code_address = to
272
273
    message_call_gas = calculate_message_call_gas(
274
        evm.message.tx_env.state, gas, to, value
275
    )
276
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
277
278
    # OPERATION
279
    evm.memory += b"\x00" * extend_memory.expand_by
280
    sender_balance = get_account(
281
        evm.message.tx_env.state, evm.message.current_target
282
    ).balance
283
    if sender_balance < value:
284
        push(evm.stack, U256(0))
285
        evm.gas_left += message_call_gas.sub_call
286
    else:
287
        generic_call(
288
            evm,
289
            GenericCall(
290
                gas=message_call_gas.sub_call,
291
                value=value,
292
                caller=evm.message.current_target,
293
                to=to,
294
                code_address=code_address,
295
                should_transfer_value=True,
296
                memory_input_start_position=memory_input_start_position,
297
                memory_input_size=memory_input_size,
298
                memory_output_start_position=memory_output_start_position,
299
                memory_output_size=memory_output_size,
300
            ),
301
        )
302
303
    # PROGRAM COUNTER
304
    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:
308
    <snip>
317
    # STACK
318
    gas = Uint(pop(evm.stack))
319
    code_address = to_address_masked(pop(evm.stack))
320
    value = pop(evm.stack)
321
    memory_input_start_position = pop(evm.stack)
322
    memory_input_size = pop(evm.stack)
323
    memory_output_start_position = pop(evm.stack)
324
    memory_output_size = pop(evm.stack)
325
326
    # GAS
327
    to = evm.message.current_target
328
329
    extend_memory = calculate_gas_extend_memory(
330
        evm.memory,
331
        [
332
            (memory_input_start_position, memory_input_size),
333
            (memory_output_start_position, memory_output_size),
334
        ],
335
    )
336
    message_call_gas = calculate_message_call_gas(
337
        evm.message.tx_env.state, gas, to, value
338
    )
339
    charge_gas(evm, message_call_gas.cost + extend_memory.cost)
340
341
    # OPERATION
342
    evm.memory += b"\x00" * extend_memory.expand_by
343
    sender_balance = get_account(
344
        evm.message.tx_env.state, evm.message.current_target
345
    ).balance
346
    if sender_balance < value:
347
        push(evm.stack, U256(0))
348
        evm.gas_left += message_call_gas.sub_call
349
    else:
350
        generic_call(
351
            evm,
352
            GenericCall(
353
                gas=message_call_gas.sub_call,
354
                value=value,
355
                caller=evm.message.current_target,
356
                to=to,
357
                code_address=code_address,
358
                should_transfer_value=True,
359
                memory_input_start_position=memory_input_start_position,
360
                memory_input_size=memory_input_size,
361
                memory_output_start_position=memory_output_start_position,
362
                memory_output_size=memory_output_size,
363
            ),
364
        )
365
366
    # PROGRAM COUNTER
367
    evm.pc += Uint(1)

selfdestruct

Halt execution and register account for later deletion.

Parameters

evm : The current EVM frame.

def selfdestruct(evm: Evm) -> None:
371
    <snip>
380
    # STACK
381
    beneficiary = to_address_masked(pop(evm.stack))
382
383
    # GAS
384
    gas_cost = GasCosts.ZERO
385
386
    originator = evm.message.current_target
387
388
    refunded_accounts = evm.accounts_to_delete
389
    parent_evm = evm.message.parent_evm
390
    while parent_evm is not None:
391
        refunded_accounts.update(parent_evm.accounts_to_delete)
392
        parent_evm = parent_evm.message.parent_evm
393
394
    if originator not in refunded_accounts:
395
        evm.refund_counter += int(GasCosts.REFUND_SELF_DESTRUCT)
396
397
    charge_gas(evm, gas_cost)
398
399
    # OPERATION
400
    beneficiary_balance = get_account(
401
        evm.message.tx_env.state, beneficiary
402
    ).balance
403
    originator_balance = get_account(
404
        evm.message.tx_env.state, originator
405
    ).balance
406
407
    # First Transfer to beneficiary
408
    set_account_balance(
409
        evm.message.tx_env.state,
410
        beneficiary,
411
        beneficiary_balance + originator_balance,
412
    )
413
    # Next, Zero the balance of the address being deleted (must come after
414
    # sending to beneficiary in case the contract named itself as the
415
    # beneficiary).
416
    set_account_balance(evm.message.tx_env.state, originator, U256(0))
417
418
    # register account for deletion
419
    evm.accounts_to_delete.add(originator)
420
421
    # HALT the execution
422
    evm.running = False
423
424
    # PROGRAM COUNTER
425
    pass

delegatecall

Message-call into an account.

Parameters

evm : The current EVM frame.

def delegatecall(evm: Evm) -> None:
429
    <snip>
438
    # STACK
439
    gas = Uint(pop(evm.stack))
440
    code_address = to_address_masked(pop(evm.stack))
441
    memory_input_start_position = pop(evm.stack)
442
    memory_input_size = pop(evm.stack)
443
    memory_output_start_position = pop(evm.stack)
444
    memory_output_size = pop(evm.stack)
445
446
    # GAS
447
    extend_memory = calculate_gas_extend_memory(
448
        evm.memory,
449
        [
450
            (memory_input_start_position, memory_input_size),
451
            (memory_output_start_position, memory_output_size),
452
        ],
453
    )
454
    charge_gas(evm, GasCosts.OPCODE_CALL_BASE + gas + extend_memory.cost)
455
456
    # OPERATION
457
    evm.memory += b"\x00" * extend_memory.expand_by
458
    generic_call(
459
        evm,
460
        GenericCall(
461
            gas=gas,
462
            value=evm.message.value,
463
            caller=evm.message.caller,
464
            to=evm.message.current_target,
465
            code_address=code_address,
466
            should_transfer_value=False,
467
            memory_input_start_position=memory_input_start_position,
468
            memory_input_size=memory_input_size,
469
            memory_output_start_position=memory_output_start_position,
470
            memory_output_size=memory_output_size,
471
        ),
472
    )
473
474
    # PROGRAM COUNTER
475
    evm.pc += Uint(1)