ethereum.prague.vm.instructions.system
Ethereum Virtual Machine (EVM) System Instructions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. contents:: Table of Contents :backlinks: none :local:
Introduction
Implementations of the EVM system related instructions.
generic_create
Core logic used by the CREATE*
family of opcodes.
def generic_create(evm: Evm, endowment: U256, contract_address: Address, memory_start_position: U256, memory_size: U256) -> None:
70 | """ |
---|---|
71 | Core logic used by the `CREATE*` family of opcodes. |
72 | """ |
73 | # This import causes a circular import error |
74 | # if it's not moved inside this method |
75 | from ...vm.interpreter import ( |
76 | MAX_INIT_CODE_SIZE, |
77 | STACK_DEPTH_LIMIT, |
78 | process_create_message, |
79 | ) |
80 | |
81 | call_data = memory_read_bytes( |
82 | evm.memory, memory_start_position, memory_size |
83 | ) |
84 | if len(call_data) > MAX_INIT_CODE_SIZE: |
85 | raise OutOfGasError |
86 | |
87 | create_message_gas = max_message_call_gas(Uint(evm.gas_left)) |
88 | evm.gas_left -= create_message_gas |
89 | if evm.message.is_static: |
90 | raise WriteInStaticContext |
91 | evm.return_data = b"" |
92 | |
93 | sender_address = evm.message.current_target |
94 | sender = get_account(evm.message.block_env.state, sender_address) |
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 | evm.gas_left += create_message_gas |
102 | push(evm.stack, U256(0)) |
103 | return |
104 | |
105 | evm.accessed_addresses.add(contract_address) |
106 | |
107 | if account_has_code_or_nonce( |
108 | evm.message.block_env.state, contract_address |
109 | ) or account_has_storage(evm.message.block_env.state, contract_address): |
110 | increment_nonce( |
111 | evm.message.block_env.state, evm.message.current_target |
112 | ) |
113 | push(evm.stack, U256(0)) |
114 | return |
115 | |
116 | increment_nonce(evm.message.block_env.state, evm.message.current_target) |
117 | |
118 | child_message = Message( |
119 | block_env=evm.message.block_env, |
120 | tx_env=evm.message.tx_env, |
121 | caller=evm.message.current_target, |
122 | target=Bytes0(), |
123 | gas=create_message_gas, |
124 | value=endowment, |
125 | data=b"", |
126 | code=call_data, |
127 | current_target=contract_address, |
128 | depth=evm.message.depth + Uint(1), |
129 | code_address=None, |
130 | should_transfer_value=True, |
131 | is_static=False, |
132 | accessed_addresses=evm.accessed_addresses.copy(), |
133 | accessed_storage_keys=evm.accessed_storage_keys.copy(), |
134 | disable_precompiles=False, |
135 | parent_evm=evm, |
136 | ) |
137 | child_evm = process_create_message(child_message) |
138 | |
139 | if child_evm.error: |
140 | incorporate_child_on_error(evm, child_evm) |
141 | evm.return_data = child_evm.output |
142 | push(evm.stack, U256(0)) |
143 | else: |
144 | incorporate_child_on_success(evm, child_evm) |
145 | evm.return_data = b"" |
146 | push(evm.stack, U256.from_be_bytes(child_evm.message.current_target)) |
create
Creates a new account with associated code.
Parameters
evm : The current EVM frame.
def create(evm: Evm) -> None:
150 | """ |
---|---|
151 | Creates a new account with associated code. |
152 |
|
153 | Parameters |
154 | ---------- |
155 | evm : |
156 | The current EVM frame. |
157 | """ |
158 | # STACK |
159 | endowment = pop(evm.stack) |
160 | memory_start_position = pop(evm.stack) |
161 | memory_size = pop(evm.stack) |
162 | |
163 | # GAS |
164 | extend_memory = calculate_gas_extend_memory( |
165 | evm.memory, [(memory_start_position, memory_size)] |
166 | ) |
167 | init_code_gas = init_code_cost(Uint(memory_size)) |
168 | |
169 | charge_gas(evm, GAS_CREATE + extend_memory.cost + init_code_gas) |
170 | |
171 | # OPERATION |
172 | evm.memory += b"\x00" * extend_memory.expand_by |
173 | contract_address = compute_contract_address( |
174 | evm.message.current_target, |
175 | get_account( |
176 | evm.message.block_env.state, evm.message.current_target |
177 | ).nonce, |
178 | ) |
179 | |
180 | generic_create( |
181 | evm, |
182 | endowment, |
183 | contract_address, |
184 | memory_start_position, |
185 | memory_size, |
186 | ) |
187 | |
188 | # PROGRAM COUNTER |
189 | evm.pc += Uint(1) |
create2
Creates a new account with associated code.
It's similar to CREATE opcode except that the address of new account depends on the init_code instead of the nonce of sender.
Parameters
evm : The current EVM frame.
def create2(evm: Evm) -> None:
193 | """ |
---|---|
194 | Creates a new account with associated code. |
195 |
|
196 | It's similar to CREATE opcode except that the address of new account |
197 | depends on the init_code instead of the nonce of sender. |
198 |
|
199 | Parameters |
200 | ---------- |
201 | evm : |
202 | The current EVM frame. |
203 | """ |
204 | # STACK |
205 | endowment = pop(evm.stack) |
206 | memory_start_position = pop(evm.stack) |
207 | memory_size = pop(evm.stack) |
208 | salt = pop(evm.stack).to_be_bytes32() |
209 | |
210 | # GAS |
211 | extend_memory = calculate_gas_extend_memory( |
212 | evm.memory, [(memory_start_position, memory_size)] |
213 | ) |
214 | call_data_words = ceil32(Uint(memory_size)) // Uint(32) |
215 | init_code_gas = init_code_cost(Uint(memory_size)) |
216 | charge_gas( |
217 | evm, |
218 | GAS_CREATE |
219 | + GAS_KECCAK256_WORD * call_data_words |
220 | + extend_memory.cost |
221 | + init_code_gas, |
222 | ) |
223 | |
224 | # OPERATION |
225 | evm.memory += b"\x00" * extend_memory.expand_by |
226 | contract_address = compute_create2_contract_address( |
227 | evm.message.current_target, |
228 | salt, |
229 | memory_read_bytes(evm.memory, memory_start_position, memory_size), |
230 | ) |
231 | |
232 | generic_create( |
233 | evm, |
234 | endowment, |
235 | contract_address, |
236 | memory_start_position, |
237 | memory_size, |
238 | ) |
239 | |
240 | # PROGRAM COUNTER |
241 | evm.pc += Uint(1) |
return_
Halts execution returning output data.
Parameters
evm : The current EVM frame.
def return_(evm: Evm) -> None:
245 | """ |
---|---|
246 | Halts execution returning output data. |
247 |
|
248 | Parameters |
249 | ---------- |
250 | evm : |
251 | The current EVM frame. |
252 | """ |
253 | # STACK |
254 | memory_start_position = pop(evm.stack) |
255 | memory_size = pop(evm.stack) |
256 | |
257 | # GAS |
258 | extend_memory = calculate_gas_extend_memory( |
259 | evm.memory, [(memory_start_position, memory_size)] |
260 | ) |
261 | |
262 | charge_gas(evm, GAS_ZERO + extend_memory.cost) |
263 | |
264 | # OPERATION |
265 | evm.memory += b"\x00" * extend_memory.expand_by |
266 | evm.output = memory_read_bytes( |
267 | evm.memory, memory_start_position, memory_size |
268 | ) |
269 | |
270 | evm.running = False |
271 | |
272 | # PROGRAM COUNTER |
273 | 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, code: Bytes, disable_precompiles: bool) -> None:
292 | """ |
---|---|
293 | Perform the core logic of the `CALL*` family of opcodes. |
294 | """ |
295 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message |
296 | |
297 | evm.return_data = b"" |
298 | |
299 | if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT: |
300 | evm.gas_left += gas |
301 | push(evm.stack, U256(0)) |
302 | return |
303 | |
304 | call_data = memory_read_bytes( |
305 | evm.memory, memory_input_start_position, memory_input_size |
306 | ) |
307 | |
308 | child_message = Message( |
309 | block_env=evm.message.block_env, |
310 | tx_env=evm.message.tx_env, |
311 | caller=caller, |
312 | target=to, |
313 | gas=gas, |
314 | value=value, |
315 | data=call_data, |
316 | code=code, |
317 | current_target=to, |
318 | depth=evm.message.depth + Uint(1), |
319 | code_address=code_address, |
320 | should_transfer_value=should_transfer_value, |
321 | is_static=True if is_staticcall else evm.message.is_static, |
322 | accessed_addresses=evm.accessed_addresses.copy(), |
323 | accessed_storage_keys=evm.accessed_storage_keys.copy(), |
324 | disable_precompiles=disable_precompiles, |
325 | parent_evm=evm, |
326 | ) |
327 | child_evm = process_message(child_message) |
328 | |
329 | if child_evm.error: |
330 | incorporate_child_on_error(evm, child_evm) |
331 | evm.return_data = child_evm.output |
332 | push(evm.stack, U256(0)) |
333 | else: |
334 | incorporate_child_on_success(evm, child_evm) |
335 | evm.return_data = child_evm.output |
336 | push(evm.stack, U256(1)) |
337 | |
338 | actual_output_size = min(memory_output_size, U256(len(child_evm.output))) |
339 | memory_write( |
340 | evm.memory, |
341 | memory_output_start_position, |
342 | child_evm.output[:actual_output_size], |
343 | ) |
call
Message-call into an account.
Parameters
evm : The current EVM frame.
def call(evm: Evm) -> None:
347 | """ |
---|---|
348 | Message-call into an account. |
349 |
|
350 | Parameters |
351 | ---------- |
352 | evm : |
353 | The current EVM frame. |
354 | """ |
355 | # STACK |
356 | gas = Uint(pop(evm.stack)) |
357 | to = to_address(pop(evm.stack)) |
358 | value = pop(evm.stack) |
359 | memory_input_start_position = pop(evm.stack) |
360 | memory_input_size = pop(evm.stack) |
361 | memory_output_start_position = pop(evm.stack) |
362 | memory_output_size = pop(evm.stack) |
363 | |
364 | # GAS |
365 | extend_memory = calculate_gas_extend_memory( |
366 | evm.memory, |
367 | [ |
368 | (memory_input_start_position, memory_input_size), |
369 | (memory_output_start_position, memory_output_size), |
370 | ], |
371 | ) |
372 | |
373 | if to in evm.accessed_addresses: |
374 | access_gas_cost = GAS_WARM_ACCESS |
375 | else: |
376 | evm.accessed_addresses.add(to) |
377 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
378 | |
379 | code_address = to |
380 | ( |
381 | disable_precompiles, |
382 | code_address, |
383 | code, |
384 | delegated_access_gas_cost, |
385 | ) = access_delegation(evm, code_address) |
386 | access_gas_cost += delegated_access_gas_cost |
387 | |
388 | create_gas_cost = ( |
389 | Uint(0) |
390 | if is_account_alive(evm.message.block_env.state, to) or value == 0 |
391 | else GAS_NEW_ACCOUNT |
392 | ) |
393 | transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE |
394 | message_call_gas = calculate_message_call_gas( |
395 | value, |
396 | gas, |
397 | Uint(evm.gas_left), |
398 | extend_memory.cost, |
399 | access_gas_cost + create_gas_cost + transfer_gas_cost, |
400 | ) |
401 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
402 | if evm.message.is_static and value != U256(0): |
403 | raise WriteInStaticContext |
404 | evm.memory += b"\x00" * extend_memory.expand_by |
405 | sender_balance = get_account( |
406 | evm.message.block_env.state, evm.message.current_target |
407 | ).balance |
408 | if sender_balance < value: |
409 | push(evm.stack, U256(0)) |
410 | evm.return_data = b"" |
411 | evm.gas_left += message_call_gas.sub_call |
412 | else: |
413 | generic_call( |
414 | evm, |
415 | message_call_gas.sub_call, |
416 | value, |
417 | evm.message.current_target, |
418 | to, |
419 | code_address, |
420 | True, |
421 | False, |
422 | memory_input_start_position, |
423 | memory_input_size, |
424 | memory_output_start_position, |
425 | memory_output_size, |
426 | code, |
427 | disable_precompiles, |
428 | ) |
429 | |
430 | # PROGRAM COUNTER |
431 | 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:
435 | """ |
---|---|
436 | Message-call into this account with alternative account’s code. |
437 |
|
438 | Parameters |
439 | ---------- |
440 | evm : |
441 | The current EVM frame. |
442 | """ |
443 | # STACK |
444 | gas = Uint(pop(evm.stack)) |
445 | code_address = to_address(pop(evm.stack)) |
446 | value = pop(evm.stack) |
447 | memory_input_start_position = pop(evm.stack) |
448 | memory_input_size = pop(evm.stack) |
449 | memory_output_start_position = pop(evm.stack) |
450 | memory_output_size = pop(evm.stack) |
451 | |
452 | # GAS |
453 | to = evm.message.current_target |
454 | |
455 | extend_memory = calculate_gas_extend_memory( |
456 | evm.memory, |
457 | [ |
458 | (memory_input_start_position, memory_input_size), |
459 | (memory_output_start_position, memory_output_size), |
460 | ], |
461 | ) |
462 | |
463 | if code_address in evm.accessed_addresses: |
464 | access_gas_cost = GAS_WARM_ACCESS |
465 | else: |
466 | evm.accessed_addresses.add(code_address) |
467 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
468 | |
469 | ( |
470 | disable_precompiles, |
471 | code_address, |
472 | code, |
473 | delegated_access_gas_cost, |
474 | ) = access_delegation(evm, code_address) |
475 | access_gas_cost += delegated_access_gas_cost |
476 | |
477 | transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE |
478 | message_call_gas = calculate_message_call_gas( |
479 | value, |
480 | gas, |
481 | Uint(evm.gas_left), |
482 | extend_memory.cost, |
483 | access_gas_cost + transfer_gas_cost, |
484 | ) |
485 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
486 | |
487 | # OPERATION |
488 | evm.memory += b"\x00" * extend_memory.expand_by |
489 | sender_balance = get_account( |
490 | evm.message.block_env.state, evm.message.current_target |
491 | ).balance |
492 | if sender_balance < value: |
493 | push(evm.stack, U256(0)) |
494 | evm.return_data = b"" |
495 | evm.gas_left += message_call_gas.sub_call |
496 | else: |
497 | generic_call( |
498 | evm, |
499 | message_call_gas.sub_call, |
500 | value, |
501 | evm.message.current_target, |
502 | to, |
503 | code_address, |
504 | True, |
505 | False, |
506 | memory_input_start_position, |
507 | memory_input_size, |
508 | memory_output_start_position, |
509 | memory_output_size, |
510 | code, |
511 | disable_precompiles, |
512 | ) |
513 | |
514 | # PROGRAM COUNTER |
515 | evm.pc += Uint(1) |
selfdestruct
Halt execution and register account for later deletion.
Parameters
evm : The current EVM frame.
def selfdestruct(evm: Evm) -> None:
519 | """ |
---|---|
520 | Halt execution and register account for later deletion. |
521 |
|
522 | Parameters |
523 | ---------- |
524 | evm : |
525 | The current EVM frame. |
526 | """ |
527 | # STACK |
528 | beneficiary = to_address(pop(evm.stack)) |
529 | |
530 | # GAS |
531 | gas_cost = GAS_SELF_DESTRUCT |
532 | if beneficiary not in evm.accessed_addresses: |
533 | evm.accessed_addresses.add(beneficiary) |
534 | gas_cost += GAS_COLD_ACCOUNT_ACCESS |
535 | |
536 | if ( |
537 | not is_account_alive(evm.message.block_env.state, beneficiary) |
538 | and get_account( |
539 | evm.message.block_env.state, evm.message.current_target |
540 | ).balance |
541 | != 0 |
542 | ): |
543 | gas_cost += GAS_SELF_DESTRUCT_NEW_ACCOUNT |
544 | |
545 | charge_gas(evm, gas_cost) |
546 | if evm.message.is_static: |
547 | raise WriteInStaticContext |
548 | |
549 | originator = evm.message.current_target |
550 | originator_balance = get_account( |
551 | evm.message.block_env.state, originator |
552 | ).balance |
553 | |
554 | move_ether( |
555 | evm.message.block_env.state, |
556 | originator, |
557 | beneficiary, |
558 | originator_balance, |
559 | ) |
560 | |
561 | # register account for deletion only if it was created |
562 | # in the same transaction |
563 | if originator in evm.message.block_env.state.created_accounts: |
564 | # If beneficiary is the same as originator, then |
565 | # the ether is burnt. |
566 | set_account_balance(evm.message.block_env.state, originator, U256(0)) |
567 | evm.accounts_to_delete.add(originator) |
568 | |
569 | # HALT the execution |
570 | evm.running = False |
571 | |
572 | # PROGRAM COUNTER |
573 | pass |
delegatecall
Message-call into an account.
Parameters
evm : The current EVM frame.
def delegatecall(evm: Evm) -> None:
577 | """ |
---|---|
578 | Message-call into an account. |
579 |
|
580 | Parameters |
581 | ---------- |
582 | evm : |
583 | The current EVM frame. |
584 | """ |
585 | # STACK |
586 | gas = Uint(pop(evm.stack)) |
587 | code_address = to_address(pop(evm.stack)) |
588 | memory_input_start_position = pop(evm.stack) |
589 | memory_input_size = pop(evm.stack) |
590 | memory_output_start_position = pop(evm.stack) |
591 | memory_output_size = pop(evm.stack) |
592 | |
593 | # GAS |
594 | extend_memory = calculate_gas_extend_memory( |
595 | evm.memory, |
596 | [ |
597 | (memory_input_start_position, memory_input_size), |
598 | (memory_output_start_position, memory_output_size), |
599 | ], |
600 | ) |
601 | |
602 | if code_address in evm.accessed_addresses: |
603 | access_gas_cost = GAS_WARM_ACCESS |
604 | else: |
605 | evm.accessed_addresses.add(code_address) |
606 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
607 | |
608 | ( |
609 | disable_precompiles, |
610 | code_address, |
611 | code, |
612 | delegated_access_gas_cost, |
613 | ) = access_delegation(evm, code_address) |
614 | access_gas_cost += delegated_access_gas_cost |
615 | |
616 | message_call_gas = calculate_message_call_gas( |
617 | U256(0), gas, Uint(evm.gas_left), extend_memory.cost, access_gas_cost |
618 | ) |
619 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
620 | |
621 | # OPERATION |
622 | evm.memory += b"\x00" * extend_memory.expand_by |
623 | generic_call( |
624 | evm, |
625 | message_call_gas.sub_call, |
626 | evm.message.value, |
627 | evm.message.caller, |
628 | evm.message.current_target, |
629 | code_address, |
630 | False, |
631 | False, |
632 | memory_input_start_position, |
633 | memory_input_size, |
634 | memory_output_start_position, |
635 | memory_output_size, |
636 | code, |
637 | disable_precompiles, |
638 | ) |
639 | |
640 | # PROGRAM COUNTER |
641 | evm.pc += Uint(1) |
staticcall
Message-call into an account.
Parameters
evm : The current EVM frame.
def staticcall(evm: Evm) -> None:
645 | """ |
---|---|
646 | Message-call into an account. |
647 |
|
648 | Parameters |
649 | ---------- |
650 | evm : |
651 | The current EVM frame. |
652 | """ |
653 | # STACK |
654 | gas = Uint(pop(evm.stack)) |
655 | to = to_address(pop(evm.stack)) |
656 | memory_input_start_position = pop(evm.stack) |
657 | memory_input_size = pop(evm.stack) |
658 | memory_output_start_position = pop(evm.stack) |
659 | memory_output_size = pop(evm.stack) |
660 | |
661 | # GAS |
662 | extend_memory = calculate_gas_extend_memory( |
663 | evm.memory, |
664 | [ |
665 | (memory_input_start_position, memory_input_size), |
666 | (memory_output_start_position, memory_output_size), |
667 | ], |
668 | ) |
669 | |
670 | if to in evm.accessed_addresses: |
671 | access_gas_cost = GAS_WARM_ACCESS |
672 | else: |
673 | evm.accessed_addresses.add(to) |
674 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
675 | |
676 | code_address = to |
677 | ( |
678 | disable_precompiles, |
679 | code_address, |
680 | code, |
681 | delegated_access_gas_cost, |
682 | ) = access_delegation(evm, code_address) |
683 | access_gas_cost += delegated_access_gas_cost |
684 | |
685 | message_call_gas = calculate_message_call_gas( |
686 | U256(0), |
687 | gas, |
688 | Uint(evm.gas_left), |
689 | extend_memory.cost, |
690 | access_gas_cost, |
691 | ) |
692 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
693 | |
694 | # OPERATION |
695 | evm.memory += b"\x00" * extend_memory.expand_by |
696 | generic_call( |
697 | evm, |
698 | message_call_gas.sub_call, |
699 | U256(0), |
700 | evm.message.current_target, |
701 | to, |
702 | code_address, |
703 | True, |
704 | True, |
705 | memory_input_start_position, |
706 | memory_input_size, |
707 | memory_output_start_position, |
708 | memory_output_size, |
709 | code, |
710 | disable_precompiles, |
711 | ) |
712 | |
713 | # PROGRAM COUNTER |
714 | 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:
718 | """ |
---|---|
719 | Stop execution and revert state changes, without consuming all provided gas |
720 | and also has the ability to return a reason |
721 | Parameters |
722 | ---------- |
723 | evm : |
724 | The current EVM frame. |
725 | """ |
726 | # STACK |
727 | memory_start_index = pop(evm.stack) |
728 | size = pop(evm.stack) |
729 | |
730 | # GAS |
731 | extend_memory = calculate_gas_extend_memory( |
732 | evm.memory, [(memory_start_index, size)] |
733 | ) |
734 | |
735 | charge_gas(evm, extend_memory.cost) |
736 | |
737 | # OPERATION |
738 | evm.memory += b"\x00" * extend_memory.expand_by |
739 | output = memory_read_bytes(evm.memory, memory_start_index, size) |
740 | evm.output = Bytes(output) |
741 | raise Revert |