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