Skip to content

Commit 1380a2e

Browse files
authored
chore: remove the hash-utils dependency from the KAMT (#2099)
We still need to do this for the HAMT, but this is progress.
1 parent 551e24a commit 1380a2e

File tree

4 files changed

+8
-35
lines changed

4 files changed

+8
-35
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ipld/kamt/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ once_cell = { workspace = true }
1717
anyhow = { workspace = true }
1818
fvm_ipld_encoding = { workspace = true }
1919
fvm_ipld_blockstore = { workspace = true }
20-
forest_hash_utils = "0.1"
2120

2221
[dev-dependencies]
2322
hex = { workspace = true }

ipld/kamt/src/id.rs

+4-29
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0, MIT
33
use std::borrow::Cow;
44

5-
use forest_hash_utils::Hash;
6-
75
use crate::{AsHashedKey, HashedKey};
86

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

4041
identity_arr!(20, 32, 64);
4142
identity_hash!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128);
42-
43-
/// Take the first 32 bytes as is.
44-
#[derive(Default)]
45-
struct IdentityHasher {
46-
bz: HashedKey<32>,
47-
}
48-
49-
impl IdentityHasher {
50-
pub fn hash<K: Hash>(key: K) -> HashedKey<32> {
51-
let mut hasher = Self::default();
52-
key.hash(&mut hasher);
53-
hasher.bz
54-
}
55-
}
56-
57-
impl std::hash::Hasher for IdentityHasher {
58-
fn finish(&self) -> u64 {
59-
0
60-
}
61-
62-
fn write(&mut self, bytes: &[u8]) {
63-
for (i, byte) in bytes.iter().take(self.bz.len()).enumerate() {
64-
self.bz[i] = *byte;
65-
}
66-
}
67-
}

ipld/kamt/tests/kamt_tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use std::collections::{HashMap, HashSet};
66
use std::fmt::Display;
77

88
use cid::Cid;
9-
use forest_hash_utils::BytesKey;
109
use fvm_ipld_blockstore::{Blockstore, MemoryBlockstore};
1110
use fvm_ipld_encoding::de::DeserializeOwned;
11+
use fvm_ipld_encoding::BytesDe;
1212
use fvm_ipld_encoding::CborStore;
1313
use fvm_ipld_kamt::id::Identity;
1414
use fvm_ipld_kamt::{Config, Error, HashedKey, Kamt};
@@ -104,7 +104,7 @@ fn test_load(factory: KamtFactory) {
104104

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

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

322-
fn tstring(v: impl Display) -> BytesKey {
323-
BytesKey(v.to_string().into_bytes())
322+
fn tstring(v: impl Display) -> BytesDe {
323+
BytesDe(v.to_string().into_bytes())
324324
}
325325

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

0 commit comments

Comments
 (0)