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