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