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