Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Derive Encode/Decode wherever we can.
  • Loading branch information
tomusdrw committed Aug 6, 2018
commit 66f642476d885a25c52a493759f442fa2c0c4d73
13 changes: 13 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions polkadot/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ polkadot-consensus = { path = "../consensus" }
polkadot-primitives = { path = "../primitives" }
substrate-bft = { path = "../../substrate/bft" }
substrate-codec = { path = "../../substrate/codec" }
substrate-codec-derive = { path = "../../substrate/codec/derive" }
substrate-network = { path = "../../substrate/network" }
substrate-primitives = { path = "../../substrate/primitives" }
ed25519 = { path = "../../substrate/ed25519" }
Expand Down
29 changes: 3 additions & 26 deletions polkadot/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ extern crate rhododendron;

#[macro_use]
extern crate log;
#[macro_use]
extern crate substrate_codec_derive;

mod collator_pool;
mod local_collations;
Expand Down Expand Up @@ -73,36 +75,11 @@ type FullStatus = GenericFullStatus<Block>;
pub type NetworkService = ::substrate_network::Service<Block, PolkadotProtocol>;

/// Status of a Polkadot node.
#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
pub struct Status {
collating_for: Option<(AccountId, ParaId)>,
}

impl Encode for Status {
fn encode_to<T: codec::Output>(&self, dest: &mut T) {
match self.collating_for {
Some(ref details) => {
dest.push_byte(1);
dest.push(details);
}
None => {
dest.push_byte(0);
}
}
}
}

impl Decode for Status {
fn decode<I: codec::Input>(input: &mut I) -> Option<Self> {
let collating_for = match input.read_byte()? {
0 => None,
1 => Some(Decode::decode(input)?),
_ => return None,
};
Some(Status { collating_for })
}
}

struct BlockDataRequest {
attempted_peers: HashSet<SessionKey>,
consensus_parent: Hash,
Expand Down
1 change: 1 addition & 0 deletions polkadot/parachain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ description = "Types and utilities for creating and working with parachains"

[dependencies]
substrate-codec = { path = "../../substrate/codec", default-features = false }
substrate-codec-derive = { path = "../../substrate/codec/derive", default-features = false }
wasmi = { version = "0.4", optional = true }
error-chain = { version = "0.12", optional = true }

Expand Down
37 changes: 5 additions & 32 deletions polkadot/parachain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
/// Re-export of substrate-codec.
pub extern crate substrate_codec as codec;

#[macro_use]
extern crate substrate_codec_derive;

#[cfg(not(feature = "std"))]
extern crate alloc;

Expand All @@ -68,7 +71,7 @@ pub mod wasm;

/// Validation parameters for evaluating the parachain validity function.
// TODO: consolidated ingress and balance downloads
#[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct ValidationParams {
/// The collation body.
Expand All @@ -77,45 +80,15 @@ pub struct ValidationParams {
pub parent_head: Vec<u8>,
}

impl Encode for ValidationParams {
fn encode_to<T: Output>(&self, dest: &mut T) {
dest.push(&self.block_data);
dest.push(&self.parent_head);
}
}

impl Decode for ValidationParams {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Some(ValidationParams {
block_data: Decode::decode(input)?,
parent_head: Decode::decode(input)?,
})
}
}

/// The result of parachain validation.
// TODO: egress and balance uploads
#[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct ValidationResult {
/// New head data that should be included in the relay chain state.
pub head_data: Vec<u8>
}

impl Encode for ValidationResult {
fn encode_to<T: Output>(&self, dest: &mut T) {
dest.push(&self.head_data);
}
}

impl Decode for ValidationResult {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Some(ValidationResult {
head_data: Decode::decode(input)?,
})
}
}

/// Load the validation params from memory when implementing a Rust parachain.
///
/// Offset and length must have been provided by the validation
Expand Down
57 changes: 13 additions & 44 deletions polkadot/parachain/tests/adder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,66 +16,35 @@

//! Basic parachain that adds a number as part of its state.

#[macro_use]
extern crate substrate_codec_derive;
extern crate substrate_codec as codec;
extern crate polkadot_parachain as parachain;
extern crate tiny_keccak;

use parachain::ValidationParams;
use parachain::codec::{Decode, Encode, Input, Output};
use codec::{Decode, Encode};

// Head data for this parachain.
#[derive(Default, Clone)]
/// Head data for this parachain.
#[derive(Default, Clone, Encode, Decode)]
struct HeadData {
// Block number
/// Block number
number: u64,
// parent block keccak256
/// parent block keccak256
parent_hash: [u8; 32],
// hash of post-execution state.
/// hash of post-execution state.
post_state: [u8; 32],
}

impl Encode for HeadData {
fn encode_to<T: Output>(&self, dest: &mut T) {
dest.push(&self.number);
dest.push(&self.parent_hash);
dest.push(&self.post_state);
}
}

impl Decode for HeadData {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Some(HeadData {
number: Decode::decode(input)?,
parent_hash: Decode::decode(input)?,
post_state: Decode::decode(input)?,
})
}
}

// Block data for this parachain.
#[derive(Default, Clone)]
/// Block data for this parachain.
#[derive(Default, Clone, Encode, Decode)]
struct BlockData {
// State to begin from.
/// State to begin from.
state: u64,
// Amount to add (overflowing)
/// Amount to add (overflowing)
add: u64,
}

impl Encode for BlockData {
fn encode_to<T: Output>(&self, dest: &mut T) {
dest.push(&self.state);
dest.push(&self.add);
}
}

impl Decode for BlockData {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Some(BlockData {
state: Decode::decode(input)?,
add: Decode::decode(input)?,
})
}
}

const TEST_CODE: &[u8] = include_bytes!("res/adder.wasm");

fn hash_state(state: u64) -> [u8; 32] {
Expand Down
1 change: 1 addition & 0 deletions polkadot/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ authors = ["Parity Technologies <[email protected]>"]
serde = { version = "1.0", default_features = false }
serde_derive = { version = "1.0", optional = true }
substrate-codec = { path = "../../substrate/codec", default_features = false }
substrate-codec-derive = { path = "../../substrate/codec/derive", default_features = false }
substrate-primitives = { path = "../../substrate/primitives", default_features = false }
substrate-runtime-std = { path = "../../substrate/runtime-std", default_features = false }
substrate-runtime-primitives = { path = "../../substrate/runtime/primitives", default_features = false }
Expand Down
22 changes: 6 additions & 16 deletions polkadot/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(alloc))]

extern crate substrate_runtime_std as rstd;
extern crate substrate_codec as codec;
extern crate substrate_primitives as primitives;
extern crate substrate_runtime_primitives as runtime_primitives;
extern crate substrate_runtime_std as rstd;

#[cfg(test)]
extern crate substrate_serializer;

extern crate substrate_codec as codec;
#[macro_use]
extern crate substrate_codec_derive;

#[cfg(feature = "std")]
#[macro_use]
Expand All @@ -42,7 +45,6 @@ use primitives::bytes;
use rstd::prelude::*;
use runtime_primitives::traits::BlakeTwo256;
use runtime_primitives::generic;
use codec::{Encode, Decode, Input, Output};

pub mod parachain;

Expand Down Expand Up @@ -105,18 +107,6 @@ pub type Balance = u128;
pub type BlockId = generic::BlockId<Block>;

/// A log entry in the block.
#[derive(PartialEq, Eq, Clone, Default)]
#[derive(PartialEq, Eq, Clone, Default, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
pub struct Log(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);

impl Decode for Log {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Vec::<u8>::decode(input).map(Log)
}
}

impl Encode for Log {
fn encode_to<T: Output>(&self, dest: &mut T) {
self.0.encode_to(dest)
}
}
Loading