zk_evm 0.153.12

ZKsync out-of-circuit EraEVM implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
use super::*;

use crate::zkevm_opcode_defs::definitions::far_call::*;

use crate::zkevm_opcode_defs::INITIAL_SP_ON_FAR_CALL;
use zk_evm_abstractions::aux::*;
use zk_evm_abstractions::queries::LogQuery;
use zk_evm_abstractions::zkevm_opcode_defs::{BlobSha256Format, VersionedHashLen32};

use crate::zkevm_opcode_defs::bitflags::bitflags;

pub const FORCED_ERGS_FOR_MSG_VALUE_SIMULATOR: bool = false;

bitflags! {
    pub struct FarCallExceptionFlags: u64 {
        const INPUT_IS_NOT_POINTER_WHEN_EXPECTED = 1u64 << 0;
        const INVALID_CODE_HASH_FORMAT = 1u64 << 1;
        const NOT_ENOUGH_ERGS_TO_DECOMMIT = 1u64 << 2;
        const NOT_ENOUGH_ERGS_TO_GROW_MEMORY = 1u64 << 3;
        const MALFORMED_ABI_QUASI_POINTER = 1u64 << 4;
        const CALL_IN_NOW_CONSTRUCTED_SYSTEM_CONTRACT = 1u64 << 5;
        const NOT_ENOUGH_ERGS_FOR_EXTRA_FAR_CALL_COSTS = 1u64 << 6;
        const CALL_TO_UNREACHABLE_ADDRESS = 1u64 << 7;
        const INPUT_IS_POINTER_WHEN_NOT_EXPECTED = 1u64 << 8;
    }
}

use crate::zkevm_opcode_defs::{FarCallABI, FarCallOpcode, Opcode};

impl<const N: usize, E: VmEncodingMode<N>> DecodedOpcode<N, E> {
    pub fn far_call_opcode_apply<
        S: zk_evm_abstractions::vm::Storage,
        M: zk_evm_abstractions::vm::Memory,
        EV: zk_evm_abstractions::vm::EventSink,
        PP: zk_evm_abstractions::vm::PrecompilesProcessor,
        DP: zk_evm_abstractions::vm::DecommittmentProcessor,
        WT: crate::witness_trace::VmWitnessTracer<N, E>,
    >(
        &self,
        vm_state: &mut VmState<S, M, EV, PP, DP, WT, N, E>,
        prestate: PreState<N, E>,
    ) -> anyhow::Result<()> {
        let PreState {
            src0,
            src1,
            new_pc,
            is_kernel_mode,
            ..
        } = prestate;
        let inner_variant = match self.variant.opcode {
            Opcode::FarCall(inner) => inner,
            _ => unreachable!(),
        };

        let PrimitiveValue {
            value: abi_src,
            is_pointer: abi_src_is_ptr,
        } = src0;
        let PrimitiveValue {
            value: call_destination_value,
            is_pointer: _,
        } = src1;

        // any call resets flags
        vm_state.reset_flags();

        let is_static_call = self.variant.flags[FAR_CALL_STATIC_FLAG_IDX];
        let is_call_shard = self.variant.flags[FAR_CALL_SHARD_FLAG_IDX];

        let exception_handler_location = self.imm_0;
        // binary interface of parameters passing

        let called_address = u256_to_address_unchecked(&call_destination_value);
        let called_address_as_u256 = call_destination_value & *U256_TO_ADDRESS_MASK;
        let dst_is_kernel = address_is_kernel(&called_address);

        // ergs, shard_id and calldata
        let mut far_call_abi = FarCallABI::from_u256(abi_src);

        // convert ergs
        far_call_abi.ergs_passed = if let Some(non_overflowing) =
            far_call_abi.ergs_passed.checked_mul(
                zkevm_opcode_defs::system_params::INTERNAL_ERGS_TO_VISIBLE_ERGS_CONVERSION_CONSTANT,
            ) {
            non_overflowing
        } else {
            u32::MAX
        };

        // we ignore extra features if not in kernel
        far_call_abi.constructor_call = far_call_abi.constructor_call & is_kernel_mode;
        far_call_abi.to_system = far_call_abi.to_system & dst_is_kernel;

        let current_stack = vm_state.local_state.callstack.get_current_stack();

        // read for case of delegatecall
        let current_address = current_stack.this_address;
        let current_msg_sender = current_stack.msg_sender;
        let current_base_page = current_stack.base_memory_page;
        let caller_shard_id = current_stack.this_shard_id;
        let remaining_ergs = current_stack.ergs_remaining;
        let current_context_u128 = current_stack.context_u128_value;

        #[allow(dropping_references)]
        drop(current_stack);

        let timestamp_for_storage_read = vm_state.timestamp_for_first_decommit_or_precompile_read();
        let tx_number_in_block = vm_state.local_state.tx_number_in_block;

        // we read code from some shard ID
        let new_code_shard_id = if is_call_shard {
            far_call_abi.shard_id
        } else {
            caller_shard_id
        };

        // but for contract awareness purposes it may still see an old one (one of the caller)
        let new_this_shard_id = if inner_variant == FarCallOpcode::Delegate {
            caller_shard_id
        } else {
            new_code_shard_id
        };

        let new_base_memory_page = vm_state.new_base_memory_page_on_call();

        let call_to_evm_emulator;
        let code_version_byte;

        // NOTE: our far-call MUST take ergs to cover storage read, but we also have a contribution
        // that depends on the actual code length, so we work with it here
        let (
            mapped_code_page,
            ergs_after_code_read_and_exceptions_resolution,
            extra_ergs_from_caller_to_callee,
            callee_stipend,
        ) = {
            let (code_hash, call_to_unreachable) = if new_code_shard_id != 0
                && !vm_state.block_properties.zkporter_is_available
            {
                // we do NOT mask it into default AA here
                // and for now formally jump to the page containing zeroes

                (U256::zero(), true)
            } else {
                let partial_query = LogQuery {
                    timestamp: timestamp_for_storage_read,
                    tx_number_in_block,
                    aux_byte: STORAGE_AUX_BYTE,
                    shard_id: new_code_shard_id,
                    address: *DEPLOYER_SYSTEM_CONTRACT_ADDRESS,
                    key: called_address_as_u256,
                    read_value: U256::zero(),
                    written_value: U256::zero(),
                    rw_flag: false,
                    rollback: false,
                    is_service: false,
                };
                let (query, _) = vm_state
                    .access_storage(vm_state.local_state.monotonic_cycle_counter, partial_query);

                let code_hash_from_storage = query.read_value;

                (code_hash_from_storage, false)
            };

            let mut exceptions = FarCallExceptionFlags::empty();
            if call_to_unreachable == true {
                exceptions.set(FarCallExceptionFlags::CALL_TO_UNREACHABLE_ADDRESS, true);
            }

            // now we handle potential exceptions

            use crate::zkevm_opcode_defs::*;

            let bytecode_hash_is_empty = code_hash == U256::zero();

            let mut buffer = [0u8; 32];
            code_hash.to_big_endian(&mut buffer);

            code_version_byte = buffer[0];

            let is_valid_as_bytecode_hash = ContractCodeSha256Format::is_valid(&buffer);
            let is_valid_as_blob_hash = BlobSha256Format::is_valid(&buffer);

            let mut mask_to_default_aa = false;

            let can_call_code_without_masking = if is_valid_as_bytecode_hash {
                let is_code_at_rest = ContractCodeSha256Format::is_code_at_rest_if_valid(&buffer);
                let is_constructed = ContractCodeSha256Format::is_in_construction_if_valid(&buffer);

                let can_call_at_rest = !far_call_abi.constructor_call && is_code_at_rest;
                let can_call_by_constructor = far_call_abi.constructor_call && is_constructed;

                let can_call_code_without_masking = can_call_at_rest || can_call_by_constructor;
                if can_call_code_without_masking == true {
                    true
                } else {
                    // calling mode is unknown, so it's most likely a normal
                    // call to contract that is still created
                    if dst_is_kernel == false {
                        mask_to_default_aa = true;
                    } else {
                        exceptions.set(
                            FarCallExceptionFlags::CALL_IN_NOW_CONSTRUCTED_SYSTEM_CONTRACT,
                            true,
                        );
                    }

                    false
                }
            } else {
                false
            };

            let can_call_evm_emulator = if is_valid_as_blob_hash {
                let is_code_at_rest = BlobSha256Format::is_code_at_rest_if_valid(&buffer);
                let is_constructed = BlobSha256Format::is_in_construction_if_valid(&buffer);

                let can_call_at_rest = !far_call_abi.constructor_call && is_code_at_rest;
                let can_call_by_constructor = far_call_abi.constructor_call && is_constructed;

                let can_call_code_without_masking = can_call_at_rest || can_call_by_constructor;
                if can_call_code_without_masking == true {
                    true
                } else {
                    // calling mode is unknown, so it's most likely a normal
                    // call to contract that is still created
                    if dst_is_kernel == false {
                        mask_to_default_aa = true;
                    } else {
                        exceptions.set(FarCallExceptionFlags::INVALID_CODE_HASH_FORMAT, true);
                    }

                    false
                }
            } else {
                false
            };

            call_to_evm_emulator = can_call_evm_emulator;

            if bytecode_hash_is_empty {
                if dst_is_kernel == false {
                    mask_to_default_aa = true;
                } else {
                    exceptions.set(FarCallExceptionFlags::INVALID_CODE_HASH_FORMAT, true);
                }
            }

            assert!(
                (mask_to_default_aa as u64)
                    + (can_call_evm_emulator as u64)
                    + (can_call_code_without_masking as u64)
                    < 2
            );

            let unknown_hash = mask_to_default_aa == false
                && can_call_evm_emulator == false
                && can_call_code_without_masking == false;
            if unknown_hash {
                exceptions.set(FarCallExceptionFlags::INVALID_CODE_HASH_FORMAT, true);
            }

            // now let's check if code format "makes sense"
            let (header, normalized_preimage, code_length_in_words) = {
                if can_call_code_without_masking {
                    // masking is not needed
                } else if can_call_evm_emulator {
                    // overwrite buffer with evm emulator bytecode hash
                    vm_state
                        .block_properties
                        .evm_emulator_code_hash
                        .to_big_endian(&mut buffer);
                } else if mask_to_default_aa {
                    // overwrite buffer with default AA code hash
                    vm_state
                        .block_properties
                        .default_aa_code_hash
                        .to_big_endian(&mut buffer);
                } else {
                    assert!(exceptions.is_empty() == false);
                }

                if exceptions.is_empty() {
                    assert!(
                        can_call_code_without_masking
                            || can_call_evm_emulator
                            || mask_to_default_aa
                    );
                    // true values
                    let length_in_words =
                        ContractCodeSha256Format::code_length_in_bytes32_words(&buffer);
                    let (header, normalized_preimage) =
                        ContractCodeSha256Format::normalize_for_decommitment(&buffer);

                    (header, normalized_preimage, length_in_words)
                } else {
                    (
                        VersionedHashHeader::default(),
                        VersionedHashNormalizedPreimage::default(),
                        0u16,
                    )
                }
            };

            // we also use code hash as an exception hatch here
            if far_call_abi.forwarding_mode == FarCallForwardPageType::ForwardFatPointer {
                if abi_src_is_ptr == false {
                    exceptions.set(
                        FarCallExceptionFlags::INPUT_IS_NOT_POINTER_WHEN_EXPECTED,
                        true,
                    );
                }
            } else {
                if abi_src_is_ptr {
                    // there is no reasonable case to try to re-interpret pointer
                    // as integer here
                    exceptions.set(
                        FarCallExceptionFlags::INPUT_IS_POINTER_WHEN_NOT_EXPECTED,
                        true,
                    );
                }
            }

            // validate that fat pointer (one a future one) we formed is somewhat valid
            let validate_as_fresh =
                far_call_abi.forwarding_mode != FarCallForwardPageType::ForwardFatPointer;

            // NOTE: one can not properly address a range [2^32 - 32..2^32] here, but we never care in practice about this case
            // as one can not ever pay to grow memory to such extent

            let pointer_validation_exceptions = far_call_abi
                .memory_quasi_fat_pointer
                .validate(validate_as_fresh);

            if pointer_validation_exceptions.is_empty() == false {
                // pointer is malformed
                exceptions.set(FarCallExceptionFlags::MALFORMED_ABI_QUASI_POINTER, true);
            }
            // this captures the case of empty slice
            if far_call_abi.memory_quasi_fat_pointer.validate_as_slice() == false {
                exceptions.set(FarCallExceptionFlags::MALFORMED_ABI_QUASI_POINTER, true);
            }

            // these modifications we can do already as all pointer formal validity related things are done
            match far_call_abi.forwarding_mode {
                FarCallForwardPageType::ForwardFatPointer => {
                    // We can formally shrink the pointer
                    // If it was malformed then we masked and overflows can not happen
                    let new_start = far_call_abi
                        .memory_quasi_fat_pointer
                        .start
                        .wrapping_add(far_call_abi.memory_quasi_fat_pointer.offset);
                    let new_length = far_call_abi
                        .memory_quasi_fat_pointer
                        .length
                        .wrapping_sub(far_call_abi.memory_quasi_fat_pointer.offset);

                    far_call_abi.memory_quasi_fat_pointer.start = new_start;
                    far_call_abi.memory_quasi_fat_pointer.length = new_length;
                    far_call_abi.memory_quasi_fat_pointer.offset = 0;
                }
                FarCallForwardPageType::UseHeap => {
                    let owned_page = heap_page_from_base(current_base_page).0;

                    far_call_abi.memory_quasi_fat_pointer.memory_page = owned_page;
                }
                FarCallForwardPageType::UseAuxHeap => {
                    let owned_page = aux_heap_page_from_base(current_base_page).0;

                    far_call_abi.memory_quasi_fat_pointer.memory_page = owned_page;
                }
            };

            // we mask out fat pointer based on:
            // - invalid code hash format
            // - call yet constructed kernel
            // - not fat pointer when expected
            // - invalid slice structure in ABI
            if exceptions.is_empty() == false {
                far_call_abi.memory_quasi_fat_pointer = FatPointer::empty();
                // even though we will not pay for memory resize,
                // we do not care
            }

            let current_stack_mut = vm_state.local_state.callstack.get_current_stack_mut();

            // potentially pay for memory growth
            let remaining_ergs_after_growth = if vm_state.version >= Version::Version27 {
                match far_call_abi.forwarding_mode {
                    a @ FarCallForwardPageType::UseHeap
                    | a @ FarCallForwardPageType::UseAuxHeap => {
                        let upper_bound = if pointer_validation_exceptions
                            .contains(FatPointerValidationException::DEREF_BEYOND_HEAP_RANGE)
                        {
                            u32::MAX
                        } else {
                            far_call_abi.memory_quasi_fat_pointer.start
                                + far_call_abi.memory_quasi_fat_pointer.length
                        };

                        let current_bound = if a == FarCallForwardPageType::UseHeap {
                            current_stack_mut.heap_bound
                        } else if a == FarCallForwardPageType::UseAuxHeap {
                            current_stack_mut.aux_heap_bound
                        } else {
                            unreachable!();
                        };

                        let growth_cost = upper_bound.saturating_sub(current_bound);

                        if remaining_ergs >= growth_cost {
                            if upper_bound > current_bound {
                                if a == FarCallForwardPageType::UseHeap {
                                    current_stack_mut.heap_bound = upper_bound;
                                } else if a == FarCallForwardPageType::UseAuxHeap {
                                    current_stack_mut.aux_heap_bound = upper_bound;
                                } else {
                                    unreachable!();
                                }
                            }

                            remaining_ergs - growth_cost
                        } else {
                            exceptions
                                .set(FarCallExceptionFlags::NOT_ENOUGH_ERGS_TO_GROW_MEMORY, true);
                            // we do not need to mask fat pointer, as we will jump to the page number 0,
                            // that can not even read it

                            0
                        }
                    }
                    FarCallForwardPageType::ForwardFatPointer => remaining_ergs,
                }
            } else {
                let memory_growth_in_bytes = match far_call_abi.forwarding_mode {
                    a @ FarCallForwardPageType::UseHeap
                    | a @ FarCallForwardPageType::UseAuxHeap => {
                        // pointer is already validated, so we do not need to check that start + length do not overflow
                        let mut upper_bound = far_call_abi.memory_quasi_fat_pointer.start
                            + far_call_abi.memory_quasi_fat_pointer.length;

                        let penalize_out_of_bounds_growth = pointer_validation_exceptions
                            .contains(FatPointerValidationException::DEREF_BEYOND_HEAP_RANGE);
                        if penalize_out_of_bounds_growth {
                            upper_bound = u32::MAX;
                        }

                        let current_bound = if a == FarCallForwardPageType::UseHeap {
                            current_stack_mut.heap_bound
                        } else if a == FarCallForwardPageType::UseAuxHeap {
                            current_stack_mut.aux_heap_bound
                        } else {
                            unreachable!();
                        };
                        let (mut diff, uf) = upper_bound.overflowing_sub(current_bound);
                        if uf {
                            // heap bound is already beyond what we pass
                            diff = 0u32;
                        } else {
                            // save new upper bound in context.
                            // Note that we are ok so save even penalizing upper bound because we will burn
                            // all the ergs in this frame anyway, and no further resizes are possible
                            if a == FarCallForwardPageType::UseHeap {
                                current_stack_mut.heap_bound = upper_bound;
                            } else if a == FarCallForwardPageType::UseAuxHeap {
                                current_stack_mut.aux_heap_bound = upper_bound;
                            } else {
                                unreachable!();
                            }
                        }

                        diff
                    }
                    FarCallForwardPageType::ForwardFatPointer => 0u32,
                };

                // MEMORY_GROWTH_ERGS_PER_BYTE is always 1
                let cost_of_memory_growth = memory_growth_in_bytes
                    .wrapping_mul(zkevm_opcode_defs::MEMORY_GROWTH_ERGS_PER_BYTE);

                if remaining_ergs >= cost_of_memory_growth {
                    remaining_ergs - cost_of_memory_growth
                } else {
                    exceptions.set(FarCallExceptionFlags::NOT_ENOUGH_ERGS_TO_GROW_MEMORY, true);
                    // we do not need to mask fat pointer, as we will jump to the page number 0,
                    // that can not even read it

                    0
                }
            };

            let (callee_stipend, mut extra_ergs_from_caller_to_callee) =
                get_stipend_and_extra_cost(&called_address, far_call_abi.to_system);

            let remaining_ergs_of_caller_frame =
                if remaining_ergs_after_growth >= extra_ergs_from_caller_to_callee {
                    remaining_ergs_after_growth - extra_ergs_from_caller_to_callee
                } else {
                    exceptions.set(
                        FarCallExceptionFlags::NOT_ENOUGH_ERGS_FOR_EXTRA_FAR_CALL_COSTS,
                        true,
                    );
                    // if tried to take and failed, but should not add it later on in this case
                    extra_ergs_from_caller_to_callee = 0;

                    0
                };

            let (code_memory_page, remaining_ergs_after_decommittment) = if exceptions.is_empty()
                == false
            {
                vm_state.set_shorthand_panic();

                // we also do not return back cost of decommittment as it was subtracted
                (MemoryPage(UNMAPPED_PAGE), remaining_ergs_of_caller_frame)
            } else {
                assert!(
                    header.0 != [0u8; 4],
                    "zero header must be masked to default aa or panic",
                );
                // we mask instead of branching
                let default_cost_of_decommittment =
                    zkevm_opcode_defs::ERGS_PER_CODE_WORD_DECOMMITTMENT
                        * (code_length_in_words as u32);
                // prepare query
                let timestamp_for_decommit =
                    vm_state.timestamp_for_first_decommit_or_precompile_read();
                let memory_page_candidate_for_code_decommittment =
                    code_page_candidate_from_base(new_base_memory_page);
                let prepared_decommmit_query = vm_state.prepare_to_decommit(
                    vm_state.local_state.monotonic_cycle_counter,
                    header,
                    normalized_preimage,
                    memory_page_candidate_for_code_decommittment,
                    timestamp_for_decommit,
                )?;
                let cost_of_decommittment = if prepared_decommmit_query.is_fresh {
                    default_cost_of_decommittment
                } else {
                    0
                };

                let remaining_ergs_after_decommittment =
                    if remaining_ergs_of_caller_frame >= cost_of_decommittment {
                        remaining_ergs_of_caller_frame - cost_of_decommittment
                    } else {
                        exceptions.set(FarCallExceptionFlags::NOT_ENOUGH_ERGS_TO_DECOMMIT, true);
                        remaining_ergs_of_caller_frame // do not burn, as it's irrelevant - we just will not perform a decommittment and call
                    };

                let memory_page = if exceptions.is_empty() {
                    vm_state.execute_decommit(
                        vm_state.local_state.monotonic_cycle_counter,
                        prepared_decommmit_query,
                    )?;

                    prepared_decommmit_query.memory_page
                } else {
                    vm_state.set_shorthand_panic();
                    MemoryPage(UNMAPPED_PAGE)
                };

                (memory_page, remaining_ergs_after_decommittment)
            };

            (
                code_memory_page,
                remaining_ergs_after_decommittment,
                extra_ergs_from_caller_to_callee,
                callee_stipend,
            )
        };

        // we have taken everything that we want from caller and now can try to pass to callee

        // resolve passed ergs, by using a value afte decommittment cost is taken
        let remaining_ergs_to_pass = ergs_after_code_read_and_exceptions_resolution;
        let max_passable = (remaining_ergs_to_pass / 64) * 63; // so callee will always have some
        let leftover = remaining_ergs_to_pass - max_passable;
        // for exception handling
        let (passed_ergs, remaining_ergs_for_this_context) = {
            let (remaining_from_max_passable, uf) =
                max_passable.overflowing_sub(far_call_abi.ergs_passed);
            if uf {
                // pass max(passable, want to pass)
                (max_passable, leftover)
            } else {
                (
                    far_call_abi.ergs_passed,
                    leftover + remaining_from_max_passable,
                )
            }
        };

        // can not overflow
        let passed_ergs = passed_ergs.wrapping_add(extra_ergs_from_caller_to_callee);
        // this one is checked
        let passed_ergs = passed_ergs
            .checked_add(callee_stipend)
            .expect("stipends must never overflow");

        // update current ergs and PC
        vm_state
            .local_state
            .callstack
            .get_current_stack_mut()
            .ergs_remaining = remaining_ergs_for_this_context;
        vm_state.local_state.callstack.get_current_stack_mut().pc = new_pc;

        let current_stack = vm_state.local_state.callstack.get_current_stack();

        // compute if call is static either by modifier or
        let new_context_is_static = current_stack.is_static | is_static_call;

        // no matter if we did execute a query or not, we need to save context at worst
        vm_state.increment_memory_pages_on_call();

        // read address for mimic_call
        let implicit_reg =
            &vm_state.local_state.registers[CALL_IMPLICIT_PARAMETER_REG_IDX as usize];
        let address_from_implicit_reg = u256_to_address_unchecked(&implicit_reg.value);

        let (address_for_next, msg_sender_for_next) = match inner_variant {
            FarCallOpcode::Normal => {
                // we set that caller of next == this
                (called_address, current_address)
            }
            FarCallOpcode::Delegate => {
                // save current address for context purposes
                (current_address, current_msg_sender)
            }
            FarCallOpcode::Mimic => {
                // we pretent to be calling from some address
                (called_address, address_from_implicit_reg)
            }
        };
        let code_address_for_next = called_address;

        let context_u128_for_next = match inner_variant {
            FarCallOpcode::Normal | FarCallOpcode::Mimic => {
                // we set that caller of next == this
                vm_state.local_state.context_u128_register
            }
            FarCallOpcode::Delegate => {
                // save current address for context purposes
                current_context_u128
            }
        };

        let is_static_to_set = if call_to_evm_emulator {
            false
        } else {
            new_context_is_static
        };

        // NOTE: this one decides based on what next frame will be, and not just on the formal
        // call target

        let memory_stipend_userspace = if code_version_byte == BlobSha256Format::VERSION_BYTE {
            zkevm_opcode_defs::system_params::NEW_EVM_FRAME_MEMORY_STIPEND
        } else {
            zkevm_opcode_defs::system_params::NEW_FRAME_MEMORY_STIPEND
        };
        let memory_stipend = if address_is_kernel(&address_for_next) {
            zkevm_opcode_defs::system_params::NEW_KERNEL_FRAME_MEMORY_STIPEND
        } else {
            memory_stipend_userspace
        };

        let new_stack = CallStackEntry {
            this_address: address_for_next,
            msg_sender: msg_sender_for_next,
            code_address: code_address_for_next,
            base_memory_page: new_base_memory_page,
            code_page: mapped_code_page,
            sp: E::PcOrImm::from_u64_clipped(INITIAL_SP_ON_FAR_CALL),
            pc: E::PcOrImm::from_u64_clipped(0u64),
            exception_handler_location,
            ergs_remaining: passed_ergs,
            this_shard_id: new_this_shard_id,
            caller_shard_id,
            code_shard_id: new_code_shard_id,
            is_static: is_static_to_set,
            is_local_frame: false,
            context_u128_value: context_u128_for_next,
            heap_bound: memory_stipend,
            aux_heap_bound: memory_stipend,
            total_pubdata_spent: PubdataCost(0i32),
            stipend: callee_stipend,
        };

        // zero out the temporary register if it was not trivial
        vm_state.local_state.context_u128_register = 0;

        // perform some extra steps to ensure that our rollbacks are properly written and saved
        // both in storage and for witness
        vm_state.start_frame(vm_state.local_state.monotonic_cycle_counter, new_stack);

        vm_state.memory.start_global_frame(
            current_base_page,
            new_base_memory_page,
            far_call_abi.memory_quasi_fat_pointer,
            Timestamp(vm_state.local_state.timestamp),
        );

        // write down calldata information

        let r1_value = PrimitiveValue {
            value: far_call_abi.memory_quasi_fat_pointer.to_u256(),
            is_pointer: true,
        };
        vm_state.local_state.registers[CALL_IMPLICIT_CALLDATA_FAT_PTR_REGISTER as usize] = r1_value;

        let mut r2_value = U256::zero();
        if far_call_abi.constructor_call {
            r2_value.0[0] |= 1u64;
        }
        if far_call_abi.to_system {
            r2_value.0[0] |= 1u64 << 1;
        }
        if call_to_evm_emulator {
            r2_value.0[0] |= (new_context_is_static as u64) << 2;
        }

        vm_state.local_state.registers[CALL_IMPLICIT_CONSTRUCTOR_MARKER_REGISTER as usize] =
            PrimitiveValue {
                value: r2_value,
                is_pointer: false,
            };

        if far_call_abi.to_system == false {
            for reg_idx in CALL_SYSTEM_ABI_REGISTERS {
                // if it's not a call to the system then we zero out those registers
                vm_state.local_state.registers[reg_idx as usize] = PrimitiveValue::empty();
            }
        } else {
            for reg_idx in CALL_SYSTEM_ABI_REGISTERS {
                // remove "ptr" markers
                vm_state.local_state.registers[reg_idx as usize].is_pointer = false;
            }
        }

        // ALL other registers are zeroed out!
        for reg_idx in CALL_RESERVED_RANGE {
            vm_state.local_state.registers[reg_idx as usize] = PrimitiveValue::empty();
        }
        vm_state.local_state.registers[CALL_IMPLICIT_PARAMETER_REG_IDX as usize] =
            PrimitiveValue::empty();

        Ok(())
    }
}