1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
// Copyright 2017, 2018 Parity Technologies // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! Generates trie root. //! //! This module should be used to generate trie root hash. #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(not(feature = "std"), feature(core_intrinsics))] #![cfg_attr(not(feature = "std"), feature(alloc))] extern crate hash_db; #[cfg(feature = "std")] use std::collections::BTreeMap; #[cfg(feature = "std")] use std::cmp; #[cfg(not(feature = "std"))] extern crate alloc; #[cfg(not(feature = "std"))] use alloc::{collections::btree_map::BTreeMap, vec::Vec}; #[cfg(not(feature = "std"))] use core::cmp; #[cfg(test)] extern crate keccak_hasher; pub use hash_db::Hasher; /// Byte-stream oriented trait for constructing closed-form tries. pub trait TrieStream { /// Construct a new `TrieStream` fn new() -> Self; /// Append an Empty node fn append_empty_data(&mut self); /// Start a new Branch node, possibly with a value; takes a list indicating /// which slots in the Branch node has further child nodes. fn begin_branch(&mut self, maybe_value: Option<&[u8]>, has_children: impl Iterator<Item = bool>); // Append an empty child node. Optional. fn append_empty_child(&mut self) {} /// Wrap up a Branch node portion of a `TrieStream` and append the value /// stored on the Branch (if any). fn end_branch(&mut self, _value: Option<&[u8]>) {} /// Append a Leaf node fn append_leaf(&mut self, key: &[u8], value: &[u8]); /// Append an Extension node fn append_extension(&mut self, key: &[u8]); /// Append a Branch of Extension substream fn append_substream<H: Hasher>(&mut self, other: Self); /// Return the finished `TrieStream` as a vector of bytes. fn out(self) -> Vec<u8>; } fn shared_prefix_len<T: Eq>(first: &[T], second: &[T]) -> usize { first.iter() .zip(second.iter()) .position(|(f, s)| f != s) .unwrap_or_else(|| cmp::min(first.len(), second.len())) } /// Generates a trie root hash for a vector of key-value tuples /// /// ```rust /// #[macro_use] extern crate hex_literal; /// extern crate trie_root; /// extern crate reference_trie; /// extern crate keccak_hasher; /// use trie_root::trie_root; /// use reference_trie::ReferenceTrieStream; /// use keccak_hasher::KeccakHasher; /// /// fn main() { /// let v = vec![ /// ("doe", "reindeer"), /// ("dog", "puppy"), /// ("dogglesworth", "cat"), /// ]; /// /// let root = hex!["0807d5393ae7f349481063ebb5dbaf6bda58db282a385ca97f37dccba717cb79"]; /// assert_eq!(trie_root::<KeccakHasher, ReferenceTrieStream, _, _, _>(v), root); /// } /// ``` pub fn trie_root<H, S, I, A, B>(input: I) -> H::Out where I: IntoIterator<Item = (A, B)>, A: AsRef<[u8]> + Ord, B: AsRef<[u8]>, H: Hasher, S: TrieStream, { // first put elements into btree to sort them and to remove duplicates let input = input .into_iter() .collect::<BTreeMap<_, _>>(); // convert to nibbles let mut nibbles = Vec::with_capacity(input.keys().map(|k| k.as_ref().len()).sum::<usize>() * 2); let mut lens = Vec::with_capacity(input.len() + 1); lens.push(0); for k in input.keys() { for &b in k.as_ref() { nibbles.push(b >> 4); nibbles.push(b & 0x0F); } lens.push(nibbles.len()); } // then move them to a vector let input = input.into_iter().zip(lens.windows(2)) .map(|((_, v), w)| (&nibbles[w[0]..w[1]], v)) .collect::<Vec<_>>(); let mut stream = S::new(); build_trie::<H, S, _, _>(&input, 0, &mut stream); H::hash(&stream.out()) } //#[cfg(test)] // consider feature="std" pub fn unhashed_trie<H, S, I, A, B>(input: I) -> Vec<u8> where I: IntoIterator<Item = (A, B)>, A: AsRef<[u8]> + Ord, B: AsRef<[u8]>, H: Hasher, S: TrieStream, { // first put elements into btree to sort them and to remove duplicates let input = input .into_iter() .collect::<BTreeMap<_, _>>(); let mut nibbles = Vec::with_capacity(input.keys().map(|k| k.as_ref().len()).sum::<usize>() * 2); let mut lens = Vec::with_capacity(input.len() + 1); lens.push(0); for k in input.keys() { for &b in k.as_ref() { nibbles.push(b >> 4); nibbles.push(b & 0x0F); } lens.push(nibbles.len()); } // then move them to a vector let input = input.into_iter().zip(lens.windows(2)) .map(|((_, v), w)| (&nibbles[w[0]..w[1]], v)) .collect::<Vec<_>>(); let mut stream = S::new(); build_trie::<H, S, _, _>(&input, 0, &mut stream); stream.out() } /// Generates a key-hashed (secure) trie root hash for a vector of key-value tuples. /// /// ```rust /// #[macro_use] extern crate hex_literal; /// extern crate trie_root; /// extern crate keccak_hasher; /// extern crate reference_trie; /// use trie_root::sec_trie_root; /// use keccak_hasher::KeccakHasher; /// use reference_trie::ReferenceTrieStream; /// /// fn main() { /// let v = vec![ /// ("doe", "reindeer"), /// ("dog", "puppy"), /// ("dogglesworth", "cat"), /// ]; /// /// let root = hex!["d6e02b2bd48aa04fd2ad87cfac1144a29ca7f7dc60f4526c7b7040763abe3d43"]; /// assert_eq!(sec_trie_root::<KeccakHasher, ReferenceTrieStream, _, _, _>(v), root); /// } /// ``` pub fn sec_trie_root<H, S, I, A, B>(input: I) -> H::Out where I: IntoIterator<Item = (A, B)>, A: AsRef<[u8]>, B: AsRef<[u8]>, H: Hasher, H::Out: Ord, S: TrieStream, { trie_root::<H, S, _, _, _>(input.into_iter().map(|(k, v)| (H::hash(k.as_ref()), v))) } /// Takes a slice of key/value tuples where the key is a slice of nibbles /// and encodes it into the provided `Stream`. // pub fn build_trie<H, S, A, B>(input: &[(A, B)], cursor: usize, stream: &mut S) fn build_trie<H, S, A, B>(input: &[(A, B)], cursor: usize, stream: &mut S) where A: AsRef<[u8]>, B: AsRef<[u8]>, H: Hasher, S: TrieStream, { match input.len() { // No input, just append empty data. 0 => stream.append_empty_data(), // Leaf node; append the remainder of the key and the value. Done. 1 => stream.append_leaf(&input[0].0.as_ref()[cursor..], &input[0].1.as_ref() ), // We have multiple items in the input. Figure out if we should add an // extension node or a branch node. _ => { let (key, value) = (&input[0].0.as_ref(), input[0].1.as_ref()); // Count the number of nibbles in the other elements that are // shared with the first key. // e.g. input = [ [1'7'3'10'12'13], [1'7'3'], [1'7'7'8'9'] ] => [1'7'] is common => 2 let shared_nibble_count = input.iter().skip(1).fold(key.len(), |acc, &(ref k, _)| { cmp::min( shared_prefix_len(key, k.as_ref()), acc ) }); // Add an extension node if the number of shared nibbles is greater // than what we saw on the last call (`cursor`): append the new part // of the path then recursively append the remainder of all items // who had this partial key. if shared_nibble_count > cursor { stream.append_extension(&key[cursor..shared_nibble_count]); build_trie_trampoline::<H, _, _, _>(input, shared_nibble_count, stream); return; } // We'll be adding a branch node because the path is as long as it gets. // First we need to figure out what entries this branch node will have... // We have a a value for exactly this key. Branch node will have a value // attached to it. let value = if cursor == key.len() { Some(value) } else { None }; // We need to know how many key nibbles each of the children account for. let mut shared_nibble_counts = [0usize; 16]; { // If the Branch node has a value then the first of the input keys // is exactly the key for that value and we don't care about it // when finding shared nibbles for our child nodes. (We know it's // the first of the input keys, because the input is sorted) let mut begin = match value { None => 0, _ => 1 }; for i in 0..16 { shared_nibble_counts[i] = input[begin..].iter() .take_while(|(k, _)| k.as_ref()[cursor] == i as u8) .count(); begin += shared_nibble_counts[i]; } } // Put out the node header: stream.begin_branch(value, shared_nibble_counts.iter().map(|&n| n > 0)); // Fill in each slot in the branch node. We don't need to bother with empty slots since they // were registered in the header. let mut begin = match value { None => 0, _ => 1 }; for &count in &shared_nibble_counts { if count > 0 { build_trie_trampoline::<H, S, _, _>(&input[begin..(begin + count)], cursor + 1, stream); begin += count; } else { stream.append_empty_child(); } } stream.end_branch(value); } } } fn build_trie_trampoline<H, S, A, B>(input: &[(A, B)], cursor: usize, stream: &mut S) where A: AsRef<[u8]>, B: AsRef<[u8]>, H: Hasher, S: TrieStream, { let mut substream = S::new(); build_trie::<H, _, _, _>(input, cursor, &mut substream); stream.append_substream::<H>(substream); }