ethereum.frontier.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 | # This import causes a circular import error |
54 | # if it's not moved inside this method |
55 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create_message |
56 | |
57 | # STACK |
58 | endowment = pop(evm.stack) |
59 | memory_start_position = pop(evm.stack) |
60 | memory_size = pop(evm.stack) |
61 | |
62 | # GAS |
63 | extend_memory = calculate_gas_extend_memory( |
64 | evm.memory, [(memory_start_position, memory_size)] |
65 | ) |
66 | |
67 | charge_gas(evm, GAS_CREATE + extend_memory.cost) |
68 | |
69 | create_message_gas = evm.gas_left |
70 | evm.gas_left = Uint(0) |
71 | |
72 | # OPERATION |
73 | evm.memory += b"\x00" * extend_memory.expand_by |
74 | sender_address = evm.message.current_target |
75 | sender = get_account(evm.message.block_env.state, sender_address) |
76 | |
77 | contract_address = compute_contract_address( |
78 | evm.message.current_target, |
79 | get_account( |
80 | evm.message.block_env.state, evm.message.current_target |
81 | ).nonce, |
82 | ) |
83 | |
84 | if ( |
85 | sender.balance < endowment |
86 | or sender.nonce == Uint(2**64 - 1) |
87 | or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT |
88 | ): |
89 | push(evm.stack, U256(0)) |
90 | evm.gas_left += create_message_gas |
91 | elif account_has_code_or_nonce( |
92 | evm.message.block_env.state, contract_address |
93 | ) or account_has_storage(evm.message.block_env.state, contract_address): |
94 | increment_nonce( |
95 | evm.message.block_env.state, evm.message.current_target |
96 | ) |
97 | push(evm.stack, U256(0)) |
98 | else: |
99 | call_data = memory_read_bytes( |
100 | evm.memory, memory_start_position, memory_size |
101 | ) |
102 |
|
103 | increment_nonce( |
104 | evm.message.block_env.state, evm.message.current_target |
105 | ) |
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 | parent_evm=evm, |
120 | ) |
121 | child_evm = process_create_message(child_message) |
122 |
|
123 | if child_evm.error: |
124 | incorporate_child_on_error(evm, child_evm) |
125 | push(evm.stack, U256(0)) |
126 | else: |
127 | incorporate_child_on_success(evm, child_evm) |
128 | push( |
129 | evm.stack, U256.from_be_bytes(child_evm.message.current_target) |
130 | ) |
131 | |
132 | # PROGRAM COUNTER |
133 | evm.pc += Uint(1) |
return_
Halts execution returning output data.
Parameters
evm : The current EVM frame.
def return_(evm: Evm) -> None:
137 | """ |
---|---|
138 | Halts execution returning output data. |
139 |
|
140 | Parameters |
141 | ---------- |
142 | evm : |
143 | The current EVM frame. |
144 | """ |
145 | # STACK |
146 | memory_start_position = pop(evm.stack) |
147 | memory_size = pop(evm.stack) |
148 | |
149 | # GAS |
150 | extend_memory = calculate_gas_extend_memory( |
151 | evm.memory, [(memory_start_position, memory_size)] |
152 | ) |
153 | |
154 | charge_gas(evm, GAS_ZERO + extend_memory.cost) |
155 | |
156 | # OPERATION |
157 | evm.memory += b"\x00" * extend_memory.expand_by |
158 | evm.output = memory_read_bytes( |
159 | evm.memory, memory_start_position, memory_size |
160 | ) |
161 | |
162 | evm.running = False |
163 | |
164 | # PROGRAM COUNTER |
165 | 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, 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 | code = get_account(evm.message.block_env.state, code_address).code |
194 | child_message = Message( |
195 | block_env=evm.message.block_env, |
196 | tx_env=evm.message.tx_env, |
197 | caller=caller, |
198 | target=to, |
199 | gas=gas, |
200 | value=value, |
201 | data=call_data, |
202 | code=code, |
203 | current_target=to, |
204 | depth=evm.message.depth + Uint(1), |
205 | code_address=code_address, |
206 | parent_evm=evm, |
207 | ) |
208 | child_evm = process_message(child_message) |
209 | |
210 | if child_evm.error: |
211 | incorporate_child_on_error(evm, child_evm) |
212 | push(evm.stack, U256(0)) |
213 | else: |
214 | incorporate_child_on_success(evm, child_evm) |
215 | push(evm.stack, U256(1)) |
216 | |
217 | actual_output_size = min(memory_output_size, U256(len(child_evm.output))) |
218 | memory_write( |
219 | evm.memory, |
220 | memory_output_start_position, |
221 | child_evm.output[:actual_output_size], |
222 | ) |
call
Message-call into an account.
Parameters
evm : The current EVM frame.
def call(evm: Evm) -> None:
226 | """ |
---|---|
227 | Message-call into an account. |
228 |
|
229 | Parameters |
230 | ---------- |
231 | evm : |
232 | The current EVM frame. |
233 | """ |
234 | # STACK |
235 | gas = Uint(pop(evm.stack)) |
236 | to = to_address(pop(evm.stack)) |
237 | value = pop(evm.stack) |
238 | memory_input_start_position = pop(evm.stack) |
239 | memory_input_size = pop(evm.stack) |
240 | memory_output_start_position = pop(evm.stack) |
241 | memory_output_size = pop(evm.stack) |
242 | |
243 | # GAS |
244 | extend_memory = calculate_gas_extend_memory( |
245 | evm.memory, |
246 | [ |
247 | (memory_input_start_position, memory_input_size), |
248 | (memory_output_start_position, memory_output_size), |
249 | ], |
250 | ) |
251 | |
252 | code_address = to |
253 | |
254 | message_call_gas = calculate_message_call_gas( |
255 | evm.message.block_env.state, gas, to, value |
256 | ) |
257 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
258 | |
259 | # OPERATION |
260 | evm.memory += b"\x00" * extend_memory.expand_by |
261 | sender_balance = get_account( |
262 | evm.message.block_env.state, evm.message.current_target |
263 | ).balance |
264 | if sender_balance < value: |
265 | push(evm.stack, U256(0)) |
266 | evm.gas_left += message_call_gas.sub_call |
267 | else: |
268 | generic_call( |
269 | evm, |
270 | message_call_gas.sub_call, |
271 | value, |
272 | evm.message.current_target, |
273 | to, |
274 | code_address, |
275 | memory_input_start_position, |
276 | memory_input_size, |
277 | memory_output_start_position, |
278 | memory_output_size, |
279 | ) |
280 | |
281 | # PROGRAM COUNTER |
282 | 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:
286 | """ |
---|---|
287 | Message-call into this account with alternative account’s code. |
288 |
|
289 | Parameters |
290 | ---------- |
291 | evm : |
292 | The current EVM frame. |
293 | """ |
294 | # STACK |
295 | gas = Uint(pop(evm.stack)) |
296 | code_address = to_address(pop(evm.stack)) |
297 | value = pop(evm.stack) |
298 | memory_input_start_position = pop(evm.stack) |
299 | memory_input_size = pop(evm.stack) |
300 | memory_output_start_position = pop(evm.stack) |
301 | memory_output_size = pop(evm.stack) |
302 | |
303 | # GAS |
304 | to = evm.message.current_target |
305 | |
306 | extend_memory = calculate_gas_extend_memory( |
307 | evm.memory, |
308 | [ |
309 | (memory_input_start_position, memory_input_size), |
310 | (memory_output_start_position, memory_output_size), |
311 | ], |
312 | ) |
313 | message_call_gas = calculate_message_call_gas( |
314 | evm.message.block_env.state, gas, to, value |
315 | ) |
316 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
317 | |
318 | # OPERATION |
319 | evm.memory += b"\x00" * extend_memory.expand_by |
320 | sender_balance = get_account( |
321 | evm.message.block_env.state, evm.message.current_target |
322 | ).balance |
323 | if sender_balance < value: |
324 | push(evm.stack, U256(0)) |
325 | evm.gas_left += message_call_gas.sub_call |
326 | else: |
327 | generic_call( |
328 | evm, |
329 | message_call_gas.sub_call, |
330 | value, |
331 | evm.message.current_target, |
332 | to, |
333 | code_address, |
334 | memory_input_start_position, |
335 | memory_input_size, |
336 | memory_output_start_position, |
337 | memory_output_size, |
338 | ) |
339 | |
340 | # PROGRAM COUNTER |
341 | evm.pc += Uint(1) |
selfdestruct
Halt execution and register account for later deletion.
Parameters
evm : The current EVM frame.
def selfdestruct(evm: Evm) -> None:
345 | """ |
---|---|
346 | Halt execution and register account for later deletion. |
347 |
|
348 | Parameters |
349 | ---------- |
350 | evm : |
351 | The current EVM frame. |
352 | """ |
353 | # STACK |
354 | beneficiary = to_address(pop(evm.stack)) |
355 | |
356 | # GAS |
357 | gas_cost = GAS_ZERO |
358 | |
359 | originator = evm.message.current_target |
360 | |
361 | refunded_accounts = evm.accounts_to_delete |
362 | parent_evm = evm.message.parent_evm |
363 | while parent_evm is not None: |
364 | refunded_accounts.update(parent_evm.accounts_to_delete) |
365 | parent_evm = parent_evm.message.parent_evm |
366 | |
367 | if originator not in refunded_accounts: |
368 | evm.refund_counter += int(REFUND_SELF_DESTRUCT) |
369 | |
370 | charge_gas(evm, gas_cost) |
371 | |
372 | # OPERATION |
373 | beneficiary_balance = get_account( |
374 | evm.message.block_env.state, beneficiary |
375 | ).balance |
376 | originator_balance = get_account( |
377 | evm.message.block_env.state, originator |
378 | ).balance |
379 | |
380 | # First Transfer to beneficiary |
381 | set_account_balance( |
382 | evm.message.block_env.state, |
383 | beneficiary, |
384 | beneficiary_balance + originator_balance, |
385 | ) |
386 | # Next, Zero the balance of the address being deleted (must come after |
387 | # sending to beneficiary in case the contract named itself as the |
388 | # beneficiary). |
389 | set_account_balance(evm.message.block_env.state, originator, U256(0)) |
390 | |
391 | # register account for deletion |
392 | evm.accounts_to_delete.add(originator) |
393 | |
394 | # HALT the execution |
395 | evm.running = False |
396 | |
397 | # PROGRAM COUNTER |
398 | pass |