Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check against eth hash #808

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions firewood/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should all contain features flags to make the build faster without the feature flag enabled

Suggested change
ethereum-types = "0.15.1"
ethereum-types = { version = "0.15.1", features = [ "ethhash" ] }

sha3 = "0.10.8"
plain_hasher = "0.2.3"
hash-db = "0.15.2"
hex-literal = "0.4.1"

[[bench]]
name = "hashops"
Expand Down
46 changes: 44 additions & 2 deletions firewood/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -1752,6 +1750,50 @@ mod tests {
}
}

#[cfg(feature = "ethhash")]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you move this test over to ethhash.rs you can avoid adding this directive

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::<KeccakHasher, _, _, _>(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;
Expand Down