1- use starknet_api:: hash:: HashOutput ;
21use starknet_patricia_storage:: errors:: { DeserializationError , StorageError } ;
32use starknet_patricia_storage:: storage_trait:: PatriciaStorageError ;
43use thiserror:: Error ;
54
6- use crate :: patricia_merkle_tree:: filled_tree:: node_serde:: PatriciaPrefix ;
75use crate :: patricia_merkle_tree:: node_data:: inner_node:: PathToBottom ;
8- use crate :: patricia_merkle_tree:: node_data:: leaf:: Leaf ;
96use crate :: patricia_merkle_tree:: original_skeleton_tree:: utils:: split_leaves;
107use crate :: patricia_merkle_tree:: types:: { NodeIndex , SortedLeafIndices , SubTreeHeight } ;
118
@@ -25,88 +22,70 @@ pub enum TraversalError {
2522
2623pub type TraversalResult < T > = Result < T , TraversalError > ;
2724
28- #[ derive( Debug , PartialEq ) ]
29- pub struct SubTree < ' a > {
30- pub sorted_leaf_indices : SortedLeafIndices < ' a > ,
31- pub root_index : NodeIndex ,
32- pub root_hash : HashOutput ,
33- }
25+ pub trait SubTreeTrait < ' a > : Sized {
26+ type ChildData : Copy ;
3427
35- impl < ' a > SubTree < ' a > {
36- pub ( crate ) fn get_height ( & self ) -> SubTreeHeight {
37- SubTreeHeight :: new ( SubTreeHeight :: ACTUAL_HEIGHT . 0 - ( self . root_index . bit_length ( ) - 1 ) )
38- }
28+ fn create_child (
29+ sorted_leaf_indices : SortedLeafIndices < ' a > ,
30+ root_index : NodeIndex ,
31+ child_data : Self :: ChildData ,
32+ ) -> Self ;
33+ fn get_root_index ( & self ) -> NodeIndex ;
34+ fn get_sorted_leaf_indices ( & self ) -> & SortedLeafIndices < ' a > ;
3935
40- pub ( crate ) fn split_leaves ( & self ) -> [ SortedLeafIndices < ' a > ; 2 ] {
41- split_leaves ( & self . root_index , & self . sorted_leaf_indices )
36+ fn get_height ( & self ) -> SubTreeHeight {
37+ SubTreeHeight :: new (
38+ SubTreeHeight :: ACTUAL_HEIGHT . 0 - ( self . get_root_index ( ) . bit_length ( ) - 1 ) ,
39+ )
4240 }
43-
44- pub fn is_unmodified ( & self ) -> bool {
45- self . sorted_leaf_indices . is_empty ( )
41+ fn split_leaves ( & self ) -> [ SortedLeafIndices < ' a > ; 2 ] {
42+ split_leaves ( & self . get_root_index ( ) , self . get_sorted_leaf_indices ( ) )
4643 }
47-
48- pub fn get_root_prefix < L : Leaf > ( & self ) -> PatriciaPrefix {
49- if self . is_leaf ( ) {
50- PatriciaPrefix :: Leaf ( L :: get_static_prefix ( ) )
51- } else {
52- PatriciaPrefix :: InnerNode
53- }
44+ fn is_unmodified ( & self ) -> bool {
45+ self . get_sorted_leaf_indices ( ) . is_empty ( )
5446 }
55-
5647 /// Returns the bottom subtree which is referred from `self` by the given path. When creating
5748 /// the bottom subtree some indices that were modified under `self` are not modified under the
5849 /// bottom subtree (leaves that were previously empty). These indices are returned as well.
59- pub fn get_bottom_subtree (
50+ fn get_bottom_subtree (
6051 & self ,
6152 path_to_bottom : & PathToBottom ,
62- bottom_hash : HashOutput ,
63- ) -> ( Self , Vec < & NodeIndex > ) {
64- let bottom_index = path_to_bottom. bottom_index ( self . root_index ) ;
53+ bottom_data : Self :: ChildData ,
54+ ) -> ( Self , Vec < NodeIndex > ) {
55+ let sorted_leaf_indices = self . get_sorted_leaf_indices ( ) ;
56+ let bottom_index = path_to_bottom. bottom_index ( self . get_root_index ( ) ) ;
6557 let bottom_height = self . get_height ( ) - SubTreeHeight :: new ( path_to_bottom. length . into ( ) ) ;
6658 let leftmost_in_subtree = bottom_index << bottom_height. into ( ) ;
6759 let rightmost_in_subtree =
6860 leftmost_in_subtree - NodeIndex :: ROOT + ( NodeIndex :: ROOT << bottom_height. into ( ) ) ;
69- let leftmost_index = self . sorted_leaf_indices . bisect_left ( & leftmost_in_subtree) ;
70- let rightmost_index = self . sorted_leaf_indices . bisect_right ( & rightmost_in_subtree) ;
71- let bottom_leaves = self . sorted_leaf_indices . subslice ( leftmost_index, rightmost_index) ;
72- let previously_empty_leaf_indices = self . sorted_leaf_indices . get_indices ( )
73- [ ..leftmost_index]
61+ let leftmost_index = sorted_leaf_indices. bisect_left ( & leftmost_in_subtree) ;
62+ let rightmost_index = sorted_leaf_indices. bisect_right ( & rightmost_in_subtree) ;
63+ let bottom_leaves = sorted_leaf_indices. subslice ( leftmost_index, rightmost_index) ;
64+ let previously_empty_leaf_indices = sorted_leaf_indices. get_indices ( ) [ ..leftmost_index]
7465 . iter ( )
75- . chain ( self . sorted_leaf_indices . get_indices ( ) [ rightmost_index..] . iter ( ) )
66+ . chain ( sorted_leaf_indices. get_indices ( ) [ rightmost_index..] . iter ( ) )
67+ . cloned ( )
7668 . collect ( ) ;
7769
7870 (
79- Self {
80- sorted_leaf_indices : bottom_leaves,
81- root_index : bottom_index,
82- root_hash : bottom_hash,
83- } ,
71+ Self :: create_child ( bottom_leaves, bottom_index, bottom_data) ,
8472 previously_empty_leaf_indices,
8573 )
8674 }
87-
88- pub fn get_children_subtrees (
75+ fn get_children_subtrees (
8976 & self ,
90- left_hash : HashOutput ,
91- right_hash : HashOutput ,
77+ left_data : Self :: ChildData ,
78+ right_data : Self :: ChildData ,
9279 ) -> ( Self , Self ) {
9380 let [ left_leaves, right_leaves] = self . split_leaves ( ) ;
94- let left_root_index = self . root_index * 2 . into ( ) ;
81+ let left_root_index = self . get_root_index ( ) * 2 . into ( ) ;
9582 (
96- SubTree {
97- sorted_leaf_indices : left_leaves,
98- root_index : left_root_index,
99- root_hash : left_hash,
100- } ,
101- SubTree {
102- sorted_leaf_indices : right_leaves,
103- root_index : left_root_index + NodeIndex :: ROOT ,
104- root_hash : right_hash,
105- } ,
83+ Self :: create_child ( left_leaves, left_root_index, left_data) ,
84+ Self :: create_child ( right_leaves, left_root_index + NodeIndex :: ROOT , right_data) ,
10685 )
10786 }
108-
109- pub fn is_leaf ( & self ) -> bool {
110- self . root_index . is_leaf ( )
87+ fn is_leaf ( & self ) -> bool {
88+ self . get_root_index ( ) . is_leaf ( )
11189 }
90+ fn should_traverse_unmodified_children ( ) -> bool ;
11291}
0 commit comments