vsdb 13.4.5

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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! Lightweight, stateless Merkle trie implementations.
//!
//! This module provides two in-memory Merkle data structures plus a
//! versioned-store integration layer:
//!
//! - **[`MptCalc`]** — Merkle Patricia Trie (16-ary, nibble-based).
//!   Best for general key-value Merkle commitments.
//! - **[`SmtCalc`]** — Sparse Merkle Tree (binary, 256-bit key-hash
//!   paths). Supports compact membership and non-membership proofs.
//! - **[`VerMapWithProof`]** — Wraps a [`VerMap`](crate::versioned::map::VerMap)
//!   with a [`TrieCalc`] back-end to provide versioned Merkle root computation
//!   with incremental diff-based updates and disposable cache persistence.
//!
//! Both `MptCalc` and `SmtCalc` are designed as **stateless computation
//! layers** on top of a versioned store (e.g. `VerMap`): the trie is
//! ephemeral and all persistence is handled by the underlying store.
//!
//! # Architecture: Trie + VerMap
//!
//! ```text
//!   VerMap<K,V>          MptCalc / SmtCalc
//!   (persistence)        (computation)
//!   +-------------+      +-------------+
//!   | branch/     |      | in-memory   |
//!   | commit/     | diff | trie nodes  |  root_hash()
//!   | merge/      |----->| (ephemeral) |-------------> [u8; 32]
//!   | rollback    |      |             |
//!   +-------------+      +-------------+
//!        |                  |         ^
//!        |           eager save    auto-load
//!        |           on sync      on new/from_map
//!        |                  |         |
//!        |                +--v--------+-+
//!        |                | disk cache  | (disposable)
//!        +----------------+-------------+
//! ```
//!
//! 1. **`VerMap`** handles persistence, branching, commits, merges.
//! 2. **`MptCalc` / `SmtCalc`** mirrors the current state as an
//!    in-memory trie, synchronized via full rebuild or incremental diff.
//! 3. **`root_hash()`** returns the 32-byte Merkle commitment.
//! 4. **Automatic cache** — on construction, the trie is silently
//!    restored from a previous cache file; after each commit sync,
//!    the clean state is eagerly saved.  No manual calls required.
//!
//! # SMT proofs
//!
//! [`SmtCalc`] additionally supports [`prove`](SmtCalc::prove) and
//! [`verify_proof`](SmtCalc::verify_proof) for Merkle inclusion and
//! exclusion proofs.  Each proof carries 256 sibling hashes (one per
//! level of the logical 256-level binary tree).  Verification is
//! constant-time: exactly 256 hash operations.

mod cache;
mod error;
mod mpt;
mod nibbles;
mod node;
pub mod proof;
mod smt;

#[cfg(test)]
mod test;

pub use error::{Result, TrieError};
pub use mpt::MptProof;
pub use proof::VerMapWithProof;
pub use smt::SmtProof;

use std::mem;

use mpt::{TrieMut, TrieRo};
use node::NodeHandle;
use vsdb_core::common::vsdb_get_system_dir;

/// Common interface for stateless, in-memory Merkle trie engines.
///
/// Implemented by [`MptCalc`] and [`SmtCalc`].  Used as the trie
/// back-end in [`VerMapWithProof`].
pub trait TrieCalc: Clone + Default {
    /// Builds a trie from key-value pairs.
    fn from_entries(
        kvs: impl IntoIterator<Item = (impl AsRef<[u8]>, impl AsRef<[u8]>)>,
    ) -> Result<Self>;

    /// Looks up a value by key.
    fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>>;

    /// Inserts a key-value pair.
    fn insert(&mut self, key: &[u8], value: &[u8]) -> Result<()>;

    /// Removes a key.
    fn remove(&mut self, key: &[u8]) -> Result<()>;

    /// Computes the 32-byte Merkle root hash.
    fn root_hash(&mut self) -> Result<Vec<u8>>;

    /// Applies a batch of insert/remove operations.
    fn batch_update(&mut self, ops: &[(&[u8], Option<&[u8]>)]) -> Result<()>;

    /// Saves the trie to a file for fast restoration.
    fn save_cache(&mut self, cache_id: u64, sync_tag: u64) -> Result<()>;

    /// Loads a previously saved trie from a file.
    fn load_cache(cache_id: u64) -> Result<(Self, u64, Vec<u8>)>;
}

/// A stateless, in-memory Merkle Patricia Trie.
///
/// `MptCalc` holds an in-memory trie that can be incrementally updated
/// with [`insert`](Self::insert) / [`remove`](Self::remove) /
/// [`batch_update`](Self::batch_update), queried with [`get`](Self::get),
/// and hashed with [`root_hash`](Self::root_hash).
///
/// Unlike a traditional persistent MPT, `MptCalc` does **not** manage
/// node storage or lifecycle.  All versioning, branching, and persistence
/// should be handled by an external store.
#[derive(Clone)]
pub struct MptCalc {
    root: NodeHandle,
}

impl Default for MptCalc {
    fn default() -> Self {
        Self::new()
    }
}

impl MptCalc {
    /// Creates an empty trie.
    pub fn new() -> Self {
        Self {
            root: NodeHandle::default(),
        }
    }

    /// Builds a trie by inserting all key-value pairs from an iterator.
    pub fn from_entries(
        kvs: impl IntoIterator<Item = (impl AsRef<[u8]>, impl AsRef<[u8]>)>,
    ) -> Result<Self> {
        let mut calc = Self::new();
        for (k, v) in kvs {
            calc.insert(k.as_ref(), v.as_ref())?;
        }
        Ok(calc)
    }

    /// Looks up a value by key.
    pub fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
        let trie = TrieRo::new(&self.root);
        trie.get(key)
    }

    /// Inserts a key-value pair into the trie.
    pub fn insert(&mut self, key: &[u8], value: &[u8]) -> Result<()> {
        let mut trie = TrieMut::new(mem::take(&mut self.root));
        trie.insert(key, value)?;
        self.root = trie.into_root();
        Ok(())
    }

    /// Removes a key from the trie.
    pub fn remove(&mut self, key: &[u8]) -> Result<()> {
        let mut trie = TrieMut::new(mem::take(&mut self.root));
        trie.remove(key)?;
        self.root = trie.into_root();
        Ok(())
    }

    /// Computes and returns the 32-byte Merkle root hash.
    ///
    /// Internally caches node hashes so that a subsequent call without
    /// intervening mutations is essentially free.
    pub fn root_hash(&mut self) -> Result<Vec<u8>> {
        let trie = TrieMut::new(mem::take(&mut self.root));
        let (hash, new_root) = trie.commit()?;
        self.root = new_root;
        Ok(hash)
    }

    /// Applies a batch of insert/remove operations.
    ///
    /// Each entry is `(key, Some(value))` for insert or `(key, None)` for remove.
    pub fn batch_update(&mut self, ops: &[(&[u8], Option<&[u8]>)]) -> Result<()> {
        let mut trie = TrieMut::new(mem::take(&mut self.root));
        for (key, val) in ops {
            if let Some(v) = val {
                trie.insert(key, v)?;
            } else {
                trie.remove(key)?;
            }
        }
        self.root = trie.into_root();
        Ok(())
    }

    // =================================================================
    // Proofs
    // =================================================================

    /// Generates a Merkle proof for the given key.
    ///
    /// The tree must be committed (call [`root_hash`](Self::root_hash)
    /// first) for proof generation to work.
    pub fn prove(&self, key: &[u8]) -> Result<MptProof> {
        mpt::proof::prove(&self.root, key)
    }

    /// Verifies an MPT proof against a root hash for a specific key.
    ///
    /// `expected_key` is the key the caller expects this proof to cover.
    pub fn verify_proof(
        root_hash: &[u8; 32],
        expected_key: &[u8],
        proof: &MptProof,
    ) -> Result<bool> {
        mpt::proof::verify_proof(root_hash, expected_key, proof)
    }

    // =================================================================
    // Cache (disposable persistence)
    // =================================================================

    /// Saves the trie to a file for fast restoration on restart.
    ///
    /// `sync_tag` is an opaque identifier (e.g. a `CommitId`) that the
    /// caller can use to determine whether the cache is still current.
    /// Call [`root_hash`](Self::root_hash) before saving to ensure node
    /// hashes are computed.
    ///
    /// The cache is **disposable**: if the file is lost or corrupted,
    /// the trie can always be rebuilt from the authoritative store.
    pub fn save_cache(&mut self, cache_id: u64, sync_tag: u64) -> Result<()> {
        let hash = self.root_hash()?;
        let path = vsdb_get_system_dir().join(format!("mpt_cache_{}.bin", cache_id));
        cache::save_to_file(&self.root, sync_tag, &hash, &path)
    }

    /// Loads a previously saved trie from a file.
    ///
    /// Returns `(MptCalc, sync_tag, root_hash)`.  The caller should
    /// compare `sync_tag` with the current store head and apply any
    /// diff via [`insert`](Self::insert)/[`remove`](Self::remove).
    pub fn load_cache(cache_id: u64) -> Result<(Self, u64, Vec<u8>)> {
        let path = vsdb_get_system_dir().join(format!("mpt_cache_{}.bin", cache_id));
        let (root, sync_tag, root_hash) = cache::load_from_file(&path)?;
        Ok((Self { root }, sync_tag, root_hash))
    }
}

impl TrieCalc for MptCalc {
    fn from_entries(
        kvs: impl IntoIterator<Item = (impl AsRef<[u8]>, impl AsRef<[u8]>)>,
    ) -> Result<Self> {
        Self::from_entries(kvs)
    }
    fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
        self.get(key)
    }
    fn insert(&mut self, key: &[u8], value: &[u8]) -> Result<()> {
        self.insert(key, value)
    }
    fn remove(&mut self, key: &[u8]) -> Result<()> {
        self.remove(key)
    }
    fn root_hash(&mut self) -> Result<Vec<u8>> {
        self.root_hash()
    }
    fn batch_update(&mut self, ops: &[(&[u8], Option<&[u8]>)]) -> Result<()> {
        self.batch_update(ops)
    }
    fn save_cache(&mut self, cache_id: u64, sync_tag: u64) -> Result<()> {
        self.save_cache(cache_id, sync_tag)
    }
    fn load_cache(cache_id: u64) -> Result<(Self, u64, Vec<u8>)> {
        Self::load_cache(cache_id)
    }
}

// =====================================================================
// SmtCalc — Sparse Merkle Tree
// =====================================================================

/// A stateless, in-memory Sparse Merkle Tree.
///
/// Keys are hashed with Keccak256 to produce 256-bit paths.
/// The tree is a binary trie with compressed paths, fixed logical
/// depth of 256, and deterministic hashing.
///
/// API mirrors [`MptCalc`]: insert/remove/batch_update/get/root_hash,
/// plus [`prove`](Self::prove) and [`verify_proof`](Self::verify_proof)
/// for Merkle inclusion/exclusion proofs.
#[derive(Clone)]
pub struct SmtCalc {
    root: smt::SmtHandle,
}

impl Default for SmtCalc {
    fn default() -> Self {
        Self::new()
    }
}

impl SmtCalc {
    /// Creates an empty SMT.
    pub fn new() -> Self {
        Self {
            root: smt::SmtHandle::default(),
        }
    }

    /// Builds an SMT from key-value pairs.
    ///
    /// Keys are hashed internally via Keccak256.
    pub fn from_entries(
        kvs: impl IntoIterator<Item = (impl AsRef<[u8]>, impl AsRef<[u8]>)>,
    ) -> Result<Self> {
        let mut calc = Self::new();
        for (k, v) in kvs {
            calc.insert(k.as_ref(), v.as_ref())?;
        }
        Ok(calc)
    }

    /// Looks up a value by key.
    pub fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
        let key_hash = Self::hash_key(key);
        let ro = smt::query::SmtRo::new(&self.root);
        ro.get(&key_hash)
    }

    /// Inserts a key-value pair.
    pub fn insert(&mut self, key: &[u8], value: &[u8]) -> Result<()> {
        let key_hash = Self::hash_key(key);
        let mut m = smt::mutation::SmtMut::new(mem::take(&mut self.root));
        m.insert(&key_hash, value)?;
        self.root = m.into_root();
        Ok(())
    }

    /// Removes a key.
    pub fn remove(&mut self, key: &[u8]) -> Result<()> {
        let key_hash = Self::hash_key(key);
        let mut m = smt::mutation::SmtMut::new(mem::take(&mut self.root));
        m.remove(&key_hash)?;
        self.root = m.into_root();
        Ok(())
    }

    /// Computes the 32-byte Merkle root hash.
    ///
    /// Caches node hashes so repeated calls without mutations are free.
    pub fn root_hash(&mut self) -> Result<Vec<u8>> {
        let m = smt::mutation::SmtMut::new(mem::take(&mut self.root));
        let (hash, new_root) = m.commit()?;
        self.root = new_root;
        Ok(hash)
    }

    /// Applies a batch of insert/remove operations.
    pub fn batch_update(&mut self, ops: &[(&[u8], Option<&[u8]>)]) -> Result<()> {
        let mut m = smt::mutation::SmtMut::new(mem::take(&mut self.root));
        for (key, val) in ops {
            let key_hash = Self::hash_key(key);
            if let Some(v) = val {
                m.insert(&key_hash, v)?;
            } else {
                m.remove(&key_hash)?;
            }
        }
        self.root = m.into_root();
        Ok(())
    }

    /// Generates a Merkle proof for the given key.
    ///
    /// The tree must be committed (call [`root_hash`](Self::root_hash)
    /// first) for proof generation to work.
    pub fn prove(&self, key: &[u8]) -> Result<SmtProof> {
        let key_hash = Self::hash_key(key);
        smt::proof::prove(&self.root, &key_hash)
    }

    /// Verifies a proof against a root hash.
    pub fn verify_proof(root_hash: &[u8; 32], proof: &SmtProof) -> Result<bool> {
        smt::proof::verify_proof(root_hash, proof)
    }

    // =================================================================
    // Cache
    // =================================================================

    /// Saves the SMT to a file for fast restoration.
    pub fn save_cache(&mut self, cache_id: u64, sync_tag: u64) -> Result<()> {
        let hash = self.root_hash()?;
        let path = vsdb_get_system_dir().join(format!("smt_cache_{}.bin", cache_id));
        smt::cache::save_to_file(&self.root, sync_tag, &hash, &path)
    }

    /// Loads a previously saved SMT from a file.
    pub fn load_cache(cache_id: u64) -> Result<(Self, u64, Vec<u8>)> {
        let path = vsdb_get_system_dir().join(format!("smt_cache_{}.bin", cache_id));
        let (root, sync_tag, root_hash) = smt::cache::load_from_file(&path)?;
        Ok((Self { root }, sync_tag, root_hash))
    }

    // =================================================================
    // Internal
    // =================================================================

    fn hash_key(key: &[u8]) -> [u8; 32] {
        use sha3::{Digest, Keccak256};
        Keccak256::digest(key).into()
    }
}

impl TrieCalc for SmtCalc {
    fn from_entries(
        kvs: impl IntoIterator<Item = (impl AsRef<[u8]>, impl AsRef<[u8]>)>,
    ) -> Result<Self> {
        Self::from_entries(kvs)
    }
    fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
        self.get(key)
    }
    fn insert(&mut self, key: &[u8], value: &[u8]) -> Result<()> {
        self.insert(key, value)
    }
    fn remove(&mut self, key: &[u8]) -> Result<()> {
        self.remove(key)
    }
    fn root_hash(&mut self) -> Result<Vec<u8>> {
        self.root_hash()
    }
    fn batch_update(&mut self, ops: &[(&[u8], Option<&[u8]>)]) -> Result<()> {
        self.batch_update(ops)
    }
    fn save_cache(&mut self, cache_id: u64, sync_tag: u64) -> Result<()> {
        self.save_cache(cache_id, sync_tag)
    }
    fn load_cache(cache_id: u64) -> Result<(Self, u64, Vec<u8>)> {
        Self::load_cache(cache_id)
    }
}