ethereum.forks.byzantium.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:
54 | """ |
---|---|
55 | Creates a new account with associated code. |
56 |
|
57 | Parameters |
58 | ---------- |
59 | evm : |
60 | The current EVM frame. |
61 |
|
62 | """ |
63 | # This import causes a circular import error |
64 | # if it's not moved inside this method |
65 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_create_message |
66 | |
67 | # STACK |
68 | endowment = pop(evm.stack) |
69 | memory_start_position = pop(evm.stack) |
70 | memory_size = pop(evm.stack) |
71 | |
72 | # GAS |
73 | extend_memory = calculate_gas_extend_memory( |
74 | evm.memory, [(memory_start_position, memory_size)] |
75 | ) |
76 | |
77 | charge_gas(evm, GAS_CREATE + extend_memory.cost) |
78 | |
79 | create_message_gas = max_message_call_gas(Uint(evm.gas_left)) |
80 | evm.gas_left -= create_message_gas |
81 | if evm.message.is_static: |
82 | raise WriteInStaticContext |
83 | evm.memory += b"\x00" * extend_memory.expand_by |
84 | evm.return_data = b"" |
85 | |
86 | sender_address = evm.message.current_target |
87 | sender = get_account(evm.message.block_env.state, sender_address) |
88 | |
89 | contract_address = compute_contract_address( |
90 | evm.message.current_target, |
91 | get_account( |
92 | evm.message.block_env.state, evm.message.current_target |
93 | ).nonce, |
94 | ) |
95 | |
96 | if ( |
97 | sender.balance < endowment |
98 | or sender.nonce == Uint(2**64 - 1) |
99 | or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT |
100 | ): |
101 | push(evm.stack, U256(0)) |
102 | evm.gas_left += create_message_gas |
103 | elif account_has_code_or_nonce( |
104 | evm.message.block_env.state, contract_address |
105 | ) or account_has_storage(evm.message.block_env.state, contract_address): |
106 | increment_nonce( |
107 | evm.message.block_env.state, evm.message.current_target |
108 | ) |
109 | push(evm.stack, U256(0)) |
110 | else: |
111 | call_data = memory_read_bytes( |
112 | evm.memory, memory_start_position, memory_size |
113 | ) |
114 |
|
115 | increment_nonce( |
116 | evm.message.block_env.state, evm.message.current_target |
117 | ) |
118 |
|
119 | child_message = Message( |
120 | block_env=evm.message.block_env, |
121 | tx_env=evm.message.tx_env, |
122 | caller=evm.message.current_target, |
123 | target=Bytes0(), |
124 | gas=create_message_gas, |
125 | value=endowment, |
126 | data=b"", |
127 | code=call_data, |
128 | current_target=contract_address, |
129 | depth=evm.message.depth + Uint(1), |
130 | code_address=None, |
131 | should_transfer_value=True, |
132 | is_static=False, |
133 | parent_evm=evm, |
134 | ) |
135 | child_evm = process_create_message(child_message) |
136 |
|
137 | if child_evm.error: |
138 | incorporate_child_on_error(evm, child_evm) |
139 | evm.return_data = child_evm.output |
140 | push(evm.stack, U256(0)) |
141 | else: |
142 | incorporate_child_on_success(evm, child_evm) |
143 | evm.return_data = b"" |
144 | push( |
145 | evm.stack, U256.from_be_bytes(child_evm.message.current_target) |
146 | ) |
147 | |
148 | # PROGRAM COUNTER |
149 | evm.pc += Uint(1) |
return_
Halts execution returning output data.
Parameters
evm : The current EVM frame.
def return_(evm: Evm) -> None:
153 | """ |
---|---|
154 | Halts execution returning output data. |
155 |
|
156 | Parameters |
157 | ---------- |
158 | evm : |
159 | The current EVM frame. |
160 |
|
161 | """ |
162 | # STACK |
163 | memory_start_position = pop(evm.stack) |
164 | memory_size = pop(evm.stack) |
165 | |
166 | # GAS |
167 | extend_memory = calculate_gas_extend_memory( |
168 | evm.memory, [(memory_start_position, memory_size)] |
169 | ) |
170 | |
171 | charge_gas(evm, GAS_ZERO + extend_memory.cost) |
172 | |
173 | # OPERATION |
174 | evm.memory += b"\x00" * extend_memory.expand_by |
175 | evm.output = memory_read_bytes( |
176 | evm.memory, memory_start_position, memory_size |
177 | ) |
178 | |
179 | evm.running = False |
180 | |
181 | # PROGRAM COUNTER |
182 | 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, is_staticcall: bool, memory_input_start_position: U256, memory_input_size: U256, memory_output_start_position: U256, memory_output_size: U256) -> None:
199 | """ |
---|---|
200 | Perform the core logic of the `CALL*` family of opcodes. |
201 | """ |
202 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message |
203 | |
204 | evm.return_data = b"" |
205 | |
206 | if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT: |
207 | evm.gas_left += gas |
208 | push(evm.stack, U256(0)) |
209 | return |
210 | |
211 | call_data = memory_read_bytes( |
212 | evm.memory, memory_input_start_position, memory_input_size |
213 | ) |
214 | code = get_account(evm.message.block_env.state, code_address).code |
215 | child_message = Message( |
216 | block_env=evm.message.block_env, |
217 | tx_env=evm.message.tx_env, |
218 | caller=caller, |
219 | target=to, |
220 | gas=gas, |
221 | value=value, |
222 | data=call_data, |
223 | code=code, |
224 | current_target=to, |
225 | depth=evm.message.depth + Uint(1), |
226 | code_address=code_address, |
227 | should_transfer_value=should_transfer_value, |
228 | is_static=True if is_staticcall else evm.message.is_static, |
229 | parent_evm=evm, |
230 | ) |
231 | child_evm = process_message(child_message) |
232 | |
233 | if child_evm.error: |
234 | incorporate_child_on_error(evm, child_evm) |
235 | evm.return_data = child_evm.output |
236 | push(evm.stack, U256(0)) |
237 | else: |
238 | incorporate_child_on_success(evm, child_evm) |
239 | evm.return_data = child_evm.output |
240 | push(evm.stack, U256(1)) |
241 | |
242 | actual_output_size = min(memory_output_size, U256(len(child_evm.output))) |
243 | memory_write( |
244 | evm.memory, |
245 | memory_output_start_position, |
246 | child_evm.output[:actual_output_size], |
247 | ) |
call
Message-call into an account.
Parameters
evm : The current EVM frame.
def call(evm: Evm) -> None:
251 | """ |
---|---|
252 | Message-call into an account. |
253 |
|
254 | Parameters |
255 | ---------- |
256 | evm : |
257 | The current EVM frame. |
258 |
|
259 | """ |
260 | # STACK |
261 | gas = Uint(pop(evm.stack)) |
262 | to = to_address_masked(pop(evm.stack)) |
263 | value = pop(evm.stack) |
264 | memory_input_start_position = pop(evm.stack) |
265 | memory_input_size = pop(evm.stack) |
266 | memory_output_start_position = pop(evm.stack) |
267 | memory_output_size = pop(evm.stack) |
268 | |
269 | # GAS |
270 | extend_memory = calculate_gas_extend_memory( |
271 | evm.memory, |
272 | [ |
273 | (memory_input_start_position, memory_input_size), |
274 | (memory_output_start_position, memory_output_size), |
275 | ], |
276 | ) |
277 | |
278 | code_address = to |
279 | |
280 | create_gas_cost = GAS_NEW_ACCOUNT |
281 | if value == 0 or is_account_alive(evm.message.block_env.state, to): |
282 | create_gas_cost = Uint(0) |
283 | transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE |
284 | message_call_gas = calculate_message_call_gas( |
285 | value, |
286 | gas, |
287 | Uint(evm.gas_left), |
288 | extend_memory.cost, |
289 | GAS_CALL + create_gas_cost + transfer_gas_cost, |
290 | ) |
291 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
292 | if evm.message.is_static and value != U256(0): |
293 | raise WriteInStaticContext |
294 | evm.memory += b"\x00" * extend_memory.expand_by |
295 | sender_balance = get_account( |
296 | evm.message.block_env.state, evm.message.current_target |
297 | ).balance |
298 | if sender_balance < value: |
299 | push(evm.stack, U256(0)) |
300 | evm.return_data = b"" |
301 | evm.gas_left += message_call_gas.sub_call |
302 | else: |
303 | generic_call( |
304 | evm, |
305 | message_call_gas.sub_call, |
306 | value, |
307 | evm.message.current_target, |
308 | to, |
309 | code_address, |
310 | True, |
311 | False, |
312 | memory_input_start_position, |
313 | memory_input_size, |
314 | memory_output_start_position, |
315 | memory_output_size, |
316 | ) |
317 | |
318 | # PROGRAM COUNTER |
319 | 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:
323 | """ |
---|---|
324 | Message-call into this account with alternative account’s code. |
325 |
|
326 | Parameters |
327 | ---------- |
328 | evm : |
329 | The current EVM frame. |
330 |
|
331 | """ |
332 | # STACK |
333 | gas = Uint(pop(evm.stack)) |
334 | code_address = to_address_masked(pop(evm.stack)) |
335 | value = pop(evm.stack) |
336 | memory_input_start_position = pop(evm.stack) |
337 | memory_input_size = pop(evm.stack) |
338 | memory_output_start_position = pop(evm.stack) |
339 | memory_output_size = pop(evm.stack) |
340 | |
341 | # GAS |
342 | to = evm.message.current_target |
343 | |
344 | extend_memory = calculate_gas_extend_memory( |
345 | evm.memory, |
346 | [ |
347 | (memory_input_start_position, memory_input_size), |
348 | (memory_output_start_position, memory_output_size), |
349 | ], |
350 | ) |
351 | transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE |
352 | message_call_gas = calculate_message_call_gas( |
353 | value, |
354 | gas, |
355 | Uint(evm.gas_left), |
356 | extend_memory.cost, |
357 | GAS_CALL + transfer_gas_cost, |
358 | ) |
359 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
360 | |
361 | # OPERATION |
362 | evm.memory += b"\x00" * extend_memory.expand_by |
363 | sender_balance = get_account( |
364 | evm.message.block_env.state, evm.message.current_target |
365 | ).balance |
366 | if sender_balance < value: |
367 | push(evm.stack, U256(0)) |
368 | evm.return_data = b"" |
369 | evm.gas_left += message_call_gas.sub_call |
370 | else: |
371 | generic_call( |
372 | evm, |
373 | message_call_gas.sub_call, |
374 | value, |
375 | evm.message.current_target, |
376 | to, |
377 | code_address, |
378 | True, |
379 | False, |
380 | memory_input_start_position, |
381 | memory_input_size, |
382 | memory_output_start_position, |
383 | memory_output_size, |
384 | ) |
385 | |
386 | # PROGRAM COUNTER |
387 | evm.pc += Uint(1) |
selfdestruct
Halt execution and register account for later deletion.
Parameters
evm : The current EVM frame.
def selfdestruct(evm: Evm) -> None:
391 | """ |
---|---|
392 | Halt execution and register account for later deletion. |
393 |
|
394 | Parameters |
395 | ---------- |
396 | evm : |
397 | The current EVM frame. |
398 |
|
399 | """ |
400 | # STACK |
401 | beneficiary = to_address_masked(pop(evm.stack)) |
402 | |
403 | # GAS |
404 | gas_cost = GAS_SELF_DESTRUCT |
405 | if ( |
406 | not is_account_alive(evm.message.block_env.state, beneficiary) |
407 | and get_account( |
408 | evm.message.block_env.state, evm.message.current_target |
409 | ).balance |
410 | != 0 |
411 | ): |
412 | gas_cost += GAS_SELF_DESTRUCT_NEW_ACCOUNT |
413 | |
414 | originator = evm.message.current_target |
415 | |
416 | refunded_accounts = evm.accounts_to_delete |
417 | parent_evm = evm.message.parent_evm |
418 | while parent_evm is not None: |
419 | refunded_accounts.update(parent_evm.accounts_to_delete) |
420 | parent_evm = parent_evm.message.parent_evm |
421 | |
422 | if originator not in refunded_accounts: |
423 | evm.refund_counter += REFUND_SELF_DESTRUCT |
424 | |
425 | charge_gas(evm, gas_cost) |
426 | if evm.message.is_static: |
427 | raise WriteInStaticContext |
428 | |
429 | beneficiary_balance = get_account( |
430 | evm.message.block_env.state, beneficiary |
431 | ).balance |
432 | originator_balance = get_account( |
433 | evm.message.block_env.state, originator |
434 | ).balance |
435 | |
436 | # First Transfer to beneficiary |
437 | set_account_balance( |
438 | evm.message.block_env.state, |
439 | beneficiary, |
440 | beneficiary_balance + originator_balance, |
441 | ) |
442 | # Next, Zero the balance of the address being deleted (must come after |
443 | # sending to beneficiary in case the contract named itself as the |
444 | # beneficiary). |
445 | set_account_balance(evm.message.block_env.state, originator, U256(0)) |
446 | |
447 | # register account for deletion |
448 | evm.accounts_to_delete.add(originator) |
449 | |
450 | # mark beneficiary as touched |
451 | if account_exists_and_is_empty(evm.message.block_env.state, beneficiary): |
452 | evm.touched_accounts.add(beneficiary) |
453 | |
454 | # HALT the execution |
455 | evm.running = False |
456 | |
457 | # PROGRAM COUNTER |
458 | pass |
delegatecall
Message-call into an account.
Parameters
evm : The current EVM frame.
def delegatecall(evm: Evm) -> None:
462 | """ |
---|---|
463 | Message-call into an account. |
464 |
|
465 | Parameters |
466 | ---------- |
467 | evm : |
468 | The current EVM frame. |
469 |
|
470 | """ |
471 | # STACK |
472 | gas = Uint(pop(evm.stack)) |
473 | code_address = to_address_masked(pop(evm.stack)) |
474 | memory_input_start_position = pop(evm.stack) |
475 | memory_input_size = pop(evm.stack) |
476 | memory_output_start_position = pop(evm.stack) |
477 | memory_output_size = pop(evm.stack) |
478 | |
479 | # GAS |
480 | extend_memory = calculate_gas_extend_memory( |
481 | evm.memory, |
482 | [ |
483 | (memory_input_start_position, memory_input_size), |
484 | (memory_output_start_position, memory_output_size), |
485 | ], |
486 | ) |
487 | message_call_gas = calculate_message_call_gas( |
488 | U256(0), gas, Uint(evm.gas_left), extend_memory.cost, GAS_CALL |
489 | ) |
490 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
491 | |
492 | # OPERATION |
493 | evm.memory += b"\x00" * extend_memory.expand_by |
494 | generic_call( |
495 | evm, |
496 | message_call_gas.sub_call, |
497 | evm.message.value, |
498 | evm.message.caller, |
499 | evm.message.current_target, |
500 | code_address, |
501 | False, |
502 | False, |
503 | memory_input_start_position, |
504 | memory_input_size, |
505 | memory_output_start_position, |
506 | memory_output_size, |
507 | ) |
508 | |
509 | # PROGRAM COUNTER |
510 | evm.pc += Uint(1) |
staticcall
Message-call into an account.
Parameters
evm : The current EVM frame.
def staticcall(evm: Evm) -> None:
514 | """ |
---|---|
515 | Message-call into an account. |
516 |
|
517 | Parameters |
518 | ---------- |
519 | evm : |
520 | The current EVM frame. |
521 |
|
522 | """ |
523 | # STACK |
524 | gas = Uint(pop(evm.stack)) |
525 | to = to_address_masked(pop(evm.stack)) |
526 | memory_input_start_position = pop(evm.stack) |
527 | memory_input_size = pop(evm.stack) |
528 | memory_output_start_position = pop(evm.stack) |
529 | memory_output_size = pop(evm.stack) |
530 | |
531 | # GAS |
532 | extend_memory = calculate_gas_extend_memory( |
533 | evm.memory, |
534 | [ |
535 | (memory_input_start_position, memory_input_size), |
536 | (memory_output_start_position, memory_output_size), |
537 | ], |
538 | ) |
539 | |
540 | code_address = to |
541 | |
542 | message_call_gas = calculate_message_call_gas( |
543 | U256(0), |
544 | gas, |
545 | Uint(evm.gas_left), |
546 | extend_memory.cost, |
547 | GAS_CALL, |
548 | ) |
549 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
550 | |
551 | # OPERATION |
552 | evm.memory += b"\x00" * extend_memory.expand_by |
553 | generic_call( |
554 | evm, |
555 | message_call_gas.sub_call, |
556 | U256(0), |
557 | evm.message.current_target, |
558 | to, |
559 | code_address, |
560 | True, |
561 | True, |
562 | memory_input_start_position, |
563 | memory_input_size, |
564 | memory_output_start_position, |
565 | memory_output_size, |
566 | ) |
567 | |
568 | # PROGRAM COUNTER |
569 | evm.pc += Uint(1) |
revert
Stop execution and revert state changes, without consuming all provided gas and also has the ability to return a reason.
Parameters
evm : The current EVM frame.
def revert(evm: Evm) -> None:
573 | """ |
---|---|
574 | Stop execution and revert state changes, without consuming all provided gas |
575 | and also has the ability to return a reason. |
576 |
|
577 | Parameters |
578 | ---------- |
579 | evm : |
580 | The current EVM frame. |
581 |
|
582 | """ |
583 | # STACK |
584 | memory_start_index = pop(evm.stack) |
585 | size = pop(evm.stack) |
586 | |
587 | # GAS |
588 | extend_memory = calculate_gas_extend_memory( |
589 | evm.memory, [(memory_start_index, size)] |
590 | ) |
591 | |
592 | charge_gas(evm, extend_memory.cost) |
593 | |
594 | # OPERATION |
595 | evm.memory += b"\x00" * extend_memory.expand_by |
596 | output = memory_read_bytes(evm.memory, memory_start_index, size) |
597 | evm.output = Bytes(output) |
598 | raise Revert |