Skip to content

Commit

Permalink
feat: remove new_from_prefix() method
Browse files Browse the repository at this point in the history
Credit goes to @dvc94ch as this code is based on
#18

The code to generate a CID from a prefix is not part of Rust IPFS Bitswap:
https://github.com/ipfs-rust/rust-ipfs/blob/d9955928bb05d07523e4955bc247e49b670f0c48/bitswap/src/prefix.rs

BREAKING CHANGE: `Cid::new_from_prefix()` was removed

Use `Cid::new_v0()` or `Cid::new_v1()` instead.
  • Loading branch information
vmx committed Mar 27, 2020
1 parent fcb8ea3 commit cd1c1eb
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 131 deletions.
14 changes: 1 addition & 13 deletions src/arb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use quickcheck::{Arbitrary, Gen};
use rand::seq::SliceRandom;
use rand::Rng;

use crate::{Cid, Codec, Prefix, Version};
use crate::{Cid, Codec, Version};

const CODECS: [Codec; 18] = [
Codec::Raw,
Expand Down Expand Up @@ -67,15 +67,3 @@ impl Arbitrary for Cid {
}
}
}

impl Arbitrary for Prefix {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
let cid: Cid = Arbitrary::arbitrary(g);
Prefix {
version: cid.version(),
codec: cid.codec(),
mh_type: cid.hash().algorithm(),
mh_len: cid.hash().digest().len(),
}
}
}
24 changes: 0 additions & 24 deletions src/cid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use unsigned_varint::{decode as varint_decode, encode as varint_encode};

use crate::codec::Codec;
use crate::error::{Error, Result};
use crate::prefix::Prefix;
use crate::version::Version;

/// Representation of a CID.
Expand Down Expand Up @@ -55,19 +54,6 @@ impl Cid {
}
}

/// Create a new CID from a prefix and some data.
pub fn new_from_prefix(prefix: &Prefix, data: &[u8]) -> Cid {
let mut hash = prefix.mh_type.hasher().unwrap().digest(data);
if prefix.mh_len < hash.digest().len() {
hash = multihash::wrap(hash.algorithm(), &hash.digest()[..prefix.mh_len]);
}
Cid {
version: prefix.version,
codec: prefix.codec,
hash,
}
}

/// Returns the cid version.
pub fn version(&self) -> Version {
self.version
Expand Down Expand Up @@ -116,16 +102,6 @@ impl Cid {
Version::V1 => self.to_bytes_v1(),
}
}

/// Return the prefix of the CID.
pub fn prefix(&self) -> Prefix {
Prefix {
version: self.version,
codec: self.codec,
mh_type: self.hash.algorithm(),
mh_len: self.hash.digest().len(),
}
}
}

#[allow(clippy::derive_hash_xor_eq)]
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
mod cid;
mod codec;
mod error;
mod prefix;
mod version;

#[cfg(any(test, feature = "test"))]
Expand All @@ -16,5 +15,4 @@ mod arb;
pub use self::cid::Cid;
pub use self::codec::Codec;
pub use self::error::{Error, Result};
pub use self::prefix::Prefix;
pub use self::version::Version;
66 changes: 0 additions & 66 deletions src/prefix.rs

This file was deleted.

29 changes: 3 additions & 26 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};
use std::str::FromStr;

use cid::{Cid, Codec, Error, Prefix, Version};
use cid::{Cid, Codec, Error, Version};
use multihash::Sha2_256;

#[test]
Expand Down Expand Up @@ -57,24 +57,6 @@ fn v0_error() {
assert_eq!(Cid::try_from(bad), Err(Error::ParsingError));
}

#[test]
fn prefix_roundtrip() {
let data = b"awesome test content";
let h = Sha2_256::digest(data);

let cid = Cid::new_v1(Codec::DagProtobuf, h);
let prefix = cid.prefix();

let cid2 = Cid::new_from_prefix(&prefix, data);

assert_eq!(cid, cid2);

let prefix_bytes = prefix.as_bytes();
let prefix2 = Prefix::new_from_bytes(&prefix_bytes).unwrap();

assert_eq!(prefix, prefix2);
}

#[test]
fn from() {
let the_hash = "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n";
Expand All @@ -95,14 +77,9 @@ fn from() {
#[test]
fn test_hash() {
let data: Vec<u8> = vec![1, 2, 3];
let prefix = Prefix {
version: Version::V0,
codec: Codec::DagProtobuf,
mh_type: multihash::Code::Sha2_256,
mh_len: 32,
};
let hash = Sha2_256::digest(&data);
let mut map = HashMap::new();
let cid = Cid::new_from_prefix(&prefix, &data);
let cid = Cid::new_v0(hash).unwrap();
map.insert(cid.clone(), data.clone());
assert_eq!(&data, map.get(&cid).unwrap());
}
Expand Down

0 comments on commit cd1c1eb

Please sign in to comment.