ethereum.berlin.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:
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 STACK_DEPTH_LIMIT, process_create_message |
75 | |
76 | call_data = memory_read_bytes( |
77 | evm.memory, memory_start_position, memory_size |
78 | ) |
79 | |
80 | create_message_gas = max_message_call_gas(Uint(evm.gas_left)) |
81 | evm.gas_left -= create_message_gas |
82 | if evm.message.is_static: |
83 | raise WriteInStaticContext |
84 | evm.return_data = b"" |
85 | |
86 | sender_address = evm.message.current_target |
87 | sender = get_account(evm.message.block_env.state, sender_address) |
88 | |
89 | if ( |
90 | sender.balance < endowment |
91 | or sender.nonce == Uint(2**64 - 1) |
92 | or evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT |
93 | ): |
94 | evm.gas_left += create_message_gas |
95 | push(evm.stack, U256(0)) |
96 | return |
97 | |
98 | evm.accessed_addresses.add(contract_address) |
99 | |
100 | if account_has_code_or_nonce( |
101 | evm.message.block_env.state, contract_address |
102 | ) or account_has_storage(evm.message.block_env.state, contract_address): |
103 | increment_nonce( |
104 | evm.message.block_env.state, evm.message.current_target |
105 | ) |
106 | push(evm.stack, U256(0)) |
107 | return |
108 | |
109 | increment_nonce(evm.message.block_env.state, evm.message.current_target) |
110 | |
111 | child_message = Message( |
112 | block_env=evm.message.block_env, |
113 | tx_env=evm.message.tx_env, |
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) |
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 | |
160 | charge_gas(evm, GAS_CREATE + extend_memory.cost) |
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( |
167 | evm.message.block_env.state, evm.message.current_target |
168 | ).nonce, |
169 | ) |
170 | |
171 | generic_create( |
172 | evm, endowment, contract_address, memory_start_position, memory_size |
173 | ) |
174 | |
175 | # PROGRAM COUNTER |
176 | 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:
180 | """ |
---|---|
181 | Creates a new account with associated code. |
182 |
|
183 | It's similar to CREATE opcode except that the address of new account |
184 | depends on the init_code instead of the nonce of sender. |
185 |
|
186 | Parameters |
187 | ---------- |
188 | evm : |
189 | The current EVM frame. |
190 | """ |
191 | # STACK |
192 | endowment = pop(evm.stack) |
193 | memory_start_position = pop(evm.stack) |
194 | memory_size = pop(evm.stack) |
195 | salt = pop(evm.stack).to_be_bytes32() |
196 | |
197 | # GAS |
198 | extend_memory = calculate_gas_extend_memory( |
199 | evm.memory, [(memory_start_position, memory_size)] |
200 | ) |
201 | call_data_words = ceil32(Uint(memory_size)) // Uint(32) |
202 | charge_gas( |
203 | evm, |
204 | GAS_CREATE + GAS_KECCAK256_WORD * call_data_words + extend_memory.cost, |
205 | ) |
206 | |
207 | # OPERATION |
208 | evm.memory += b"\x00" * extend_memory.expand_by |
209 | contract_address = compute_create2_contract_address( |
210 | evm.message.current_target, |
211 | salt, |
212 | memory_read_bytes(evm.memory, memory_start_position, memory_size), |
213 | ) |
214 | |
215 | generic_create( |
216 | evm, endowment, contract_address, memory_start_position, memory_size |
217 | ) |
218 | |
219 | # PROGRAM COUNTER |
220 | evm.pc += Uint(1) |
return_
Halts execution returning output data.
Parameters
evm : The current EVM frame.
def return_(evm: Evm) -> None:
224 | """ |
---|---|
225 | Halts execution returning output data. |
226 |
|
227 | Parameters |
228 | ---------- |
229 | evm : |
230 | The current EVM frame. |
231 | """ |
232 | # STACK |
233 | memory_start_position = pop(evm.stack) |
234 | memory_size = pop(evm.stack) |
235 | |
236 | # GAS |
237 | extend_memory = calculate_gas_extend_memory( |
238 | evm.memory, [(memory_start_position, memory_size)] |
239 | ) |
240 | |
241 | charge_gas(evm, GAS_ZERO + extend_memory.cost) |
242 | |
243 | # OPERATION |
244 | evm.memory += b"\x00" * extend_memory.expand_by |
245 | evm.output = memory_read_bytes( |
246 | evm.memory, memory_start_position, memory_size |
247 | ) |
248 | |
249 | evm.running = False |
250 | |
251 | # PROGRAM COUNTER |
252 | 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:
269 | """ |
---|---|
270 | Perform the core logic of the `CALL*` family of opcodes. |
271 | """ |
272 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message |
273 | |
274 | evm.return_data = b"" |
275 | |
276 | if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT: |
277 | evm.gas_left += gas |
278 | push(evm.stack, U256(0)) |
279 | return |
280 | |
281 | call_data = memory_read_bytes( |
282 | evm.memory, memory_input_start_position, memory_input_size |
283 | ) |
284 | code = get_account(evm.message.block_env.state, code_address).code |
285 | child_message = Message( |
286 | block_env=evm.message.block_env, |
287 | tx_env=evm.message.tx_env, |
288 | caller=caller, |
289 | target=to, |
290 | gas=gas, |
291 | value=value, |
292 | data=call_data, |
293 | code=code, |
294 | current_target=to, |
295 | depth=evm.message.depth + Uint(1), |
296 | code_address=code_address, |
297 | should_transfer_value=should_transfer_value, |
298 | is_static=True if is_staticcall else evm.message.is_static, |
299 | accessed_addresses=evm.accessed_addresses.copy(), |
300 | accessed_storage_keys=evm.accessed_storage_keys.copy(), |
301 | parent_evm=evm, |
302 | ) |
303 | child_evm = process_message(child_message) |
304 | |
305 | if child_evm.error: |
306 | incorporate_child_on_error(evm, child_evm) |
307 | evm.return_data = child_evm.output |
308 | push(evm.stack, U256(0)) |
309 | else: |
310 | incorporate_child_on_success(evm, child_evm) |
311 | evm.return_data = child_evm.output |
312 | push(evm.stack, U256(1)) |
313 | |
314 | actual_output_size = min(memory_output_size, U256(len(child_evm.output))) |
315 | memory_write( |
316 | evm.memory, |
317 | memory_output_start_position, |
318 | child_evm.output[:actual_output_size], |
319 | ) |
call
Message-call into an account.
Parameters
evm : The current EVM frame.
def call(evm: Evm) -> None:
323 | """ |
---|---|
324 | Message-call into an account. |
325 |
|
326 | Parameters |
327 | ---------- |
328 | evm : |
329 | The current EVM frame. |
330 | """ |
331 | # STACK |
332 | gas = Uint(pop(evm.stack)) |
333 | to = to_address(pop(evm.stack)) |
334 | value = pop(evm.stack) |
335 | memory_input_start_position = pop(evm.stack) |
336 | memory_input_size = pop(evm.stack) |
337 | memory_output_start_position = pop(evm.stack) |
338 | memory_output_size = pop(evm.stack) |
339 | |
340 | # GAS |
341 | extend_memory = calculate_gas_extend_memory( |
342 | evm.memory, |
343 | [ |
344 | (memory_input_start_position, memory_input_size), |
345 | (memory_output_start_position, memory_output_size), |
346 | ], |
347 | ) |
348 | |
349 | if to in evm.accessed_addresses: |
350 | access_gas_cost = GAS_WARM_ACCESS |
351 | else: |
352 | evm.accessed_addresses.add(to) |
353 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
354 | |
355 | code_address = to |
356 | |
357 | create_gas_cost = ( |
358 | Uint(0) |
359 | if is_account_alive(evm.message.block_env.state, to) or value == 0 |
360 | else GAS_NEW_ACCOUNT |
361 | ) |
362 | transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE |
363 | message_call_gas = calculate_message_call_gas( |
364 | value, |
365 | gas, |
366 | Uint(evm.gas_left), |
367 | extend_memory.cost, |
368 | access_gas_cost + create_gas_cost + transfer_gas_cost, |
369 | ) |
370 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
371 | if evm.message.is_static and value != U256(0): |
372 | raise WriteInStaticContext |
373 | evm.memory += b"\x00" * extend_memory.expand_by |
374 | sender_balance = get_account( |
375 | evm.message.block_env.state, evm.message.current_target |
376 | ).balance |
377 | if sender_balance < value: |
378 | push(evm.stack, U256(0)) |
379 | evm.return_data = b"" |
380 | evm.gas_left += message_call_gas.sub_call |
381 | else: |
382 | generic_call( |
383 | evm, |
384 | message_call_gas.sub_call, |
385 | value, |
386 | evm.message.current_target, |
387 | to, |
388 | code_address, |
389 | True, |
390 | False, |
391 | memory_input_start_position, |
392 | memory_input_size, |
393 | memory_output_start_position, |
394 | memory_output_size, |
395 | ) |
396 | |
397 | # PROGRAM COUNTER |
398 | 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:
402 | """ |
---|---|
403 | Message-call into this account with alternative account’s code. |
404 |
|
405 | Parameters |
406 | ---------- |
407 | evm : |
408 | The current EVM frame. |
409 | """ |
410 | # STACK |
411 | gas = Uint(pop(evm.stack)) |
412 | code_address = to_address(pop(evm.stack)) |
413 | value = pop(evm.stack) |
414 | memory_input_start_position = pop(evm.stack) |
415 | memory_input_size = pop(evm.stack) |
416 | memory_output_start_position = pop(evm.stack) |
417 | memory_output_size = pop(evm.stack) |
418 | |
419 | # GAS |
420 | to = evm.message.current_target |
421 | |
422 | extend_memory = calculate_gas_extend_memory( |
423 | evm.memory, |
424 | [ |
425 | (memory_input_start_position, memory_input_size), |
426 | (memory_output_start_position, memory_output_size), |
427 | ], |
428 | ) |
429 | |
430 | if code_address in evm.accessed_addresses: |
431 | access_gas_cost = GAS_WARM_ACCESS |
432 | else: |
433 | evm.accessed_addresses.add(code_address) |
434 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
435 | |
436 | transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE |
437 | message_call_gas = calculate_message_call_gas( |
438 | value, |
439 | gas, |
440 | Uint(evm.gas_left), |
441 | extend_memory.cost, |
442 | access_gas_cost + transfer_gas_cost, |
443 | ) |
444 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
445 | |
446 | # OPERATION |
447 | evm.memory += b"\x00" * extend_memory.expand_by |
448 | sender_balance = get_account( |
449 | evm.message.block_env.state, evm.message.current_target |
450 | ).balance |
451 | if sender_balance < value: |
452 | push(evm.stack, U256(0)) |
453 | evm.return_data = b"" |
454 | evm.gas_left += message_call_gas.sub_call |
455 | else: |
456 | generic_call( |
457 | evm, |
458 | message_call_gas.sub_call, |
459 | value, |
460 | evm.message.current_target, |
461 | to, |
462 | code_address, |
463 | True, |
464 | False, |
465 | memory_input_start_position, |
466 | memory_input_size, |
467 | memory_output_start_position, |
468 | memory_output_size, |
469 | ) |
470 | |
471 | # PROGRAM COUNTER |
472 | evm.pc += Uint(1) |
selfdestruct
Halt execution and register account for later deletion.
Parameters
evm : The current EVM frame.
def selfdestruct(evm: Evm) -> None:
476 | """ |
---|---|
477 | Halt execution and register account for later deletion. |
478 |
|
479 | Parameters |
480 | ---------- |
481 | evm : |
482 | The current EVM frame. |
483 | """ |
484 | # STACK |
485 | beneficiary = to_address(pop(evm.stack)) |
486 | |
487 | # GAS |
488 | gas_cost = GAS_SELF_DESTRUCT |
489 | if beneficiary not in evm.accessed_addresses: |
490 | evm.accessed_addresses.add(beneficiary) |
491 | gas_cost += GAS_COLD_ACCOUNT_ACCESS |
492 | |
493 | if ( |
494 | not is_account_alive(evm.message.block_env.state, beneficiary) |
495 | and get_account( |
496 | evm.message.block_env.state, evm.message.current_target |
497 | ).balance |
498 | != 0 |
499 | ): |
500 | gas_cost += GAS_SELF_DESTRUCT_NEW_ACCOUNT |
501 | |
502 | originator = evm.message.current_target |
503 | |
504 | refunded_accounts = evm.accounts_to_delete |
505 | parent_evm = evm.message.parent_evm |
506 | while parent_evm is not None: |
507 | refunded_accounts.update(parent_evm.accounts_to_delete) |
508 | parent_evm = parent_evm.message.parent_evm |
509 | |
510 | if originator not in refunded_accounts: |
511 | evm.refund_counter += REFUND_SELF_DESTRUCT |
512 | |
513 | charge_gas(evm, gas_cost) |
514 | if evm.message.is_static: |
515 | raise WriteInStaticContext |
516 | |
517 | originator = evm.message.current_target |
518 | beneficiary_balance = get_account( |
519 | evm.message.block_env.state, beneficiary |
520 | ).balance |
521 | originator_balance = get_account( |
522 | evm.message.block_env.state, originator |
523 | ).balance |
524 | |
525 | # First Transfer to beneficiary |
526 | set_account_balance( |
527 | evm.message.block_env.state, |
528 | beneficiary, |
529 | beneficiary_balance + originator_balance, |
530 | ) |
531 | # Next, Zero the balance of the address being deleted (must come after |
532 | # sending to beneficiary in case the contract named itself as the |
533 | # beneficiary). |
534 | set_account_balance(evm.message.block_env.state, originator, U256(0)) |
535 | |
536 | # register account for deletion |
537 | evm.accounts_to_delete.add(originator) |
538 | |
539 | # mark beneficiary as touched |
540 | if account_exists_and_is_empty(evm.message.block_env.state, beneficiary): |
541 | evm.touched_accounts.add(beneficiary) |
542 | |
543 | # HALT the execution |
544 | evm.running = False |
545 | |
546 | # PROGRAM COUNTER |
547 | pass |
delegatecall
Message-call into an account.
Parameters
evm : The current EVM frame.
def delegatecall(evm: Evm) -> None:
551 | """ |
---|---|
552 | Message-call into an account. |
553 |
|
554 | Parameters |
555 | ---------- |
556 | evm : |
557 | The current EVM frame. |
558 | """ |
559 | # STACK |
560 | gas = Uint(pop(evm.stack)) |
561 | code_address = to_address(pop(evm.stack)) |
562 | memory_input_start_position = pop(evm.stack) |
563 | memory_input_size = pop(evm.stack) |
564 | memory_output_start_position = pop(evm.stack) |
565 | memory_output_size = pop(evm.stack) |
566 | |
567 | # GAS |
568 | extend_memory = calculate_gas_extend_memory( |
569 | evm.memory, |
570 | [ |
571 | (memory_input_start_position, memory_input_size), |
572 | (memory_output_start_position, memory_output_size), |
573 | ], |
574 | ) |
575 | |
576 | if code_address in evm.accessed_addresses: |
577 | access_gas_cost = GAS_WARM_ACCESS |
578 | else: |
579 | evm.accessed_addresses.add(code_address) |
580 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
581 | |
582 | message_call_gas = calculate_message_call_gas( |
583 | U256(0), gas, Uint(evm.gas_left), extend_memory.cost, access_gas_cost |
584 | ) |
585 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
586 | |
587 | # OPERATION |
588 | evm.memory += b"\x00" * extend_memory.expand_by |
589 | generic_call( |
590 | evm, |
591 | message_call_gas.sub_call, |
592 | evm.message.value, |
593 | evm.message.caller, |
594 | evm.message.current_target, |
595 | code_address, |
596 | False, |
597 | False, |
598 | memory_input_start_position, |
599 | memory_input_size, |
600 | memory_output_start_position, |
601 | memory_output_size, |
602 | ) |
603 | |
604 | # PROGRAM COUNTER |
605 | evm.pc += Uint(1) |
staticcall
Message-call into an account.
Parameters
evm : The current EVM frame.
def staticcall(evm: Evm) -> None:
609 | """ |
---|---|
610 | Message-call into an account. |
611 |
|
612 | Parameters |
613 | ---------- |
614 | evm : |
615 | The current EVM frame. |
616 | """ |
617 | # STACK |
618 | gas = Uint(pop(evm.stack)) |
619 | to = to_address(pop(evm.stack)) |
620 | memory_input_start_position = pop(evm.stack) |
621 | memory_input_size = pop(evm.stack) |
622 | memory_output_start_position = pop(evm.stack) |
623 | memory_output_size = pop(evm.stack) |
624 | |
625 | # GAS |
626 | extend_memory = calculate_gas_extend_memory( |
627 | evm.memory, |
628 | [ |
629 | (memory_input_start_position, memory_input_size), |
630 | (memory_output_start_position, memory_output_size), |
631 | ], |
632 | ) |
633 | |
634 | if to in evm.accessed_addresses: |
635 | access_gas_cost = GAS_WARM_ACCESS |
636 | else: |
637 | evm.accessed_addresses.add(to) |
638 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
639 | |
640 | code_address = to |
641 | |
642 | message_call_gas = calculate_message_call_gas( |
643 | U256(0), |
644 | gas, |
645 | Uint(evm.gas_left), |
646 | extend_memory.cost, |
647 | access_gas_cost, |
648 | ) |
649 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
650 | |
651 | # OPERATION |
652 | evm.memory += b"\x00" * extend_memory.expand_by |
653 | generic_call( |
654 | evm, |
655 | message_call_gas.sub_call, |
656 | U256(0), |
657 | evm.message.current_target, |
658 | to, |
659 | code_address, |
660 | True, |
661 | True, |
662 | memory_input_start_position, |
663 | memory_input_size, |
664 | memory_output_start_position, |
665 | memory_output_size, |
666 | ) |
667 | |
668 | # PROGRAM COUNTER |
669 | 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:
673 | """ |
---|---|
674 | Stop execution and revert state changes, without consuming all provided gas |
675 | and also has the ability to return a reason |
676 | Parameters |
677 | ---------- |
678 | evm : |
679 | The current EVM frame. |
680 | """ |
681 | # STACK |
682 | memory_start_index = pop(evm.stack) |
683 | size = pop(evm.stack) |
684 | |
685 | # GAS |
686 | extend_memory = calculate_gas_extend_memory( |
687 | evm.memory, [(memory_start_index, size)] |
688 | ) |
689 | |
690 | charge_gas(evm, extend_memory.cost) |
691 | |
692 | # OPERATION |
693 | evm.memory += b"\x00" * extend_memory.expand_by |
694 | output = memory_read_bytes(evm.memory, memory_start_index, size) |
695 | evm.output = Bytes(output) |
696 | raise Revert |