Skip to content

Commit

Permalink
chore: clean up dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Dec 20, 2023
1 parent 8b3db2b commit e4bcf1c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 57 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ jobs:
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true
# Only run tests on latest stable and above
- name: build
if: ${{ matrix.rust == '1.65' }} # MSRV
run: cargo build --workspace ${{ matrix.flags }}
- name: test
if: ${{ matrix.rust != '1.65' }} # MSRV
Expand Down
17 changes: 5 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ and proof generator for prefix-sorted nibbles
alloy-primitives = { version = "0.5", features = ["rlp"] }
alloy-rlp = { version = "0.3", features = ["derive"] }
derive_more = "0.99"
itertools = "0.12"
nybbles = "0.1"
serde = { version = "1.0", features = ["derive"], optional = true }
tracing = "0.1"
smallvec = "1.11"
tracing = "0.1"

# triehash-compat
hash-db = { version = "0.15", optional = true }
plain_hasher = { version = "0.2", optional = true }
# serde
serde = { version = "1.0", features = ["derive"], optional = true }

# arbitrary
arbitrary = { version = "1.3", optional = true }
Expand All @@ -29,11 +26,8 @@ proptest = { version = "1.4", optional = true }
proptest-derive = { version = "0.4", optional = true }

[dev-dependencies]
alloy-trie = { version = "0.1", path = ".", features = ["triehash-compat"] }
alloy-primitives = { version = "0.5", features = ["rlp", "serde", "rand", "arbitrary"] }
alloy-rlp = { version = "0.3", features = ["arrayvec"] }
bytes = "1.5"
proptest = "1.4"
hash-db = "0.15"
plain_hasher = "0.2"
triehash = "0.8.4"

[features]
Expand All @@ -45,4 +39,3 @@ arbitrary = [
"dep:proptest-derive",
"alloy-primitives/arbitrary",
]
triehash-compat = ["dep:hash-db", "dep:plain_hasher"]
46 changes: 29 additions & 17 deletions src/hash_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use super::{
BranchNodeCompact, Nibbles, TrieMask, EMPTY_ROOT_HASH,
};
use alloy_primitives::{keccak256, Bytes, B256};
use itertools::Itertools;
use std::{
collections::{BTreeMap, HashMap},
fmt::Debug,
Expand Down Expand Up @@ -109,7 +108,7 @@ impl HashBuilder {
println!("============ END STACK ===============");
}

/// Adds a new leaf element & its value to the trie hash builder.
/// Adds a new leaf element and its value to the trie hash builder.
pub fn add_leaf(&mut self, key: Nibbles, value: &[u8]) {
assert!(key > self.key);
if !self.key.is_empty() {
Expand All @@ -118,7 +117,7 @@ impl HashBuilder {
self.set_key_value(key, value);
}

/// Adds a new branch element & its hash to the trie hash builder.
/// Adds a new branch element and its hash to the trie hash builder.
pub fn add_branch(&mut self, key: Nibbles, value: B256, stored_in_database: bool) {
assert!(key > self.key || (self.key.is_empty() && key.is_empty()));
if !self.key.is_empty() {
Expand Down Expand Up @@ -214,7 +213,7 @@ impl HashBuilder {
trace!(
target: "trie::hash_builder",
?extra_digit,
groups = ?self.groups.iter().format(", "),
groups = ?self.groups,
);

// Adjust the tree masks for exporting to the DB
Expand Down Expand Up @@ -417,23 +416,33 @@ impl HashBuilder {
#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::{hex, B256, U256};
use alloy_primitives::{b256, hex, keccak256, B256, U256};
use alloy_rlp::Encodable;
use bytes::BytesMut;
use proptest::prelude::*;
use std::collections::{BTreeMap, HashMap};

fn trie_root<I, K, V>(iter: I) -> B256
fn triehash_trie_root<I, K, V>(iter: I) -> B256
where
I: IntoIterator<Item = (K, V)>,
K: AsRef<[u8]> + Ord,
V: AsRef<[u8]>,
{
struct Keccak256Hasher;
impl hash_db::Hasher for Keccak256Hasher {
type Out = B256;
type StdHasher = plain_hasher::PlainHasher;

const LENGTH: usize = 32;

fn hash(x: &[u8]) -> Self::Out {
keccak256(x)
}
}

// We use `trie_root` instead of `sec_trie_root` because we assume
// the incoming keys are already hashed, which makes sense given
// we're going to be using the Hashed tables & pre-hash the data
// on the way in.
triehash::trie_root::<crate::triehash_compat::KeccakHasher, _, _, _>(iter)
triehash::trie_root::<Keccak256Hasher, _, _, _>(iter)
}

// Hashes the keys, RLP encodes the values, compares the trie builder with the upstream root.
Expand All @@ -443,7 +452,7 @@ mod tests {
K: AsRef<[u8]> + Ord,
{
let hashed = iter
.map(|(k, v)| (keccak256(k.as_ref()), alloy_rlp::encode_fixed_size(v).to_vec()))
.map(|(k, v)| (keccak256(k.as_ref()), alloy_rlp::encode(v).to_vec()))
// Collect into a btree map to sort the data
.collect::<BTreeMap<_, _>>();

Expand All @@ -454,7 +463,7 @@ mod tests {
hb.add_leaf(nibbles, val);
});

assert_eq!(hb.root(), trie_root(&hashed));
assert_eq!(hb.root(), triehash_trie_root(&hashed));
}

// No hashing involved
Expand All @@ -471,7 +480,8 @@ mod tests {
let nibbles = Nibbles::unpack(key);
hb.add_leaf(nibbles, val.as_ref());
});
assert_eq!(hb.root(), trie_root(data));

assert_eq!(hb.root(), triehash_trie_root(data));
}

#[test]
Expand All @@ -480,8 +490,10 @@ mod tests {
}

#[test]
#[cfg(feature = "arbitrary")]
#[cfg_attr(miri, ignore = "no proptest")]
fn arbitrary_hashed_root() {
use proptest::prelude::*;
proptest!(|(state: BTreeMap<B256, U256>)| {
assert_hashed_trie_root(state.iter());
});
Expand Down Expand Up @@ -535,7 +547,7 @@ mod tests {
let nibbles = Nibbles::unpack(key);
hb.add_leaf(nibbles, val.as_ref());
});
let root = hb.root();
let _root = hb.root();

let (_, updates) = hb.split();

Expand All @@ -545,7 +557,7 @@ mod tests {
assert_eq!(update.hash_mask, TrieMask::new(6)); // in the 1st nibble, the ones with 1 and 2 are branches with `hashes`
assert_eq!(update.hashes.len(), 2); // calculated while the builder is running

assert_eq!(root, trie_root(data));
assert_eq!(_root, triehash_trie_root(data));
}

#[test]
Expand All @@ -570,7 +582,7 @@ mod tests {

#[test]
fn test_root_known_hash() {
let root_hash = B256::random();
let root_hash = b256!("45596e474b536a6b4d64764e4f75514d544577646c414e684271706871446456");
let mut hb = HashBuilder::default();
hb.add_branch(Nibbles::default(), root_hash, false);
assert_eq!(hb.root(), root_hash);
Expand Down Expand Up @@ -601,15 +613,15 @@ mod tests {
// leaves. We set this to `7` because the 2nd element of Leaf 1 is `7`.
branch[4] = &leaf1;
branch[7] = &leaf2;
let mut branch_node_rlp = BytesMut::new();
let mut branch_node_rlp = Vec::new();
alloy_rlp::encode_list::<_, dyn Encodable>(&branch, &mut branch_node_rlp);
let branch_node_hash = keccak256(branch_node_rlp);

let mut hb2 = HashBuilder::default();
// Insert the branch with the `0x6` shared prefix.
hb2.add_branch(Nibbles::from_nibbles_unchecked([0x6]), branch_node_hash, false);

let expected = trie_root(raw_input.clone());
let expected = triehash_trie_root(raw_input.clone());
assert_eq!(hb.root(), expected);
assert_eq!(hb2.root(), expected);
}
Expand Down
4 changes: 0 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@
)]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![deny(unused_must_use, rust_2018_idioms)]
// #![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

#[cfg(feature = "triehash-compat")]
pub mod triehash_compat;

pub mod nodes;
pub use nodes::BranchNodeCompact;

Expand Down
22 changes: 0 additions & 22 deletions src/triehash_compat.rs

This file was deleted.

0 comments on commit e4bcf1c

Please sign in to comment.