1use crate::Nibbles;
2use alloc::{sync::Arc, vec::Vec};
3use alloy_primitives::map::{B256Map, B256Set};
4use core::ops::Range;
5
6#[derive(Clone, Default, Debug)]
8pub struct TriePrefixSetsMut {
9 pub account_prefix_set: PrefixSetMut,
11 pub storage_prefix_sets: B256Map<PrefixSetMut>,
14 pub destroyed_accounts: B256Set,
16}
17
18impl TriePrefixSetsMut {
19 pub fn is_empty(&self) -> bool {
21 self.account_prefix_set.is_empty() &&
22 self.storage_prefix_sets.is_empty() &&
23 self.destroyed_accounts.is_empty()
24 }
25
26 pub fn extend(&mut self, other: Self) {
28 self.account_prefix_set.extend(other.account_prefix_set);
29 for (hashed_address, prefix_set) in other.storage_prefix_sets {
30 self.storage_prefix_sets.entry(hashed_address).or_default().extend(prefix_set);
31 }
32 self.destroyed_accounts.extend(other.destroyed_accounts);
33 }
34
35 pub fn freeze(self) -> TriePrefixSets {
39 TriePrefixSets {
40 account_prefix_set: self.account_prefix_set.freeze(),
41 storage_prefix_sets: self
42 .storage_prefix_sets
43 .into_iter()
44 .map(|(hashed_address, prefix_set)| (hashed_address, prefix_set.freeze()))
45 .collect(),
46 destroyed_accounts: self.destroyed_accounts,
47 }
48 }
49
50 pub fn clear(&mut self) {
52 self.destroyed_accounts.clear();
53 self.storage_prefix_sets.clear();
54 self.account_prefix_set.clear();
55 }
56}
57
58#[derive(Default, Debug, Clone)]
60pub struct TriePrefixSets {
61 pub account_prefix_set: PrefixSet,
63 pub storage_prefix_sets: B256Map<PrefixSet>,
66 pub destroyed_accounts: B256Set,
68}
69
70#[derive(PartialEq, Eq, Clone, Default, Debug)]
100pub struct PrefixSetMut {
101 all: bool,
104 keys: Vec<Nibbles>,
105}
106
107impl<I> From<I> for PrefixSetMut
108where
109 I: IntoIterator<Item = Nibbles>,
110{
111 fn from(value: I) -> Self {
112 Self { all: false, keys: value.into_iter().collect() }
113 }
114}
115
116impl PrefixSetMut {
117 pub fn with_capacity(capacity: usize) -> Self {
119 Self { all: false, keys: Vec::with_capacity(capacity) }
120 }
121
122 pub const fn all() -> Self {
124 Self { all: true, keys: Vec::new() }
125 }
126
127 pub fn insert(&mut self, nibbles: Nibbles) {
129 self.keys.push(nibbles);
130 }
131
132 pub fn extend(&mut self, other: Self) {
134 self.all |= other.all;
135 self.keys.extend(other.keys);
136 }
137
138 pub fn extend_keys<I>(&mut self, keys: I)
140 where
141 I: IntoIterator<Item = Nibbles>,
142 {
143 self.keys.extend(keys);
144 }
145
146 pub const fn len(&self) -> usize {
148 self.keys.len()
149 }
150
151 pub const fn is_empty(&self) -> bool {
153 !self.all && self.keys.is_empty()
154 }
155
156 pub fn clear(&mut self) {
158 self.all = false;
159 self.keys.clear();
160 }
161
162 pub fn freeze(mut self) -> PrefixSet {
166 if self.all {
167 PrefixSet { index: 0, all: true, keys: Arc::new(Vec::new()) }
168 } else {
169 self.keys.sort_unstable();
170 self.keys.dedup();
171 self.keys.shrink_to_fit();
173 PrefixSet { index: 0, all: false, keys: Arc::new(self.keys) }
174 }
175 }
176}
177
178#[derive(Debug, Default, Clone)]
182pub struct PrefixSet {
183 all: bool,
185 index: usize,
186 keys: Arc<Vec<Nibbles>>,
187}
188
189impl PrefixSet {
190 #[inline]
205 pub fn contains(&mut self, prefix: &Nibbles) -> bool {
206 if self.all {
207 return true
208 }
209
210 while self.index > 0 && &self.keys[self.index] > prefix {
211 self.index -= 1;
212 }
213
214 for (idx, key) in self.keys[self.index..].iter().enumerate() {
215 if key.starts_with(prefix) {
216 self.index += idx;
217 return true
218 }
219
220 if key > prefix {
221 self.index += idx;
222 return false
223 }
224 }
225
226 false
227 }
228
229 #[inline]
235 pub fn contains_range(&mut self, range: Range<&Nibbles>) -> bool {
236 if self.all {
237 return true
238 }
239
240 while self.index > 0 && &self.keys[self.index] >= range.end {
241 self.index -= 1;
242 }
243
244 for (idx, key) in self.keys[self.index..].iter().enumerate() {
245 if key >= range.start && key < range.end {
246 self.index += idx;
247 return true
248 }
249
250 if key >= range.end {
251 self.index += idx;
252 return false
253 }
254 }
255
256 false
257 }
258
259 pub fn iter(&self) -> core::slice::Iter<'_, Nibbles> {
261 self.keys.iter()
262 }
263
264 pub const fn all(&self) -> bool {
266 self.all
267 }
268
269 pub fn len(&self) -> usize {
271 self.keys.len()
272 }
273
274 pub fn is_empty(&self) -> bool {
276 !self.all && self.keys.is_empty()
277 }
278}
279
280impl<'a> IntoIterator for &'a PrefixSet {
281 type Item = &'a Nibbles;
282 type IntoIter = core::slice::Iter<'a, Nibbles>;
283 fn into_iter(self) -> Self::IntoIter {
284 self.iter()
285 }
286}
287
288#[cfg(test)]
289mod tests {
290 use super::*;
291
292 #[test]
293 fn test_contains_with_multiple_inserts_and_duplicates() {
294 let mut prefix_set_mut = PrefixSetMut::default();
295 prefix_set_mut.insert(Nibbles::from_nibbles([1, 2, 3]));
296 prefix_set_mut.insert(Nibbles::from_nibbles([1, 2, 4]));
297 prefix_set_mut.insert(Nibbles::from_nibbles([4, 5, 6]));
298 prefix_set_mut.insert(Nibbles::from_nibbles([1, 2, 3])); let mut prefix_set = prefix_set_mut.freeze();
301 assert!(prefix_set.contains(&Nibbles::from_nibbles_unchecked([1, 2])));
302 assert!(prefix_set.contains(&Nibbles::from_nibbles_unchecked([4, 5])));
303 assert!(!prefix_set.contains(&Nibbles::from_nibbles_unchecked([7, 8])));
304 assert_eq!(prefix_set.len(), 3); }
306
307 #[test]
308 fn test_freeze_shrinks_capacity() {
309 let mut prefix_set_mut = PrefixSetMut::default();
310 prefix_set_mut.insert(Nibbles::from_nibbles([1, 2, 3]));
311 prefix_set_mut.insert(Nibbles::from_nibbles([1, 2, 4]));
312 prefix_set_mut.insert(Nibbles::from_nibbles([4, 5, 6]));
313 prefix_set_mut.insert(Nibbles::from_nibbles([1, 2, 3])); assert_eq!(prefix_set_mut.keys.len(), 4); assert_eq!(prefix_set_mut.keys.capacity(), 4); let mut prefix_set = prefix_set_mut.freeze();
319 assert!(prefix_set.contains(&Nibbles::from_nibbles_unchecked([1, 2])));
320 assert!(prefix_set.contains(&Nibbles::from_nibbles_unchecked([4, 5])));
321 assert!(!prefix_set.contains(&Nibbles::from_nibbles_unchecked([7, 8])));
322 assert_eq!(prefix_set.keys.len(), 3); assert_eq!(prefix_set.keys.capacity(), 3); }
325
326 #[test]
327 fn test_freeze_shrinks_existing_capacity() {
328 let mut prefix_set_mut = PrefixSetMut::with_capacity(101);
330 prefix_set_mut.insert(Nibbles::from_nibbles([1, 2, 3]));
331 prefix_set_mut.insert(Nibbles::from_nibbles([1, 2, 4]));
332 prefix_set_mut.insert(Nibbles::from_nibbles([4, 5, 6]));
333 prefix_set_mut.insert(Nibbles::from_nibbles([1, 2, 3])); assert_eq!(prefix_set_mut.keys.len(), 4); assert_eq!(prefix_set_mut.keys.capacity(), 101); let mut prefix_set = prefix_set_mut.freeze();
339 assert!(prefix_set.contains(&Nibbles::from_nibbles_unchecked([1, 2])));
340 assert!(prefix_set.contains(&Nibbles::from_nibbles_unchecked([4, 5])));
341 assert!(!prefix_set.contains(&Nibbles::from_nibbles_unchecked([7, 8])));
342 assert_eq!(prefix_set.keys.len(), 3); assert_eq!(prefix_set.keys.capacity(), 3); }
345
346 #[test]
347 fn test_prefix_set_all_extend() {
348 let mut prefix_set_mut = PrefixSetMut::default();
349 prefix_set_mut.extend(PrefixSetMut::all());
350 assert!(prefix_set_mut.all);
351 }
352}