Serialization with serde.

This commit is contained in:
Tomasz Drwięga
2017-11-10 21:31:48 +01:00
parent 74ec849f7e
commit 799d03254f
9 changed files with 444 additions and 22 deletions
+57 -5
View File
@@ -24,7 +24,7 @@ pub type HeaderHash = H256;
pub type ProofHash = H256;
/// Unique identifier of a parachain.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Serialize, Deserialize)]
pub struct ParachainId(u64);
impl From<ParachainId> for u64 {
@@ -35,19 +35,23 @@ impl From<u64> for ParachainId {
fn from(x: u64) -> Self { ParachainId(x) }
}
/// A wrapper type for parachain block header raw bytes.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ParachainBlockHeader(pub Vec<u8>);
/// A parachain block proposal.
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ParachainProposal {
/// The ID of the parachain this is a proposal for.
pub parachain: ParachainId,
/// Parachain block header bytes.
pub header: Vec<u8>,
pub header: ParachainBlockHeader,
/// Hash of data necessary to prove validity of the header.
pub proof_hash: ProofHash,
}
/// A relay chain block header.
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Header {
/// Block parent's hash.
pub parent_hash: HeaderHash,
@@ -63,8 +67,56 @@ pub struct Header {
///
/// Included candidates should be sorted by parachain ID, and without duplicate
/// IDs.
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Body {
/// Parachain proposal blocks.
pub para_blocks: Vec<ParachainProposal>,
}
#[cfg(test)]
mod tests {
use super::*;
use polkadot_serializer as ser;
#[test]
fn test_header_serialization() {
assert_eq!(ser::to_string_pretty(&Header {
parent_hash: 5.into(),
state_root: 3.into(),
timestamp: 10,
number: 67,
}), r#"{
"parent_hash": "0x0000000000000000000000000000000000000000000000000000000000000005",
"state_root": "0x0000000000000000000000000000000000000000000000000000000000000003",
"timestamp": 10,
"number": 67
}"#);
}
#[test]
fn test_body_serialization() {
assert_eq!(ser::to_string_pretty(&Body {
para_blocks: vec![
ParachainProposal {
parachain: 5.into(),
header: ParachainBlockHeader(vec![1, 2, 3, 4]),
proof_hash: 5.into(),
}
],
}), r#"{
"para_blocks": [
{
"parachain": 5,
"header": [
1,
2,
3,
4
],
"proof_hash": "0x0000000000000000000000000000000000000000000000000000000000000005"
}
]
}"#);
}
}