vsdb 14.0.8

A std-collection-like database
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
//!
//! SMT insert, remove, and commit (hashing).
//!

use std::mem;

use crate::trie::error::{Result, TrieError};

use super::bitpath::BitPath;
use super::codec::{hash_internal, hash_leaf, wrap_hash};
use super::{EMPTY_HASH, SmtHandle, SmtNode};

pub struct SmtMut {
    root: SmtHandle,
}

impl SmtMut {
    pub fn new(root: SmtHandle) -> Self {
        Self { root }
    }

    pub fn insert(&mut self, key_hash: &[u8; 32], value: &[u8]) -> Result<()> {
        let full_path = BitPath::from_hash(key_hash);
        let new_root = insert_rec(
            mem::take(&mut self.root),
            &full_path,
            0,
            *key_hash,
            value.to_vec(),
        )?;
        self.root = new_root;
        Ok(())
    }

    pub fn remove(&mut self, key_hash: &[u8; 32]) -> Result<()> {
        let full_path = BitPath::from_hash(key_hash);
        let (new_root, _changed) =
            remove_rec(mem::take(&mut self.root), &full_path, 0, key_hash)?;
        self.root = new_root;
        Ok(())
    }

    /// Hashes the entire tree in place and returns the 32-byte root hash.
    ///
    /// On success `self`'s root holds the freshly hashed tree, so a
    /// subsequent call without intervening mutations is essentially
    /// free.  `commit_rec` can only fail on a `Cached` hash whose
    /// length isn't 32 bytes — a state neither mutation (all hashes are
    /// Keccak-256 outputs) nor cache loading (the deserializer rejects
    /// non-32-byte cached hashes) can produce — so the defensive error
    /// arm below, which would drop the already-consumed working tree,
    /// is unreachable.
    pub fn commit(&mut self) -> Result<Vec<u8>> {
        let root = mem::take(&mut self.root);
        match commit_rec(root) {
            Ok(root) => {
                let result = root.expect_hash().map(<[u8]>::to_vec);
                self.root = root;
                result
            }
            Err(e) => Err(e),
        }
    }

    pub fn into_root(self) -> SmtHandle {
        self.root
    }
}

// =========================================================================
// Insert
// =========================================================================

fn insert_rec(
    handle: SmtHandle,
    full_path: &BitPath,
    depth: usize,
    key_hash: [u8; 32],
    value: Vec<u8>,
) -> Result<SmtHandle> {
    let node = handle.into_node();

    match node {
        SmtNode::Empty => {
            // Place a leaf with the remaining bits as its path.
            let remaining = full_path.slice(depth, full_path.len());
            Ok(SmtHandle::InMemory(Box::new(SmtNode::Leaf {
                path: remaining,
                key_hash,
                value,
            })))
        }

        SmtNode::Leaf {
            path: leaf_path,
            key_hash: leaf_kh,
            value: leaf_val,
        } => {
            if leaf_kh == key_hash {
                // Same key — update value.
                return Ok(SmtHandle::InMemory(Box::new(SmtNode::Leaf {
                    path: leaf_path,
                    key_hash,
                    value,
                })));
            }

            // Different keys: find where the remaining paths diverge.
            // `leaf_path` equals the full path suffix from `depth`, so
            // comparing it against `full_path[depth..]` needs no slice.
            let common = full_path.common_prefix_from(depth, &leaf_path);

            // Create the new leaf.
            let new_leaf_path = full_path.slice(depth + common + 1, full_path.len());
            let new_leaf = SmtHandle::InMemory(Box::new(SmtNode::Leaf {
                path: new_leaf_path,
                key_hash,
                value,
            }));

            // Adjust the old leaf's path.
            let old_leaf_path = leaf_path.slice(common + 1, leaf_path.len());
            let old_leaf = SmtHandle::InMemory(Box::new(SmtNode::Leaf {
                path: old_leaf_path,
                key_hash: leaf_kh,
                value: leaf_val,
            }));

            // The divergence bit determines left/right.
            let new_bit = full_path.bit_at(depth + common);
            let (left, right) = if new_bit == 0 {
                (new_leaf, old_leaf)
            } else {
                (old_leaf, new_leaf)
            };

            let prefix = leaf_path.slice(0, common);
            Ok(SmtHandle::InMemory(Box::new(SmtNode::Internal {
                path: prefix,
                left,
                right,
            })))
        }

        SmtNode::Internal { path, left, right } => {
            let common = full_path.common_prefix_from(depth, &path);

            if common < path.len() {
                // Path diverges within the compressed prefix.
                // Split the internal node.
                let diverge_bit_in_path = path.bit_at(common);
                let diverge_bit_in_key = full_path.bit_at(depth + common);

                // New leaf for the inserted key.
                let new_leaf_path = full_path.slice(depth + common + 1, full_path.len());
                let new_leaf = SmtHandle::InMemory(Box::new(SmtNode::Leaf {
                    path: new_leaf_path,
                    key_hash,
                    value,
                }));

                // Old internal with shortened path.
                let old_suffix = path.slice(common + 1, path.len());
                let old_internal = SmtHandle::InMemory(Box::new(SmtNode::Internal {
                    path: old_suffix,
                    left,
                    right,
                }));

                let (new_left, new_right) = if diverge_bit_in_key == 0 {
                    debug_assert_eq!(diverge_bit_in_path, 1);
                    (new_leaf, old_internal)
                } else {
                    debug_assert_eq!(diverge_bit_in_path, 0);
                    (old_internal, new_leaf)
                };

                let prefix = path.slice(0, common);
                Ok(SmtHandle::InMemory(Box::new(SmtNode::Internal {
                    path: prefix,
                    left: new_left,
                    right: new_right,
                })))
            } else {
                // Full prefix matches. Descend into the appropriate child.
                let next_depth = depth + path.len();
                if next_depth >= full_path.len() {
                    // Defensive only: organic trees cannot reach this
                    // (two distinct 256-bit key hashes always diverge
                    // within 256 bits), and the cache deserializer
                    // rejects trees whose cumulative depth could.  If
                    // it ever fired, the consumed working tree would be
                    // dropped — `SmtMut::insert` cannot restore what
                    // `insert_rec` has already moved out of.
                    return Err(TrieError::InvalidState(
                        "SMT depth exceeded 256 bits".into(),
                    ));
                }

                let bit = full_path.bit_at(next_depth);
                let child_depth = next_depth + 1;

                let (new_left, new_right) = if bit == 0 {
                    let new_left =
                        insert_rec(left, full_path, child_depth, key_hash, value)?;
                    (new_left, right)
                } else {
                    let new_right =
                        insert_rec(right, full_path, child_depth, key_hash, value)?;
                    (left, new_right)
                };

                Ok(SmtHandle::InMemory(Box::new(SmtNode::Internal {
                    path,
                    left: new_left,
                    right: new_right,
                })))
            }
        }
    }
}

// =========================================================================
// Remove
// =========================================================================

/// Re-wraps a node into a handle, preserving a precomputed hash when the
/// node is unchanged — mirrors MPT's `TrieMut::rewrap` — so a no-change
/// `remove_rec` path doesn't force a re-hash of the whole ancestor chain.
fn rewrap(cached_hash: &Option<Vec<u8>>, node: SmtNode) -> SmtHandle {
    match cached_hash {
        Some(h) => SmtHandle::Cached(h.clone(), Box::new(node)),
        None => SmtHandle::InMemory(Box::new(node)),
    }
}

fn remove_rec(
    handle: SmtHandle,
    full_path: &BitPath,
    depth: usize,
    key_hash: &[u8; 32],
) -> Result<(SmtHandle, bool)> {
    // Fast-path: inspect the node without consuming the handle.
    // If the key is not in this subtree, return the original handle
    // unchanged — preserving any Cached hash.
    match handle.node() {
        SmtNode::Empty => return Ok((handle, false)),
        SmtNode::Leaf {
            key_hash: leaf_kh, ..
        } => {
            if leaf_kh != key_hash {
                return Ok((handle, false));
            }
            // Key matches — fall through to consume the handle.
        }
        SmtNode::Internal { path, .. } => {
            if !full_path.starts_with_from(depth, path) {
                return Ok((handle, false));
            }
            let next_depth = depth + path.len();
            if next_depth >= full_path.len() {
                return Ok((handle, false));
            }
            // Path matches — fall through to consume the handle.
        }
    }

    // Past here the node *may* be modified — peek its cached hash
    // first (before consuming) so the Internal case below can restore
    // it if neither child subtree actually changed: the fast-path
    // check above only guarantees the key's path is *plausible*
    // through this node, not that the key truly exists deeper down.
    let cached_hash = handle.hash().map(|h| h.to_vec());
    let node = handle.into_node();

    match node {
        SmtNode::Empty => unreachable!("handled above"),

        SmtNode::Leaf { .. } => {
            // Key matched — delete by returning empty.
            Ok((SmtHandle::default(), true))
        }

        SmtNode::Internal { path, left, right } => {
            let next_depth = depth + path.len();
            let bit = full_path.bit_at(next_depth);
            let child_depth = next_depth + 1;

            let (new_left, new_right, changed) = if bit == 0 {
                let (new_left, changed) =
                    remove_rec(left, full_path, child_depth, key_hash)?;
                (new_left, right, changed)
            } else {
                let (new_right, changed) =
                    remove_rec(right, full_path, child_depth, key_hash)?;
                (left, new_right, changed)
            };

            if !changed {
                // Neither child actually changed — restore this node's
                // Cached hash instead of unconditionally reconstructing
                // it via `compact` (which would force a re-hash of the
                // whole ancestor chain on the next `commit`).
                let n = SmtNode::Internal {
                    path,
                    left: new_left,
                    right: new_right,
                };
                return Ok((rewrap(&cached_hash, n), false));
            }

            Ok((compact(path, new_left, new_right)?, true))
        }
    }
}

/// Compacts an Internal node after a child becomes empty.
fn compact(path: BitPath, left: SmtHandle, right: SmtHandle) -> Result<SmtHandle> {
    let left_empty = left.is_empty();
    let right_empty = right.is_empty();

    if left_empty && right_empty {
        return Ok(SmtHandle::default());
    }

    if !left_empty && !right_empty {
        // Both non-empty — keep the internal node.
        return Ok(SmtHandle::InMemory(Box::new(SmtNode::Internal {
            path,
            left,
            right,
        })));
    }

    // Exactly one child is empty — promote the survivor.
    let (surviving, surviving_bit) = if left_empty {
        (right, 1u8)
    } else {
        (left, 0u8)
    };

    let surviving_node = surviving.into_node();
    match surviving_node {
        SmtNode::Leaf {
            path: child_path,
            key_hash,
            value,
        } => {
            // Absorb: parent_path + bit + child_path → new leaf path.
            let bit_path = BitPath::from_bits(&[surviving_bit]);
            let new_path = path.concat(&bit_path).concat(&child_path);
            Ok(SmtHandle::InMemory(Box::new(SmtNode::Leaf {
                path: new_path,
                key_hash,
                value,
            })))
        }
        SmtNode::Internal {
            path: child_path,
            left: cl,
            right: cr,
        } => {
            // Merge paths: parent_path + bit + child_path.
            let bit_path = BitPath::from_bits(&[surviving_bit]);
            let new_path = path.concat(&bit_path).concat(&child_path);
            Ok(SmtHandle::InMemory(Box::new(SmtNode::Internal {
                path: new_path,
                left: cl,
                right: cr,
            })))
        }
        SmtNode::Empty => Ok(SmtHandle::default()),
    }
}

// =========================================================================
// Commit (hash all nodes)
// =========================================================================

fn commit_rec(handle: SmtHandle) -> Result<SmtHandle> {
    match handle {
        SmtHandle::InMemory(mut node) => {
            let hash = match *node {
                SmtNode::Empty => {
                    return Ok(SmtHandle::Cached(EMPTY_HASH.to_vec(), node));
                }
                SmtNode::Leaf {
                    ref key_hash,
                    ref value,
                    ..
                } => {
                    // Depth-independent leaf shortcut: a lone-leaf
                    // subtree commits to the leaf hash directly; the
                    // residual path is NOT folded into the hash (the
                    // full key_hash already binds the position).
                    hash_leaf(key_hash, value)
                }
                SmtNode::Internal {
                    ref path,
                    ref mut left,
                    ref mut right,
                } => {
                    // Recursively commit children first.
                    *left = commit_rec(mem::take(left))?;
                    *right = commit_rec(mem::take(right))?;

                    // Combine children at the split point, then wrap
                    // through the compressed path.
                    let left_h: [u8; 32] = left
                        .expect_hash()?
                        .try_into()
                        .map_err(|_| TrieError::InvalidState("bad hash len".into()))?;
                    let right_h: [u8; 32] = right
                        .expect_hash()?
                        .try_into()
                        .map_err(|_| TrieError::InvalidState("bad hash len".into()))?;

                    let internal_h = hash_internal(&left_h, &right_h);
                    wrap_hash(internal_h, path)
                }
            };
            Ok(SmtHandle::Cached(hash.to_vec(), node))
        }
        cached => Ok(cached),
    }
}