heika_state_transition/tree/
mod.rs1mod full;
7mod lazy;
8
9use heika_core::hash::Hash256;
10use heika_core::patricia::TreeNode;
11
12pub use full::PatriciaTree;
13pub use lazy::LazyTree;
14
15const KEY_NIBBLES: usize = 64;
17
18pub const EMPTY_ROOT: Hash256 = Hash256([0; 32]);
20
21pub type Position = (u8, [u8; 32]);
23
24pub const ROOT: Position = (0, [0; 32]);
26
27#[derive(Debug)]
29pub enum StateTree {
30 Full(PatriciaTree),
31 Lazy(LazyTree),
32}
33
34impl Default for StateTree {
35 fn default() -> Self {
36 Self::Full(PatriciaTree::default())
37 }
38}
39
40impl StateTree {
41 pub fn set(&mut self, key: Hash256, value: Hash256) {
42 match self {
43 Self::Full(tree) => tree.set(key, value),
44 Self::Lazy(tree) => tree.set(key, value),
45 }
46 }
47
48 pub fn remove(&mut self, key: &Hash256) {
49 match self {
50 Self::Full(tree) => tree.remove(key),
51 Self::Lazy(tree) => tree.remove(key),
52 }
53 }
54
55 pub fn root(&mut self) -> Hash256 {
56 match self {
57 Self::Full(tree) => tree.root(),
58 Self::Lazy(tree) => tree.root(),
59 }
60 }
61
62 pub fn take_node_changes(&mut self) -> Vec<(Position, Option<TreeNode>)> {
63 match self {
64 Self::Full(tree) => tree.take_node_changes(),
65 Self::Lazy(tree) => tree.take_node_changes(),
66 }
67 }
68
69 pub fn forget_node_changes(&mut self) {
70 match self {
71 Self::Full(tree) => tree.forget_node_changes(),
72 Self::Lazy(tree) => tree.forget_node_changes(),
73 }
74 }
75
76 pub fn track_node_changes(&mut self, track: bool) {
77 if let Self::Full(tree) = self {
78 tree.track_node_changes(track);
79 }
80 }
81
82 pub fn as_full(&self) -> Option<&PatriciaTree> {
84 match self {
85 Self::Full(tree) => Some(tree),
86 Self::Lazy(_) => None,
87 }
88 }
89
90 pub fn as_full_mut(&mut self) -> Option<&mut PatriciaTree> {
91 match self {
92 Self::Full(tree) => Some(tree),
93 Self::Lazy(_) => None,
94 }
95 }
96
97 pub fn as_lazy_mut(&mut self) -> Option<&mut LazyTree> {
98 match self {
99 Self::Full(_) => None,
100 Self::Lazy(tree) => Some(tree),
101 }
102 }
103}
104
105pub fn node_key(cache: u8, position: &Position) -> Vec<u8> {
107 let (depth, prefix) = position;
108 let mut out = vec![cache, *depth];
109 out.extend_from_slice(&prefix[..usize::from(*depth).div_ceil(2)]);
110 out
111}
112
113pub fn parse_node_key(key: &[u8]) -> Option<(u8, Position)> {
115 let (&cache, rest) = key.split_first()?;
116 let (&depth, bytes) = rest.split_first()?;
117 if bytes.len() != usize::from(depth).div_ceil(2) || usize::from(depth) > KEY_NIBBLES {
118 return None;
119 }
120 let mut prefix = [0; 32];
121 prefix[..bytes.len()].copy_from_slice(bytes);
122 Some((cache, (depth, mask(&prefix, usize::from(depth)))))
123}
124
125pub fn walk<E>(
127 key: &Hash256,
128 mut node: impl FnMut(&Position) -> Result<Option<TreeNode>, E>,
129) -> Result<Vec<TreeNode>, E> {
130 let mut nodes = Vec::new();
131 let mut depth = 0;
132 while let Some(found) = node(&(depth as u8, mask(&key.0, depth)))? {
133 let next = match &found {
134 TreeNode::Leaf { .. } => None,
135 TreeNode::Branch { path, .. } => {
136 let end = depth + path.len();
137 let key_path: Vec<u8> = (depth..end).map(|index| nibble(&key.0, index)).collect();
138 (key_path == *path).then_some(end + 1)
139 }
140 };
141 nodes.push(found);
142 let Some(next) = next else {
143 break;
144 };
145 depth = next;
146 }
147 Ok(nodes)
148}
149
150pub(crate) fn nibble(key: &[u8; 32], index: usize) -> u8 {
151 let byte = key[index / 2];
152 if index.is_multiple_of(2) {
153 byte >> 4
154 } else {
155 byte & 0x0F
156 }
157}
158
159pub(crate) fn set_nibble(key: &mut [u8; 32], index: usize, value: u8) {
160 let byte = &mut key[index / 2];
161 if index.is_multiple_of(2) {
162 *byte = (*byte & 0x0F) | (value << 4);
163 } else {
164 *byte = (*byte & 0xF0) | value;
165 }
166}
167
168pub(crate) fn mask(key: &[u8; 32], depth: usize) -> [u8; 32] {
170 let mut out = [0; 32];
171 out[..depth / 2].copy_from_slice(&key[..depth / 2]);
172 if depth % 2 == 1 {
173 out[depth / 2] = key[depth / 2] & 0xF0;
174 }
175 out
176}