Derivable Encode & Decode (#509)

* Derive macro for simple structs.

* Derive Encode/Decode wherever we can.

* Derive for enums.

* Support discriminant.

* Get rid of some repetition.

* Support custom indices.

* Derive codec for enums wherever possible.

* Fix no_std

* WASM rebuilt

* Avoid excessive import.

* Fix compilation.

* Address review grumbles.
This commit is contained in:
Tomasz Drwięga
2018-08-08 17:47:22 +02:00
committed by Sergey Pepyakin
parent 66432490fa
commit 5fe73de49f
18 changed files with 79 additions and 437 deletions
+1
View File
@@ -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 }
+6 -33
View File
@@ -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;
@@ -61,14 +64,14 @@ extern crate error_chain;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use codec::{Encode, Decode, Input, Output};
use codec::{Encode, Decode};
#[cfg(feature = "std")]
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.
@@ -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
+13 -44
View File
@@ -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] {
Binary file not shown.