ethereum_triedb/
lib.rs

1// Copyright (C) Polytope Labs Ltd.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// 	http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#![cfg_attr(not(feature = "std"), no_std)]
17#![doc = include_str!("../README.md")]
18
19extern crate alloc;
20
21use core::marker::PhantomData;
22use hash_db::Hasher;
23use primitive_types::H256;
24use trie_db::TrieLayout;
25
26mod node_codec;
27mod storage_proof;
28
29#[cfg(test)]
30mod tests;
31
32pub use storage_proof::{MemoryDB, StorageProof};
33
34/// Trie layout for EIP-1186 state proof nodes.
35#[derive(Default, Clone)]
36pub struct EIP1186Layout<H>(PhantomData<H>);
37
38impl<H: Hasher<Out = H256>> TrieLayout for EIP1186Layout<H> {
39	const USE_EXTENSION: bool = true;
40	const ALLOW_EMPTY: bool = false;
41	const MAX_INLINE_VALUE: Option<u32> = None;
42	type Hash = H;
43	type Codec = node_codec::RlpNodeCodec<H>;
44}
45
46/// Keccak hasher implementation, but only for std uses. You'd probably want to delegate
47/// hashing to wasm host functions in `no_std`.
48#[cfg(feature = "std")]
49pub mod keccak {
50	use super::*;
51	use hash256_std_hasher::Hash256StdHasher;
52	use tiny_keccak::{Hasher, Keccak};
53
54	/// Concrete implementation of Hasher using Keccak 256-bit hashes
55	#[derive(Debug)]
56	pub struct KeccakHasher;
57
58	impl hash_db::Hasher for KeccakHasher {
59		type Out = H256;
60		type StdHasher = Hash256StdHasher;
61		const LENGTH: usize = 32;
62
63		fn hash(x: &[u8]) -> Self::Out {
64			keccak_256(x).into()
65		}
66	}
67
68	/// Performs a Keccak-256 hash on the given input.
69	pub fn keccak_256(input: &[u8]) -> [u8; 32] {
70		let mut out = [0u8; 32];
71		let mut k = Keccak::v256();
72		k.update(input);
73		k.finalize(&mut out);
74		out
75	}
76}