ethereum.shanghai.vm.instructions.systemethereum.cancun.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_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) > 2 * MAX_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 | 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(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:
148 | """ |
---|---|
149 | Creates a new account with associated code. |
150 |
|
151 | Parameters |
152 | ---------- |
153 | evm : |
154 | The current EVM frame. |
155 | """ |
156 | # STACK |
157 | endowment = pop(evm.stack) |
158 | memory_start_position = pop(evm.stack) |
159 | memory_size = pop(evm.stack) |
160 | |
161 | # GAS |
162 | extend_memory = calculate_gas_extend_memory( |
163 | evm.memory, [(memory_start_position, memory_size)] |
164 | ) |
165 | init_code_gas = init_code_cost(Uint(memory_size)) |
166 | |
167 | charge_gas(evm, GAS_CREATE + extend_memory.cost + init_code_gas) |
168 | |
169 | # OPERATION |
170 | evm.memory += b"\x00" * extend_memory.expand_by |
171 | contract_address = compute_contract_address( |
172 | evm.message.current_target, |
173 | get_account( |
174 | evm.message.block_env.state, evm.message.current_target |
175 | ).nonce, |
176 | ) |
177 | |
178 | generic_create( |
179 | evm, |
180 | endowment, |
181 | contract_address, |
182 | memory_start_position, |
183 | memory_size, |
184 | ) |
185 | |
186 | # PROGRAM COUNTER |
187 | 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:
191 | """ |
---|---|
192 | Creates a new account with associated code. |
193 |
|
194 | It's similar to CREATE opcode except that the address of new account |
195 | depends on the init_code instead of the nonce of sender. |
196 |
|
197 | Parameters |
198 | ---------- |
199 | evm : |
200 | The current EVM frame. |
201 | """ |
202 | # STACK |
203 | endowment = pop(evm.stack) |
204 | memory_start_position = pop(evm.stack) |
205 | memory_size = pop(evm.stack) |
206 | salt = pop(evm.stack).to_be_bytes32() |
207 | |
208 | # GAS |
209 | extend_memory = calculate_gas_extend_memory( |
210 | evm.memory, [(memory_start_position, memory_size)] |
211 | ) |
212 | call_data_words = ceil32(Uint(memory_size)) // Uint(32) |
213 | init_code_gas = init_code_cost(Uint(memory_size)) |
214 | charge_gas( |
215 | evm, |
216 | GAS_CREATE |
217 | + GAS_KECCAK256_WORD * call_data_words |
218 | + extend_memory.cost |
219 | + init_code_gas, |
220 | ) |
221 | |
222 | # OPERATION |
223 | evm.memory += b"\x00" * extend_memory.expand_by |
224 | contract_address = compute_create2_contract_address( |
225 | evm.message.current_target, |
226 | salt, |
227 | memory_read_bytes(evm.memory, memory_start_position, memory_size), |
228 | ) |
229 | |
230 | generic_create( |
231 | evm, |
232 | endowment, |
233 | contract_address, |
234 | memory_start_position, |
235 | memory_size, |
236 | ) |
237 | |
238 | # PROGRAM COUNTER |
239 | evm.pc += Uint(1) |
return_
Halts execution returning output data.
Parameters
evm : The current EVM frame.
def return_(evm: Evm) -> None:
243 | """ |
---|---|
244 | Halts execution returning output data. |
245 |
|
246 | Parameters |
247 | ---------- |
248 | evm : |
249 | The current EVM frame. |
250 | """ |
251 | # STACK |
252 | memory_start_position = pop(evm.stack) |
253 | memory_size = pop(evm.stack) |
254 | |
255 | # GAS |
256 | extend_memory = calculate_gas_extend_memory( |
257 | evm.memory, [(memory_start_position, memory_size)] |
258 | ) |
259 | |
260 | charge_gas(evm, GAS_ZERO + extend_memory.cost) |
261 | |
262 | # OPERATION |
263 | evm.memory += b"\x00" * extend_memory.expand_by |
264 | evm.output = memory_read_bytes( |
265 | evm.memory, memory_start_position, memory_size |
266 | ) |
267 | |
268 | evm.running = False |
269 | |
270 | # PROGRAM COUNTER |
271 | 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:
288 | """ |
---|---|
289 | Perform the core logic of the `CALL*` family of opcodes. |
290 | """ |
291 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message |
292 | |
293 | evm.return_data = b"" |
294 | |
295 | if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT: |
296 | evm.gas_left += gas |
297 | push(evm.stack, U256(0)) |
298 | return |
299 | |
300 | call_data = memory_read_bytes( |
301 | evm.memory, memory_input_start_position, memory_input_size |
302 | ) |
303 | code = get_account(evm.message.block_env.state, code_address).code |
304 | child_message = Message( |
305 | block_env=evm.message.block_env, |
306 | tx_env=evm.message.tx_env, |
307 | caller=caller, |
308 | target=to, |
309 | gas=gas, |
310 | value=value, |
311 | data=call_data, |
312 | code=code, |
313 | current_target=to, |
314 | depth=evm.message.depth + Uint(1), |
315 | code_address=code_address, |
316 | should_transfer_value=should_transfer_value, |
317 | is_static=True if is_staticcall else evm.message.is_static, |
318 | accessed_addresses=evm.accessed_addresses.copy(), |
319 | accessed_storage_keys=evm.accessed_storage_keys.copy(), |
320 | parent_evm=evm, |
321 | ) |
322 | child_evm = process_message(child_message) |
323 | |
324 | if child_evm.error: |
325 | incorporate_child_on_error(evm, child_evm) |
326 | evm.return_data = child_evm.output |
327 | push(evm.stack, U256(0)) |
328 | else: |
329 | incorporate_child_on_success(evm, child_evm) |
330 | evm.return_data = child_evm.output |
331 | push(evm.stack, U256(1)) |
332 | |
333 | actual_output_size = min(memory_output_size, U256(len(child_evm.output))) |
334 | memory_write( |
335 | evm.memory, |
336 | memory_output_start_position, |
337 | child_evm.output[:actual_output_size], |
338 | ) |
call
Message-call into an account.
Parameters
evm : The current EVM frame.
def call(evm: Evm) -> None:
342 | """ |
---|---|
343 | Message-call into an account. |
344 |
|
345 | Parameters |
346 | ---------- |
347 | evm : |
348 | The current EVM frame. |
349 | """ |
350 | # STACK |
351 | gas = Uint(pop(evm.stack)) |
352 | to = to_address(pop(evm.stack)) |
353 | value = pop(evm.stack) |
354 | memory_input_start_position = pop(evm.stack) |
355 | memory_input_size = pop(evm.stack) |
356 | memory_output_start_position = pop(evm.stack) |
357 | memory_output_size = pop(evm.stack) |
358 | |
359 | # GAS |
360 | extend_memory = calculate_gas_extend_memory( |
361 | evm.memory, |
362 | [ |
363 | (memory_input_start_position, memory_input_size), |
364 | (memory_output_start_position, memory_output_size), |
365 | ], |
366 | ) |
367 | |
368 | if to in evm.accessed_addresses: |
369 | access_gas_cost = GAS_WARM_ACCESS |
370 | else: |
371 | evm.accessed_addresses.add(to) |
372 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
373 | |
374 | code_address = to |
375 | |
376 | create_gas_cost = ( |
377 | Uint(0) |
378 | if is_account_alive(evm.message.block_env.state, to) or value == 0 |
379 | else GAS_NEW_ACCOUNT |
380 | ) |
381 | transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE |
382 | message_call_gas = calculate_message_call_gas( |
383 | value, |
384 | gas, |
385 | Uint(evm.gas_left), |
386 | extend_memory.cost, |
387 | access_gas_cost + create_gas_cost + transfer_gas_cost, |
388 | ) |
389 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
390 | if evm.message.is_static and value != U256(0): |
391 | raise WriteInStaticContext |
392 | evm.memory += b"\x00" * extend_memory.expand_by |
393 | sender_balance = get_account( |
394 | evm.message.block_env.state, evm.message.current_target |
395 | ).balance |
396 | if sender_balance < value: |
397 | push(evm.stack, U256(0)) |
398 | evm.return_data = b"" |
399 | evm.gas_left += message_call_gas.sub_call |
400 | else: |
401 | generic_call( |
402 | evm, |
403 | message_call_gas.sub_call, |
404 | value, |
405 | evm.message.current_target, |
406 | to, |
407 | code_address, |
408 | True, |
409 | False, |
410 | memory_input_start_position, |
411 | memory_input_size, |
412 | memory_output_start_position, |
413 | memory_output_size, |
414 | ) |
415 | |
416 | # PROGRAM COUNTER |
417 | 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:
421 | """ |
---|---|
422 | Message-call into this account with alternative account’s code. |
423 |
|
424 | Parameters |
425 | ---------- |
426 | evm : |
427 | The current EVM frame. |
428 | """ |
429 | # STACK |
430 | gas = Uint(pop(evm.stack)) |
431 | code_address = to_address(pop(evm.stack)) |
432 | value = pop(evm.stack) |
433 | memory_input_start_position = pop(evm.stack) |
434 | memory_input_size = pop(evm.stack) |
435 | memory_output_start_position = pop(evm.stack) |
436 | memory_output_size = pop(evm.stack) |
437 | |
438 | # GAS |
439 | to = evm.message.current_target |
440 | |
441 | extend_memory = calculate_gas_extend_memory( |
442 | evm.memory, |
443 | [ |
444 | (memory_input_start_position, memory_input_size), |
445 | (memory_output_start_position, memory_output_size), |
446 | ], |
447 | ) |
448 | |
449 | if code_address in evm.accessed_addresses: |
450 | access_gas_cost = GAS_WARM_ACCESS |
451 | else: |
452 | evm.accessed_addresses.add(code_address) |
453 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
454 | |
455 | transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE |
456 | message_call_gas = calculate_message_call_gas( |
457 | value, |
458 | gas, |
459 | Uint(evm.gas_left), |
460 | extend_memory.cost, |
461 | access_gas_cost + transfer_gas_cost, |
462 | ) |
463 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
464 | |
465 | # OPERATION |
466 | evm.memory += b"\x00" * extend_memory.expand_by |
467 | sender_balance = get_account( |
468 | evm.message.block_env.state, evm.message.current_target |
469 | ).balance |
470 | if sender_balance < value: |
471 | push(evm.stack, U256(0)) |
472 | evm.return_data = b"" |
473 | evm.gas_left += message_call_gas.sub_call |
474 | else: |
475 | generic_call( |
476 | evm, |
477 | message_call_gas.sub_call, |
478 | value, |
479 | evm.message.current_target, |
480 | to, |
481 | code_address, |
482 | True, |
483 | False, |
484 | memory_input_start_position, |
485 | memory_input_size, |
486 | memory_output_start_position, |
487 | memory_output_size, |
488 | ) |
489 | |
490 | # PROGRAM COUNTER |
491 | evm.pc += Uint(1) |
selfdestruct
Halt execution and register account for later deletion.
Parameters
evm : The current EVM frame.
def selfdestruct(evm: Evm) -> None:
495 | """ |
---|---|
496 | Halt execution and register account for later deletion. |
497 |
|
498 | Parameters |
499 | ---------- |
500 | evm : |
501 | The current EVM frame. |
502 | """ |
503 | # STACK |
504 | beneficiary = to_address(pop(evm.stack)) |
505 | |
506 | # GAS |
507 | gas_cost = GAS_SELF_DESTRUCT |
508 | if beneficiary not in evm.accessed_addresses: |
509 | evm.accessed_addresses.add(beneficiary) |
510 | gas_cost += GAS_COLD_ACCOUNT_ACCESS |
511 | |
512 | if ( |
513 | not is_account_alive(evm.message.block_env.state, beneficiary) |
514 | and get_account( |
515 | evm.message.block_env.state, evm.message.current_target |
516 | ).balance |
517 | != 0 |
518 | ): |
519 | gas_cost += GAS_SELF_DESTRUCT_NEW_ACCOUNT |
520 | |
521 | charge_gas(evm, gas_cost) |
522 | if evm.message.is_static: |
523 | raise WriteInStaticContext |
524 | |
525 | originator = evm.message.current_target |
525 | beneficiary_balance = get_account( |
526 | evm.message.block_env.state, beneficiary |
527 | ).balance |
526 | originator_balance = get_account( |
527 | evm.message.block_env.state, originator |
528 | ).balance |
529 | |
532 | # First Transfer to beneficiary |
533 | set_account_balance( |
530 | move_ether( |
531 | evm.message.block_env.state, |
532 | originator, |
533 | beneficiary, |
536 | beneficiary_balance + originator_balance, |
534 | originator_balance, |
535 | ) |
538 | # Next, Zero the balance of the address being deleted (must come after |
539 | # sending to beneficiary in case the contract named itself as the |
540 | # beneficiary). |
541 | set_account_balance(evm.message.block_env.state, originator, U256(0)) |
536 | |
543 | # register account for deletion |
544 | evm.accounts_to_delete.add(originator) |
537 | # register account for deletion only if it was created |
538 | # in the same transaction |
539 | if originator in evm.message.block_env.state.created_accounts: |
540 | # If beneficiary is the same as originator, then |
541 | # the ether is burnt. |
542 | set_account_balance(evm.message.block_env.state, originator, U256(0)) |
543 | evm.accounts_to_delete.add(originator) |
544 | |
545 | # HALT the execution |
546 | evm.running = False |
547 | |
548 | # PROGRAM COUNTER |
549 | pass |
delegatecall
Message-call into an account.
Parameters
evm : The current EVM frame.
def delegatecall(evm: Evm) -> None:
553 | """ |
---|---|
554 | Message-call into an account. |
555 |
|
556 | Parameters |
557 | ---------- |
558 | evm : |
559 | The current EVM frame. |
560 | """ |
561 | # STACK |
562 | gas = Uint(pop(evm.stack)) |
563 | code_address = to_address(pop(evm.stack)) |
564 | memory_input_start_position = pop(evm.stack) |
565 | memory_input_size = pop(evm.stack) |
566 | memory_output_start_position = pop(evm.stack) |
567 | memory_output_size = pop(evm.stack) |
568 | |
569 | # GAS |
570 | extend_memory = calculate_gas_extend_memory( |
571 | evm.memory, |
572 | [ |
573 | (memory_input_start_position, memory_input_size), |
574 | (memory_output_start_position, memory_output_size), |
575 | ], |
576 | ) |
577 | |
578 | if code_address in evm.accessed_addresses: |
579 | access_gas_cost = GAS_WARM_ACCESS |
580 | else: |
581 | evm.accessed_addresses.add(code_address) |
582 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
583 | |
584 | message_call_gas = calculate_message_call_gas( |
585 | U256(0), gas, Uint(evm.gas_left), extend_memory.cost, access_gas_cost |
586 | ) |
587 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
588 | |
589 | # OPERATION |
590 | evm.memory += b"\x00" * extend_memory.expand_by |
591 | generic_call( |
592 | evm, |
593 | message_call_gas.sub_call, |
594 | evm.message.value, |
595 | evm.message.caller, |
596 | evm.message.current_target, |
597 | code_address, |
598 | False, |
599 | False, |
600 | memory_input_start_position, |
601 | memory_input_size, |
602 | memory_output_start_position, |
603 | memory_output_size, |
604 | ) |
605 | |
606 | # PROGRAM COUNTER |
607 | evm.pc += Uint(1) |
staticcall
Message-call into an account.
Parameters
evm : The current EVM frame.
def staticcall(evm: Evm) -> None:
611 | """ |
---|---|
612 | Message-call into an account. |
613 |
|
614 | Parameters |
615 | ---------- |
616 | evm : |
617 | The current EVM frame. |
618 | """ |
619 | # STACK |
620 | gas = Uint(pop(evm.stack)) |
621 | to = to_address(pop(evm.stack)) |
622 | memory_input_start_position = pop(evm.stack) |
623 | memory_input_size = pop(evm.stack) |
624 | memory_output_start_position = pop(evm.stack) |
625 | memory_output_size = pop(evm.stack) |
626 | |
627 | # GAS |
628 | extend_memory = calculate_gas_extend_memory( |
629 | evm.memory, |
630 | [ |
631 | (memory_input_start_position, memory_input_size), |
632 | (memory_output_start_position, memory_output_size), |
633 | ], |
634 | ) |
635 | |
636 | if to in evm.accessed_addresses: |
637 | access_gas_cost = GAS_WARM_ACCESS |
638 | else: |
639 | evm.accessed_addresses.add(to) |
640 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
641 | |
642 | code_address = to |
643 | |
644 | message_call_gas = calculate_message_call_gas( |
645 | U256(0), |
646 | gas, |
647 | Uint(evm.gas_left), |
648 | extend_memory.cost, |
649 | access_gas_cost, |
650 | ) |
651 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
652 | |
653 | # OPERATION |
654 | evm.memory += b"\x00" * extend_memory.expand_by |
655 | generic_call( |
656 | evm, |
657 | message_call_gas.sub_call, |
658 | U256(0), |
659 | evm.message.current_target, |
660 | to, |
661 | code_address, |
662 | True, |
663 | True, |
664 | memory_input_start_position, |
665 | memory_input_size, |
666 | memory_output_start_position, |
667 | memory_output_size, |
668 | ) |
669 | |
670 | # PROGRAM COUNTER |
671 | 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:
675 | """ |
---|---|
676 | Stop execution and revert state changes, without consuming all provided gas |
677 | and also has the ability to return a reason |
678 | Parameters |
679 | ---------- |
680 | evm : |
681 | The current EVM frame. |
682 | """ |
683 | # STACK |
684 | memory_start_index = pop(evm.stack) |
685 | size = pop(evm.stack) |
686 | |
687 | # GAS |
688 | extend_memory = calculate_gas_extend_memory( |
689 | evm.memory, [(memory_start_index, size)] |
690 | ) |
691 | |
692 | charge_gas(evm, extend_memory.cost) |
693 | |
694 | # OPERATION |
695 | evm.memory += b"\x00" * extend_memory.expand_by |
696 | output = memory_read_bytes(evm.memory, memory_start_index, size) |
697 | evm.output = Bytes(output) |
698 | raise Revert |