1#![cfg_attr(not(feature = "std"), no_std)]
15
16#[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
83pub type DBValue = Vec<u8>;
85
86#[derive(PartialEq, Eq, Clone, Debug)]
91pub enum TrieError<T, E> {
92 InvalidStateRoot(T),
94 IncompleteDatabase(T),
96 ValueAtIncompleteKey(Vec<u8>, u8),
100 DecoderError(T, E),
102 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
140pub type Result<T, H, E> = crate::rstd::result::Result<T, Box<TrieError<H, E>>>;
143
144pub type TrieItem<U, E> = Result<(Vec<u8>, DBValue), U, E>;
146
147pub type TrieKeyItem<U, E> = Result<Vec<u8>, U, E>;
149
150pub trait Query<H: Hasher> {
152 type Item;
154
155 fn decode(self, data: &[u8]) -> Self::Item;
157}
158
159#[cfg_attr(feature = "std", derive(Debug))]
165pub enum TrieAccess<'a, H> {
166 NodeOwned { hash: H, node_owned: &'a NodeOwned<H> },
168 EncodedNode { hash: H, encoded_node: rstd::borrow::Cow<'a, [u8]> },
170 Value { hash: H, value: rstd::borrow::Cow<'a, [u8]>, full_key: &'a [u8] },
176 Hash { full_key: &'a [u8] },
180 NonExisting { full_key: &'a [u8] },
184}
185
186#[derive(Debug, Clone, Copy)]
188pub enum RecordedForKey {
189 Value,
198 Hash,
209 None,
213}
214
215impl RecordedForKey {
216 pub fn is_none(&self) -> bool {
218 matches!(self, Self::None)
219 }
220}
221
222pub trait TrieRecorder<H> {
227 fn record<'a>(&mut self, access: TrieAccess<'a, H>);
232
233 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
249pub trait Trie<L: TrieLayout> {
251 fn root(&self) -> &TrieHash<L>;
253
254 fn is_empty(&self) -> bool {
256 *self.root() == L::Codec::hashed_null_node()
257 }
258
259 fn contains(&self, key: &[u8]) -> Result<bool, TrieHash<L>, CError<L>> {
261 self.get(key).map(|x| x.is_some())
262 }
263
264 fn get_hash(&self, key: &[u8]) -> Result<Option<TrieHash<L>>, TrieHash<L>, CError<L>>;
266
267 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 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 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 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
299pub trait TrieMut<L: TrieLayout> {
301 fn root(&mut self) -> &TrieHash<L>;
303
304 fn is_empty(&self) -> bool;
306
307 fn contains(&self, key: &[u8]) -> Result<bool, TrieHash<L>, CError<L>> {
309 self.get(key).map(|x| x.is_some())
310 }
311
312 fn get<'a, 'key>(&'a self, key: &'key [u8]) -> Result<Option<DBValue>, TrieHash<L>, CError<L>>
314 where
315 'a: 'key;
316
317 fn insert(
320 &mut self,
321 key: &[u8],
322 value: &[u8],
323 ) -> Result<Option<Value<L>>, TrieHash<L>, CError<L>>;
324
325 fn remove(&mut self, key: &[u8]) -> Result<Option<Value<L>>, TrieHash<L>, CError<L>>;
328}
329
330pub trait TrieIterator<L: TrieLayout>: Iterator {
332 fn seek(&mut self, key: &[u8]) -> Result<(), TrieHash<L>, CError<L>>;
334}
335
336#[derive(PartialEq, Clone)]
338#[cfg_attr(feature = "std", derive(Debug))]
339pub enum TrieSpec {
340 Generic,
342 Secure,
344 Fat,
346}
347
348impl Default for TrieSpec {
349 fn default() -> TrieSpec {
350 TrieSpec::Secure
351 }
352}
353
354#[derive(Default, Clone)]
356pub struct TrieFactory {
357 spec: TrieSpec,
358}
359
360pub enum TrieKinds<'db, 'cache, L: TrieLayout> {
363 Generic(TrieDB<'db, 'cache, L>),
365 Secure(SecTrieDB<'db, 'cache, L>),
367 Fat(FatDB<'db, 'cache, L>),
369}
370
371macro_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 pub fn new(spec: TrieSpec) -> Self {
431 TrieFactory { spec }
432 }
433
434 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 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 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 pub fn is_fat(&self) -> bool {
475 self.spec == TrieSpec::Fat
476 }
477}
478
479pub trait TrieLayout {
483 const USE_EXTENSION: bool;
487 const ALLOW_EMPTY: bool = false;
489 const MAX_INLINE_VALUE: Option<u32>;
492
493 type Hash: Hasher;
495 type Codec: NodeCodec<HashOut = <Self::Hash as Hasher>::Out>;
497}
498
499pub trait TrieConfiguration: Sized + TrieLayout {
503 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 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 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 fn encode_index(input: u32) -> Vec<u8> {
540 input.to_be_bytes().to_vec()
542 }
543 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
556pub type TrieHash<L> = <<L as TrieLayout>::Hash as Hasher>::Out;
558pub type CError<L> = <<L as TrieLayout>::Codec as NodeCodec>::Error;
560
561#[derive(Clone, Debug)]
563pub enum CachedValue<H> {
564 NonExisting,
566 ExistingHash(H),
568 Existing {
570 hash: H,
572 data: BytesWeak,
578 },
579}
580
581impl<H: Copy> CachedValue<H> {
582 pub fn data(&self) -> Option<Option<Bytes>> {
588 match self {
589 Self::Existing { data, .. } => Some(data.upgrade()),
590 _ => None,
591 }
592 }
593
594 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
629pub trait TrieCache<NC: NodeCodec> {
646 fn lookup_value_for_key(&mut self, key: &[u8]) -> Option<&CachedValue<NC::HashOut>>;
660
661 fn cache_value_for_key(&mut self, key: &[u8], value: CachedValue<NC::HashOut>);
669
670 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 fn get_node(&mut self, hash: &NC::HashOut) -> Option<&NodeOwned<NC::HashOut>>;
685}
686
687#[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#[derive(Clone, Debug)]
725pub struct BytesWeak(rstd::sync::Weak<[u8]>);
726
727impl BytesWeak {
728 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}