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, 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( |
107 | evm.env.state, contract_address |
108 | ) or account_has_storage(evm.env.state, contract_address): |
109 | increment_nonce(evm.env.state, evm.message.current_target) |
110 | push(evm.stack, U256(0)) |
111 | return |
112 | |
113 | increment_nonce(evm.env.state, evm.message.current_target) |
114 | |
115 | child_message = Message( |
116 | caller=evm.message.current_target, |
117 | target=Bytes0(), |
118 | gas=create_message_gas, |
119 | value=endowment, |
120 | data=b"", |
121 | code=call_data, |
122 | current_target=contract_address, |
123 | depth=evm.message.depth + Uint(1), |
124 | code_address=None, |
125 | should_transfer_value=True, |
126 | is_static=False, |
127 | accessed_addresses=evm.accessed_addresses.copy(), |
128 | accessed_storage_keys=evm.accessed_storage_keys.copy(), |
129 | parent_evm=evm, |
130 | ) |
131 | child_evm = process_create_message(child_message, evm.env) |
132 | |
133 | if child_evm.error: |
134 | incorporate_child_on_error(evm, child_evm) |
135 | evm.return_data = child_evm.output |
136 | push(evm.stack, U256(0)) |
137 | else: |
138 | incorporate_child_on_success(evm, child_evm) |
139 | evm.return_data = b"" |
140 | 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:
144 | """ |
---|---|
145 | Creates a new account with associated code. |
146 |
|
147 | Parameters |
148 | ---------- |
149 | evm : |
150 | The current EVM frame. |
151 | """ |
152 | # STACK |
153 | endowment = pop(evm.stack) |
154 | memory_start_position = pop(evm.stack) |
155 | memory_size = pop(evm.stack) |
156 | |
157 | # GAS |
158 | extend_memory = calculate_gas_extend_memory( |
159 | evm.memory, [(memory_start_position, memory_size)] |
160 | ) |
161 | init_code_gas = init_code_cost(Uint(memory_size)) |
162 | |
163 | charge_gas(evm, GAS_CREATE + extend_memory.cost + init_code_gas) |
164 | |
165 | # OPERATION |
166 | evm.memory += b"\x00" * extend_memory.expand_by |
167 | contract_address = compute_contract_address( |
168 | evm.message.current_target, |
169 | get_account(evm.env.state, evm.message.current_target).nonce, |
170 | ) |
171 | |
172 | generic_create( |
173 | evm, |
174 | endowment, |
175 | contract_address, |
176 | memory_start_position, |
177 | memory_size, |
178 | init_code_gas, |
179 | ) |
180 | |
181 | # PROGRAM COUNTER |
182 | 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:
186 | """ |
---|---|
187 | Creates a new account with associated code. |
188 |
|
189 | It's similar to CREATE opcode except that the address of new account |
190 | depends on the init_code instead of the nonce of sender. |
191 |
|
192 | Parameters |
193 | ---------- |
194 | evm : |
195 | The current EVM frame. |
196 | """ |
197 | # STACK |
198 | endowment = pop(evm.stack) |
199 | memory_start_position = pop(evm.stack) |
200 | memory_size = pop(evm.stack) |
201 | salt = pop(evm.stack).to_be_bytes32() |
202 | |
203 | # GAS |
204 | extend_memory = calculate_gas_extend_memory( |
205 | evm.memory, [(memory_start_position, memory_size)] |
206 | ) |
207 | call_data_words = ceil32(Uint(memory_size)) // Uint(32) |
208 | init_code_gas = init_code_cost(Uint(memory_size)) |
209 | charge_gas( |
210 | evm, |
211 | GAS_CREATE |
212 | + GAS_KECCAK256_WORD * call_data_words |
213 | + extend_memory.cost |
214 | + init_code_gas, |
215 | ) |
216 | |
217 | # OPERATION |
218 | evm.memory += b"\x00" * extend_memory.expand_by |
219 | contract_address = compute_create2_contract_address( |
220 | evm.message.current_target, |
221 | salt, |
222 | memory_read_bytes(evm.memory, memory_start_position, memory_size), |
223 | ) |
224 | |
225 | generic_create( |
226 | evm, |
227 | endowment, |
228 | contract_address, |
229 | memory_start_position, |
230 | memory_size, |
231 | init_code_gas, |
232 | ) |
233 | |
234 | # PROGRAM COUNTER |
235 | evm.pc += Uint(1) |
return_
Halts execution returning output data.
Parameters
evm : The current EVM frame.
def return_(evm: Evm) -> None:
239 | """ |
---|---|
240 | Halts execution returning output data. |
241 |
|
242 | Parameters |
243 | ---------- |
244 | evm : |
245 | The current EVM frame. |
246 | """ |
247 | # STACK |
248 | memory_start_position = pop(evm.stack) |
249 | memory_size = pop(evm.stack) |
250 | |
251 | # GAS |
252 | extend_memory = calculate_gas_extend_memory( |
253 | evm.memory, [(memory_start_position, memory_size)] |
254 | ) |
255 | |
256 | charge_gas(evm, GAS_ZERO + extend_memory.cost) |
257 | |
258 | # OPERATION |
259 | evm.memory += b"\x00" * extend_memory.expand_by |
260 | evm.output = memory_read_bytes( |
261 | evm.memory, memory_start_position, memory_size |
262 | ) |
263 | |
264 | evm.running = False |
265 | |
266 | # PROGRAM COUNTER |
267 | 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:
284 | """ |
---|---|
285 | Perform the core logic of the `CALL*` family of opcodes. |
286 | """ |
287 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message |
288 | |
289 | evm.return_data = b"" |
290 | |
291 | if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT: |
292 | evm.gas_left += gas |
293 | push(evm.stack, U256(0)) |
294 | return |
295 | |
296 | call_data = memory_read_bytes( |
297 | evm.memory, memory_input_start_position, memory_input_size |
298 | ) |
299 | code = get_account(evm.env.state, code_address).code |
300 | child_message = Message( |
301 | caller=caller, |
302 | target=to, |
303 | gas=gas, |
304 | value=value, |
305 | data=call_data, |
306 | code=code, |
307 | current_target=to, |
308 | depth=evm.message.depth + Uint(1), |
309 | code_address=code_address, |
310 | should_transfer_value=should_transfer_value, |
311 | is_static=True if is_staticcall else evm.message.is_static, |
312 | accessed_addresses=evm.accessed_addresses.copy(), |
313 | accessed_storage_keys=evm.accessed_storage_keys.copy(), |
314 | parent_evm=evm, |
315 | ) |
316 | child_evm = process_message(child_message, evm.env) |
317 | |
318 | if child_evm.error: |
319 | incorporate_child_on_error(evm, child_evm) |
320 | evm.return_data = child_evm.output |
321 | push(evm.stack, U256(0)) |
322 | else: |
323 | incorporate_child_on_success(evm, child_evm) |
324 | evm.return_data = child_evm.output |
325 | push(evm.stack, U256(1)) |
326 | |
327 | actual_output_size = min(memory_output_size, U256(len(child_evm.output))) |
328 | memory_write( |
329 | evm.memory, |
330 | memory_output_start_position, |
331 | child_evm.output[:actual_output_size], |
332 | ) |
call
Message-call into an account.
Parameters
evm : The current EVM frame.
def call(evm: Evm) -> None:
336 | """ |
---|---|
337 | Message-call into an account. |
338 |
|
339 | Parameters |
340 | ---------- |
341 | evm : |
342 | The current EVM frame. |
343 | """ |
344 | # STACK |
345 | gas = Uint(pop(evm.stack)) |
346 | to = to_address(pop(evm.stack)) |
347 | value = pop(evm.stack) |
348 | memory_input_start_position = pop(evm.stack) |
349 | memory_input_size = pop(evm.stack) |
350 | memory_output_start_position = pop(evm.stack) |
351 | memory_output_size = pop(evm.stack) |
352 | |
353 | # GAS |
354 | extend_memory = calculate_gas_extend_memory( |
355 | evm.memory, |
356 | [ |
357 | (memory_input_start_position, memory_input_size), |
358 | (memory_output_start_position, memory_output_size), |
359 | ], |
360 | ) |
361 | |
362 | if to in evm.accessed_addresses: |
363 | access_gas_cost = GAS_WARM_ACCESS |
364 | else: |
365 | evm.accessed_addresses.add(to) |
366 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
367 | |
368 | create_gas_cost = ( |
369 | Uint(0) |
370 | if is_account_alive(evm.env.state, to) or value == 0 |
371 | else GAS_NEW_ACCOUNT |
372 | ) |
373 | transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE |
374 | message_call_gas = calculate_message_call_gas( |
375 | value, |
376 | gas, |
377 | Uint(evm.gas_left), |
378 | extend_memory.cost, |
379 | access_gas_cost + create_gas_cost + transfer_gas_cost, |
380 | ) |
381 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
382 | if evm.message.is_static and value != U256(0): |
383 | raise WriteInStaticContext |
384 | evm.memory += b"\x00" * extend_memory.expand_by |
385 | sender_balance = get_account( |
386 | evm.env.state, evm.message.current_target |
387 | ).balance |
388 | if sender_balance < value: |
389 | push(evm.stack, U256(0)) |
390 | evm.return_data = b"" |
391 | evm.gas_left += message_call_gas.stipend |
392 | else: |
393 | generic_call( |
394 | evm, |
395 | message_call_gas.stipend, |
396 | value, |
397 | evm.message.current_target, |
398 | to, |
399 | to, |
400 | True, |
401 | False, |
402 | memory_input_start_position, |
403 | memory_input_size, |
404 | memory_output_start_position, |
405 | memory_output_size, |
406 | ) |
407 | |
408 | # PROGRAM COUNTER |
409 | 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:
413 | """ |
---|---|
414 | Message-call into this account with alternative account’s code. |
415 |
|
416 | Parameters |
417 | ---------- |
418 | evm : |
419 | The current EVM frame. |
420 | """ |
421 | # STACK |
422 | gas = Uint(pop(evm.stack)) |
423 | code_address = to_address(pop(evm.stack)) |
424 | value = pop(evm.stack) |
425 | memory_input_start_position = pop(evm.stack) |
426 | memory_input_size = pop(evm.stack) |
427 | memory_output_start_position = pop(evm.stack) |
428 | memory_output_size = pop(evm.stack) |
429 | |
430 | # GAS |
431 | to = evm.message.current_target |
432 | |
433 | extend_memory = calculate_gas_extend_memory( |
434 | evm.memory, |
435 | [ |
436 | (memory_input_start_position, memory_input_size), |
437 | (memory_output_start_position, memory_output_size), |
438 | ], |
439 | ) |
440 | |
441 | if code_address in evm.accessed_addresses: |
442 | access_gas_cost = GAS_WARM_ACCESS |
443 | else: |
444 | evm.accessed_addresses.add(code_address) |
445 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
446 | |
447 | transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE |
448 | message_call_gas = calculate_message_call_gas( |
449 | value, |
450 | gas, |
451 | Uint(evm.gas_left), |
452 | extend_memory.cost, |
453 | access_gas_cost + transfer_gas_cost, |
454 | ) |
455 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
456 | |
457 | # OPERATION |
458 | evm.memory += b"\x00" * extend_memory.expand_by |
459 | sender_balance = get_account( |
460 | evm.env.state, evm.message.current_target |
461 | ).balance |
462 | if sender_balance < value: |
463 | push(evm.stack, U256(0)) |
464 | evm.return_data = b"" |
465 | evm.gas_left += message_call_gas.stipend |
466 | else: |
467 | generic_call( |
468 | evm, |
469 | message_call_gas.stipend, |
470 | value, |
471 | evm.message.current_target, |
472 | to, |
473 | code_address, |
474 | True, |
475 | False, |
476 | memory_input_start_position, |
477 | memory_input_size, |
478 | memory_output_start_position, |
479 | memory_output_size, |
480 | ) |
481 | |
482 | # PROGRAM COUNTER |
483 | evm.pc += Uint(1) |
selfdestruct
Halt execution and register account for later deletion.
Parameters
evm : The current EVM frame.
def selfdestruct(evm: Evm) -> None:
487 | """ |
---|---|
488 | Halt execution and register account for later deletion. |
489 |
|
490 | Parameters |
491 | ---------- |
492 | evm : |
493 | The current EVM frame. |
494 | """ |
495 | # STACK |
496 | beneficiary = to_address(pop(evm.stack)) |
497 | |
498 | # GAS |
499 | gas_cost = GAS_SELF_DESTRUCT |
500 | if beneficiary not in evm.accessed_addresses: |
501 | evm.accessed_addresses.add(beneficiary) |
502 | gas_cost += GAS_COLD_ACCOUNT_ACCESS |
503 | |
504 | if ( |
505 | not is_account_alive(evm.env.state, beneficiary) |
506 | and get_account(evm.env.state, evm.message.current_target).balance != 0 |
507 | ): |
508 | gas_cost += GAS_SELF_DESTRUCT_NEW_ACCOUNT |
509 | |
510 | charge_gas(evm, gas_cost) |
511 | if evm.message.is_static: |
512 | raise WriteInStaticContext |
513 | |
514 | originator = evm.message.current_target |
515 | beneficiary_balance = get_account(evm.env.state, beneficiary).balance |
516 | originator_balance = get_account(evm.env.state, originator).balance |
517 | |
518 | # First Transfer to beneficiary |
519 | set_account_balance( |
520 | evm.env.state, beneficiary, beneficiary_balance + originator_balance |
521 | ) |
522 | # Next, Zero the balance of the address being deleted (must come after |
523 | # sending to beneficiary in case the contract named itself as the |
524 | # beneficiary). |
525 | set_account_balance(evm.env.state, originator, U256(0)) |
526 | |
527 | # register account for deletion |
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 |