ethereum.forks.berlin.vm.instructions.systemethereum.forks.london.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 | evm.accessed_addresses.add(contract_address) |
97 | |
98 | if account_has_code_or_nonce( |
99 | evm.message.block_env.state, contract_address |
100 | ) or account_has_storage(evm.message.block_env.state, contract_address): |
101 | increment_nonce( |
102 | evm.message.block_env.state, evm.message.current_target |
103 | ) |
104 | push(evm.stack, U256(0)) |
105 | return |
106 | |
107 | increment_nonce(evm.message.block_env.state, evm.message.current_target) |
108 | |
109 | child_message = Message( |
110 | block_env=evm.message.block_env, |
111 | tx_env=evm.message.tx_env, |
112 | caller=evm.message.current_target, |
113 | target=Bytes0(), |
114 | gas=create_message_gas, |
115 | value=endowment, |
116 | data=b"", |
117 | code=call_data, |
118 | current_target=contract_address, |
119 | depth=evm.message.depth + Uint(1), |
120 | code_address=None, |
121 | should_transfer_value=True, |
122 | is_static=False, |
123 | accessed_addresses=evm.accessed_addresses.copy(), |
124 | accessed_storage_keys=evm.accessed_storage_keys.copy(), |
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 | """ |
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 | |
159 | charge_gas(evm, GAS_CREATE + extend_memory.cost) |
160 | |
161 | # OPERATION |
162 | evm.memory += b"\x00" * extend_memory.expand_by |
163 | contract_address = compute_contract_address( |
164 | evm.message.current_target, |
165 | get_account( |
166 | evm.message.block_env.state, evm.message.current_target |
167 | ).nonce, |
168 | ) |
169 | |
170 | generic_create( |
171 | evm, endowment, contract_address, memory_start_position, memory_size |
172 | ) |
173 | |
174 | # PROGRAM COUNTER |
175 | 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:
179 | """ |
---|---|
180 | Creates a new account with associated code. |
181 |
|
182 | It's similar to CREATE opcode except that the address of new account |
183 | depends on the init_code instead of the nonce of sender. |
184 |
|
185 | Parameters |
186 | ---------- |
187 | evm : |
188 | The current EVM frame. |
189 |
|
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 | """ |
233 | # STACK |
234 | memory_start_position = pop(evm.stack) |
235 | memory_size = pop(evm.stack) |
236 | |
237 | # GAS |
238 | extend_memory = calculate_gas_extend_memory( |
239 | evm.memory, [(memory_start_position, memory_size)] |
240 | ) |
241 | |
242 | charge_gas(evm, GAS_ZERO + extend_memory.cost) |
243 | |
244 | # OPERATION |
245 | evm.memory += b"\x00" * extend_memory.expand_by |
246 | evm.output = memory_read_bytes( |
247 | evm.memory, memory_start_position, memory_size |
248 | ) |
249 | |
250 | evm.running = False |
251 | |
252 | # PROGRAM COUNTER |
253 | 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:
270 | """ |
---|---|
271 | Perform the core logic of the `CALL*` family of opcodes. |
272 | """ |
273 | from ...vm.interpreter import STACK_DEPTH_LIMIT, process_message |
274 | |
275 | evm.return_data = b"" |
276 | |
277 | if evm.message.depth + Uint(1) > STACK_DEPTH_LIMIT: |
278 | evm.gas_left += gas |
279 | push(evm.stack, U256(0)) |
280 | return |
281 | |
282 | call_data = memory_read_bytes( |
283 | evm.memory, memory_input_start_position, memory_input_size |
284 | ) |
285 | code = get_account(evm.message.block_env.state, code_address).code |
286 | child_message = Message( |
287 | block_env=evm.message.block_env, |
288 | tx_env=evm.message.tx_env, |
289 | caller=caller, |
290 | target=to, |
291 | gas=gas, |
292 | value=value, |
293 | data=call_data, |
294 | code=code, |
295 | current_target=to, |
296 | depth=evm.message.depth + Uint(1), |
297 | code_address=code_address, |
298 | should_transfer_value=should_transfer_value, |
299 | is_static=True if is_staticcall else evm.message.is_static, |
300 | accessed_addresses=evm.accessed_addresses.copy(), |
301 | accessed_storage_keys=evm.accessed_storage_keys.copy(), |
302 | parent_evm=evm, |
303 | ) |
304 | child_evm = process_message(child_message) |
305 | |
306 | if child_evm.error: |
307 | incorporate_child_on_error(evm, child_evm) |
308 | evm.return_data = child_evm.output |
309 | push(evm.stack, U256(0)) |
310 | else: |
311 | incorporate_child_on_success(evm, child_evm) |
312 | evm.return_data = child_evm.output |
313 | push(evm.stack, U256(1)) |
314 | |
315 | actual_output_size = min(memory_output_size, U256(len(child_evm.output))) |
316 | memory_write( |
317 | evm.memory, |
318 | memory_output_start_position, |
319 | child_evm.output[:actual_output_size], |
320 | ) |
call
Message-call into an account.
Parameters
evm : The current EVM frame.
def call(evm: Evm) -> None:
324 | """ |
---|---|
325 | Message-call into an account. |
326 |
|
327 | Parameters |
328 | ---------- |
329 | evm : |
330 | The current EVM frame. |
331 |
|
332 | """ |
333 | # STACK |
334 | gas = Uint(pop(evm.stack)) |
335 | to = to_address_masked(pop(evm.stack)) |
336 | value = pop(evm.stack) |
337 | memory_input_start_position = pop(evm.stack) |
338 | memory_input_size = pop(evm.stack) |
339 | memory_output_start_position = pop(evm.stack) |
340 | memory_output_size = pop(evm.stack) |
341 | |
342 | # GAS |
343 | extend_memory = calculate_gas_extend_memory( |
344 | evm.memory, |
345 | [ |
346 | (memory_input_start_position, memory_input_size), |
347 | (memory_output_start_position, memory_output_size), |
348 | ], |
349 | ) |
350 | |
351 | if to in evm.accessed_addresses: |
352 | access_gas_cost = GAS_WARM_ACCESS |
353 | else: |
354 | evm.accessed_addresses.add(to) |
355 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
356 | |
357 | code_address = to |
358 | |
359 | create_gas_cost = GAS_NEW_ACCOUNT |
360 | if value == 0 or is_account_alive(evm.message.block_env.state, to): |
361 | create_gas_cost = Uint(0) |
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 | """ |
411 | # STACK |
412 | gas = Uint(pop(evm.stack)) |
413 | code_address = to_address_masked(pop(evm.stack)) |
414 | value = pop(evm.stack) |
415 | memory_input_start_position = pop(evm.stack) |
416 | memory_input_size = pop(evm.stack) |
417 | memory_output_start_position = pop(evm.stack) |
418 | memory_output_size = pop(evm.stack) |
419 | |
420 | # GAS |
421 | to = evm.message.current_target |
422 | |
423 | extend_memory = calculate_gas_extend_memory( |
424 | evm.memory, |
425 | [ |
426 | (memory_input_start_position, memory_input_size), |
427 | (memory_output_start_position, memory_output_size), |
428 | ], |
429 | ) |
430 | |
431 | if code_address in evm.accessed_addresses: |
432 | access_gas_cost = GAS_WARM_ACCESS |
433 | else: |
434 | evm.accessed_addresses.add(code_address) |
435 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
436 | |
437 | transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE |
438 | message_call_gas = calculate_message_call_gas( |
439 | value, |
440 | gas, |
441 | Uint(evm.gas_left), |
442 | extend_memory.cost, |
443 | access_gas_cost + transfer_gas_cost, |
444 | ) |
445 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
446 | |
447 | # OPERATION |
448 | evm.memory += b"\x00" * extend_memory.expand_by |
449 | sender_balance = get_account( |
450 | evm.message.block_env.state, evm.message.current_target |
451 | ).balance |
452 | if sender_balance < value: |
453 | push(evm.stack, U256(0)) |
454 | evm.return_data = b"" |
455 | evm.gas_left += message_call_gas.sub_call |
456 | else: |
457 | generic_call( |
458 | evm, |
459 | message_call_gas.sub_call, |
460 | value, |
461 | evm.message.current_target, |
462 | to, |
463 | code_address, |
464 | True, |
465 | False, |
466 | memory_input_start_position, |
467 | memory_input_size, |
468 | memory_output_start_position, |
469 | memory_output_size, |
470 | ) |
471 | |
472 | # PROGRAM COUNTER |
473 | evm.pc += Uint(1) |
selfdestruct
Halt execution and register account for later deletion.
Parameters
evm : The current EVM frame.
def selfdestruct(evm: Evm) -> None:
477 | """ |
---|---|
478 | Halt execution and register account for later deletion. |
479 |
|
480 | Parameters |
481 | ---------- |
482 | evm : |
483 | The current EVM frame. |
484 |
|
485 | """ |
486 | # STACK |
487 | beneficiary = to_address_masked(pop(evm.stack)) |
488 | |
489 | # GAS |
490 | gas_cost = GAS_SELF_DESTRUCT |
491 | if beneficiary not in evm.accessed_addresses: |
492 | evm.accessed_addresses.add(beneficiary) |
493 | gas_cost += GAS_COLD_ACCOUNT_ACCESS |
494 | |
495 | if ( |
496 | not is_account_alive(evm.message.block_env.state, beneficiary) |
497 | and get_account( |
498 | evm.message.block_env.state, evm.message.current_target |
499 | ).balance |
500 | != 0 |
501 | ): |
502 | gas_cost += GAS_SELF_DESTRUCT_NEW_ACCOUNT |
504 | |
505 | originator = evm.message.current_target |
506 | |
507 | refunded_accounts = evm.accounts_to_delete |
508 | parent_evm = evm.message.parent_evm |
509 | while parent_evm is not None: |
510 | refunded_accounts.update(parent_evm.accounts_to_delete) |
511 | parent_evm = parent_evm.message.parent_evm |
512 | |
513 | if originator not in refunded_accounts: |
514 | evm.refund_counter += REFUND_SELF_DESTRUCT |
503 | |
504 | charge_gas(evm, gas_cost) |
505 | if evm.message.is_static: |
506 | raise WriteInStaticContext |
507 | |
508 | originator = evm.message.current_target |
509 | beneficiary_balance = get_account( |
510 | evm.message.block_env.state, beneficiary |
511 | ).balance |
512 | originator_balance = get_account( |
513 | evm.message.block_env.state, originator |
514 | ).balance |
515 | |
516 | # First Transfer to beneficiary |
517 | set_account_balance( |
518 | evm.message.block_env.state, |
519 | beneficiary, |
520 | 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.message.block_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.message.block_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 | """ |
551 | # STACK |
552 | gas = Uint(pop(evm.stack)) |
553 | code_address = to_address_masked(pop(evm.stack)) |
554 | memory_input_start_position = pop(evm.stack) |
555 | memory_input_size = pop(evm.stack) |
556 | memory_output_start_position = pop(evm.stack) |
557 | memory_output_size = pop(evm.stack) |
558 | |
559 | # GAS |
560 | extend_memory = calculate_gas_extend_memory( |
561 | evm.memory, |
562 | [ |
563 | (memory_input_start_position, memory_input_size), |
564 | (memory_output_start_position, memory_output_size), |
565 | ], |
566 | ) |
567 | |
568 | if code_address in evm.accessed_addresses: |
569 | access_gas_cost = GAS_WARM_ACCESS |
570 | else: |
571 | evm.accessed_addresses.add(code_address) |
572 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
573 | |
574 | message_call_gas = calculate_message_call_gas( |
575 | U256(0), gas, Uint(evm.gas_left), extend_memory.cost, access_gas_cost |
576 | ) |
577 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
578 | |
579 | # OPERATION |
580 | evm.memory += b"\x00" * extend_memory.expand_by |
581 | generic_call( |
582 | evm, |
583 | message_call_gas.sub_call, |
584 | evm.message.value, |
585 | evm.message.caller, |
586 | evm.message.current_target, |
587 | code_address, |
588 | False, |
589 | False, |
590 | memory_input_start_position, |
591 | memory_input_size, |
592 | memory_output_start_position, |
593 | memory_output_size, |
594 | ) |
595 | |
596 | # PROGRAM COUNTER |
597 | evm.pc += Uint(1) |
staticcall
Message-call into an account.
Parameters
evm : The current EVM frame.
def staticcall(evm: Evm) -> None:
601 | """ |
---|---|
602 | Message-call into an account. |
603 |
|
604 | Parameters |
605 | ---------- |
606 | evm : |
607 | The current EVM frame. |
608 |
|
609 | """ |
610 | # STACK |
611 | gas = Uint(pop(evm.stack)) |
612 | to = to_address_masked(pop(evm.stack)) |
613 | memory_input_start_position = pop(evm.stack) |
614 | memory_input_size = pop(evm.stack) |
615 | memory_output_start_position = pop(evm.stack) |
616 | memory_output_size = pop(evm.stack) |
617 | |
618 | # GAS |
619 | extend_memory = calculate_gas_extend_memory( |
620 | evm.memory, |
621 | [ |
622 | (memory_input_start_position, memory_input_size), |
623 | (memory_output_start_position, memory_output_size), |
624 | ], |
625 | ) |
626 | |
627 | if to in evm.accessed_addresses: |
628 | access_gas_cost = GAS_WARM_ACCESS |
629 | else: |
630 | evm.accessed_addresses.add(to) |
631 | access_gas_cost = GAS_COLD_ACCOUNT_ACCESS |
632 | |
633 | code_address = to |
634 | |
635 | message_call_gas = calculate_message_call_gas( |
636 | U256(0), |
637 | gas, |
638 | Uint(evm.gas_left), |
639 | extend_memory.cost, |
640 | access_gas_cost, |
641 | ) |
642 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
643 | |
644 | # OPERATION |
645 | evm.memory += b"\x00" * extend_memory.expand_by |
646 | generic_call( |
647 | evm, |
648 | message_call_gas.sub_call, |
649 | U256(0), |
650 | evm.message.current_target, |
651 | to, |
652 | code_address, |
653 | True, |
654 | True, |
655 | memory_input_start_position, |
656 | memory_input_size, |
657 | memory_output_start_position, |
658 | memory_output_size, |
659 | ) |
660 | |
661 | # PROGRAM COUNTER |
662 | 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:
666 | """ |
---|---|
667 | Stop execution and revert state changes, without consuming all provided gas |
668 | and also has the ability to return a reason. |
669 |
|
670 | Parameters |
671 | ---------- |
672 | evm : |
673 | The current EVM frame. |
674 |
|
675 | """ |
676 | # STACK |
677 | memory_start_index = pop(evm.stack) |
678 | size = pop(evm.stack) |
679 | |
680 | # GAS |
681 | extend_memory = calculate_gas_extend_memory( |
682 | evm.memory, [(memory_start_index, size)] |
683 | ) |
684 | |
685 | charge_gas(evm, extend_memory.cost) |
686 | |
687 | # OPERATION |
688 | evm.memory += b"\x00" * extend_memory.expand_by |
689 | output = memory_read_bytes(evm.memory, memory_start_index, size) |
690 | evm.output = Bytes(output) |
691 | raise Revert |