Commit c014d085 authored by Arkadiy Paronyan's avatar Arkadiy Paronyan Committed by Gav Wood
Browse files

Refactored Slicable (#324)

* Refactored Slicable

* Docs

* Wasm build

* Wasm build

* Renamed traits

* Review nits

* Renamed Slicable as well
parent 80351c5b
......@@ -93,7 +93,7 @@ impl<B: LocalBackend<Block>> PolkadotApi for Client<B, LocalCallExecutor<B, Nati
fn evaluate_block(&self, at: &BlockId, block: Block) -> Result<bool> {
use substrate_executor::error::ErrorKind as ExecErrorKind;
use codec::Slicable;
use codec::{Decode, Encode};
use runtime::Block as RuntimeBlock;
let encoded = block.encode();
......@@ -142,13 +142,13 @@ impl<B: LocalBackend<Block>> PolkadotApi for Client<B, LocalCallExecutor<B, Nati
}
fn inherent_extrinsics(&self, at: &BlockId, timestamp: Timestamp, new_heads: Vec<CandidateReceipt>) -> Result<Vec<UncheckedExtrinsic>> {
use codec::Slicable;
use codec::{Encode, Decode};
with_runtime!(self, at, || {
let extrinsics = ::runtime::inherent_extrinsics(timestamp, new_heads);
extrinsics.into_iter()
.map(|x| x.encode()) // get encoded representation
.map(|x| Slicable::decode(&mut &x[..])) // get byte-vec equivalent to extrinsic
.map(|x| Decode::decode(&mut &x[..])) // get byte-vec equivalent to extrinsic
.map(|x| x.expect("UncheckedExtrinsic has encoded representation equivalent to Vec<u8>; qed"))
.collect()
})
......
......@@ -19,7 +19,7 @@
use std::sync::Arc;
use client::backend::{Backend, RemoteBackend};
use client::{Client, CallExecutor};
use codec::Slicable;
use codec::Decode;
use primitives::{AccountId, Block, BlockId, Hash, Index, SessionKey, Timestamp, UncheckedExtrinsic};
use runtime::Address;
use primitives::parachain::{CandidateReceipt, DutyRoster, Id as ParaId};
......
......@@ -79,7 +79,7 @@ use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use substrate_telemetry::{init_telemetry, TelemetryConfig};
use polkadot_primitives::BlockId;
use codec::Slicable;
use codec::{Decode, Encode};
use client::BlockOrigin;
use runtime_primitives::generic::SignedBlock;
......@@ -407,7 +407,7 @@ fn import_blocks<E>(matches: &clap::ArgMatches, exit: E) -> error::Result<()>
};
info!("Importing blocks");
let count: u32 = Slicable::decode(&mut file).ok_or("Error reading file")?;
let count: u32 = Decode::decode(&mut file).ok_or("Error reading file")?;
let mut block = 0;
for _ in 0 .. count {
if exit_recv.try_recv().is_ok() {
......
......@@ -18,7 +18,7 @@
use super::MAX_TRANSACTIONS_SIZE;
use codec::Slicable;
use codec::{Decode, Encode};
use polkadot_runtime::{Block as PolkadotGenericBlock, CheckedBlock};
use polkadot_primitives::{Block, Hash, BlockNumber, Timestamp};
use polkadot_primitives::parachain::Id as ParaId;
......@@ -78,13 +78,13 @@ pub fn evaluate_initial(
) -> Result<CheckedBlock> {
const MAX_TIMESTAMP_DRIFT: Timestamp = 60;
let encoded = Slicable::encode(proposal);
let encoded = Encode::encode(proposal);
let proposal = PolkadotGenericBlock::decode(&mut &encoded[..])
.and_then(|b| CheckedBlock::new(b).ok())
.ok_or_else(|| ErrorKind::ProposalNotForPolkadot)?;
let transactions_size = proposal.extrinsics.iter().fold(0, |a, tx| {
a + Slicable::encode(tx).len()
a + Encode::encode(tx).len()
});
if transactions_size > MAX_TRANSACTIONS_SIZE {
......
......@@ -65,7 +65,7 @@ use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::time::{Duration, Instant};
use codec::Slicable;
use codec::{Decode, Encode};
use polkadot_api::PolkadotApi;
use polkadot_primitives::{Hash, Block, BlockId, BlockNumber, Header, Timestamp, SessionKey};
use polkadot_primitives::parachain::{Id as ParaId, Chain, DutyRoster, BlockData, Extrinsic as ParachainExtrinsic, CandidateReceipt, CandidateSignature};
......@@ -710,7 +710,7 @@ impl<C> CreateProposal<C> where C: PolkadotApi {
.join(", ")
);
let substrate_block = Slicable::decode(&mut polkadot_block.encode().as_slice())
let substrate_block = Decode::decode(&mut polkadot_block.encode().as_slice())
.expect("polkadot blocks defined to serialize to substrate blocks correctly; qed");
// TODO: full re-evaluation
......
......@@ -47,7 +47,7 @@ mod collator_pool;
mod router;
pub mod consensus;
use codec::Slicable;
use codec::{Decode, Encode};
use futures::sync::oneshot;
use parking_lot::Mutex;
use polkadot_consensus::{Statement, SignedStatement, GenericStatement};
......@@ -81,25 +81,25 @@ pub struct Status {
collating_for: Option<(AccountId, ParaId)>,
}
impl Slicable for Status {
fn encode(&self) -> Vec<u8> {
let mut v = Vec::new();
impl Encode for Status {
fn encode_to<T: codec::Output>(&self, dest: &mut T) {
match self.collating_for {
Some(ref details) => {
v.push(1);
details.using_encoded(|s| v.extend(s));
dest.push_byte(1);
dest.push(details);
}
None => {
v.push(0);
dest.push_byte(0);
}
}
v
}
}
fn decode<I: ::codec::Input>(input: &mut I) -> Option<Self> {
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(Slicable::decode(input)?),
1 => Some(Decode::decode(input)?),
_ => return None,
};
Some(Status { collating_for })
......
......@@ -23,7 +23,7 @@ use polkadot_consensus::GenericStatement;
use polkadot_primitives::{Block, Hash, SessionKey};
use polkadot_primitives::parachain::{CandidateReceipt, HeadData, BlockData};
use substrate_primitives::H512;
use codec::Slicable;
use codec::Encode;
use substrate_network::{PeerId, PeerInfo, ClientHandle, Context, message::Message as SubstrateMessage, message::Role, specialization::Specialization, generic_message::Message as GenericMessage};
use std::sync::Arc;
......
......@@ -61,7 +61,7 @@ extern crate error_chain;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use codec::Slicable;
use codec::{Encode, Decode, Input, Output};
#[cfg(feature = "std")]
pub mod wasm;
......@@ -77,20 +77,18 @@ pub struct ValidationParams {
pub parent_head: Vec<u8>,
}
impl Slicable for ValidationParams {
fn encode(&self) -> Vec<u8> {
let mut v = Vec::new();
self.block_data.using_encoded(|s| v.extend(s));
self.parent_head.using_encoded(|s| v.extend(s));
v
impl Encode for ValidationParams {
fn encode_to<T: Output>(&self, dest: &mut T) {
dest.push(&self.block_data);
dest.push(&self.parent_head);
}
}
fn decode<I: codec::Input>(input: &mut I) -> Option<Self> {
impl Decode for ValidationParams {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Some(ValidationParams {
block_data: Slicable::decode(input)?,
parent_head: Slicable::decode(input)?,
block_data: Decode::decode(input)?,
parent_head: Decode::decode(input)?,
})
}
}
......@@ -104,14 +102,16 @@ pub struct ValidationResult {
pub head_data: Vec<u8>
}
impl Slicable for ValidationResult {
fn encode(&self) -> Vec<u8> {
self.head_data.encode()
impl Encode for ValidationResult {
fn encode_to<T: Output>(&self, dest: &mut T) {
dest.push(&self.head_data);
}
}
fn decode<I: codec::Input>(input: &mut I) -> Option<Self> {
impl Decode for ValidationResult {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Some(ValidationResult {
head_data: Slicable::decode(input)?,
head_data: Decode::decode(input)?,
})
}
}
......
......@@ -20,7 +20,7 @@
//! Assuming the parameters are correct, this module provides a wrapper around
//! a WASM VM for re-execution of a parachain candidate.
use codec::Slicable;
use codec::{Decode, Encode};
use wasmi::{self, Module, ModuleInstance, MemoryInstance, MemoryDescriptor, MemoryRef, ModuleImportResolver};
use wasmi::{memory_units, RuntimeValue};
......
......@@ -33,7 +33,7 @@ extern crate wee_alloc;
extern crate tiny_keccak;
extern crate pwasm_libc;
use parachain::codec::{Slicable, Input};
use parachain::codec::{Decode, Encode, Input, Output};
#[cfg(not(feature = "std"))]
mod wasm;
......@@ -41,9 +41,6 @@ mod wasm;
#[cfg(not(feature = "std"))]
pub use wasm::*;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
// Define global allocator.
#[cfg(not(feature = "std"))]
#[global_allocator]
......@@ -60,22 +57,20 @@ struct HeadData {
post_state: [u8; 32],
}
impl Slicable for HeadData {
fn encode(&self) -> Vec<u8> {
let mut v = Vec::new();
self.number.using_encoded(|s| v.extend(s));
self.parent_hash.using_encoded(|s| v.extend(s));
self.post_state.using_encoded(|s| v.extend(s));
v
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: Slicable::decode(input)?,
parent_hash: Slicable::decode(input)?,
post_state: Slicable::decode(input)?,
number: Decode::decode(input)?,
parent_hash: Decode::decode(input)?,
post_state: Decode::decode(input)?,
})
}
}
......@@ -89,20 +84,18 @@ struct BlockData {
add: u64,
}
impl Slicable for BlockData {
fn encode(&self) -> Vec<u8> {
let mut v = Vec::new();
self.state.using_encoded(|s| v.extend(s));
self.add.using_encoded(|s| v.extend(s));
v
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: Slicable::decode(input)?,
add: Slicable::decode(input)?,
state: Decode::decode(input)?,
add: Decode::decode(input)?,
})
}
}
......
......@@ -18,7 +18,7 @@
use core::{intrinsics, panic, alloc};
use parachain::{self, ValidationResult};
use parachain::codec::Slicable;
use parachain::codec::{Encode, Decode};
use super::{HeadData, BlockData};
#[panic_implementation]
......
......@@ -20,7 +20,7 @@ extern crate polkadot_parachain as parachain;
extern crate tiny_keccak;
use parachain::ValidationParams;
use parachain::codec::{Slicable, Input};
use parachain::codec::{Decode, Encode, Input, Output};
// Head data for this parachain.
#[derive(Default, Clone)]
......@@ -33,22 +33,20 @@ struct HeadData {
post_state: [u8; 32],
}
impl Slicable for HeadData {
fn encode(&self) -> Vec<u8> {
let mut v = Vec::new();
self.number.using_encoded(|s| v.extend(s));
self.parent_hash.using_encoded(|s| v.extend(s));
self.post_state.using_encoded(|s| v.extend(s));
v
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: Slicable::decode(input)?,
parent_hash: Slicable::decode(input)?,
post_state: Slicable::decode(input)?,
number: Decode::decode(input)?,
parent_hash: Decode::decode(input)?,
post_state: Decode::decode(input)?,
})
}
}
......@@ -62,20 +60,18 @@ struct BlockData {
add: u64,
}
impl Slicable for BlockData {
fn encode(&self) -> Vec<u8> {
let mut v = Vec::new();
self.state.using_encoded(|s| v.extend(s));
self.add.using_encoded(|s| v.extend(s));
v
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: Slicable::decode(input)?,
add: Slicable::decode(input)?,
state: Decode::decode(input)?,
add: Decode::decode(input)?,
})
}
}
......
......@@ -42,7 +42,7 @@ use primitives::bytes;
use rstd::prelude::*;
use runtime_primitives::traits::BlakeTwo256;
use runtime_primitives::generic;
use codec::{Input, Slicable};
use codec::{Encode, Decode, Input, Output};
pub mod parachain;
......@@ -109,12 +109,14 @@ pub type BlockId = generic::BlockId<Block>;
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
pub struct Log(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);
impl Slicable for Log {
impl Decode for Log {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Vec::<u8>::decode(input).map(Log)
}
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.using_encoded(f)
impl Encode for Log {
fn encode_to<T: Output>(&self, dest: &mut T) {
self.0.encode_to(dest)
}
}
......@@ -16,7 +16,7 @@
//! Polkadot parachain types.
use codec::{Slicable, Input};
use codec::{Encode, Decode, Input, Output};
use rstd::prelude::*;
use rstd::cmp::Ordering;
use super::Hash;
......@@ -47,11 +47,13 @@ impl Id {
}
}
impl Slicable for Id {
impl Decode for Id {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
u32::decode(input).map(Id)
}
}
impl Encode for Id {
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.using_encoded(f)
}
......@@ -67,30 +69,26 @@ pub enum Chain {
Parachain(Id),
}
impl Slicable for Chain {
impl Decode for Chain {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
let disc = input.read_byte()?;
match disc {
0 => Some(Chain::Relay),
1 => Some(Chain::Parachain(Slicable::decode(input)?)),
1 => Some(Chain::Parachain(Decode::decode(input)?)),
_ => None,
}
}
}
fn encode(&self) -> Vec<u8> {
let mut v = Vec::new();
impl Encode for Chain {
fn encode_to<T: Output>(&self, dest: &mut T) {
match *self {
Chain::Relay => { v.push(0); }
Chain::Relay => { dest.push_byte(0); }
Chain::Parachain(id) => {
v.push(1u8);
id.using_encoded(|s| v.extend(s));
dest.push_byte(1u8);
dest.push(&id);
}
}
v
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(&self.encode().as_slice())
}
}
......@@ -105,25 +103,19 @@ pub struct DutyRoster {
pub guarantor_duty: Vec<Chain>,
}
impl Slicable for DutyRoster {
impl Decode for DutyRoster {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Some(DutyRoster {
validator_duty: Slicable::decode(input)?,
guarantor_duty: Slicable::decode(input)?,
validator_duty: Decode::decode(input)?,
guarantor_duty: Decode::decode(input)?,
})
}
}
fn encode(&self) -> Vec<u8> {
let mut v = Vec::new();
v.extend(self.validator_duty.encode());
v.extend(self.guarantor_duty.encode());
v
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(&self.encode().as_slice())
impl Encode for DutyRoster {
fn encode_to<T: Output>(&self, dest: &mut T) {
dest.push(&self.validator_duty);
dest.push(&self.guarantor_duty);
}
}
......@@ -158,32 +150,30 @@ pub struct CandidateReceipt {
pub block_data_hash: Hash,
}
impl Slicable for CandidateReceipt {
fn encode(&self) -> Vec<u8> {
let mut v = Vec::new();
self.parachain_index.using_encoded(|s| v.extend(s));
self.collator.using_encoded(|s| v.extend(s));
self.signature.using_encoded(|s| v.extend(s));
self.head_data.0.using_encoded(|s| v.extend(s));
self.balance_uploads.using_encoded(|s| v.extend(s));
self.egress_queue_roots.using_encoded(|s| v.extend(s));
self.fees.using_encoded(|s| v.extend(s));
self.block_data_hash.using_encoded(|s| v.extend(s));
v
impl Encode for CandidateReceipt {
fn encode_to<T: Output>(&self, dest: &mut T) {
dest.push(&self.parachain_index);
dest.push(&self.collator);
dest.push(&self.signature);
dest.push(&self.head_data.0);
dest.push(&self.balance_uploads);
dest.push(&self.egress_queue_roots);
dest.push(&self.fees);
dest.push(&self.block_data_hash);
}
}
impl Decode for CandidateReceipt {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Some(CandidateReceipt {
parachain_index: Slicable::decode(input)?,
collator: Slicable::decode(input)?,
signature: Slicable::decode(input)?,
head_data: Slicable::decode(input).map(HeadData)?,
balance_uploads: Slicable::decode(input)?,
egress_queue_roots: Slicable::decode(input)?,
fees: Slicable::decode(input)?,
block_data_hash: Slicable::decode(input)?,
parachain_index: Decode::decode(input)?,
collator: Decode::decode(input)?,
signature: Decode::decode(input)?,
head_data: Decode::decode(input).map(HeadData)?,
balance_uploads: Decode::decode(input)?,
egress_queue_roots: Decode::decode(input)?,
fees: Decode::decode(input)?,
block_data_hash: Decode::decode(input)?,
})
}
}
......@@ -283,13 +273,15 @@ pub struct ValidationCode(#[cfg_attr(feature = "std", serde(with="bytes"))] pub
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
pub struct Activity(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);
impl Slicable for Activity {
impl Decode for Activity {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Vec::<u8>::decode(input).map(Activity)
}
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.using_encoded(f)
impl Encode for Activity {
fn encode_to<T: Output>(&self, dest: &mut T) {
self.0.encode_to(dest)
}
}
......
......@@ -257,7 +257,7 @@ pub mod api {
mod tests {
use super::*;
use substrate_primitives as primitives;
use ::codec::Slicable;
use codec::{Encode, Decode};
use substrate_primitives::hexdisplay::HexDisplay;
use substrate_serializer as ser;
use runtime_primitives::traits::{Digest as DigestT, Header as HeaderT};
......@@ -376,7 +376,7 @@ mod tests {
// df0f0200
// 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
let v = Slicable::encode(&tx);
let v = Encode::encode(&tx);
assert_eq!(&v[..], &hex!["6f000000ff0101010101010101010101010101010101010101010101010101010101010101e70300000300df0f02000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"][..]);
println!("{}", HexDisplay::from(&v));
assert_eq!(UncheckedExtrinsic::decode(&mut &v[..]).unwrap(), tx);
......@@ -393,7 +393,7 @@ mod tests {