From 10b34bc77e93ce62ea0d53e40ec5918aac0e5162 Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Wed, 26 Feb 2025 18:34:59 -0800 Subject: [PATCH 1/2] basic test using triehash --- firewood/Cargo.toml | 5 +++++ firewood/src/merkle.rs | 46 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/firewood/Cargo.toml b/firewood/Cargo.toml index 4f339a364..7139f3b89 100644 --- a/firewood/Cargo.toml +++ b/firewood/Cargo.toml @@ -48,6 +48,11 @@ clap = { version = "4.5.0", features = ['derive'] } pprof = { version = "0.14.0", features = ["flamegraph"] } tempfile = "3.12.0" tokio = { version = "1.36.0", features = ["rt", "sync", "macros", "rt-multi-thread"] } +ethereum-types = "0.15.1" +sha3 = "0.10.8" +plain_hasher = "0.2.3" +hash-db = "0.15.2" +hex-literal = "0.4.1" [[bench]] name = "hashops" diff --git a/firewood/src/merkle.rs b/firewood/src/merkle.rs index f01ad7775..619f0cd30 100644 --- a/firewood/src/merkle.rs +++ b/firewood/src/merkle.rs @@ -1038,8 +1038,6 @@ mod tests { use rand::rngs::StdRng; use rand::{rng, Rng, SeedableRng}; use storage::{MemStore, MutableProposal, NodeStore, RootReader}; - - #[cfg(not(feature = "ethhash"))] use test_case::test_case; // Returns n random key-value pairs. @@ -1752,6 +1750,50 @@ mod tests { } } + #[cfg(feature = "ethhash")] + mod ethhasher { + use ethereum_types::H256; + use sha3::{Digest, Keccak256}; + use plain_hasher::PlainHasher; + use hash_db::Hasher; + + #[derive(Default, Debug, Clone, PartialEq, Eq, Hash)] + pub struct KeccakHasher; + + impl Hasher for KeccakHasher { + type Out = H256; + type StdHasher = PlainHasher; + const LENGTH: usize = 32; + + #[inline] + fn hash(x: &[u8]) -> Self::Out { + let mut hasher = Keccak256::new(); + hasher.update(x); + let result = hasher.finalize(); + H256::from_slice(result.as_slice()) + } + } + } + + #[cfg(feature = "ethhash")] + #[test_case(vec![("doe", "reindeer"),("dog", "puppy"),("dogglesworth", "cat")])] + fn test_root_hash_eth_compatible(kvs: Vec<(&str, &str)>) { + use ethhasher::KeccakHasher; + use triehash::trie_root; + use ethereum_types::H256; + + let merkle = merkle_build_test(kvs.clone()).unwrap().hash(); + let Some(firewood_hash) = merkle.nodestore.root_hash().unwrap() else { + // Empty trie + assert_eq!(kvs.len(), 0); + return; + }; + let eth_hash = trie_root::(kvs); + let firewood_hash = H256::from_slice(firewood_hash.as_ref().into()); + + assert_eq!(firewood_hash, eth_hash); + } + #[test] fn test_root_hash_fuzz_insertions() -> Result<(), MerkleError> { use rand::rngs::StdRng; From f2acdd3d61629d5c05f80bee45e2a8bd28bdd3d6 Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Wed, 26 Feb 2025 19:52:25 -0800 Subject: [PATCH 2/2] remove unused dependency --- firewood/Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/firewood/Cargo.toml b/firewood/Cargo.toml index 7139f3b89..c15c67892 100644 --- a/firewood/Cargo.toml +++ b/firewood/Cargo.toml @@ -52,7 +52,6 @@ ethereum-types = "0.15.1" sha3 = "0.10.8" plain_hasher = "0.2.3" hash-db = "0.15.2" -hex-literal = "0.4.1" [[bench]] name = "hashops"