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