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