ethereum.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 |
73 | from ...vm.interpreter import ( |
74 | MAX_INIT_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) > MAX_INIT_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 | |
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( |
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, |
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( |
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_masked(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 = GAS_NEW_ACCOUNT |
376 | if value == 0 or is_account_alive(evm.message.block_env.state, to): |
377 | create_gas_cost = Uint(0) |
378 | transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE |
379 | message_call_gas = calculate_message_call_gas( |
380 | value, |
381 | gas, |
382 | Uint(evm.gas_left), |
383 | extend_memory.cost, |
384 | access_gas_cost + create_gas_cost + transfer_gas_cost, |
385 | ) |
386 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
387 | if evm.message.is_static and value != U256(0): |
388 | raise WriteInStaticContext |
389 | evm.memory += b"\x00" * extend_memory.expand_by |
390 | sender_balance = get_account( |
391 | evm.message.block_env.state, evm.message.current_target |
392 | ).balance |
393 | if sender_balance < value: |
394 | push(evm.stack, U256(0)) |
395 | evm.return_data = b"" |
396 | evm.gas_left += message_call_gas.sub_call |
397 | else: |
398 | generic_call( |
399 | evm, |
400 | message_call_gas.sub_call, |
401 | value, |
402 | evm.message.current_target, |
403 | to, |
404 | code_address, |
405 | True, |
406 | False, |
407 | memory_input_start_position, |
408 | memory_input_size, |
409 | memory_output_start_position, |
410 | memory_output_size, |
411 | ) |
412 | |
413 | # PROGRAM COUNTER |
414 | 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:
418 | """ |
---|---|
419 | Message-call into this account with alternative account’s code. |
420 |
|
421 | Parameters |
422 | ---------- |
423 | evm : |
424 | The current EVM frame. |
425 | """ |
426 | # STACK |
427 | gas = Uint(pop(evm.stack)) |
428 | code_address = to_address_masked(pop(evm.stack)) |
429 | value = pop(evm.stack) |
430 | memory_input_start_position = pop(evm.stack) |
431 | memory_input_size = pop(evm.stack) |
432 | memory_output_start_position = pop(evm.stack) |
433 | memory_output_size = pop(evm.stack) |
434 | |
435 | # GAS |
436 | to = evm.message.current_target |
437 | |
438 | extend_memory = calculate_gas_extend_memory( |
439 | evm.memory, |
440 | [ |
441 | (memory_input_start_position, memory_input_size), |
442 | (memory_output_start_position, memory_output_size), |
443 | ], |
444 | ) |
445 | |
446 | if code_address in evm.accessed_addresses: |
447 | access_gas_cost = GAS_WARM_ACCESS |
448 | else: |
449 | evm.accessed_addresses.add(code_address) |
450 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
451 | |
452 | transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE |
453 | message_call_gas = calculate_message_call_gas( |
454 | value, |
455 | gas, |
456 | Uint(evm.gas_left), |
457 | extend_memory.cost, |
458 | access_gas_cost + transfer_gas_cost, |
459 | ) |
460 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
461 | |
462 | # OPERATION |
463 | evm.memory += b"\x00" * extend_memory.expand_by |
464 | sender_balance = get_account( |
465 | evm.message.block_env.state, evm.message.current_target |
466 | ).balance |
467 | if sender_balance < value: |
468 | push(evm.stack, U256(0)) |
469 | evm.return_data = b"" |
470 | evm.gas_left += message_call_gas.sub_call |
471 | else: |
472 | generic_call( |
473 | evm, |
474 | message_call_gas.sub_call, |
475 | value, |
476 | evm.message.current_target, |
477 | to, |
478 | code_address, |
479 | True, |
480 | False, |
481 | memory_input_start_position, |
482 | memory_input_size, |
483 | memory_output_start_position, |
484 | memory_output_size, |
485 | ) |
486 | |
487 | # PROGRAM COUNTER |
488 | evm.pc += Uint(1) |
selfdestruct
Halt execution and register account for later deletion.
Parameters
evm : The current EVM frame.
def selfdestruct(evm: Evm) -> None:
492 | """ |
---|---|
493 | Halt execution and register account for later deletion. |
494 |
|
495 | Parameters |
496 | ---------- |
497 | evm : |
498 | The current EVM frame. |
499 | """ |
500 | # STACK |
501 | beneficiary = to_address_masked(pop(evm.stack)) |
502 | |
503 | # GAS |
504 | gas_cost = GAS_SELF_DESTRUCT |
505 | if beneficiary not in evm.accessed_addresses: |
506 | evm.accessed_addresses.add(beneficiary) |
507 | gas_cost += GAS_COLD_ACCOUNT_ACCESS |
508 | |
509 | if ( |
510 | not is_account_alive(evm.message.block_env.state, beneficiary) |
511 | and get_account( |
512 | evm.message.block_env.state, evm.message.current_target |
513 | ).balance |
514 | != 0 |
515 | ): |
516 | gas_cost += GAS_SELF_DESTRUCT_NEW_ACCOUNT |
517 | |
518 | charge_gas(evm, gas_cost) |
519 | if evm.message.is_static: |
520 | raise WriteInStaticContext |
521 | |
522 | originator = evm.message.current_target |
523 | beneficiary_balance = get_account( |
524 | evm.message.block_env.state, beneficiary |
525 | ).balance |
526 | originator_balance = get_account( |
527 | evm.message.block_env.state, originator |
528 | ).balance |
529 | |
530 | # First Transfer to beneficiary |
531 | set_account_balance( |
532 | evm.message.block_env.state, |
533 | beneficiary, |
534 | beneficiary_balance + originator_balance, |
535 | ) |
536 | # Next, Zero the balance of the address being deleted (must come after |
537 | # sending to beneficiary in case the contract named itself as the |
538 | # beneficiary). |
539 | set_account_balance(evm.message.block_env.state, originator, U256(0)) |
540 | |
541 | # register account for deletion |
542 | evm.accounts_to_delete.add(originator) |
543 | |
544 | # HALT the execution |
545 | evm.running = False |
546 | |
547 | # PROGRAM COUNTER |
548 | pass |
delegatecall
Message-call into an account.
Parameters
evm : The current EVM frame.
def delegatecall(evm: Evm) -> None:
552 | """ |
---|---|
553 | Message-call into an account. |
554 |
|
555 | Parameters |
556 | ---------- |
557 | evm : |
558 | The current EVM frame. |
559 | """ |
560 | # STACK |
561 | gas = Uint(pop(evm.stack)) |
562 | code_address = to_address_masked(pop(evm.stack)) |
563 | memory_input_start_position = pop(evm.stack) |
564 | memory_input_size = pop(evm.stack) |
565 | memory_output_start_position = pop(evm.stack) |
566 | memory_output_size = pop(evm.stack) |
567 | |
568 | # GAS |
569 | extend_memory = calculate_gas_extend_memory( |
570 | evm.memory, |
571 | [ |
572 | (memory_input_start_position, memory_input_size), |
573 | (memory_output_start_position, memory_output_size), |
574 | ], |
575 | ) |
576 | |
577 | if code_address in evm.accessed_addresses: |
578 | access_gas_cost = GAS_WARM_ACCESS |
579 | else: |
580 | evm.accessed_addresses.add(code_address) |
581 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
582 | |
583 | message_call_gas = calculate_message_call_gas( |
584 | U256(0), gas, Uint(evm.gas_left), extend_memory.cost, access_gas_cost |
585 | ) |
586 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
587 | |
588 | # OPERATION |
589 | evm.memory += b"\x00" * extend_memory.expand_by |
590 | generic_call( |
591 | evm, |
592 | message_call_gas.sub_call, |
593 | evm.message.value, |
594 | evm.message.caller, |
595 | evm.message.current_target, |
596 | code_address, |
597 | False, |
598 | False, |
599 | memory_input_start_position, |
600 | memory_input_size, |
601 | memory_output_start_position, |
602 | memory_output_size, |
603 | ) |
604 | |
605 | # PROGRAM COUNTER |
606 | evm.pc += Uint(1) |
staticcall
Message-call into an account.
Parameters
evm : The current EVM frame.
def staticcall(evm: Evm) -> None:
610 | """ |
---|---|
611 | Message-call into an account. |
612 |
|
613 | Parameters |
614 | ---------- |
615 | evm : |
616 | The current EVM frame. |
617 | """ |
618 | # STACK |
619 | gas = Uint(pop(evm.stack)) |
620 | to = to_address_masked(pop(evm.stack)) |
621 | memory_input_start_position = pop(evm.stack) |
622 | memory_input_size = pop(evm.stack) |
623 | memory_output_start_position = pop(evm.stack) |
624 | memory_output_size = pop(evm.stack) |
625 | |
626 | # GAS |
627 | extend_memory = calculate_gas_extend_memory( |
628 | evm.memory, |
629 | [ |
630 | (memory_input_start_position, memory_input_size), |
631 | (memory_output_start_position, memory_output_size), |
632 | ], |
633 | ) |
634 | |
635 | if to in evm.accessed_addresses: |
636 | access_gas_cost = GAS_WARM_ACCESS |
637 | else: |
638 | evm.accessed_addresses.add(to) |
639 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
640 | |
641 | code_address = to |
642 | |
643 | message_call_gas = calculate_message_call_gas( |
644 | U256(0), |
645 | gas, |
646 | Uint(evm.gas_left), |
647 | extend_memory.cost, |
648 | access_gas_cost, |
649 | ) |
650 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
651 | |
652 | # OPERATION |
653 | evm.memory += b"\x00" * extend_memory.expand_by |
654 | generic_call( |
655 | evm, |
656 | message_call_gas.sub_call, |
657 | U256(0), |
658 | evm.message.current_target, |
659 | to, |
660 | code_address, |
661 | True, |
662 | True, |
663 | memory_input_start_position, |
664 | memory_input_size, |
665 | memory_output_start_position, |
666 | memory_output_size, |
667 | ) |
668 | |
669 | # PROGRAM COUNTER |
670 | 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:
674 | """ |
---|---|
675 | Stop execution and revert state changes, without consuming all provided gas |
676 | and also has the ability to return a reason |
677 | Parameters |
678 | ---------- |
679 | evm : |
680 | The current EVM frame. |
681 | """ |
682 | # STACK |
683 | memory_start_index = pop(evm.stack) |
684 | size = pop(evm.stack) |
685 | |
686 | # GAS |
687 | extend_memory = calculate_gas_extend_memory( |
688 | evm.memory, [(memory_start_index, size)] |
689 | ) |
690 | |
691 | charge_gas(evm, extend_memory.cost) |
692 | |
693 | # OPERATION |
694 | evm.memory += b"\x00" * extend_memory.expand_by |
695 | output = memory_read_bytes(evm.memory, memory_start_index, size) |
696 | evm.output = Bytes(output) |
697 | raise Revert |