trie/
mutable.rs

1use bigint::H256;
2use rlp::{self, Rlp};
3use sha3::{Digest, Keccak256};
4
5use std::marker::PhantomData;
6
7/// Represents a trie that is mutable.
8pub trait TrieMut {
9    /// Get the root hash of the current trie.
10    fn root(&self) -> H256;
11    /// Insert a value to the trie.
12    fn insert(&mut self, key: &[u8], value: &[u8]);
13    /// Delete a value in the trie.
14    fn delete(&mut self, key: &[u8]);
15    /// Get a value in the trie.
16    fn get(&self, key: &[u8]) -> Option<Vec<u8>>;
17}
18
19/// Represents a mutable trie that is operated on any RLP values.
20#[derive(Clone, Debug)]
21pub struct AnyTrieMut<T: TrieMut>(T);
22
23impl<T: TrieMut + Default> Default for AnyTrieMut<T> {
24    fn default() -> Self {
25        AnyTrieMut::new(T::default())
26    }
27}
28
29impl<T: TrieMut> AnyTrieMut<T> {
30    /// Into the underlying TrieMut object.
31    pub fn to_trie(self) -> T {
32        self.0
33    }
34
35    /// Initialize a new mutable trie.
36    pub fn new(trie: T) -> Self {
37        AnyTrieMut(trie)
38    }
39
40    /// Get the root hash of the current trie.
41    pub fn root(&self) -> H256 {
42        self.0.root()
43    }
44
45    /// Insert a value to the trie.
46    pub fn insert<K: rlp::Encodable, V: rlp::Encodable>(&mut self, key: &K, value: &V) {
47        let key = rlp::encode(key).to_vec();
48        let value = rlp::encode(value).to_vec();
49
50        self.0.insert(&key, &value)
51    }
52
53    /// Delete a value in the trie.
54    pub fn delete<K: rlp::Encodable>(&mut self, key: &K) {
55        let key = rlp::encode(key).to_vec();
56
57        self.0.delete(&key)
58    }
59
60    /// Get a value in the trie.
61    pub fn get<K: rlp::Encodable, V: rlp::Decodable>(&self, key: &K) -> Option<V> {
62        let key = rlp::encode(key).to_vec();
63        let value = self.0.get(&key);
64
65        match value {
66            Some(value) => Some(rlp::decode(&value)),
67            None => None,
68        }
69    }
70}
71
72/// Represents a mutable trie that is operated on a fixed RLP value type.
73#[derive(Clone, Debug)]
74pub struct FixedTrieMut<T: TrieMut, K: rlp::Encodable, V: rlp::Encodable + rlp::Decodable>(AnyTrieMut<T>, PhantomData<(K, V)>);
75
76impl<T: TrieMut + Default, K: rlp::Encodable, V: rlp::Encodable + rlp::Decodable> Default for FixedTrieMut<T, K, V> {
77    fn default() -> Self {
78        FixedTrieMut::new(T::default())
79    }
80}
81
82impl<T: TrieMut, K: rlp::Encodable, V: rlp::Encodable + rlp::Decodable> FixedTrieMut<T, K, V> {
83    /// Into the underlying TrieMut object.
84    pub fn to_trie(self) -> T {
85        self.0.to_trie()
86    }
87
88    /// Initialize a new mutable trie.
89    pub fn new(trie: T) -> Self {
90        FixedTrieMut(AnyTrieMut::new(trie), PhantomData)
91    }
92
93    /// Get the root hash of the current trie.
94    pub fn root(&self) -> H256 {
95        self.0.root()
96    }
97
98    /// Insert a value to the trie.
99    pub fn insert(&mut self, key: &K, value: &V) {
100        self.0.insert(key, value)
101    }
102
103    /// Delete a value in the trie.
104    pub fn delete(&mut self, key: &K) {
105        self.0.delete(key)
106    }
107
108    /// Get a value in the trie.
109    pub fn get(&self, key: &K) -> Option<V> {
110        self.0.get(key)
111    }
112}
113
114/// Represents a secure mutable trie where the key is hashed.
115#[derive(Clone, Debug)]
116pub struct SecureTrieMut<T: TrieMut>(T);
117
118impl<T: TrieMut + Default> Default for SecureTrieMut<T> {
119    fn default() -> Self {
120        SecureTrieMut::new(T::default())
121    }
122}
123
124impl<T: TrieMut> SecureTrieMut<T> {
125    /// Into the underlying TrieMut object.
126    pub fn to_trie(self) -> T {
127        self.0
128    }
129
130    /// Initialize a new mutable trie.
131    pub fn new(trie: T) -> Self {
132        SecureTrieMut(trie)
133    }
134
135    fn secure_key<K: AsRef<[u8]>>(key: &K) -> Vec<u8> {
136        Keccak256::digest(key.as_ref()).as_slice().into()
137    }
138
139    /// Get the root hash of the current trie.
140    pub fn root(&self) -> H256 {
141        self.0.root()
142    }
143
144    /// Insert a value to the trie.
145    pub fn insert<K: AsRef<[u8]>>(&mut self, key: &K, value: &[u8]) {
146        self.0.insert(&Self::secure_key(key), value)
147    }
148
149    /// Delete a value in the trie.
150    pub fn delete<K: AsRef<[u8]>>(&mut self, key: &K) {
151        self.0.delete(&Self::secure_key(key))
152    }
153
154    /// Get a value in the trie.
155    pub fn get<K: AsRef<[u8]>>(&self, key: &K) -> Option<Vec<u8>> {
156        self.0.get(&Self::secure_key(key))
157    }
158}
159
160/// Represents a secure mutable trie where the key is hashed, and
161/// operated on any RLP values.
162#[derive(Clone, Debug)]
163pub struct AnySecureTrieMut<T: TrieMut>(SecureTrieMut<T>);
164
165impl<T: TrieMut + Default> Default for AnySecureTrieMut<T> {
166    fn default() -> Self {
167        AnySecureTrieMut::new(T::default())
168    }
169}
170
171impl<T: TrieMut> AnySecureTrieMut<T> {
172    /// Into the underlying TrieMut object.
173    pub fn to_trie(self) -> T {
174        self.0.to_trie()
175    }
176
177    /// Initialize a new mutable trie.
178    pub fn new(trie: T) -> Self {
179        AnySecureTrieMut(SecureTrieMut::new(trie))
180    }
181
182    /// Get the root hash of the current trie.
183    pub fn root(&self) -> H256 {
184        self.0.root()
185    }
186
187    /// Insert a value to the trie.
188    pub fn insert<K: AsRef<[u8]>, V: rlp::Encodable>(&mut self, key: &K, value: &V) {
189        self.0.insert(&key, &rlp::encode(value).to_vec())
190    }
191
192    /// Delete a value in the trie.
193    pub fn delete<K: AsRef<[u8]>>(&mut self, key: &K) {
194        self.0.delete(&key)
195    }
196
197    /// Get a value in the trie.
198    pub fn get<K: AsRef<[u8]>, V: rlp::Decodable>(&self, key: &K) -> Option<V> {
199        let value = self.0.get(&key);
200
201        match value {
202            Some(value) => Some(rlp::decode(&value)),
203            None => None,
204        }
205    }
206}
207
208/// Represents a secure mutable trie where the key is hashed, and
209/// operated on a fixed RLP value type.
210#[derive(Clone, Debug)]
211pub struct FixedSecureTrieMut<T: TrieMut, K: AsRef<[u8]>, V: rlp::Encodable + rlp::Decodable>(AnySecureTrieMut<T>, PhantomData<(K, V)>);
212
213impl<T: TrieMut + Default, K: AsRef<[u8]>, V: rlp::Encodable + rlp::Decodable> Default for FixedSecureTrieMut<T, K, V> {
214    fn default() -> Self {
215        FixedSecureTrieMut::new(T::default())
216    }
217}
218
219impl<T: TrieMut, K: AsRef<[u8]>, V: rlp::Encodable + rlp::Decodable> FixedSecureTrieMut<T, K, V> {
220    /// Into the underlying TrieMut object.
221    pub fn to_trie(self) -> T {
222        self.0.to_trie()
223    }
224
225    /// Initialize a new mutable trie.
226    pub fn new(trie: T) -> Self {
227        FixedSecureTrieMut(AnySecureTrieMut::new(trie), PhantomData)
228    }
229
230    /// Get the root hash of the current trie.
231    pub fn root(&self) -> H256 {
232        self.0.root()
233    }
234
235    /// Insert a value to the trie.
236    pub fn insert(&mut self, key: &K, value: &V) {
237        self.0.insert(key, value)
238    }
239
240    /// Delete a value in the trie.
241    pub fn delete(&mut self, key: &K) {
242        self.0.delete(key)
243    }
244
245    /// Get a value in the trie.
246    pub fn get(&self, key: &K) -> Option<V> {
247        self.0.get(key)
248    }
249}