Skip to main content

trie_db/
lib.rs

1// Copyright 2017, 2021 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#![cfg_attr(not(feature = "std"), no_std)]
15
16//! Trie interface and implementation.
17
18#[cfg(not(feature = "std"))]
19extern crate alloc;
20
21#[cfg(feature = "std")]
22mod rstd {
23	pub use std::{
24		borrow, boxed, cmp, collections::VecDeque, convert, error::Error, fmt, hash, iter, marker,
25		mem, ops, rc, result, sync, vec,
26	};
27}
28
29#[cfg(not(feature = "std"))]
30mod rstd {
31	pub use alloc::{borrow, boxed, collections::VecDeque, rc, sync, vec};
32	pub use core::{cmp, convert, fmt, hash, iter, marker, mem, ops, result};
33	pub trait Error {}
34	impl<T> Error for T {}
35}
36
37#[cfg(feature = "std")]
38use self::rstd::{fmt, Error};
39
40use self::rstd::{boxed::Box, vec::Vec};
41use hash_db::MaybeDebug;
42use node::NodeOwned;
43
44pub mod node;
45pub mod proof;
46pub mod recorder;
47pub mod sectriedb;
48pub mod sectriedbmut;
49pub mod triedb;
50pub mod triedbmut;
51
52mod fatdb;
53mod fatdbmut;
54mod iter_build;
55mod iterator;
56mod lookup;
57mod nibble;
58mod node_codec;
59mod trie_codec;
60
61pub use self::{
62	fatdb::{FatDB, FatDBIterator},
63	fatdbmut::FatDBMut,
64	lookup::Lookup,
65	nibble::{nibble_ops, NibbleSlice, NibbleVec},
66	recorder::Recorder,
67	sectriedb::SecTrieDB,
68	sectriedbmut::SecTrieDBMut,
69	triedb::{TrieDB, TrieDBBuilder, TrieDBIterator, TrieDBKeyIterator},
70	triedbmut::{ChildReference, TrieDBMut, TrieDBMutBuilder, Value},
71};
72pub use crate::{
73	iter_build::{trie_visit, ProcessEncodedNode, TrieBuilder, TrieRoot, TrieRootUnhashed},
74	iterator::{TrieDBNodeIterator, TrieDBRawIterator},
75	node_codec::{NodeCodec, Partial},
76	trie_codec::{decode_compact, decode_compact_from_iter, encode_compact},
77};
78pub use hash_db::{HashDB, HashDBRef, Hasher};
79
80#[cfg(feature = "std")]
81pub use crate::iter_build::TrieRootPrint;
82
83/// Database value
84pub type DBValue = Vec<u8>;
85
86/// Trie Errors.
87///
88/// These borrow the data within them to avoid excessive copying on every
89/// trie operation.
90#[derive(PartialEq, Eq, Clone, Debug)]
91pub enum TrieError<T, E> {
92	/// Attempted to create a trie with a state root not in the DB.
93	InvalidStateRoot(T),
94	/// Trie item not found in the database,
95	IncompleteDatabase(T),
96	/// A value was found in the trie with a nibble key that was not byte-aligned.
97	/// The first parameter is the byte-aligned part of the prefix and the second parameter is the
98	/// remaining nibble.
99	ValueAtIncompleteKey(Vec<u8>, u8),
100	/// Corrupt Trie item.
101	DecoderError(T, E),
102	/// Hash is not value.
103	InvalidHash(T, Vec<u8>),
104}
105
106#[cfg(feature = "std")]
107impl<T, E> fmt::Display for TrieError<T, E>
108where
109	T: MaybeDebug,
110	E: MaybeDebug,
111{
112	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
113		match *self {
114			TrieError::InvalidStateRoot(ref root) => write!(f, "Invalid state root: {:?}", root),
115			TrieError::IncompleteDatabase(ref missing) =>
116				write!(f, "Database missing expected key: {:?}", missing),
117			TrieError::ValueAtIncompleteKey(ref bytes, ref extra) =>
118				write!(f, "Value found in trie at incomplete key {:?} + {:?}", bytes, extra),
119			TrieError::DecoderError(ref hash, ref decoder_err) => {
120				write!(f, "Decoding failed for hash {:?}; err: {:?}", hash, decoder_err)
121			},
122			TrieError::InvalidHash(ref hash, ref data) => write!(
123				f,
124				"Encoded node {:?} contains invalid hash reference with length: {}",
125				hash,
126				data.len()
127			),
128		}
129	}
130}
131
132#[cfg(feature = "std")]
133impl<T, E> Error for TrieError<T, E>
134where
135	T: fmt::Debug,
136	E: Error,
137{
138}
139
140/// Trie result type.
141/// Boxed to avoid copying around extra space for the `Hasher`s `Out` on successful queries.
142pub type Result<T, H, E> = crate::rstd::result::Result<T, Box<TrieError<H, E>>>;
143
144/// Trie-Item type used for iterators over trie data.
145pub type TrieItem<U, E> = Result<(Vec<u8>, DBValue), U, E>;
146
147/// Trie-Item type used for iterators over trie key only.
148pub type TrieKeyItem<U, E> = Result<Vec<u8>, U, E>;
149
150/// Description of what kind of query will be made to the trie.
151pub trait Query<H: Hasher> {
152	/// Output item.
153	type Item;
154
155	/// Decode a byte-slice into the desired item.
156	fn decode(self, data: &[u8]) -> Self::Item;
157}
158
159/// Used to report the trie access to the [`TrieRecorder`].
160///
161/// As the trie can use a [`TrieCache`], there are multiple kinds of accesses.
162/// If a cache is used, [`Self::Key`] and [`Self::NodeOwned`] are possible
163/// values. Otherwise only [`Self::EncodedNode`] is a possible value.
164#[cfg_attr(feature = "std", derive(Debug))]
165pub enum TrieAccess<'a, H> {
166	/// The given [`NodeOwned`] was accessed using its `hash`.
167	NodeOwned { hash: H, node_owned: &'a NodeOwned<H> },
168	/// The given `encoded_node` was accessed using its `hash`.
169	EncodedNode { hash: H, encoded_node: rstd::borrow::Cow<'a, [u8]> },
170	/// The given `value` was accessed using its `hash`.
171	///
172	/// The given `full_key` is the key to access this value in the trie.
173	///
174	/// Should map to [`RecordedForKey::Value`] when checking the recorder.
175	Value { hash: H, value: rstd::borrow::Cow<'a, [u8]>, full_key: &'a [u8] },
176	/// The hash of the value for the given `full_key` was accessed.
177	///
178	/// Should map to [`RecordedForKey::Hash`] when checking the recorder.
179	Hash { full_key: &'a [u8] },
180	/// The value/hash for `full_key` was accessed, but it couldn't be found in the trie.
181	///
182	/// Should map to [`RecordedForKey::Value`] when checking the recorder.
183	NonExisting { full_key: &'a [u8] },
184}
185
186/// Result of [`TrieRecorder::trie_nodes_recorded_for_key`].
187#[derive(Debug, Clone, Copy)]
188pub enum RecordedForKey {
189	/// We recorded all trie nodes up to the value for a storage key.
190	///
191	/// This should be returned when the recorder has seen the following [`TrieAccess`]:
192	///
193	/// - [`TrieAccess::Value`]: If we see this [`TrieAccess`], it means we have recorded all the
194	///   trie nodes up to the value.
195	/// - [`TrieAccess::NonExisting`]: If we see this [`TrieAccess`], it means we have recorded all
196	///   the necessary  trie nodes to prove that the value doesn't exist in the trie.
197	Value,
198	/// We recorded all trie nodes up to the value hash for a storage key.
199	///
200	/// If we have a [`RecordedForKey::Value`], it means that we also have the hash of this value.
201	/// This also means that if we first have recorded the hash of a value and then also record the
202	/// value, the access should be upgraded to [`RecordedForKey::Value`].
203	///
204	/// This should be returned when the recorder has seen the following [`TrieAccess`]:
205	///
206	/// - [`TrieAccess::Hash`]: If we see this [`TrieAccess`], it means we have recorded all trie
207	///   nodes to have the hash of the value.
208	Hash,
209	/// We haven't recorded any trie nodes yet for a storage key.
210	///
211	/// This means we have not seen any [`TrieAccess`] referencing the searched key.
212	None,
213}
214
215impl RecordedForKey {
216	/// Is `self` equal to [`Self::None`]?
217	pub fn is_none(&self) -> bool {
218		matches!(self, Self::None)
219	}
220}
221
222/// A trie recorder that can be used to record all kind of [`TrieAccess`]'s.
223///
224/// To build a trie proof a recorder is required that records all trie accesses. These recorded trie
225/// accesses can then be used to create the proof.
226pub trait TrieRecorder<H> {
227	/// Record the given [`TrieAccess`].
228	///
229	/// Depending on the [`TrieAccess`] a call of [`Self::trie_nodes_recorded_for_key`] afterwards
230	/// must return the correct recorded state.
231	fn record<'a>(&mut self, access: TrieAccess<'a, H>);
232
233	/// Check if we have recorded any trie nodes for the given `key`.
234	///
235	/// Returns [`RecordedForKey`] to express the state of the recorded trie nodes.
236	fn trie_nodes_recorded_for_key(&self, key: &[u8]) -> RecordedForKey;
237}
238
239impl<F, T, H: Hasher> Query<H> for F
240where
241	F: for<'a> FnOnce(&'a [u8]) -> T,
242{
243	type Item = T;
244	fn decode(self, value: &[u8]) -> T {
245		(self)(value)
246	}
247}
248
249/// A key-value datastore implemented as a database-backed modified Merkle tree.
250pub trait Trie<L: TrieLayout> {
251	/// Return the root of the trie.
252	fn root(&self) -> &TrieHash<L>;
253
254	/// Is the trie empty?
255	fn is_empty(&self) -> bool {
256		*self.root() == L::Codec::hashed_null_node()
257	}
258
259	/// Does the trie contain a given key?
260	fn contains(&self, key: &[u8]) -> Result<bool, TrieHash<L>, CError<L>> {
261		self.get(key).map(|x| x.is_some())
262	}
263
264	/// Returns the hash of the value for `key`.
265	fn get_hash(&self, key: &[u8]) -> Result<Option<TrieHash<L>>, TrieHash<L>, CError<L>>;
266
267	/// What is the value of the given key in this trie?
268	fn get(&self, key: &[u8]) -> Result<Option<DBValue>, TrieHash<L>, CError<L>> {
269		self.get_with(key, |v: &[u8]| v.to_vec())
270	}
271
272	/// Search for the key with the given query parameter. See the docs of the `Query`
273	/// trait for more details.
274	fn get_with<Q: Query<L::Hash>>(
275		&self,
276		key: &[u8],
277		query: Q,
278	) -> Result<Option<Q::Item>, TrieHash<L>, CError<L>>;
279
280	/// Returns a depth-first iterator over the elements of trie.
281	fn iter<'a>(
282		&'a self,
283	) -> Result<
284		Box<dyn TrieIterator<L, Item = TrieItem<TrieHash<L>, CError<L>>> + 'a>,
285		TrieHash<L>,
286		CError<L>,
287	>;
288
289	/// Returns a depth-first iterator over the keys of elemets of trie.
290	fn key_iter<'a>(
291		&'a self,
292	) -> Result<
293		Box<dyn TrieIterator<L, Item = TrieKeyItem<TrieHash<L>, CError<L>>> + 'a>,
294		TrieHash<L>,
295		CError<L>,
296	>;
297}
298
299/// A key-value datastore implemented as a database-backed modified Merkle tree.
300pub trait TrieMut<L: TrieLayout> {
301	/// Return the root of the trie.
302	fn root(&mut self) -> &TrieHash<L>;
303
304	/// Is the trie empty?
305	fn is_empty(&self) -> bool;
306
307	/// Does the trie contain a given key?
308	fn contains(&self, key: &[u8]) -> Result<bool, TrieHash<L>, CError<L>> {
309		self.get(key).map(|x| x.is_some())
310	}
311
312	/// What is the value of the given key in this trie?
313	fn get<'a, 'key>(&'a self, key: &'key [u8]) -> Result<Option<DBValue>, TrieHash<L>, CError<L>>
314	where
315		'a: 'key;
316
317	/// Insert a `key`/`value` pair into the trie. An empty value is equivalent to removing
318	/// `key` from the trie. Returns the old value associated with this key, if it existed.
319	fn insert(
320		&mut self,
321		key: &[u8],
322		value: &[u8],
323	) -> Result<Option<Value<L>>, TrieHash<L>, CError<L>>;
324
325	/// Remove a `key` from the trie. Equivalent to making it equal to the empty
326	/// value. Returns the old value associated with this key, if it existed.
327	fn remove(&mut self, key: &[u8]) -> Result<Option<Value<L>>, TrieHash<L>, CError<L>>;
328}
329
330/// A trie iterator that also supports random access (`seek()`).
331pub trait TrieIterator<L: TrieLayout>: Iterator {
332	/// Position the iterator on the first element with key >= `key`
333	fn seek(&mut self, key: &[u8]) -> Result<(), TrieHash<L>, CError<L>>;
334}
335
336/// Trie types
337#[derive(PartialEq, Clone)]
338#[cfg_attr(feature = "std", derive(Debug))]
339pub enum TrieSpec {
340	/// Generic trie.
341	Generic,
342	/// Secure trie.
343	Secure,
344	///	Secure trie with fat database.
345	Fat,
346}
347
348impl Default for TrieSpec {
349	fn default() -> TrieSpec {
350		TrieSpec::Secure
351	}
352}
353
354/// Trie factory.
355#[derive(Default, Clone)]
356pub struct TrieFactory {
357	spec: TrieSpec,
358}
359
360/// All different kinds of tries.
361/// This is used to prevent a heap allocation for every created trie.
362pub enum TrieKinds<'db, 'cache, L: TrieLayout> {
363	/// A generic trie db.
364	Generic(TrieDB<'db, 'cache, L>),
365	/// A secure trie db.
366	Secure(SecTrieDB<'db, 'cache, L>),
367	/// A fat trie db.
368	Fat(FatDB<'db, 'cache, L>),
369}
370
371// wrapper macro for making the match easier to deal with.
372macro_rules! wrapper {
373	($me: ident, $f_name: ident, $($param: ident),*) => {
374		match *$me {
375			TrieKinds::Generic(ref t) => t.$f_name($($param),*),
376			TrieKinds::Secure(ref t) => t.$f_name($($param),*),
377			TrieKinds::Fat(ref t) => t.$f_name($($param),*),
378		}
379	}
380}
381
382impl<'db, 'cache, L: TrieLayout> Trie<L> for TrieKinds<'db, 'cache, L> {
383	fn root(&self) -> &TrieHash<L> {
384		wrapper!(self, root,)
385	}
386
387	fn is_empty(&self) -> bool {
388		wrapper!(self, is_empty,)
389	}
390
391	fn contains(&self, key: &[u8]) -> Result<bool, TrieHash<L>, CError<L>> {
392		wrapper!(self, contains, key)
393	}
394
395	fn get_hash(&self, key: &[u8]) -> Result<Option<TrieHash<L>>, TrieHash<L>, CError<L>> {
396		wrapper!(self, get_hash, key)
397	}
398
399	fn get_with<Q: Query<L::Hash>>(
400		&self,
401		key: &[u8],
402		query: Q,
403	) -> Result<Option<Q::Item>, TrieHash<L>, CError<L>> {
404		wrapper!(self, get_with, key, query)
405	}
406
407	fn iter<'a>(
408		&'a self,
409	) -> Result<
410		Box<dyn TrieIterator<L, Item = TrieItem<TrieHash<L>, CError<L>>> + 'a>,
411		TrieHash<L>,
412		CError<L>,
413	> {
414		wrapper!(self, iter,)
415	}
416
417	fn key_iter<'a>(
418		&'a self,
419	) -> Result<
420		Box<dyn TrieIterator<L, Item = TrieKeyItem<TrieHash<L>, CError<L>>> + 'a>,
421		TrieHash<L>,
422		CError<L>,
423	> {
424		wrapper!(self, key_iter,)
425	}
426}
427
428impl TrieFactory {
429	/// Creates new factory.
430	pub fn new(spec: TrieSpec) -> Self {
431		TrieFactory { spec }
432	}
433
434	/// Create new immutable instance of Trie.
435	pub fn readonly<'db, 'cache, L: TrieLayout>(
436		&self,
437		db: &'db dyn HashDBRef<L::Hash, DBValue>,
438		root: &'db TrieHash<L>,
439	) -> TrieKinds<'db, 'cache, L> {
440		match self.spec {
441			TrieSpec::Generic => TrieKinds::Generic(TrieDBBuilder::new(db, root).build()),
442			TrieSpec::Secure => TrieKinds::Secure(SecTrieDB::new(db, root)),
443			TrieSpec::Fat => TrieKinds::Fat(FatDB::new(db, root)),
444		}
445	}
446
447	/// Create new mutable instance of Trie.
448	pub fn create<'db, L: TrieLayout + 'db>(
449		&self,
450		db: &'db mut dyn HashDB<L::Hash, DBValue>,
451		root: &'db mut TrieHash<L>,
452	) -> Box<dyn TrieMut<L> + 'db> {
453		match self.spec {
454			TrieSpec::Generic => Box::new(TrieDBMutBuilder::<L>::new(db, root).build()),
455			TrieSpec::Secure => Box::new(SecTrieDBMut::<L>::new(db, root)),
456			TrieSpec::Fat => Box::new(FatDBMut::<L>::new(db, root)),
457		}
458	}
459
460	/// Create new mutable instance of trie and check for errors.
461	pub fn from_existing<'db, L: TrieLayout + 'db>(
462		&self,
463		db: &'db mut dyn HashDB<L::Hash, DBValue>,
464		root: &'db mut TrieHash<L>,
465	) -> Box<dyn TrieMut<L> + 'db> {
466		match self.spec {
467			TrieSpec::Generic => Box::new(TrieDBMutBuilder::<L>::from_existing(db, root).build()),
468			TrieSpec::Secure => Box::new(SecTrieDBMut::<L>::from_existing(db, root)),
469			TrieSpec::Fat => Box::new(FatDBMut::<L>::from_existing(db, root)),
470		}
471	}
472
473	/// Returns true iff the trie DB is a fat DB (allows enumeration of keys).
474	pub fn is_fat(&self) -> bool {
475		self.spec == TrieSpec::Fat
476	}
477}
478
479/// Trait with definition of trie layout.
480/// Contains all associated trait needed for
481/// a trie definition or implementation.
482pub trait TrieLayout {
483	/// If true, the trie will use extension nodes and
484	/// no partial in branch, if false the trie will only
485	/// use branch and node with partials in both.
486	const USE_EXTENSION: bool;
487	/// If true, the trie will allow empty values into `TrieDBMut`
488	const ALLOW_EMPTY: bool = false;
489	/// Threshold above which an external node should be
490	/// use to store a node value.
491	const MAX_INLINE_VALUE: Option<u32>;
492
493	/// Hasher to use for this trie.
494	type Hash: Hasher;
495	/// Codec to use (needs to match hasher and nibble ops).
496	type Codec: NodeCodec<HashOut = <Self::Hash as Hasher>::Out>;
497}
498
499/// This trait associates a trie definition with preferred methods.
500/// It also contains own default implementations and can be
501/// used to allow switching implementation.
502pub trait TrieConfiguration: Sized + TrieLayout {
503	/// Operation to build a trie db from its ordered iterator over its key/values.
504	fn trie_build<DB, I, A, B>(db: &mut DB, input: I) -> <Self::Hash as Hasher>::Out
505	where
506		DB: HashDB<Self::Hash, DBValue>,
507		I: IntoIterator<Item = (A, B)>,
508		A: AsRef<[u8]> + Ord,
509		B: AsRef<[u8]>,
510	{
511		let mut cb = TrieBuilder::<Self, DB>::new(db);
512		trie_visit::<Self, _, _, _, _>(input.into_iter(), &mut cb);
513		cb.root.unwrap_or_default()
514	}
515	/// Determines a trie root given its ordered contents, closed form.
516	fn trie_root<I, A, B>(input: I) -> <Self::Hash as Hasher>::Out
517	where
518		I: IntoIterator<Item = (A, B)>,
519		A: AsRef<[u8]> + Ord,
520		B: AsRef<[u8]>,
521	{
522		let mut cb = TrieRoot::<Self>::default();
523		trie_visit::<Self, _, _, _, _>(input.into_iter(), &mut cb);
524		cb.root.unwrap_or_default()
525	}
526	/// Determines a trie root node's data given its ordered contents, closed form.
527	fn trie_root_unhashed<I, A, B>(input: I) -> Vec<u8>
528	where
529		I: IntoIterator<Item = (A, B)>,
530		A: AsRef<[u8]> + Ord,
531		B: AsRef<[u8]>,
532	{
533		let mut cb = TrieRootUnhashed::<Self>::default();
534		trie_visit::<Self, _, _, _, _>(input.into_iter(), &mut cb);
535		cb.root.unwrap_or_default()
536	}
537	/// Encoding of index as a key (when reusing general trie for
538	/// indexed trie).
539	fn encode_index(input: u32) -> Vec<u8> {
540		// be for byte ordering
541		input.to_be_bytes().to_vec()
542	}
543	/// A trie root formed from the items, with keys attached according to their
544	/// compact-encoded index (using `parity-codec` crate).
545	fn ordered_trie_root<I, A>(input: I) -> <Self::Hash as Hasher>::Out
546	where
547		I: IntoIterator<Item = A>,
548		A: AsRef<[u8]>,
549	{
550		Self::trie_root(
551			input.into_iter().enumerate().map(|(i, v)| (Self::encode_index(i as u32), v)),
552		)
553	}
554}
555
556/// Alias accessor to hasher hash output type from a `TrieLayout`.
557pub type TrieHash<L> = <<L as TrieLayout>::Hash as Hasher>::Out;
558/// Alias accessor to `NodeCodec` associated `Error` type from a `TrieLayout`.
559pub type CError<L> = <<L as TrieLayout>::Codec as NodeCodec>::Error;
560
561/// A value as cached by the [`TrieCache`].
562#[derive(Clone, Debug)]
563pub enum CachedValue<H> {
564	/// The value doesn't exist in the trie.
565	NonExisting,
566	/// We cached the hash, because we did not yet accessed the data.
567	ExistingHash(H),
568	/// The value exists in the trie.
569	Existing {
570		/// The hash of the value.
571		hash: H,
572		/// The actual data of the value stored as [`BytesWeak`].
573		///
574		/// The original data [`Bytes`] is stored in the trie node
575		/// that is also cached by the [`TrieCache`]. If this node is dropped,
576		/// this data will also not be "upgradeable" anymore.
577		data: BytesWeak,
578	},
579}
580
581impl<H: Copy> CachedValue<H> {
582	/// Returns the data of the value.
583	///
584	/// If a value doesn't exist in the trie or only the value hash is cached, this function returns
585	/// `None`. If the reference to the data couldn't be upgraded (see [`Bytes::upgrade`]), this
586	/// function returns `Some(None)`, aka the data needs to be fetched again from the trie.
587	pub fn data(&self) -> Option<Option<Bytes>> {
588		match self {
589			Self::Existing { data, .. } => Some(data.upgrade()),
590			_ => None,
591		}
592	}
593
594	/// Returns the hash of the value.
595	///
596	/// Returns only `None` when the value doesn't exist.
597	pub fn hash(&self) -> Option<H> {
598		match self {
599			Self::ExistingHash(hash) | Self::Existing { hash, .. } => Some(*hash),
600			Self::NonExisting => None,
601		}
602	}
603}
604
605impl<H> From<(Bytes, H)> for CachedValue<H> {
606	fn from(value: (Bytes, H)) -> Self {
607		Self::Existing { hash: value.1, data: value.0.into() }
608	}
609}
610
611impl<H> From<H> for CachedValue<H> {
612	fn from(value: H) -> Self {
613		Self::ExistingHash(value)
614	}
615}
616
617impl<H> From<Option<(Bytes, H)>> for CachedValue<H> {
618	fn from(value: Option<(Bytes, H)>) -> Self {
619		value.map_or(Self::NonExisting, |v| Self::Existing { hash: v.1, data: v.0.into() })
620	}
621}
622
623impl<H> From<Option<H>> for CachedValue<H> {
624	fn from(value: Option<H>) -> Self {
625		value.map_or(Self::NonExisting, |v| Self::ExistingHash(v))
626	}
627}
628
629/// A cache that can be used to speed-up certain operations when accessing the trie.
630///
631/// The [`TrieDB`]/[`TrieDBMut`] by default are working with the internal hash-db in a non-owning
632/// mode. This means that for every lookup in the trie, every node is always fetched and decoded on
633/// the fly. Fetching and decoding a node always takes some time and can kill the performance of any
634/// application that is doing quite a lot of trie lookups. To circumvent this performance
635/// degradation, a cache can be used when looking up something in the trie. Any cache that should be
636/// used with the [`TrieDB`]/[`TrieDBMut`] needs to implement this trait.
637///
638/// The trait is laying out a two level cache, first the trie nodes cache and then the value cache.
639/// The trie nodes cache, as the name indicates, is for caching trie nodes as [`NodeOwned`]. These
640/// trie nodes are referenced by their hash. The value cache is caching [`CachedValue`]'s and these
641/// are referenced by the key to look them up in the trie. As multiple different tries can have
642/// different values under the same key, it up to the cache implementation to ensure that the
643/// correct value is returned. As each trie has a different root, this root can be used to
644/// differentiate values under the same key.
645pub trait TrieCache<NC: NodeCodec> {
646	/// Lookup value for the given `key`.
647	///
648	/// Returns the `None` if the `key` is unknown or otherwise `Some(_)` with the associated
649	/// value.
650	///
651	/// [`Self::cache_data_for_key`] is used to make the cache aware of data that is associated
652	/// to a `key`.
653	///
654	/// # Attention
655	///
656	/// The cache can be used for different tries, aka with different roots. This means
657	/// that the cache implementation needs to take care of always returning the correct value
658	/// for the current trie root.
659	fn lookup_value_for_key(&mut self, key: &[u8]) -> Option<&CachedValue<NC::HashOut>>;
660
661	/// Cache the given `value` for the given `key`.
662	///
663	/// # Attention
664	///
665	/// The cache can be used for different tries, aka with different roots. This means
666	/// that the cache implementation needs to take care of caching `value` for the current
667	/// trie root.
668	fn cache_value_for_key(&mut self, key: &[u8], value: CachedValue<NC::HashOut>);
669
670	/// Get or insert a [`NodeOwned`].
671	///
672	/// The cache implementation should look up based on the given `hash` if the node is already
673	/// known. If the node is not yet known, the given `fetch_node` function can be used to fetch
674	/// the particular node.
675	///
676	/// Returns the [`NodeOwned`] or an error that happened on fetching the node.
677	fn get_or_insert_node(
678		&mut self,
679		hash: NC::HashOut,
680		fetch_node: &mut dyn FnMut() -> Result<NodeOwned<NC::HashOut>, NC::HashOut, NC::Error>,
681	) -> Result<&NodeOwned<NC::HashOut>, NC::HashOut, NC::Error>;
682
683	/// Get the [`NodeOwned`] that corresponds to the given `hash`.
684	fn get_node(&mut self, hash: &NC::HashOut) -> Option<&NodeOwned<NC::HashOut>>;
685}
686
687/// A container for storing bytes.
688///
689/// This uses a reference counted pointer internally, so it is cheap to clone this object.
690#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
691pub struct Bytes(rstd::sync::Arc<[u8]>);
692
693impl rstd::ops::Deref for Bytes {
694	type Target = [u8];
695
696	fn deref(&self) -> &Self::Target {
697		self.0.deref()
698	}
699}
700
701impl From<Vec<u8>> for Bytes {
702	fn from(bytes: Vec<u8>) -> Self {
703		Self(bytes.into())
704	}
705}
706
707impl From<&[u8]> for Bytes {
708	fn from(bytes: &[u8]) -> Self {
709		Self(bytes.into())
710	}
711}
712
713impl<T: AsRef<[u8]>> PartialEq<T> for Bytes {
714	fn eq(&self, other: &T) -> bool {
715		self.as_ref() == other.as_ref()
716	}
717}
718
719/// A weak reference of [`Bytes`].
720///
721/// A weak reference means that it doesn't prevent [`Bytes`] from being dropped because
722/// it holds a non-owning reference to the associated [`Bytes`] object. With [`Self::upgrade`] it
723/// is possible to upgrade it again to [`Bytes`] if the reference is still valid.
724#[derive(Clone, Debug)]
725pub struct BytesWeak(rstd::sync::Weak<[u8]>);
726
727impl BytesWeak {
728	/// Upgrade to [`Bytes`].
729	///
730	/// Returns `None` when the inner value was already dropped.
731	pub fn upgrade(&self) -> Option<Bytes> {
732		self.0.upgrade().map(Bytes)
733	}
734}
735
736impl From<Bytes> for BytesWeak {
737	fn from(bytes: Bytes) -> Self {
738		Self(rstd::sync::Arc::downgrade(&bytes.0))
739	}
740}