ethereum.tangerine_whistle.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:
52 | """ |
---|---|
53 | Creates a new account with associated code. |
54 |
|
55 | Parameters |
56 | ---------- |
57 | evm : |
58 | The current EVM frame. |
59 | """ |
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, GAS_CREATE + extend_memory.cost) |
75 | |
76 | create_message_gas = max_message_call_gas(Uint(evm.gas_left)) |
77 | evm.gas_left -= create_message_gas |
78 | |
79 | # OPERATION |
80 | evm.memory += b"\x00" * extend_memory.expand_by |
81 | sender_address = evm.message.current_target |
82 | sender = get_account(evm.env.state, sender_address) |
83 | |
84 | contract_address = compute_contract_address( |
85 | evm.message.current_target, |
86 | get_account(evm.env.state, evm.message.current_target).nonce, |
87 | ) |
88 | |
89 | if ( |
90 | sender.balance < endowment |
91 | or sender.nonce == Uint(2**64 - 1) |
92 | or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT |
93 | ): |
94 | push(evm.stack, U256(0)) |
95 | evm.gas_left += create_message_gas |
96 | elif account_has_code_or_nonce( |
97 | evm.env.state, contract_address |
98 | ) or account_has_storage(evm.env.state, contract_address): |
99 | increment_nonce(evm.env.state, evm.message.current_target) |
100 | push(evm.stack, U256(0)) |
101 | else: |
102 | call_data = memory_read_bytes( |
103 | evm.memory, memory_start_position, memory_size |
104 | ) |
105 |
|
106 | increment_nonce(evm.env.state, evm.message.current_target) |
107 |
|
108 | child_message = Message( |
109 | caller=evm.message.current_target, |
110 | target=Bytes0(), |
111 | gas=create_message_gas, |
112 | value=endowment, |
113 | data=b"", |
114 | code=call_data, |
115 | current_target=contract_address, |
116 | depth=evm.message.depth + Uint(1), |
117 | code_address=None, |
118 | should_transfer_value=True, |
119 | parent_evm=evm, |
120 | ) |
121 | child_evm = process_create_message(child_message, evm.env) |
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, should_transfer_value: bool, memory_input_start_position: U256, memory_input_size: U256, memory_output_start_position: U256, memory_output_size: U256) -> None:
181 | """ |
---|---|
182 | Perform the core logic of the `CALL*` family of opcodes. |
183 | """ |
184 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message |
185 | |
186 | if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT: |
187 | evm.gas_left += gas |
188 | push(evm.stack, U256(0)) |
189 | return |
190 | |
191 | call_data = memory_read_bytes( |
192 | evm.memory, memory_input_start_position, memory_input_size |
193 | ) |
194 | code = get_account(evm.env.state, code_address).code |
195 | child_message = Message( |
196 | caller=caller, |
197 | target=to, |
198 | gas=gas, |
199 | value=value, |
200 | data=call_data, |
201 | code=code, |
202 | current_target=to, |
203 | depth=evm.message.depth + Uint(1), |
204 | code_address=code_address, |
205 | should_transfer_value=should_transfer_value, |
206 | parent_evm=evm, |
207 | ) |
208 | child_evm = process_message(child_message, evm.env) |
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 | _account_exists = account_exists(evm.env.state, to) |
252 | create_gas_cost = Uint(0) if _account_exists else GAS_NEW_ACCOUNT |
253 | transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE |
254 | message_call_gas = calculate_message_call_gas( |
255 | value, |
256 | gas, |
257 | Uint(evm.gas_left), |
258 | extend_memory.cost, |
259 | GAS_CALL + create_gas_cost + transfer_gas_cost, |
260 | ) |
261 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
262 | |
263 | # OPERATION |
264 | evm.memory += b"\x00" * extend_memory.expand_by |
265 | sender_balance = get_account( |
266 | evm.env.state, evm.message.current_target |
267 | ).balance |
268 | if sender_balance < value: |
269 | push(evm.stack, U256(0)) |
270 | evm.gas_left += message_call_gas.stipend |
271 | else: |
272 | generic_call( |
273 | evm, |
274 | message_call_gas.stipend, |
275 | value, |
276 | evm.message.current_target, |
277 | to, |
278 | to, |
279 | True, |
280 | memory_input_start_position, |
281 | memory_input_size, |
282 | memory_output_start_position, |
283 | memory_output_size, |
284 | ) |
285 | |
286 | # PROGRAM COUNTER |
287 | 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:
291 | """ |
---|---|
292 | Message-call into this account with alternative account’s code. |
293 |
|
294 | Parameters |
295 | ---------- |
296 | evm : |
297 | The current EVM frame. |
298 | """ |
299 | # STACK |
300 | gas = Uint(pop(evm.stack)) |
301 | code_address = to_address(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 | transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE |
319 | message_call_gas = calculate_message_call_gas( |
320 | value, |
321 | gas, |
322 | Uint(evm.gas_left), |
323 | extend_memory.cost, |
324 | GAS_CALL + transfer_gas_cost, |
325 | ) |
326 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
327 | |
328 | # OPERATION |
329 | evm.memory += b"\x00" * extend_memory.expand_by |
330 | sender_balance = get_account( |
331 | evm.env.state, evm.message.current_target |
332 | ).balance |
333 | if sender_balance < value: |
334 | push(evm.stack, U256(0)) |
335 | evm.gas_left += message_call_gas.stipend |
336 | else: |
337 | generic_call( |
338 | evm, |
339 | message_call_gas.stipend, |
340 | value, |
341 | evm.message.current_target, |
342 | to, |
343 | code_address, |
344 | True, |
345 | memory_input_start_position, |
346 | memory_input_size, |
347 | memory_output_start_position, |
348 | memory_output_size, |
349 | ) |
350 | |
351 | # PROGRAM COUNTER |
352 | evm.pc += Uint(1) |
selfdestruct
Halt execution and register account for later deletion.
Parameters
evm : The current EVM frame.
def selfdestruct(evm: Evm) -> None:
356 | """ |
---|---|
357 | Halt execution and register account for later deletion. |
358 |
|
359 | Parameters |
360 | ---------- |
361 | evm : |
362 | The current EVM frame. |
363 | """ |
364 | # STACK |
365 | beneficiary = to_address(pop(evm.stack)) |
366 | |
367 | # GAS |
368 | gas_cost = GAS_SELF_DESTRUCT |
369 | if not account_exists(evm.env.state, beneficiary): |
370 | gas_cost += GAS_SELF_DESTRUCT_NEW_ACCOUNT |
371 | |
372 | originator = evm.message.current_target |
373 | |
374 | refunded_accounts = evm.accounts_to_delete |
375 | parent_evm = evm.message.parent_evm |
376 | while parent_evm is not None: |
377 | refunded_accounts.update(parent_evm.accounts_to_delete) |
378 | parent_evm = parent_evm.message.parent_evm |
379 | |
380 | if originator not in refunded_accounts: |
381 | evm.refund_counter += REFUND_SELF_DESTRUCT |
382 | |
383 | charge_gas(evm, gas_cost) |
384 | |
385 | # OPERATION |
386 | beneficiary_balance = get_account(evm.env.state, beneficiary).balance |
387 | originator_balance = get_account(evm.env.state, originator).balance |
388 | |
389 | # First Transfer to beneficiary |
390 | set_account_balance( |
391 | evm.env.state, beneficiary, 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.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 | # STACK |
418 | gas = Uint(pop(evm.stack)) |
419 | code_address = to_address(pop(evm.stack)) |
420 | memory_input_start_position = pop(evm.stack) |
421 | memory_input_size = pop(evm.stack) |
422 | memory_output_start_position = pop(evm.stack) |
423 | memory_output_size = pop(evm.stack) |
424 | |
425 | # GAS |
426 | extend_memory = calculate_gas_extend_memory( |
427 | evm.memory, |
428 | [ |
429 | (memory_input_start_position, memory_input_size), |
430 | (memory_output_start_position, memory_output_size), |
431 | ], |
432 | ) |
433 | message_call_gas = calculate_message_call_gas( |
434 | U256(0), gas, Uint(evm.gas_left), extend_memory.cost, GAS_CALL |
435 | ) |
436 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
437 | |
438 | # OPERATION |
439 | evm.memory += b"\x00" * extend_memory.expand_by |
440 | generic_call( |
441 | evm, |
442 | message_call_gas.stipend, |
443 | evm.message.value, |
444 | evm.message.caller, |
445 | evm.message.current_target, |
446 | code_address, |
447 | False, |
448 | memory_input_start_position, |
449 | memory_input_size, |
450 | memory_output_start_position, |
451 | memory_output_size, |
452 | ) |
453 | |
454 | # PROGRAM COUNTER |
455 | evm.pc += Uint(1) |