tetsy_trie_db/
node_codec.rs

1// Copyright 2017, 2018 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
15//! Generic trait for trie node encoding/decoding. Takes a `tetsy_hash_db::Hasher`
16//! to parametrize the hashes used in the codec.
17
18use crate::MaybeDebug;
19use crate::node::{Node, NodePlan};
20use crate::ChildReference;
21
22use crate::rstd::{borrow::Borrow, Error, hash, vec::Vec};
23
24
25/// Representation of a nible slice (right aligned).
26/// It contains a right aligned padded first byte (first pair element is the number of nibbles
27/// (0 to max nb nibble - 1), second pair element is the padded nibble), and a slice over
28/// the remaining bytes.
29pub type Partial<'a> = ((u8, u8), &'a[u8]);
30
31/// Trait for trie node encoding/decoding.
32pub trait NodeCodec: Sized {
33	/// Codec error type.
34	type Error: Error;
35
36	/// Output type of encoded node hasher.
37	type HashOut: AsRef<[u8]> + AsMut<[u8]> + Default + MaybeDebug + PartialEq + Eq
38		+ hash::Hash + Send + Sync + Clone + Copy;
39
40	/// Get the hashed null node.
41	fn hashed_null_node() -> Self::HashOut;
42
43	/// Decode bytes to a `NodePlan`. Returns `Self::E` on failure.
44	fn decode_plan(data: &[u8]) -> Result<NodePlan, Self::Error>;
45
46	/// Decode bytes to a `Node`. Returns `Self::E` on failure.
47	fn decode(data: &[u8]) -> Result<Node, Self::Error> {
48		Ok(Self::decode_plan(data)?.build(data))
49	}
50
51	/// Check if the provided bytes correspond to the codecs "empty" node.
52	fn is_empty_node(data: &[u8]) -> bool;
53
54	/// Returns an encoded empty node.
55	fn empty_node() -> &'static [u8];
56
57	/// Returns an encoded leaf node
58	fn leaf_node(partial: Partial, value: &[u8]) -> Vec<u8>;
59
60	/// Returns an encoded extension node
61	/// Note that number_nibble is the number of element of the iterator
62	/// it can possibly be obtain by `Iterator` `size_hint`, but
63	/// for simplicity it is used directly as a parameter.
64	fn extension_node(
65		partial: impl Iterator<Item = u8>,
66		number_nibble: usize,
67		child_ref: ChildReference<Self::HashOut>,
68	) -> Vec<u8>;
69
70	/// Returns an encoded branch node.
71	/// Takes an iterator yielding `ChildReference<Self::HashOut>` and an optional value.
72	fn branch_node(
73		children: impl Iterator<Item = impl Borrow<Option<ChildReference<Self::HashOut>>>>,
74		value: Option<&[u8]>,
75	) -> Vec<u8>;
76
77	/// Returns an encoded branch node with a possible partial path.
78	/// `number_nibble` is the partial path length as in `extension_node`.
79	fn branch_node_nibbled(
80		partial: impl Iterator<Item = u8>,
81		number_nibble: usize,
82		children: impl Iterator<Item = impl Borrow<Option<ChildReference<Self::HashOut>>>>,
83		value: Option<&[u8]>
84	) -> Vec<u8>;
85}