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
use std::marker::PhantomData;

use oasis_core_runtime::storage::mkvs;

use super::Store;

/// A key-value store that hashes all keys and stores them as `H(k) || k`.
pub struct HashedStore<S: Store, D: digest::Digest> {
    parent: S,
    _digest: PhantomData<D>,
}

impl<S: Store, D: digest::Digest> HashedStore<S, D> {
    /// Create a new hashed store.
    pub fn new(parent: S) -> Self {
        Self {
            parent,
            _digest: PhantomData,
        }
    }
}

impl<S: Store, D: digest::Digest> Store for HashedStore<S, D> {
    fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
        self.parent.get(&[&D::digest(key), key].concat())
    }

    fn insert(&mut self, key: &[u8], value: &[u8]) {
        self.parent.insert(&[&D::digest(key), key].concat(), value);
    }

    fn remove(&mut self, key: &[u8]) {
        self.parent.remove(&[&D::digest(key), key].concat());
    }

    fn iter(&self) -> Box<dyn mkvs::Iterator + '_> {
        self.parent.iter()
    }

    fn prefetch_prefixes(&mut self, prefixes: Vec<mkvs::Prefix>, limit: u16) {
        self.parent.prefetch_prefixes(prefixes, limit);
    }
}