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

@ -65,9 +65,9 @@ tempfile = "3.3"
sha2 = "0.10.7"
itertools = "0.11"
# See to replace hashers to simplify libraries
plain_hasher = "0.2"
hash-db = "~0.15"
# `test-utils` feature
plain_hasher = { version = "0.2", optional = true }
hash-db = { version = "~0.15", optional = true }
# arbitrary utils
arbitrary = { workspace = true, features = ["derive"], optional = true }
@ -88,6 +88,9 @@ assert_matches.workspace = true
toml = "0.7.4"
triehash = "0.8"
plain_hasher = "0.2"
hash-db = "~0.15"
# necessary so we don't hit a "undeclared 'std'":
# https://github.com/paradigmxyz/reth/pull/177#discussion_r1021172198
secp256k1.workspace = true
@ -97,7 +100,7 @@ pprof = { version = "0.12", features = ["flamegraph", "frame-pointer", "criterio
[features]
default = []
arbitrary = ["revm-primitives/arbitrary", "dep:arbitrary", "dep:proptest", "dep:proptest-derive"]
test-utils = []
test-utils = ["dep:plain_hasher", "dep:hash-db"]
[[bench]]
name = "recover_ecdsa_crit"
@ -105,5 +108,5 @@ harness = false
[[bench]]
name = "trie_root"
required-features = ["arbitrary"]
required-features = ["arbitrary", "test-utils"]
harness = false

View File

@ -4,7 +4,7 @@ use proptest::{
strategy::{Strategy, ValueTree},
test_runner::TestRunner,
};
use reth_primitives::{proofs::KeccakHasher, ReceiptWithBloom, H256};
use reth_primitives::{proofs::triehash::KeccakHasher, ReceiptWithBloom, H256};
/// Benchmarks different implementations of the root calculation.
pub fn trie_root_benchmark(c: &mut Criterion) {

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};

View File

@ -415,7 +415,7 @@ impl HashBuilder {
#[cfg(test)]
mod tests {
use super::*;
use crate::{hex_literal::hex, proofs::KeccakHasher, H256, U256};
use crate::{hex_literal::hex, proofs::triehash::KeccakHasher, H256, U256};
use proptest::prelude::*;
use std::collections::{BTreeMap, HashMap};

View File

@ -1,5 +1,5 @@
use crate::account::EthAccount;
use reth_primitives::{proofs::KeccakHasher, Account, Address, H256, U256};
use reth_primitives::{proofs::triehash::KeccakHasher, Account, Address, H256, U256};
use reth_rlp::{encode_fixed_size, Encodable};
/// Re-export of [triehash].

View File

@ -552,7 +552,7 @@ mod tests {
use reth_primitives::{
hex_literal::hex,
keccak256,
proofs::KeccakHasher,
proofs::triehash::KeccakHasher,
trie::{BranchNodeCompact, TrieMask},
Account, Address, H256, MAINNET, U256,
};