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