triehash_vapory/
lib.rs

1// Copyright 2015-2020 Parity Technologies (UK) Ltd.
2// This file is part of Tetsy Vapory.
3
4// Tetsy Vapory is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Tetsy Vapory is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Tetsy Vapory.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Generates Keccak-flavoured trie roots.
18
19extern crate vapory_types;
20extern crate tetsy_keccak_hasher;
21extern crate tetsy_triehash;
22
23use vapory_types::H256;
24use tetsy_keccak_hasher::KeccakHasher;
25
26/// Generates a trie root hash for a vector of key-value tuples
27pub fn tetsy_trie_root<I, K, V>(input: I) -> H256
28where
29    I: IntoIterator<Item = (K, V)>,
30    K: AsRef<[u8]> + Ord,
31    V: AsRef<[u8]>,
32{
33    tetsy_triehash::trie_root::<KeccakHasher, _, _, _>(input)
34}
35
36/// Generates a key-hashed (secure) trie root hash for a vector of key-value tuples.
37pub fn sec_trie_root<I, K, V>(input: I) -> H256
38where
39    I: IntoIterator<Item = (K, V)>,
40    K: AsRef<[u8]>,
41    V: AsRef<[u8]>,
42{
43    tetsy_triehash::sec_trie_root::<KeccakHasher, _, _, _>(input)
44}
45
46/// Generates a trie root hash for a vector of values
47pub fn ordered_trie_root<I, V>(input: I) -> H256
48where
49    I: IntoIterator<Item = V>,
50    V: AsRef<[u8]>,
51{
52    tetsy_triehash::ordered_trie_root::<KeccakHasher, I>(input)
53}
54
55#[cfg(test)]
56mod tests {
57	use super::{tetsy_trie_root, sec_trie_root, ordered_trie_root, H256};
58    use tetsy_triehash;
59	use tetsy_keccak_hasher::KeccakHasher;
60	use std::str::FromStr;
61
62	#[test]
63	fn simple_test() {
64		assert_eq!(tetsy_trie_root(vec![
65			(b"A", b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" as &[u8])
66		]), H256::from_str("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab").unwrap());
67	}
68
69	#[test]
70	fn proxy_works() {
71        let input = vec![(b"A", b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" as &[u8])];
72		assert_eq!(
73            tetsy_trie_root(input.clone()),
74            tetsy_triehash::tetsy_trie_root::<KeccakHasher, _, _, _>(input.clone())
75        );
76
77		assert_eq!(
78            sec_trie_root(input.clone()),
79            tetsy_triehash::sec_trie_root::<KeccakHasher, _, _, _>(input.clone())
80        );
81
82        let data = &["cake", "pie", "candy"];
83		assert_eq!(
84            ordered_trie_root(data),
85            tetsy_triehash::ordered_trie_root::<KeccakHasher, _>(data)
86        );
87	}
88}