Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Serialization #13

Merged
merged 3 commits into from
Nov 11, 2017
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
127 changes: 122 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ authors = ["Parity Team <[email protected]>"]

[dependencies]
polkadot-cli = { path = "cli" }
polkadot-primitives = { path = "primitives" }

[workspace]
members = [
"primitives",
"serializer"
]
9 changes: 8 additions & 1 deletion primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ authors = ["Parity Team <[email protected]>"]

[dependencies]
crunchy = "0.1.5"
uint = { git = "https://github.com/paritytech/primitives.git" }
fixed-hash = { git = "https://github.com/paritytech/primitives.git" }
rustc-hex = "1.0"
serde = "1.0"
serde_derive = "1.0"
uint = { git = "https://github.com/paritytech/primitives.git" }

[dev-dependencies]
polkadot-serializer = { path = "../serializer", version = "0.1" }
pretty_assertions = "0.4"

[features]
default = ["std"]
Expand Down
62 changes: 57 additions & 5 deletions primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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"
}
]
}"#);
}
}
Loading