Skip to content

Commit

Permalink
chore: remove the hash-utils dependency from the KAMT (#2099)
Browse files Browse the repository at this point in the history
We still need to do this for the HAMT, but this is progress.
  • Loading branch information
Stebalien authored Jan 7, 2025
1 parent 551e24a commit 1380a2e
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 35 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion ipld/kamt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ once_cell = { workspace = true }
anyhow = { workspace = true }
fvm_ipld_encoding = { workspace = true }
fvm_ipld_blockstore = { workspace = true }
forest_hash_utils = "0.1"

[dev-dependencies]
hex = { workspace = true }
Expand Down
33 changes: 4 additions & 29 deletions ipld/kamt/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// SPDX-License-Identifier: Apache-2.0, MIT
use std::borrow::Cow;

use forest_hash_utils::Hash;

use crate::{AsHashedKey, HashedKey};

/// Convenience hasher for docstrings and tests,
Expand All @@ -30,7 +28,10 @@ macro_rules! identity_hash {
$(
impl AsHashedKey<$t, 32> for Identity {
fn as_hashed_key(key: &$t) -> Cow<HashedKey<32>> {
Cow::Owned(IdentityHasher::hash(key))
const BYTES: usize = <$t>::BITS as usize / 8;
let mut output = [0u8; 32];
output[..BYTES].copy_from_slice(&<$t>::to_ne_bytes(*key));
Cow::Owned(output)
}
}
)*
Expand All @@ -39,29 +40,3 @@ macro_rules! identity_hash {

identity_arr!(20, 32, 64);
identity_hash!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128);

/// Take the first 32 bytes as is.
#[derive(Default)]
struct IdentityHasher {
bz: HashedKey<32>,
}

impl IdentityHasher {
pub fn hash<K: Hash>(key: K) -> HashedKey<32> {
let mut hasher = Self::default();
key.hash(&mut hasher);
hasher.bz
}
}

impl std::hash::Hasher for IdentityHasher {
fn finish(&self) -> u64 {
0
}

fn write(&mut self, bytes: &[u8]) {
for (i, byte) in bytes.iter().take(self.bz.len()).enumerate() {
self.bz[i] = *byte;
}
}
}
8 changes: 4 additions & 4 deletions ipld/kamt/tests/kamt_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::collections::{HashMap, HashSet};
use std::fmt::Display;

use cid::Cid;
use forest_hash_utils::BytesKey;
use fvm_ipld_blockstore::{Blockstore, MemoryBlockstore};
use fvm_ipld_encoding::de::DeserializeOwned;
use fvm_ipld_encoding::BytesDe;
use fvm_ipld_encoding::CborStore;
use fvm_ipld_kamt::id::Identity;
use fvm_ipld_kamt::{Config, Error, HashedKey, Kamt};
Expand Down Expand Up @@ -104,7 +104,7 @@ fn test_load(factory: KamtFactory) {

// loading from an empty store does not work
let empty_store = MemoryBlockstore::default();
assert!(factory.load::<_, u32, BytesKey>(&c2, &empty_store).is_err());
assert!(factory.load::<_, u32, BytesDe>(&c2, &empty_store).is_err());

// storing the kamt should produce the same cid as storing the root
let c3 = kamt.flush().unwrap();
Expand Down Expand Up @@ -319,8 +319,8 @@ fn prop_cid_ops_reduced<const N: u32>(factory: KamtFactory, ops: LimitedKeyOps<N
cid1 == cid2
}

fn tstring(v: impl Display) -> BytesKey {
BytesKey(v.to_string().into_bytes())
fn tstring(v: impl Display) -> BytesDe {
BytesDe(v.to_string().into_bytes())
}

fn kstring(v: impl Display) -> HashedKey<32> {
Expand Down

0 comments on commit 1380a2e

Please sign in to comment.