Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DAGJson support for Ipld #390

Merged
merged 22 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ clean:

lint: license clean
cargo fmt --all
cargo clippy -- -D warnings
cargo clippy --all-features -- -D warnings

build:
cargo build --bin forest
Expand Down
2 changes: 1 addition & 1 deletion blockchain/blocks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ beacon = { package = "beacon", path = "../beacon" }
crypto = { path = "../../crypto" }
message = { package = "forest_message", path = "../../vm/message" }
clock = { path = "../../node/clock" }
cid = { package = "forest_cid", path = "../../ipld/cid", features = ["serde_derive"] }
cid = { package = "forest_cid", path = "../../ipld/cid", features = ["cbor"] }
derive_builder = "0.9"
serde = { version = "1.0", features = ["derive"] }
encoding = { package = "forest_encoding", path = "../../encoding" }
Expand Down
10 changes: 9 additions & 1 deletion ipld/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ version = "0.1.0"
authors = ["ChainSafe Systems <info@chainsafe.io>"]
edition = "2018"

[package.metadata.docs.rs]
features = ["json"]

[dependencies]
encoding = { package = "forest_encoding", path = "../encoding" }
serde = { version = "1.0", features = ["derive"] }
thiserror = "1.0"
serde_json = { version = "1.0", optional = true }
multibase = { version = "0.8.0", optional = true }

[dependencies.cid]
package = "forest_cid"
path = "../ipld/cid"
features = ["serde_derive"]
features = ["cbor"]

[features]
json = ["serde_json", "multibase"]
20 changes: 10 additions & 10 deletions ipld/blockstore/src/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ mod tests {
use super::*;
use cid::multihash::{Blake2b256, Identity};
use commcid::{commitment_to_cid, FilecoinMultihashCode};
use forest_ipld::Ipld;
use std::collections::BTreeMap;
use forest_ipld::{ipld, Ipld};

#[test]
fn basic_buffered_store() {
Expand All @@ -212,14 +211,15 @@ mod tests {
let identity_cid = buf_store.put(&0u8, Identity).unwrap();

// Create map to insert into store
let mut map: BTreeMap<String, Ipld> = Default::default();
map.insert("array".to_owned(), Ipld::Link(arr_cid.clone()));
let sealed_comm_cid = commitment_to_cid(&[7u8; 32], FilecoinMultihashCode::SealedV1);
map.insert("sealed".to_owned(), Ipld::Link(sealed_comm_cid.clone()));
let unsealed_comm_cid = commitment_to_cid(&[5u8; 32], FilecoinMultihashCode::UnsealedV1);
map.insert("unsealed".to_owned(), Ipld::Link(unsealed_comm_cid.clone()));
map.insert("identity".to_owned(), Ipld::Link(identity_cid.clone()));
map.insert("value".to_owned(), Ipld::String(str_val.to_owned()));
let map = ipld!({
"array": Link(arr_cid.clone()),
"sealed": Link(sealed_comm_cid.clone()),
"unsealed": Link(unsealed_comm_cid.clone()),
"identity": Link(identity_cid.clone()),
"value": str_val,
});
let map_cid = buf_store.put(&map, Blake2b256).unwrap();

let root_cid = buf_store.put(&(map_cid.clone(), 1u8), Blake2b256).unwrap();
Expand All @@ -238,10 +238,10 @@ mod tests {
mem.get::<(String, u8)>(&arr_cid).unwrap(),
Some((str_val.to_owned(), value))
);
assert_eq!(mem.get::<Ipld>(&map_cid).unwrap(), Some(Ipld::Map(map)));
assert_eq!(mem.get::<Ipld>(&map_cid).unwrap(), Some(map));
assert_eq!(
mem.get::<Ipld>(&root_cid).unwrap(),
Some(Ipld::List(vec![Ipld::Link(map_cid), Ipld::Integer(1)]))
Some(ipld!([Link(map_cid), 1]))
);
assert_eq!(buf_store.get::<u8>(&identity_cid).unwrap(), None);
assert_eq!(buf_store.get::<Ipld>(&unsealed_comm_cid).unwrap(), None);
Expand Down
2 changes: 1 addition & 1 deletion ipld/car/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"

[dependencies]
unsigned-varint = "0.3.1"
cid = { package = "forest_cid", path = "../cid", features = ["serde_derive"] }
cid = { package = "forest_cid", path = "../cid", features = ["cbor"] }
forest_encoding = { path = "../../encoding" }
blockstore = { package = "ipld_blockstore", path = "../blockstore" }
serde = { version = "1.0", features = ["derive"] }
Expand Down
8 changes: 6 additions & 2 deletions ipld/cid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["ChainSafe Systems <info@chainsafe.io>"]
edition = "2018"

[package.metadata.docs.rs]
features = ["serde_derive"]
features = ["cbor", "json"]

[dependencies]
multihash = "0.10.0"
Expand All @@ -16,5 +16,9 @@ serde_cbor = { version = "0.11.0", features = ["tags"], optional = true }
serde_bytes = { version = "0.11.3", optional = true }
thiserror = "1.0"

[dev-dependencies]
serde_json = "1.0"

[features]
serde_derive = ["serde", "serde_bytes", "serde_cbor"]
cbor = ["serde", "serde_bytes", "serde_cbor"]
json = ["serde"]
37 changes: 37 additions & 0 deletions ipld/cid/src/json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use super::Cid;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};

/// Wrapper for serializing and deserializing a Cid from JSON.
#[derive(Deserialize, Serialize)]
#[serde(transparent)]
pub struct CidJson(#[serde(with = "self")] pub Cid);

/// Wrapper for serializing a cid reference to JSON.
#[derive(Serialize)]
#[serde(transparent)]
pub struct CidJsonRef<'a>(#[serde(with = "self")] pub &'a Cid);

pub fn serialize<S>(c: &Cid, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
CidMap { cid: c.to_string() }.serialize(serializer)
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<Cid, D::Error>
where
D: Deserializer<'de>,
{
let CidMap { cid } = Deserialize::deserialize(deserializer)?;
cid.parse().map_err(de::Error::custom)
}

/// Struct just used as a helper to serialize a cid into a map with key "/"
#[derive(Serialize, Deserialize)]
struct CidMap {
#[serde(rename = "/")]
cid: String,
}
17 changes: 10 additions & 7 deletions ipld/cid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,23 @@ use std::convert::TryInto;
use std::fmt;
use std::io::Cursor;

#[cfg(feature = "serde_derive")]
#[cfg(feature = "cbor")]
use serde::{de, ser};
#[cfg(feature = "serde_derive")]
#[cfg(feature = "cbor")]
use serde_cbor::tags::Tagged;
#[cfg(feature = "serde_derive")]
#[cfg(feature = "cbor")]
use std::convert::TryFrom;

#[cfg(feature = "serde_derive")]
#[cfg(feature = "cbor")]
const CBOR_TAG_CID: u64 = 42;
/// multibase identity prefix
/// https://github.com/ipld/specs/blob/master/block-layer/codecs/dag-cbor.md#link-format
#[cfg(feature = "serde_derive")]
#[cfg(feature = "cbor")]
const MULTIBASE_IDENTITY: u8 = 0;

#[cfg(feature = "json")]
pub mod json;

/// Prefix represents all metadata of a CID, without the actual content.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Prefix {
Expand All @@ -53,7 +56,7 @@ impl Default for Cid {
}
}

#[cfg(feature = "serde_derive")]
#[cfg(feature = "cbor")]
impl ser::Serialize for Cid {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -75,7 +78,7 @@ impl ser::Serialize for Cid {
}
}

#[cfg(feature = "serde_derive")]
#[cfg(feature = "cbor")]
impl<'de> de::Deserialize<'de> for Cid {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down
61 changes: 61 additions & 0 deletions ipld/cid/tests/json_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

#![cfg(feature = "json")]

use forest_cid::{
json::{self, CidJson, CidJsonRef},
Cid,
};
use serde::{Deserialize, Serialize};
use serde_json::{from_str, to_string};

#[test]
fn symmetric_json_serialization() {
let cid: Cid = "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n"
.parse()
.unwrap();
let cid_json = r#"{"/":"QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n"}"#;

// Deserialize
let CidJson(cid_d) = from_str(cid_json).unwrap();
assert_eq!(&cid_d, &cid, "Deserialized cid does not match");

// Serialize
let ser_cid = to_string(&CidJsonRef(&cid_d)).unwrap();
assert_eq!(ser_cid, cid_json);
}

#[test]
fn annotating_struct_json() {
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct TestStruct {
#[serde(with = "json")]
cid_one: Cid,
#[serde(with = "json")]
cid_two: Cid,
other: String,
}
let test_json = r#"
{
"cid_one": {
"/": "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n"
},
"cid_two": {
"/": "bafy2bzaceaa466o2jfc4g4ggrmtf55ygigvkmxvkr5mvhy4qbwlxetbmlkqjk"
},
"other": "Some data"
}
"#;
let expected = TestStruct {
cid_one: "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n"
.parse()
.unwrap(),
cid_two: "bafy2bzaceaa466o2jfc4g4ggrmtf55ygigvkmxvkr5mvhy4qbwlxetbmlkqjk"
.parse()
.unwrap(),
other: "Some data".to_owned(),
};

assert_eq!(from_str::<TestStruct>(test_json).unwrap(), expected);
}
2 changes: 1 addition & 1 deletion ipld/cid/tests/serde_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

#![cfg(feature = "serde_derive")]
#![cfg(feature = "cbor")]

use forest_cid::Cid;
use multihash::Blake2b256;
Expand Down
2 changes: 1 addition & 1 deletion ipld/hamt/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ where
Pointer::Values(vals) => {
// Update, if the key already exists.
if let Some(i) = vals.iter().position(|p| p.key() == &key) {
std::mem::replace(&mut vals[i].1, value);
vals[i].1 = value;
return Ok(());
}

Expand Down
7 changes: 7 additions & 0 deletions ipld/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use encoding::error::Error as CborError;
use serde::ser;
use std::fmt;
use thiserror::Error;
Expand All @@ -19,3 +20,9 @@ impl ser::Error for Error {
Error::Encoding(msg.to_string())
}
}

impl From<CborError> for Error {
fn from(e: CborError) -> Error {
Error::Encoding(e.to_string())
}
}
Loading