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