diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index c741c845a5..3b715cee8d 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -553,6 +553,7 @@ dependencies = [ name = "native-runtime" version = "0.1.0" dependencies = [ + "polkadot-runtime-codec 0.1.0", "runtime-std 0.1.0", "rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -730,6 +731,7 @@ dependencies = [ "native-runtime 0.1.0", "parity-wasm 0.15.4 (registry+https://github.com/rust-lang/crates.io-index)", "polkadot-primitives 0.1.0", + "polkadot-runtime-codec 0.1.0", "polkadot-serializer 0.1.0", "polkadot-state-machine 0.1.0", "runtime-std 0.1.0", @@ -780,6 +782,10 @@ dependencies = [ "polkadot-rpc 0.1.0", ] +[[package]] +name = "polkadot-runtime-codec" +version = "0.1.0" + [[package]] name = "polkadot-serializer" version = "0.1.0" diff --git a/substrate/Cargo.toml b/substrate/Cargo.toml index 21232d39ef..f01f860386 100644 --- a/substrate/Cargo.toml +++ b/substrate/Cargo.toml @@ -18,6 +18,7 @@ members = [ "rpc", "rpc_servers", "native-runtime", + "runtime-codec", "serializer", "state_machine", "validator", diff --git a/substrate/executor/Cargo.toml b/substrate/executor/Cargo.toml index 5329eb7023..f27f7e2b26 100644 --- a/substrate/executor/Cargo.toml +++ b/substrate/executor/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Parity Technologies "] [dependencies] error-chain = "0.11" +polkadot-runtime-codec = { path = "../runtime-codec", version = "0.1" } polkadot-primitives = { path = "../primitives", version = "0.1" } polkadot-serializer = { path = "../serializer", version = "0.1" } polkadot-state-machine = { path = "../state_machine" , version = "0.1" } diff --git a/substrate/executor/src/lib.rs b/substrate/executor/src/lib.rs index 9935f8aec8..10684b1531 100644 --- a/substrate/executor/src/lib.rs +++ b/substrate/executor/src/lib.rs @@ -27,6 +27,7 @@ #![warn(missing_docs)] +extern crate polkadot_runtime_codec as codec; extern crate polkadot_primitives as primitives; extern crate polkadot_serializer as serializer; extern crate polkadot_state_machine as state_machine; diff --git a/substrate/executor/src/native_executor.rs b/substrate/executor/src/native_executor.rs index b8c90f2d9a..d47719662c 100644 --- a/substrate/executor/src/native_executor.rs +++ b/substrate/executor/src/native_executor.rs @@ -41,8 +41,8 @@ impl CodeExecutor for NativeExecutor { #[cfg(test)] mod tests { use super::*; - use native_runtime::codec::KeyedVec; - use native_runtime::support::{TestExternalities, one, two, StaticHexInto}; + use codec::KeyedVec; + use native_runtime::support::{one, two, TestExternalities, StaticHexInto}; use native_runtime::runtime::staking::balance; use primitives::twox_128; diff --git a/substrate/executor/src/wasm_executor.rs b/substrate/executor/src/wasm_executor.rs index 439defd449..01601ac379 100644 --- a/substrate/executor/src/wasm_executor.rs +++ b/substrate/executor/src/wasm_executor.rs @@ -280,8 +280,8 @@ mod tests { use rustc_hex::FromHex; use primitives::{blake2_256, twox_128}; use runtime_std; + use codec::KeyedVec; use native_runtime::support::{one, two, StaticHexInto, TestExternalities}; - use native_runtime::codec::KeyedVec; use native_runtime::runtime::staking::balance; #[test] diff --git a/substrate/native-runtime/Cargo.toml b/substrate/native-runtime/Cargo.toml index 25091c2cef..60dbbe19c6 100644 --- a/substrate/native-runtime/Cargo.toml +++ b/substrate/native-runtime/Cargo.toml @@ -3,11 +3,12 @@ name = "native-runtime" version = "0.1.0" authors = ["Parity Technologies "] +[dependencies] +polkadot-runtime-codec = { path = "../runtime-codec", version = "0.1" } +runtime-std = { path = "./std", version = "0.1" } +rustc-hex = "1.0" + [features] default = ["with-std"] with-std = [] -without-std = [] - -[dependencies] -runtime-std = { path = "./std", version = "0.1" } -rustc-hex = "1.0" +without-std = ["polkadot-runtime-codec/no-std"] diff --git a/substrate/runtime-codec/Cargo.toml b/substrate/runtime-codec/Cargo.toml new file mode 100644 index 0000000000..3221a55802 --- /dev/null +++ b/substrate/runtime-codec/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "polkadot-runtime-codec" +description = "Serialization and deserialization codec for runtime values" +version = "0.1.0" +authors = ["Parity Technologies "] + +[dependencies] + +[features] +no-std = [] +default = [] diff --git a/substrate/wasm-runtime/polkadot/src/codec/endiansensitive.rs b/substrate/runtime-codec/src/endiansensitive.rs similarity index 100% rename from substrate/wasm-runtime/polkadot/src/codec/endiansensitive.rs rename to substrate/runtime-codec/src/endiansensitive.rs diff --git a/substrate/wasm-runtime/polkadot/src/codec/joiner.rs b/substrate/runtime-codec/src/joiner.rs similarity index 69% rename from substrate/wasm-runtime/polkadot/src/codec/joiner.rs rename to substrate/runtime-codec/src/joiner.rs index a7a9f618eb..1d19851243 100644 --- a/substrate/wasm-runtime/polkadot/src/codec/joiner.rs +++ b/substrate/runtime-codec/src/joiner.rs @@ -14,19 +14,20 @@ // You should have received a copy of the GNU General Public License // along with Polkadot. If not, see . -//! Vec serialiser. +//! Trait -use runtime_std::prelude::*; +use std::iter::Extend; use super::slicable::Slicable; -/// Trait to allow itself to be serialised into a `Vec` +/// Trait to allow itself to be serialised into a value which can be extended +/// by bytes. pub trait Joiner { - fn join(self, value: &T) -> Self; + fn join(self, value: &V) -> Self; } -impl Joiner for Vec { - fn join(mut self, value: &T) -> Vec { - value.as_slice_then(|s| self.extend_from_slice(s)); +impl Joiner for T where T: for<'a> Extend<&'a u8> { + fn join(mut self, value: &V) -> Self { + value.as_slice_then(|s| self.extend(s)); self } } diff --git a/substrate/wasm-runtime/polkadot/src/codec/keyedvec.rs b/substrate/runtime-codec/src/keyedvec.rs similarity index 92% rename from substrate/wasm-runtime/polkadot/src/codec/keyedvec.rs rename to substrate/runtime-codec/src/keyedvec.rs index 2c55cb5648..2b4bc99fbd 100644 --- a/substrate/wasm-runtime/polkadot/src/codec/keyedvec.rs +++ b/substrate/runtime-codec/src/keyedvec.rs @@ -16,8 +16,9 @@ //! Serialiser and prepender. -use runtime_std::prelude::*; -use super::slicable::Slicable; +use slicable::Slicable; +use std::iter::Extend; +use std::vec::Vec; /// Trait to allow itselg to be serialised and prepended by a given slice. pub trait KeyedVec { @@ -29,7 +30,7 @@ macro_rules! impl_non_endians { impl KeyedVec for $t { fn to_keyed_vec(&self, prepend_key: &[u8]) -> Vec { let mut r = prepend_key.to_vec(); - r.extend_from_slice(&self[..]); + r.extend(&self[..]); r } } @@ -42,7 +43,7 @@ macro_rules! impl_endians { fn to_keyed_vec(&self, prepend_key: &[u8]) -> Vec { self.as_slice_then(|slice| { let mut r = prepend_key.to_vec(); - r.extend_from_slice(slice); + r.extend(slice); r }) } diff --git a/substrate/wasm-runtime/polkadot/src/codec/mod.rs b/substrate/runtime-codec/src/lib.rs similarity index 77% rename from substrate/wasm-runtime/polkadot/src/codec/mod.rs rename to substrate/runtime-codec/src/lib.rs index 94afc574c1..060df4e95c 100644 --- a/substrate/wasm-runtime/polkadot/src/codec/mod.rs +++ b/substrate/runtime-codec/src/lib.rs @@ -14,7 +14,11 @@ // You should have received a copy of the GNU General Public License // along with Polkadot. If not, see . -//! Codec utils. +//! Implements the serialization and deserialization codec for polkadot runtime +//! values. + +#![cfg_attr(feature = "no-std", no_std)] +#![cfg_attr(feature = "no-std", feature(alloc))] mod endiansensitive; mod slicable; @@ -27,3 +31,11 @@ pub use self::slicable::{Slicable, NonTrivialSlicable}; pub use self::streamreader::StreamReader; pub use self::joiner::Joiner; pub use self::keyedvec::KeyedVec; + +#[cfg(feature = "no-std")] +mod std { + extern crate alloc; + + pub use core::*; + pub use self::alloc::vec; +} diff --git a/substrate/wasm-runtime/polkadot/src/codec/slicable.rs b/substrate/runtime-codec/src/slicable.rs similarity index 88% rename from substrate/wasm-runtime/polkadot/src/codec/slicable.rs rename to substrate/runtime-codec/src/slicable.rs index 0f1ef812a6..82b527c771 100644 --- a/substrate/wasm-runtime/polkadot/src/codec/slicable.rs +++ b/substrate/runtime-codec/src/slicable.rs @@ -16,15 +16,16 @@ //! Serialisation. -use runtime_std::prelude::*; -use runtime_std::{mem, slice}; +use std::{mem, slice}; +use std::vec::Vec; use super::joiner::Joiner; use super::endiansensitive::EndianSensitive; /// Trait that allows zero-copy read/write of value-references to/from slices in LE format. pub trait Slicable: Sized { + /// Attempt to deserialise the value from a slice. fn from_slice(value: &[u8]) -> Option { - Self::set_as_slice(&|out, offset| if value.len() >= out.len() + offset { + Self::set_as_slice(|out, offset| if value.len() >= out.len() + offset { let value = &value[offset..]; let len = out.len(); out.copy_from_slice(&value[0..len]); @@ -36,7 +37,7 @@ pub trait Slicable: Sized { fn to_vec(&self) -> Vec { self.as_slice_then(|s| s.to_vec()) } - fn set_as_slice bool>(set_slice: &F) -> Option; + fn set_as_slice bool>(fill_slice: F) -> Option; fn as_slice_then R>(&self, f: F) -> R { f(&self.to_vec()) } @@ -47,7 +48,7 @@ pub trait Slicable: Sized { pub trait NonTrivialSlicable: Slicable {} impl Slicable for T { - fn set_as_slice bool>(fill_slice: &F) -> Option { + fn set_as_slice bool>(fill_slice: F) -> Option { let size = mem::size_of::(); assert!(size > 0, "EndianSensitive can never be implemented for a zero-sized type."); let mut result: T = unsafe { mem::zeroed() }; @@ -81,8 +82,8 @@ impl Slicable for Vec { fn from_slice(value: &[u8]) -> Option { Some(value[4..].to_vec()) } - fn set_as_slice bool>(fill_slice: &F) -> Option { - u32::set_as_slice(fill_slice).and_then(|len| { + fn set_as_slice bool>(fill_slice: F) -> Option { + u32::set_as_slice(&fill_slice).and_then(|len| { let mut v = Vec::with_capacity(len as usize); unsafe { v.set_len(len as usize); } if fill_slice(&mut v, 4) { @@ -117,7 +118,7 @@ impl Slicable for Vec { Some(r) } - fn set_as_slice bool>(_fill_slice: &F) -> Option { + fn set_as_slice bool>(_fill_slice: F) -> Option { unimplemented!(); } diff --git a/substrate/wasm-runtime/polkadot/src/codec/streamreader.rs b/substrate/runtime-codec/src/streamreader.rs similarity index 98% rename from substrate/wasm-runtime/polkadot/src/codec/streamreader.rs rename to substrate/runtime-codec/src/streamreader.rs index b745f8afd9..33056a5ac3 100644 --- a/substrate/wasm-runtime/polkadot/src/codec/streamreader.rs +++ b/substrate/runtime-codec/src/streamreader.rs @@ -16,7 +16,7 @@ //! Deserialiser. -use super::slicable::Slicable; +use slicable::Slicable; /// Simple deserialiser. pub struct StreamReader<'a> { diff --git a/substrate/wasm-runtime/Cargo.lock b/substrate/wasm-runtime/Cargo.lock index 15ccc67c64..93bc305136 100644 --- a/substrate/wasm-runtime/Cargo.lock +++ b/substrate/wasm-runtime/Cargo.lock @@ -1,3 +1,7 @@ +[[package]] +name = "polkadot-runtime-codec" +version = "0.1.0" + [[package]] name = "pwasm-alloc" version = "0.1.0" @@ -13,6 +17,7 @@ version = "0.1.0" name = "runtime-polkadot" version = "0.1.0" dependencies = [ + "polkadot-runtime-codec 0.1.0", "runtime-std 0.1.0", ] diff --git a/substrate/wasm-runtime/polkadot/Cargo.toml b/substrate/wasm-runtime/polkadot/Cargo.toml index 669ccac6fa..5cc45d4c01 100644 --- a/substrate/wasm-runtime/polkadot/Cargo.toml +++ b/substrate/wasm-runtime/polkadot/Cargo.toml @@ -7,9 +7,10 @@ authors = ["Parity Technologies "] crate-type = ["cdylib"] [dependencies] +polkadot-runtime-codec = { path = "../../runtime-codec", version = "0.1" } runtime-std = { path = "../std", version = "0.1" } [features] default = ["without-std"] with-std = [] -without-std = [] +without-std = ["polkadot-runtime-codec/no-std"] diff --git a/substrate/wasm-runtime/polkadot/src/lib.rs b/substrate/wasm-runtime/polkadot/src/lib.rs index 1eeff3c649..ceec42af21 100644 --- a/substrate/wasm-runtime/polkadot/src/lib.rs +++ b/substrate/wasm-runtime/polkadot/src/lib.rs @@ -17,7 +17,6 @@ //! The Polkadot runtime. This can be compiled with #[no_std], ready for Wasm. #![cfg_attr(feature = "without-std", no_std)] -#![cfg_attr(feature = "strict", deny(warnings))] #[macro_use] extern crate runtime_std; @@ -25,7 +24,8 @@ extern crate runtime_std; #[cfg(feature = "with-std")] extern crate rustc_hex; -pub mod codec; +extern crate polkadot_runtime_codec as codec; + #[macro_use] pub mod support; pub mod primitives; diff --git a/substrate/wasm-runtime/polkadot/src/primitives/block.rs b/substrate/wasm-runtime/polkadot/src/primitives/block.rs index d33b51609c..a884bcccec 100644 --- a/substrate/wasm-runtime/polkadot/src/primitives/block.rs +++ b/substrate/wasm-runtime/polkadot/src/primitives/block.rs @@ -38,7 +38,7 @@ impl Slicable for Block { }) } - fn set_as_slice bool>(_fill_slice: &F) -> Option { + fn set_as_slice bool>(_fill_slice: F) -> Option { unimplemented!(); } diff --git a/substrate/wasm-runtime/polkadot/src/primitives/header.rs b/substrate/wasm-runtime/polkadot/src/primitives/header.rs index c97d5ebf29..4c1ec328ea 100644 --- a/substrate/wasm-runtime/polkadot/src/primitives/header.rs +++ b/substrate/wasm-runtime/polkadot/src/primitives/header.rs @@ -49,7 +49,7 @@ impl Slicable for Header { }) } - fn set_as_slice bool>(_fill_slice: &F) -> Option { + fn set_as_slice bool>(_fill_slice: F) -> Option { unimplemented!(); } diff --git a/substrate/wasm-runtime/polkadot/src/primitives/proposal.rs b/substrate/wasm-runtime/polkadot/src/primitives/proposal.rs index 1b8aa6b13e..8eb050cf67 100644 --- a/substrate/wasm-runtime/polkadot/src/primitives/proposal.rs +++ b/substrate/wasm-runtime/polkadot/src/primitives/proposal.rs @@ -65,10 +65,10 @@ pub struct Proposal { } impl Slicable for Proposal { - fn set_as_slice bool>(fill_slice: &F) -> Option { + fn set_as_slice bool>(fill_slice: F) -> Option { Some(Proposal { - function: InternalFunction::from_u8(Slicable::set_as_slice(fill_slice)?)?, - input_data: Slicable::set_as_slice(&|s, o| fill_slice(s, o + 1))?, + function: InternalFunction::from_u8(Slicable::set_as_slice(&fill_slice)?)?, + input_data: Slicable::set_as_slice(|s, o| fill_slice(s, o + 1))?, }) } diff --git a/substrate/wasm-runtime/polkadot/src/primitives/transaction.rs b/substrate/wasm-runtime/polkadot/src/primitives/transaction.rs index 9c72e31d34..73ab9edd29 100644 --- a/substrate/wasm-runtime/polkadot/src/primitives/transaction.rs +++ b/substrate/wasm-runtime/polkadot/src/primitives/transaction.rs @@ -45,7 +45,7 @@ impl Slicable for Transaction { }) } - fn set_as_slice bool>(_fill_slice: &F) -> Option { + fn set_as_slice bool>(_fill_slice: F) -> Option { unimplemented!(); } diff --git a/substrate/wasm-runtime/polkadot/src/primitives/uncheckedtransaction.rs b/substrate/wasm-runtime/polkadot/src/primitives/uncheckedtransaction.rs index ec14befb55..83984e08a4 100644 --- a/substrate/wasm-runtime/polkadot/src/primitives/uncheckedtransaction.rs +++ b/substrate/wasm-runtime/polkadot/src/primitives/uncheckedtransaction.rs @@ -63,7 +63,7 @@ impl Slicable for UncheckedTransaction { }) } - fn set_as_slice bool>(_fill_slice: &F) -> Option { + fn set_as_slice bool>(_fill_slice: F) -> Option { unimplemented!(); } diff --git a/substrate/wasm-runtime/polkadot/src/support/storage.rs b/substrate/wasm-runtime/polkadot/src/support/storage.rs index 3ff8b1c75d..bf9d605eb5 100644 --- a/substrate/wasm-runtime/polkadot/src/support/storage.rs +++ b/substrate/wasm-runtime/polkadot/src/support/storage.rs @@ -24,7 +24,7 @@ use codec::{Slicable, KeyedVec}; /// Return the value of the item in storage under `key`, or `None` if there is no explicit entry. pub fn get(key: &[u8]) -> Option { - Slicable::set_as_slice(&|out, offset| + Slicable::set_as_slice(|out, offset| runtime_std::read_storage(&twox_128(key)[..], out, offset) >= out.len() ) } diff --git a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.compact.wasm b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.compact.wasm index db7dab9917..898f194e80 100644 Binary files a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.compact.wasm and b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.compact.wasm differ diff --git a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.wasm b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.wasm index a0dc5a2754..a4c416d6da 100644 Binary files a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.wasm and b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.wasm differ diff --git a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm index 63e7666aad..a1127c4e66 100644 Binary files a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm and b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm differ diff --git a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_test.wasm b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_test.wasm index c1b257f3a3..f8244dc3a2 100644 Binary files a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_test.wasm and b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_test.wasm differ