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

use primitive_types::H256;
use sha3::{Digest, Keccak256};

/// Represents a trie that is mutable.
pub trait TrieMut {
    /// Get the root hash of the current trie.
    fn root(&self) -> H256;
    /// Insert a value to the trie.
    fn insert(&mut self, key: &[u8], value: &[u8]);
    /// Delete a value in the trie.
    fn delete(&mut self, key: &[u8]);
    /// Get a value in the trie.
    fn get(&self, key: &[u8]) -> Option<Vec<u8>>;
}

/// Represents a mutable trie that is operated on any RLP values.
#[derive(Clone, Default, Debug)]
pub struct AnyTrieMut<T: TrieMut>(T);

impl<T: TrieMut> AnyTrieMut<T> {
    /// Into the underlying TrieMut object.
    #[allow(clippy::wrong_self_convention)]
    pub fn to_trie(self) -> T {
        self.0
    }

    /// Initialize a new mutable trie.
    pub fn new(trie: T) -> Self {
        AnyTrieMut(trie)
    }

    /// Get the root hash of the current trie.
    pub fn root(&self) -> H256 {
        self.0.root()
    }

    /// Insert a value to the trie.
    pub fn insert<K: rlp::Encodable, V: rlp::Encodable>(&mut self, key: &K, value: &V) {
        let key = rlp::encode(key).to_vec();
        let value = rlp::encode(value).to_vec();

        self.0.insert(&key, &value)
    }

    /// Delete a value in the trie.
    pub fn delete<K: rlp::Encodable>(&mut self, key: &K) {
        let key = rlp::encode(key).to_vec();

        self.0.delete(&key)
    }

    /// Get a value in the trie.
    pub fn get<K: rlp::Encodable, V: rlp::Decodable>(&self, key: &K) -> Option<V> {
        let key = rlp::encode(key).to_vec();
        let value = self.0.get(&key);

        match value {
            Some(value) => Some(rlp::decode(&value).expect("Unable to decode value")),
            None => None,
        }
    }
}

/// Represents a mutable trie that is operated on a fixed RLP value type.
#[derive(Clone, Debug)]
pub struct FixedTrieMut<T: TrieMut, K: rlp::Encodable, V: rlp::Encodable + rlp::Decodable>(
    AnyTrieMut<T>,
    PhantomData<(K, V)>,
);

impl<T: TrieMut + Default, K: rlp::Encodable, V: rlp::Encodable + rlp::Decodable> Default
    for FixedTrieMut<T, K, V>
{
    fn default() -> Self {
        FixedTrieMut::new(T::default())
    }
}

impl<T: TrieMut, K: rlp::Encodable, V: rlp::Encodable + rlp::Decodable> FixedTrieMut<T, K, V> {
    /// Into the underlying TrieMut object.
    #[allow(clippy::wrong_self_convention)]
    pub fn to_trie(self) -> T {
        self.0.to_trie()
    }

    /// Initialize a new mutable trie.
    pub fn new(trie: T) -> Self {
        FixedTrieMut(AnyTrieMut::new(trie), PhantomData)
    }

    /// Get the root hash of the current trie.
    pub fn root(&self) -> H256 {
        self.0.root()
    }

    /// Insert a value to the trie.
    pub fn insert(&mut self, key: &K, value: &V) {
        self.0.insert(key, value)
    }

    /// Delete a value in the trie.
    pub fn delete(&mut self, key: &K) {
        self.0.delete(key)
    }

    /// Get a value in the trie.
    pub fn get(&self, key: &K) -> Option<V> {
        self.0.get(key)
    }
}

/// Represents a secure mutable trie where the key is hashed.
#[derive(Clone, Debug)]
pub struct SecureTrieMut<T: TrieMut>(T);

impl<T: TrieMut + Default> Default for SecureTrieMut<T> {
    fn default() -> Self {
        SecureTrieMut::new(T::default())
    }
}

impl<T: TrieMut> SecureTrieMut<T> {
    /// Into the underlying TrieMut object.
    #[allow(clippy::wrong_self_convention)]
    pub fn to_trie(self) -> T {
        self.0
    }

    /// Initialize a new mutable trie.
    pub fn new(trie: T) -> Self {
        SecureTrieMut(trie)
    }

    fn secure_key<K: AsRef<[u8]>>(key: &K) -> Vec<u8> {
        Keccak256::digest(key.as_ref()).as_slice().into()
    }

    /// Get the root hash of the current trie.
    pub fn root(&self) -> H256 {
        self.0.root()
    }

    /// Insert a value to the trie.
    pub fn insert<K: AsRef<[u8]>>(&mut self, key: &K, value: &[u8]) {
        self.0.insert(&Self::secure_key(key), value)
    }

    /// Delete a value in the trie.
    pub fn delete<K: AsRef<[u8]>>(&mut self, key: &K) {
        self.0.delete(&Self::secure_key(key))
    }

    /// Get a value in the trie.
    pub fn get<K: AsRef<[u8]>>(&self, key: &K) -> Option<Vec<u8>> {
        self.0.get(&Self::secure_key(key))
    }
}

/// Represents a secure mutable trie where the key is hashed, and
/// operated on any RLP values.
#[derive(Clone, Debug)]
pub struct AnySecureTrieMut<T: TrieMut>(SecureTrieMut<T>);

impl<T: TrieMut + Default> Default for AnySecureTrieMut<T> {
    fn default() -> Self {
        AnySecureTrieMut::new(T::default())
    }
}

impl<T: TrieMut> AnySecureTrieMut<T> {
    /// Into the underlying TrieMut object.
    #[allow(clippy::wrong_self_convention)]
    pub fn to_trie(self) -> T {
        self.0.to_trie()
    }

    /// Initialize a new mutable trie.
    pub fn new(trie: T) -> Self {
        AnySecureTrieMut(SecureTrieMut::new(trie))
    }

    /// Get the root hash of the current trie.
    pub fn root(&self) -> H256 {
        self.0.root()
    }

    /// Insert a value to the trie.
    pub fn insert<K: AsRef<[u8]>, V: rlp::Encodable>(&mut self, key: &K, value: &V) {
        self.0.insert(&key, &rlp::encode(value).to_vec())
    }

    /// Delete a value in the trie.
    pub fn delete<K: AsRef<[u8]>>(&mut self, key: &K) {
        self.0.delete(&key)
    }

    /// Get a value in the trie.
    pub fn get<K: AsRef<[u8]>, V: rlp::Decodable>(&self, key: &K) -> Option<V> {
        let value = self.0.get(&key);

        match value {
            Some(value) => Some(rlp::decode(&value).expect("Unable to decode value")),
            None => None,
        }
    }
}

/// Represents a secure mutable trie where the key is hashed, and
/// operated on a fixed RLP value type.
#[derive(Clone, Debug)]
pub struct FixedSecureTrieMut<T: TrieMut, K: AsRef<[u8]>, V: rlp::Encodable + rlp::Decodable>(
    AnySecureTrieMut<T>,
    PhantomData<(K, V)>,
);

impl<T: TrieMut + Default, K: AsRef<[u8]>, V: rlp::Encodable + rlp::Decodable> Default
    for FixedSecureTrieMut<T, K, V>
{
    fn default() -> Self {
        FixedSecureTrieMut::new(T::default())
    }
}

impl<T: TrieMut, K: AsRef<[u8]>, V: rlp::Encodable + rlp::Decodable> FixedSecureTrieMut<T, K, V> {
    /// Into the underlying TrieMut object.
    #[allow(clippy::wrong_self_convention)]
    pub fn to_trie(self) -> T {
        self.0.to_trie()
    }

    /// Initialize a new mutable trie.
    pub fn new(trie: T) -> Self {
        FixedSecureTrieMut(AnySecureTrieMut::new(trie), PhantomData)
    }

    /// Get the root hash of the current trie.
    pub fn root(&self) -> H256 {
        self.0.root()
    }

    /// Insert a value to the trie.
    pub fn insert(&mut self, key: &K, value: &V) {
        self.0.insert(key, value)
    }

    /// Delete a value in the trie.
    pub fn delete(&mut self, key: &K) {
        self.0.delete(key)
    }

    /// Get a value in the trie.
    pub fn get(&self, key: &K) -> Option<V> {
        self.0.get(key)
    }
}