ethereum.forks.istanbul.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 | """ |
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 | parent_evm=evm, |
301 | ) |
302 | child_evm = process_message(child_message) |
303 | |
304 | if child_evm.error: |
305 | incorporate_child_on_error(evm, child_evm) |
306 | evm.return_data = child_evm.output |
307 | push(evm.stack, U256(0)) |
308 | else: |
309 | incorporate_child_on_success(evm, child_evm) |
310 | evm.return_data = child_evm.output |
311 | push(evm.stack, U256(1)) |
312 | |
313 | actual_output_size = min(memory_output_size, U256(len(child_evm.output))) |
314 | memory_write( |
315 | evm.memory, |
316 | memory_output_start_position, |
317 | child_evm.output[:actual_output_size], |
318 | ) |
call
Message-call into an account.
Parameters
evm : The current EVM frame.
def call(evm: Evm) -> None:
322 | """ |
---|---|
323 | Message-call into an account. |
324 |
|
325 | Parameters |
326 | ---------- |
327 | evm : |
328 | The current EVM frame. |
329 |
|
330 | """ |
331 | # STACK |
332 | gas = Uint(pop(evm.stack)) |
333 | to = to_address_masked(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 | code_address = to |
350 | |
351 | create_gas_cost = GAS_NEW_ACCOUNT |
352 | if value == 0 or is_account_alive(evm.message.block_env.state, to): |
353 | create_gas_cost = Uint(0) |
354 | transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE |
355 | message_call_gas = calculate_message_call_gas( |
356 | value, |
357 | gas, |
358 | Uint(evm.gas_left), |
359 | extend_memory.cost, |
360 | GAS_CALL + create_gas_cost + transfer_gas_cost, |
361 | ) |
362 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
363 | if evm.message.is_static and value != U256(0): |
364 | raise WriteInStaticContext |
365 | evm.memory += b"\x00" * extend_memory.expand_by |
366 | sender_balance = get_account( |
367 | evm.message.block_env.state, evm.message.current_target |
368 | ).balance |
369 | if sender_balance < value: |
370 | push(evm.stack, U256(0)) |
371 | evm.return_data = b"" |
372 | evm.gas_left += message_call_gas.sub_call |
373 | else: |
374 | generic_call( |
375 | evm, |
376 | message_call_gas.sub_call, |
377 | value, |
378 | evm.message.current_target, |
379 | to, |
380 | code_address, |
381 | True, |
382 | False, |
383 | memory_input_start_position, |
384 | memory_input_size, |
385 | memory_output_start_position, |
386 | memory_output_size, |
387 | ) |
388 | |
389 | # PROGRAM COUNTER |
390 | 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:
394 | """ |
---|---|
395 | Message-call into this account with alternative account’s code. |
396 |
|
397 | Parameters |
398 | ---------- |
399 | evm : |
400 | The current EVM frame. |
401 |
|
402 | """ |
403 | # STACK |
404 | gas = Uint(pop(evm.stack)) |
405 | code_address = to_address_masked(pop(evm.stack)) |
406 | value = pop(evm.stack) |
407 | memory_input_start_position = pop(evm.stack) |
408 | memory_input_size = pop(evm.stack) |
409 | memory_output_start_position = pop(evm.stack) |
410 | memory_output_size = pop(evm.stack) |
411 | |
412 | # GAS |
413 | to = evm.message.current_target |
414 | |
415 | extend_memory = calculate_gas_extend_memory( |
416 | evm.memory, |
417 | [ |
418 | (memory_input_start_position, memory_input_size), |
419 | (memory_output_start_position, memory_output_size), |
420 | ], |
421 | ) |
422 | transfer_gas_cost = Uint(0) if value == 0 else GAS_CALL_VALUE |
423 | message_call_gas = calculate_message_call_gas( |
424 | value, |
425 | gas, |
426 | Uint(evm.gas_left), |
427 | extend_memory.cost, |
428 | GAS_CALL + transfer_gas_cost, |
429 | ) |
430 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
431 | |
432 | # OPERATION |
433 | evm.memory += b"\x00" * extend_memory.expand_by |
434 | sender_balance = get_account( |
435 | evm.message.block_env.state, evm.message.current_target |
436 | ).balance |
437 | if sender_balance < value: |
438 | push(evm.stack, U256(0)) |
439 | evm.return_data = b"" |
440 | evm.gas_left += message_call_gas.sub_call |
441 | else: |
442 | generic_call( |
443 | evm, |
444 | message_call_gas.sub_call, |
445 | value, |
446 | evm.message.current_target, |
447 | to, |
448 | code_address, |
449 | True, |
450 | False, |
451 | memory_input_start_position, |
452 | memory_input_size, |
453 | memory_output_start_position, |
454 | memory_output_size, |
455 | ) |
456 | |
457 | # PROGRAM COUNTER |
458 | evm.pc += Uint(1) |
selfdestruct
Halt execution and register account for later deletion.
Parameters
evm : The current EVM frame.
def selfdestruct(evm: Evm) -> None:
462 | """ |
---|---|
463 | Halt execution and register account for later deletion. |
464 |
|
465 | Parameters |
466 | ---------- |
467 | evm : |
468 | The current EVM frame. |
469 |
|
470 | """ |
471 | # STACK |
472 | beneficiary = to_address_masked(pop(evm.stack)) |
473 | |
474 | # GAS |
475 | gas_cost = GAS_SELF_DESTRUCT |
476 | if ( |
477 | not is_account_alive(evm.message.block_env.state, beneficiary) |
478 | and get_account( |
479 | evm.message.block_env.state, evm.message.current_target |
480 | ).balance |
481 | != 0 |
482 | ): |
483 | gas_cost += GAS_SELF_DESTRUCT_NEW_ACCOUNT |
484 | |
485 | originator = evm.message.current_target |
486 | |
487 | refunded_accounts = evm.accounts_to_delete |
488 | parent_evm = evm.message.parent_evm |
489 | while parent_evm is not None: |
490 | refunded_accounts.update(parent_evm.accounts_to_delete) |
491 | parent_evm = parent_evm.message.parent_evm |
492 | |
493 | if originator not in refunded_accounts: |
494 | evm.refund_counter += REFUND_SELF_DESTRUCT |
495 | |
496 | charge_gas(evm, gas_cost) |
497 | if evm.message.is_static: |
498 | raise WriteInStaticContext |
499 | |
500 | originator = evm.message.current_target |
501 | beneficiary_balance = get_account( |
502 | evm.message.block_env.state, beneficiary |
503 | ).balance |
504 | originator_balance = get_account( |
505 | evm.message.block_env.state, originator |
506 | ).balance |
507 | |
508 | # First Transfer to beneficiary |
509 | set_account_balance( |
510 | evm.message.block_env.state, |
511 | beneficiary, |
512 | beneficiary_balance + originator_balance, |
513 | ) |
514 | # Next, Zero the balance of the address being deleted (must come after |
515 | # sending to beneficiary in case the contract named itself as the |
516 | # beneficiary). |
517 | set_account_balance(evm.message.block_env.state, originator, U256(0)) |
518 | |
519 | # register account for deletion |
520 | evm.accounts_to_delete.add(originator) |
521 | |
522 | # mark beneficiary as touched |
523 | if account_exists_and_is_empty(evm.message.block_env.state, beneficiary): |
524 | evm.touched_accounts.add(beneficiary) |
525 | |
526 | # HALT the execution |
527 | evm.running = False |
528 | |
529 | # PROGRAM COUNTER |
530 | pass |
delegatecall
Message-call into an account.
Parameters
evm : The current EVM frame.
def delegatecall(evm: Evm) -> None:
534 | """ |
---|---|
535 | Message-call into an account. |
536 |
|
537 | Parameters |
538 | ---------- |
539 | evm : |
540 | The current EVM frame. |
541 |
|
542 | """ |
543 | # STACK |
544 | gas = Uint(pop(evm.stack)) |
545 | code_address = to_address_masked(pop(evm.stack)) |
546 | memory_input_start_position = pop(evm.stack) |
547 | memory_input_size = pop(evm.stack) |
548 | memory_output_start_position = pop(evm.stack) |
549 | memory_output_size = pop(evm.stack) |
550 | |
551 | # GAS |
552 | extend_memory = calculate_gas_extend_memory( |
553 | evm.memory, |
554 | [ |
555 | (memory_input_start_position, memory_input_size), |
556 | (memory_output_start_position, memory_output_size), |
557 | ], |
558 | ) |
559 | message_call_gas = calculate_message_call_gas( |
560 | U256(0), gas, Uint(evm.gas_left), extend_memory.cost, GAS_CALL |
561 | ) |
562 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
563 | |
564 | # OPERATION |
565 | evm.memory += b"\x00" * extend_memory.expand_by |
566 | generic_call( |
567 | evm, |
568 | message_call_gas.sub_call, |
569 | evm.message.value, |
570 | evm.message.caller, |
571 | evm.message.current_target, |
572 | code_address, |
573 | False, |
574 | False, |
575 | memory_input_start_position, |
576 | memory_input_size, |
577 | memory_output_start_position, |
578 | memory_output_size, |
579 | ) |
580 | |
581 | # PROGRAM COUNTER |
582 | evm.pc += Uint(1) |
staticcall
Message-call into an account.
Parameters
evm : The current EVM frame.
def staticcall(evm: Evm) -> None:
586 | """ |
---|---|
587 | Message-call into an account. |
588 |
|
589 | Parameters |
590 | ---------- |
591 | evm : |
592 | The current EVM frame. |
593 |
|
594 | """ |
595 | # STACK |
596 | gas = Uint(pop(evm.stack)) |
597 | to = to_address_masked(pop(evm.stack)) |
598 | memory_input_start_position = pop(evm.stack) |
599 | memory_input_size = pop(evm.stack) |
600 | memory_output_start_position = pop(evm.stack) |
601 | memory_output_size = pop(evm.stack) |
602 | |
603 | # GAS |
604 | extend_memory = calculate_gas_extend_memory( |
605 | evm.memory, |
606 | [ |
607 | (memory_input_start_position, memory_input_size), |
608 | (memory_output_start_position, memory_output_size), |
609 | ], |
610 | ) |
611 | |
612 | code_address = to |
613 | |
614 | message_call_gas = calculate_message_call_gas( |
615 | U256(0), |
616 | gas, |
617 | Uint(evm.gas_left), |
618 | extend_memory.cost, |
619 | GAS_CALL, |
620 | ) |
621 | charge_gas(evm, message_call_gas.cost + extend_memory.cost) |
622 | |
623 | # OPERATION |
624 | evm.memory += b"\x00" * extend_memory.expand_by |
625 | generic_call( |
626 | evm, |
627 | message_call_gas.sub_call, |
628 | U256(0), |
629 | evm.message.current_target, |
630 | to, |
631 | code_address, |
632 | True, |
633 | True, |
634 | memory_input_start_position, |
635 | memory_input_size, |
636 | memory_output_start_position, |
637 | memory_output_size, |
638 | ) |
639 | |
640 | # PROGRAM COUNTER |
641 | 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:
645 | """ |
---|---|
646 | Stop execution and revert state changes, without consuming all provided gas |
647 | and also has the ability to return a reason. |
648 |
|
649 | Parameters |
650 | ---------- |
651 | evm : |
652 | The current EVM frame. |
653 |
|
654 | """ |
655 | # STACK |
656 | memory_start_index = pop(evm.stack) |
657 | size = pop(evm.stack) |
658 | |
659 | # GAS |
660 | extend_memory = calculate_gas_extend_memory( |
661 | evm.memory, [(memory_start_index, size)] |
662 | ) |
663 | |
664 | charge_gas(evm, extend_memory.cost) |
665 | |
666 | # OPERATION |
667 | evm.memory += b"\x00" * extend_memory.expand_by |
668 | output = memory_read_bytes(evm.memory, memory_start_index, size) |
669 | evm.output = Bytes(output) |
670 | raise Revert |