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

Update message json format to match Lotus #870

Merged
merged 3 commits into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 20 additions & 0 deletions ipld/cid/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,23 @@ pub mod vec {
deserializer.deserialize_any(GoVecVisitor::<Cid, CidJson>::new())
}
}

pub mod opt {
use super::{Cid, CidJson, CidJsonRef};
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};

pub fn serialize<S>(v: &Option<Cid>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
v.as_ref().map(|s| CidJsonRef(s)).serialize(serializer)
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Cid>, D::Error>
where
D: Deserializer<'de>,
{
let s: Option<CidJson> = Deserialize::deserialize(deserializer)?;
Ok(s.map(|v| v.0))
}
}
1 change: 1 addition & 0 deletions vm/address/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const MAX_ADDRESS_LEN: usize = 84 + 2;
const MAINNET_PREFIX: &str = "f";
const TESTNET_PREFIX: &str = "t";

#[cfg(feature = "json")]
const UNDEF_ADDR_STRING: &str = "<empty>";

// TODO pull network from config (probably)
Expand Down
2 changes: 1 addition & 1 deletion vm/message/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ fil_types = { path = "../../types", optional = true, version = "0.1" }
serde_json = "1.0"

[features]
json = ["base64", "crypto/json", "forest_json_utils"]
json = ["base64", "crypto/json", "forest_json_utils", "cid/json"]
# TODO try to prune out this dependency before releasing
proofs = ["fil_types"]
6 changes: 5 additions & 1 deletion vm/message/src/signed_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ impl Cbor for SignedMessage {
pub mod json {
use super::*;
use crate::unsigned_message;
use cid::Cid;
use crypto::signature;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::{ser, Deserialize, Deserializer, Serialize, Serializer};

/// Wrapper for serializing and deserializing a SignedMessage from JSON.
#[derive(Deserialize, Serialize)]
Expand Down Expand Up @@ -157,10 +158,13 @@ pub mod json {
message: &'a UnsignedMessage,
#[serde(with = "signature::json")]
signature: &'a Signature,
#[serde(default, rename = "CID", with = "cid::json::opt")]
cid: Option<Cid>,
}
SignedMessageSer {
message: &m.message,
signature: &m.signature,
cid: Some(m.cid().map_err(ser::Error::custom)?),
}
.serialize(serializer)
}
Expand Down
6 changes: 5 additions & 1 deletion vm/message/src/unsigned_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ impl Cbor for UnsignedMessage {}
#[cfg(feature = "json")]
pub mod json {
use super::*;
use serde::de;
use cid::Cid;
use serde::{de, ser};

/// Wrapper for serializing and deserializing a UnsignedMessage from JSON.
#[derive(Deserialize, Serialize, Debug)]
Expand Down Expand Up @@ -253,6 +254,8 @@ pub mod json {
#[serde(rename = "Method")]
method_num: u64,
params: Option<String>,
#[serde(default, rename = "CID", with = "cid::json::opt")]
cid: Option<Cid>,
}

pub fn serialize<S>(m: &UnsignedMessage, serializer: S) -> Result<S::Ok, S::Error>
Expand All @@ -270,6 +273,7 @@ pub mod json {
gas_premium: m.gas_premium.to_string(),
method_num: m.method_num,
params: Some(base64::encode(m.params.bytes())),
cid: Some(m.cid().map_err(ser::Error::custom)?),
}
.serialize(serializer)
}
Expand Down
4 changes: 2 additions & 2 deletions vm/message/tests/message_json_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use vm::Serialized;

#[test]
fn unsigned_symmetric_json() {
let message_json = r#"{"Version":9,"To":"t01234","From":"t01234","Nonce":42,"Value":"0","GasLimit":1,"GasFeeCap":"2","GasPremium":"9","Method":1,"Params":"Ynl0ZSBhcnJheQ=="}"#;
let message_json = r#"{"Version":9,"To":"t01234","From":"t01234","Nonce":42,"Value":"0","GasLimit":1,"GasFeeCap":"2","GasPremium":"9","Method":1,"Params":"Ynl0ZSBhcnJheQ==","CID":{"/":"bafy2bzacea5z7ywqogtuxykqcis76lrhl4fl27bg63firldlry5ml6bbahoy6"}}"#;

// Deserialize
let UnsignedMessageJson(cid_d) = from_str(message_json).unwrap();
Expand All @@ -34,7 +34,7 @@ fn unsigned_symmetric_json() {

#[test]
fn signed_symmetric_json() {
let message_json = r#"{"Message":{"Version":9,"To":"t01234","From":"t01234","Nonce":42,"Value":"0","GasLimit":1,"GasFeeCap":"2","GasPremium":"9","Method":1,"Params":"Ynl0ZSBhcnJheQ=="},"Signature":{"Type":2,"Data":"Ynl0ZSBhcnJheQ=="}}"#;
let message_json = r#"{"Message":{"Version":9,"To":"t01234","From":"t01234","Nonce":42,"Value":"0","GasLimit":1,"GasFeeCap":"2","GasPremium":"9","Method":1,"Params":"Ynl0ZSBhcnJheQ==","CID":{"/":"bafy2bzacea5z7ywqogtuxykqcis76lrhl4fl27bg63firldlry5ml6bbahoy6"}},"Signature":{"Type":2,"Data":"Ynl0ZSBhcnJheQ=="},"CID":{"/":"bafy2bzacea5z7ywqogtuxykqcis76lrhl4fl27bg63firldlry5ml6bbahoy6"}}"#;

// Deserialize
let SignedMessageJson(cid_d) = from_str(message_json).unwrap();
Expand Down