chore(primitives): move KeccakHasher behind test-utils feature flag (#4804)

This commit is contained in:
Roman Krasiuk
2023-09-26 19:02:57 +03:00
committed by GitHub
parent f7c3bbcc99
commit d0ef5af580
6 changed files with 38 additions and 27 deletions

View File

@ -5,10 +5,8 @@ use crate::{
Withdrawal, H256,
};
use bytes::{BufMut, BytesMut};
use hash_db::Hasher;
use hex_literal::hex;
use itertools::Itertools;
use plain_hasher::PlainHasher;
use reth_rlp::Encodable;
use std::collections::HashMap;
@ -20,22 +18,6 @@ pub const EMPTY_LIST_HASH: H256 =
pub const EMPTY_ROOT: H256 =
H256(hex!("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"));
/// A [Hasher] that calculates a keccak256 hash of the given data.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct KeccakHasher;
impl Hasher for KeccakHasher {
type Out = H256;
type StdHasher = PlainHasher;
const LENGTH: usize = 32;
fn hash(x: &[u8]) -> Self::Out {
keccak256(x)
}
}
/// Adjust the index of an item for rlp encoding.
pub const fn adjust_index_for_rlp(i: usize, len: usize) -> usize {
if i > 0x7f {
@ -144,6 +126,32 @@ pub fn genesis_state_root(genesis_alloc: &HashMap<Address, GenesisAccount>) -> H
hb.root()
}
/// Implementation of hasher using our keccak256 hashing function
/// for compatibility with `triehash` crate.
#[cfg(any(test, feature = "test-utils"))]
pub mod triehash {
use super::{keccak256, H256};
use hash_db::Hasher;
use plain_hasher::PlainHasher;
/// A [Hasher] that calculates a keccak256 hash of the given data.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct KeccakHasher;
#[cfg(any(test, feature = "test-utils"))]
impl Hasher for KeccakHasher {
type Out = H256;
type StdHasher = PlainHasher;
const LENGTH: usize = 32;
fn hash(x: &[u8]) -> Self::Out {
keccak256(x)
}
}
}
#[cfg(test)]
mod tests {
use super::{calculate_withdrawals_root, EMPTY_ROOT};