trie_db/
sectriedb.rs

1// Copyright 2017, 2020 Parity Technologies
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::{
16	rstd::boxed::Box, triedb::TrieDB, CError, DBValue, MerkleValue, Query, Result, Trie,
17	TrieDBBuilder, TrieHash, TrieItem, TrieIterator, TrieKeyItem, TrieLayout,
18};
19use hash_db::{HashDBRef, Hasher};
20
21/// A `Trie` implementation which hashes keys and uses a generic `HashDB` backing database.
22///
23/// Use it as a `Trie` trait object. You can use `raw()` to get the backing `TrieDB` object.
24pub struct SecTrieDB<'db, 'cache, L>
25where
26	L: TrieLayout,
27{
28	raw: TrieDB<'db, 'cache, L>,
29}
30
31impl<'db, 'cache, L> SecTrieDB<'db, 'cache, L>
32where
33	L: TrieLayout,
34{
35	/// Create a new trie with the backing database `db` and `root`.
36	///
37	/// Initialise to the state entailed by the genesis block.
38	/// This guarantees the trie is built correctly.
39	pub fn new(db: &'db dyn HashDBRef<L::Hash, DBValue>, root: &'db TrieHash<L>) -> Self {
40		SecTrieDB { raw: TrieDBBuilder::new(db, root).build() }
41	}
42
43	/// Get a reference to the underlying raw `TrieDB` struct.
44	pub fn raw(&self) -> &TrieDB<'db, 'cache, L> {
45		&self.raw
46	}
47
48	/// Get a mutable reference to the underlying raw `TrieDB` struct.
49	pub fn raw_mut(&mut self) -> &mut TrieDB<'db, 'cache, L> {
50		&mut self.raw
51	}
52}
53
54impl<'db, 'cache, L> Trie<L> for SecTrieDB<'db, 'cache, L>
55where
56	L: TrieLayout,
57{
58	fn root(&self) -> &TrieHash<L> {
59		self.raw.root()
60	}
61
62	fn contains(&self, key: &[u8]) -> Result<bool, TrieHash<L>, CError<L>> {
63		self.raw.contains(L::Hash::hash(key).as_ref())
64	}
65
66	fn get_hash(&self, key: &[u8]) -> Result<Option<TrieHash<L>>, TrieHash<L>, CError<L>> {
67		self.raw.get_hash(key)
68	}
69
70	fn get_with<Q: Query<L::Hash>>(
71		&self,
72		key: &[u8],
73		query: Q,
74	) -> Result<Option<Q::Item>, TrieHash<L>, CError<L>> {
75		self.raw.get_with(L::Hash::hash(key).as_ref(), query)
76	}
77
78	fn lookup_first_descendant(
79		&self,
80		key: &[u8],
81	) -> Result<Option<MerkleValue<TrieHash<L>>>, TrieHash<L>, CError<L>> {
82		self.raw.lookup_first_descendant(key)
83	}
84
85	fn iter<'a>(
86		&'a self,
87	) -> Result<
88		Box<dyn TrieIterator<L, Item = TrieItem<TrieHash<L>, CError<L>>> + 'a>,
89		TrieHash<L>,
90		CError<L>,
91	> {
92		TrieDB::iter(&self.raw)
93	}
94
95	fn key_iter<'a>(
96		&'a self,
97	) -> Result<
98		Box<dyn TrieIterator<L, Item = TrieKeyItem<TrieHash<L>, CError<L>>> + 'a>,
99		TrieHash<L>,
100		CError<L>,
101	> {
102		TrieDB::key_iter(&self.raw)
103	}
104}