diff --git a/Cargo.toml b/Cargo.toml index 4689b5d3..5c1b57b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,11 +19,10 @@ libipld-core = { version = "0.16.0", path = "core" } libipld-json = { version = "0.16.0", path = "dag-json", optional = true } libipld-macro = { version = "0.16.0", path = "macro" } libipld-pb = { version = "0.16.0", path = "dag-pb", optional = true } -log = "0.4.14" -multihash = { version = "0.18.0", default-features = false, features = ["multihash-impl"] } -thiserror = "1.0.25" +multihash-codetable = "0.1.4" [dev-dependencies] +multihash-codetable = { version = "0.1.4", features = ["blake3"] } async-std = { version = "1.9.0", features = ["attributes"] } criterion = "0.3.4" proptest = "1.0.0" diff --git a/core/Cargo.toml b/core/Cargo.toml index b550976f..4b234680 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -15,9 +15,9 @@ arb = ["quickcheck", "cid/arb"] [dependencies] anyhow = { version = "1.0.40", default-features = false } -cid = { version = "0.10.0", default-features = false, features = ["alloc"] } +cid = { version = "0.11.1", default-features = false, features = ["alloc"] } core2 = { version = "0.4", default-features = false, features = ["alloc"] } -multihash = { version = "0.18.0", default-features = false, features = ["alloc"] } +multihash = { version = "0.19.3", default-features = false, features = ["alloc"] } multibase = { version = "0.9.1", default-features = false, optional = true } serde = { version = "1.0.132", default-features = false, features = ["alloc"], optional = true } @@ -25,7 +25,7 @@ thiserror = {version = "1.0.25", optional = true } quickcheck = { version = "1.0", optional = true } [dev-dependencies] -multihash = { version = "0.18.0", default-features = false, features = ["multihash-impl", "blake3"] } +multihash-codetable = { version = "0.1.4", features = ["blake3"] } serde_test = "1.0.132" serde_bytes = "0.11.5" serde_json = "1.0.79" diff --git a/core/src/ipld.rs b/core/src/ipld.rs index 68fb80a9..3a9fab0e 100644 --- a/core/src/ipld.rs +++ b/core/src/ipld.rs @@ -75,13 +75,13 @@ pub enum IpldIndex<'a> { MapRef(&'a str), } -impl<'a> From for IpldIndex<'a> { +impl From for IpldIndex<'_> { fn from(index: usize) -> Self { Self::List(index) } } -impl<'a> From for IpldIndex<'a> { +impl From for IpldIndex<'_> { fn from(key: String) -> Self { Self::Map(key) } @@ -195,7 +195,7 @@ impl<'a> Iterator for IpldIter<'a> { mod tests { use super::*; use crate::cid::Cid; - use crate::multihash::{Code, MultihashDigest}; + use multihash_codetable::{Code, MultihashDigest}; #[test] fn test_ipld_bool_from() { diff --git a/core/src/serde/ser.rs b/core/src/serde/ser.rs index 760b8c09..6af736a8 100644 --- a/core/src/serde/ser.rs +++ b/core/src/serde/ser.rs @@ -208,13 +208,13 @@ impl serde::Serializer for Serializer { } #[inline] - fn serialize_newtype_struct( + fn serialize_newtype_struct( self, name: &'static str, value: &T, ) -> Result where - T: ser::Serialize, + T: ser::Serialize + ?Sized, { let ipld = value.serialize(self); if name == CID_SERDE_PRIVATE_IDENTIFIER { @@ -227,7 +227,7 @@ impl serde::Serializer for Serializer { ipld } - fn serialize_newtype_variant( + fn serialize_newtype_variant( self, _name: &'static str, _variant_index: u32, @@ -235,7 +235,7 @@ impl serde::Serializer for Serializer { value: &T, ) -> Result where - T: ser::Serialize, + T: ser::Serialize + ?Sized, { let values = BTreeMap::from([(variant.to_owned(), value.serialize(self)?)]); Ok(Self::Ok::Map(values)) @@ -247,9 +247,9 @@ impl serde::Serializer for Serializer { } #[inline] - fn serialize_some(self, value: &T) -> Result + fn serialize_some(self, value: &T) -> Result where - T: ser::Serialize, + T: ser::Serialize + ?Sized, { value.serialize(self) } @@ -342,9 +342,9 @@ impl ser::SerializeSeq for SerializeVec { type Ok = Ipld; type Error = SerdeError; - fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> + fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> where - T: ser::Serialize, + T: ser::Serialize + ?Sized, { self.vec.push(value.serialize(Serializer)?); Ok(()) @@ -359,9 +359,9 @@ impl ser::SerializeTuple for SerializeVec { type Ok = Ipld; type Error = SerdeError; - fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> + fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> where - T: ser::Serialize, + T: ser::Serialize + ?Sized, { ser::SerializeSeq::serialize_element(self, value) } @@ -375,9 +375,9 @@ impl ser::SerializeTupleStruct for SerializeVec { type Ok = Ipld; type Error = SerdeError; - fn serialize_field(&mut self, value: &T) -> Result<(), Self::Error> + fn serialize_field(&mut self, value: &T) -> Result<(), Self::Error> where - T: ser::Serialize, + T: ser::Serialize + ?Sized, { ser::SerializeSeq::serialize_element(self, value) } @@ -391,9 +391,9 @@ impl ser::SerializeTupleVariant for SerializeTupleVariant { type Ok = Ipld; type Error = SerdeError; - fn serialize_field(&mut self, value: &T) -> Result<(), Self::Error> + fn serialize_field(&mut self, value: &T) -> Result<(), Self::Error> where - T: ser::Serialize, + T: ser::Serialize + ?Sized, { self.vec.push(value.serialize(Serializer)?); Ok(()) @@ -409,9 +409,9 @@ impl ser::SerializeMap for SerializeMap { type Ok = Ipld; type Error = SerdeError; - fn serialize_key(&mut self, key: &T) -> Result<(), Self::Error> + fn serialize_key(&mut self, key: &T) -> Result<(), Self::Error> where - T: ser::Serialize, + T: ser::Serialize + ?Sized, { match key.serialize(Serializer)? { Ipld::String(string_key) => { @@ -422,9 +422,9 @@ impl ser::SerializeMap for SerializeMap { } } - fn serialize_value(&mut self, value: &T) -> Result<(), Self::Error> + fn serialize_value(&mut self, value: &T) -> Result<(), Self::Error> where - T: ser::Serialize, + T: ser::Serialize + ?Sized, { let key = self.next_key.take(); // Panic because this indicates a bug in the program rather than an @@ -443,13 +443,13 @@ impl ser::SerializeStruct for SerializeMap { type Ok = Ipld; type Error = SerdeError; - fn serialize_field( + fn serialize_field( &mut self, key: &'static str, value: &T, ) -> Result<(), Self::Error> where - T: ser::Serialize, + T: ser::Serialize + ?Sized, { serde::ser::SerializeMap::serialize_key(self, key)?; serde::ser::SerializeMap::serialize_value(self, value) @@ -464,13 +464,13 @@ impl ser::SerializeStructVariant for SerializeStructVariant { type Ok = Ipld; type Error = SerdeError; - fn serialize_field( + fn serialize_field( &mut self, key: &'static str, value: &T, ) -> Result<(), Self::Error> where - T: ser::Serialize, + T: ser::Serialize + ?Sized, { self.map .insert(key.to_string(), value.serialize(Serializer)?); diff --git a/dag-cbor-derive/src/attr.rs b/dag-cbor-derive/src/attr.rs index e8431ca0..f8330cc4 100644 --- a/dag-cbor-derive/src/attr.rs +++ b/dag-cbor-derive/src/attr.rs @@ -10,6 +10,7 @@ mod kw { custom_keyword!(default); } +#[allow(dead_code)] #[derive(Debug)] pub struct Attrs { pub paren: syn::token::Paren, @@ -25,6 +26,7 @@ impl Parse for Attrs { } } +#[allow(dead_code)] #[derive(Debug)] pub struct Attr { pub key: K, diff --git a/dag-cbor/Cargo.toml b/dag-cbor/Cargo.toml index 3cac8d39..8f6462e5 100644 --- a/dag-cbor/Cargo.toml +++ b/dag-cbor/Cargo.toml @@ -15,6 +15,6 @@ thiserror = "1.0.25" [dev-dependencies] hex = "0.4.3" libipld-macro = { path = "../macro" } -multihash = "0.17.0" +multihash-codetable = { version = "0.1.4", features = ["blake3"] } quickcheck = "1.0.3" serde_cbor = { version = "0.11.1", features = ["tags"] } diff --git a/dag-cbor/src/encode.rs b/dag-cbor/src/encode.rs index 37c6ef51..f1110344 100644 --- a/dag-cbor/src/encode.rs +++ b/dag-cbor/src/encode.rs @@ -27,10 +27,10 @@ pub fn write_null(w: &mut W) -> Result<()> { pub fn write_u8(w: &mut W, major: MajorKind, value: u8) -> Result<()> { let major = major as u8; if value <= 0x17 { - let buf = [major << 5 | value]; + let buf = [(major << 5) | value]; w.write_all(&buf)?; } else { - let buf = [major << 5 | 24, value]; + let buf = [(major << 5) | 24, value]; w.write_all(&buf)?; } Ok(()) @@ -38,10 +38,10 @@ pub fn write_u8(w: &mut W, major: MajorKind, value: u8) -> Result<()> /// Writes a u16 to a cbor encoded byte stream. pub fn write_u16(w: &mut W, major: MajorKind, value: u16) -> Result<()> { - if value <= u16::from(u8::max_value()) { + if value <= u16::from(u8::MAX) { write_u8(w, major, value as u8)?; } else { - let mut buf = [(major as u8) << 5 | 25, 0, 0]; + let mut buf = [((major as u8) << 5) | 25, 0, 0]; BigEndian::write_u16(&mut buf[1..], value); w.write_all(&buf)?; } @@ -50,10 +50,10 @@ pub fn write_u16(w: &mut W, major: MajorKind, value: u16) -> Result<() /// Writes a u32 to a cbor encoded byte stream. pub fn write_u32(w: &mut W, major: MajorKind, value: u32) -> Result<()> { - if value <= u32::from(u16::max_value()) { + if value <= u32::from(u16::MAX) { write_u16(w, major, value as u16)?; } else { - let mut buf = [(major as u8) << 5 | 26, 0, 0, 0, 0]; + let mut buf = [((major as u8) << 5) | 26, 0, 0, 0, 0]; BigEndian::write_u32(&mut buf[1..], value); w.write_all(&buf)?; } @@ -62,10 +62,10 @@ pub fn write_u32(w: &mut W, major: MajorKind, value: u32) -> Result<() /// Writes a u64 to a cbor encoded byte stream. pub fn write_u64(w: &mut W, major: MajorKind, value: u64) -> Result<()> { - if value <= u64::from(u32::max_value()) { + if value <= u64::from(u32::MAX) { write_u32(w, major, value as u32)?; } else { - let mut buf = [(major as u8) << 5 | 27, 0, 0, 0, 0, 0, 0, 0, 0]; + let mut buf = [((major as u8) << 5) | 27, 0, 0, 0, 0, 0, 0, 0, 0]; BigEndian::write_u64(&mut buf[1..], value); w.write_all(&buf)?; } @@ -224,12 +224,12 @@ impl Encode for String { impl Encode for i128 { fn encode(&self, _: DagCbor, w: &mut W) -> Result<()> { if *self < 0 { - if -(*self + 1) > u64::max_value() as i128 { + if -(*self + 1) > u64::MAX as i128 { return Err(NumberOutOfRange::new::().into()); } write_u64(w, MajorKind::NegativeInt, -(*self + 1) as u64)?; } else { - if *self > u64::max_value() as i128 { + if *self > u64::MAX as i128 { return Err(NumberOutOfRange::new::().into()); } write_u64(w, MajorKind::UnsignedInt, *self as u64)?; diff --git a/dag-cbor/src/lib.rs b/dag-cbor/src/lib.rs index 00cf7547..5c7021ab 100644 --- a/dag-cbor/src/lib.rs +++ b/dag-cbor/src/lib.rs @@ -42,7 +42,7 @@ mod tests { use libipld_core::cid::Cid; use libipld_core::codec::assert_roundtrip; use libipld_core::ipld::Ipld; - use libipld_core::multihash::{Code, MultihashDigest}; + use multihash_codetable::{Code, MultihashDigest}; use libipld_macro::ipld; use std::collections::HashSet; diff --git a/dag-json/Cargo.toml b/dag-json/Cargo.toml index cd043c69..46eea5b3 100644 --- a/dag-json/Cargo.toml +++ b/dag-json/Cargo.toml @@ -9,6 +9,6 @@ repository = "https://github.com/ipfs-rust/rust-ipld" [dependencies] libipld-core = { version = "0.16.0", path = "../core" } -multihash = "0.18.0" serde_json = { version = "1.0.64", features = ["float_roundtrip"] } serde = { version = "1.0.126", features = ["derive"] } +multihash-codetable = { version = "0.1.4", features = ["blake3"] } diff --git a/dag-json/src/codec.rs b/dag-json/src/codec.rs index 0d97be04..0b39d3e0 100644 --- a/dag-json/src/codec.rs +++ b/dag-json/src/codec.rs @@ -62,7 +62,7 @@ fn deserialize<'de, D: de::Deserializer<'de>>(deserializer: D) -> Result(&'a Ipld); -impl<'a> Serialize for Wrapper<'a> { +impl Serialize for Wrapper<'_> { fn serialize(&self, serializer: S) -> Result where S: ser::Serializer, diff --git a/dag-json/src/lib.rs b/dag-json/src/lib.rs index b22d5b83..2c28f942 100644 --- a/dag-json/src/lib.rs +++ b/dag-json/src/lib.rs @@ -60,7 +60,7 @@ impl References for Ipld { mod tests { use super::*; use libipld_core::cid::Cid; - use libipld_core::multihash::{Code, MultihashDigest}; + use multihash_codetable::{Code, MultihashDigest}; use std::collections::BTreeMap; #[test] diff --git a/dag-pb/Cargo.toml b/dag-pb/Cargo.toml index 3ddf1d18..296ab229 100644 --- a/dag-pb/Cargo.toml +++ b/dag-pb/Cargo.toml @@ -9,12 +9,11 @@ repository = "https://github.com/ipfs-rust/rust-ipld" [dependencies] libipld-core = { version = "0.16.0", path = "../core" } -thiserror = "1.0.25" quick-protobuf = "0.8.1" bytes = "1.3.0" [dev-dependencies] libipld-macro = { path = "../macro" } libipld = { path = "../" } -multihash = "0.18.0" +multihash-codetable = { version = "0.1.4", features = ["blake3"] } hex = "0.4.3" diff --git a/dag-pb/src/codec.rs b/dag-pb/src/codec.rs index 4fe10dbc..06a8391d 100644 --- a/dag-pb/src/codec.rs +++ b/dag-pb/src/codec.rs @@ -258,7 +258,7 @@ impl MessageWrite for PbLink { size += 1 + sizeof_len(l); if let Some(ref name) = self.name { - size += 1 + sizeof_len(name.as_bytes().len()); + size += 1 + sizeof_len(name.len()); } if let Some(tsize) = self.size { diff --git a/dag-pb/src/lib.rs b/dag-pb/src/lib.rs index 960713a7..0fd5b846 100644 --- a/dag-pb/src/lib.rs +++ b/dag-pb/src/lib.rs @@ -67,7 +67,7 @@ impl References for Ipld { mod tests { use super::*; use libipld_core::cid::Cid; - use libipld_core::multihash::{Code, MultihashDigest}; + use multihash_codetable::{Code, MultihashDigest}; use std::collections::BTreeMap; #[test] diff --git a/macro/Cargo.toml b/macro/Cargo.toml index f78b97f9..023df516 100644 --- a/macro/Cargo.toml +++ b/macro/Cargo.toml @@ -11,4 +11,4 @@ repository = "https://github.com/ipfs-rust/rust-ipld" libipld-core = { version = "0.16.0", path = "../core" } [dev-dependencies] -multihash = { version = "0.18.0", features = ["blake3"] } +multihash-codetable = { version = "0.1.4", features = ["blake3"] } diff --git a/macro/src/lib.rs b/macro/src/lib.rs index b998b355..682979bb 100644 --- a/macro/src/lib.rs +++ b/macro/src/lib.rs @@ -293,7 +293,7 @@ macro_rules! ipld_unexpected { mod tests { use super::*; use libipld_core::cid::Cid; - use libipld_core::multihash::{Code, MultihashDigest}; + use multihash_codetable::{Code, MultihashDigest}; #[test] fn test_macro() { diff --git a/src/block.rs b/src/block.rs index 8eec9ce9..c26b634c 100644 --- a/src/block.rs +++ b/src/block.rs @@ -1,14 +1,14 @@ //! Block validation -use crate::cid::Cid; +use crate::cid::{self, Cid}; use crate::codec::{Codec, Decode, Encode, References}; use crate::error::{BlockTooLarge, InvalidMultihash, Result, UnsupportedMultihash}; use crate::ipld::Ipld; -use crate::multihash::MultihashDigest; use crate::store::StoreParams; use core::borrow::Borrow; use core::convert::TryFrom; use core::marker::PhantomData; use core::ops::Deref; +use multihash_codetable::MultihashDigest; /// Block #[derive(Clone)] @@ -81,6 +81,14 @@ fn verify_cid, const S: usize>(cid: &Cid, payload: &[u8]) Ok(()) } +// Helper function to create a CID with the correct multihash version +fn create_cid(codec: u64, mh_bytes: &[u8]) -> Result { + // Convert via the raw bytes + let mh = cid::multihash::Multihash::from_bytes(mh_bytes) + .map_err(|_| InvalidMultihash(mh_bytes.to_vec()))?; + Ok(Cid::new_v1(codec, mh)) +} + impl Block { /// Creates a new block. Returns an error if the hash doesn't match /// the data. @@ -114,13 +122,10 @@ impl Block { } /// Encode a block.` - pub fn encode + ?Sized>( - codec: CE, - hcode: S::Hashes, - payload: &T, - ) -> Result + pub fn encode(codec: CE, hcode: S::Hashes, payload: &T) -> Result where - CE: Into, + CE: Codec + Into, + T: Encode + ?Sized, { debug_assert_eq!( Into::::into(codec), @@ -131,7 +136,9 @@ impl Block { return Err(BlockTooLarge(data.len()).into()); } let mh = hcode.digest(&data); - let cid = Cid::new_v1(codec.into(), mh); + // Convert multihash to bytes and create CID using our helper + let mh_bytes = mh.to_bytes(); + let cid = create_cid::<64>(codec.into(), &mh_bytes)?; Ok(Self { _marker: PhantomData, cid, @@ -149,7 +156,7 @@ impl Block { /// use libipld::block::Block; /// use libipld::cbor::DagCborCodec; /// use libipld::ipld::Ipld; - /// use libipld::multihash::Code; + /// use multihash_codetable::Code; /// use libipld::store::DefaultParams; /// /// let block = @@ -193,9 +200,9 @@ mod tests { use crate::codec_impl::IpldCodec; use crate::ipld; use crate::ipld::Ipld; - use crate::multihash::Code; use crate::store::DefaultParams; use fnv::FnvHashSet; + use multihash_codetable::Code; type IpldBlock = Block; diff --git a/src/lib.rs b/src/lib.rs index b9b26978..e9cde6b8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,6 +25,7 @@ pub use codec_impl::IpldCodec; pub use error::Result; pub use ipld::Ipld; pub use link::Link; -pub use multihash::Multihash; +// Don't re-export multihash to avoid version conflicts +// pub use multihash::Multihash; pub use path::{DagPath, Path}; pub use store::DefaultParams; diff --git a/src/path.rs b/src/path.rs index 40dac980..7e51799f 100644 --- a/src/path.rs +++ b/src/path.rs @@ -45,15 +45,17 @@ impl From for Path { } } -impl ToString for Path { - fn to_string(&self) -> String { - let mut path = "".to_string(); +impl core::fmt::Display for Path { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + let mut first = true; for seg in &self.0 { - path.push_str(seg.as_str()); - path.push('/'); + if !first { + write!(f, "/")?; + } + write!(f, "{}", seg)?; + first = false; } - path.pop(); - path + Ok(()) } } diff --git a/src/store.rs b/src/store.rs index e4894f60..ac1d270b 100644 --- a/src/store.rs +++ b/src/store.rs @@ -17,7 +17,7 @@ //! incrementally deleting unaliased blocks until the size target is no longer exceeded. It is //! implementation defined in which order unaliased blocks get removed. use crate::codec::Codec; -use crate::multihash::MultihashDigest; +use multihash_codetable::{Code, MultihashDigest}; /// The store parameters. pub trait StoreParams: std::fmt::Debug + Clone + Send + Sync + Unpin + 'static { @@ -36,5 +36,5 @@ pub struct DefaultParams; impl StoreParams for DefaultParams { const MAX_BLOCK_SIZE: usize = 1_048_576; type Codecs = crate::IpldCodec; - type Hashes = crate::multihash::Code; + type Hashes = Code; }