fn update_ancestor_versions<P: TreeParams>(&mut self, version: u64) {
        let mut indices: Vec<_> = self.leaves.keys().copied().collect();
        indices.sort_unstable();

        for internal_level in self.internal.iter_mut().rev() {
            let mut prev_index = None;
            indices = indices
                .into_iter()
                .filter_map(|idx| {
                    let parent_idx = idx >> P::INTERNAL_NODE_DEPTH;
                    let parent = internal_level.get_mut(&parent_idx).unwrap();
                    parent
                        .child_mut((idx % u64::from(max_node_children::<P>())) as usize)
                        .version = version;

                    if prev_index == Some(parent_idx) {
                        None
                    } else {
                        prev_index = Some(parent_idx);
                        Some(parent_idx)
                    }
                })
                .collect();
        }
    }
Updates ancestor's `ChildRef` version for all loaded internal nodes. This should be called before adding new leaves
to the tree; it works because the loaded leaves are exactly the leaves for which ancestor versions must be updated.
core/lib/zk_os_merkle_tree/src/storage/patch.rs