diff --git a/polkadot/Cargo.lock b/polkadot/Cargo.lock index 600901d4ba..6c7d330107 100644 --- a/polkadot/Cargo.lock +++ b/polkadot/Cargo.lock @@ -2358,9 +2358,8 @@ version = "0.1.0" dependencies = [ "derive_more 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", "parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "parity-codec-derive 3.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", + "sr-std 2.0.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", "tiny-keccak 1.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "wasmi 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/polkadot/parachain/Cargo.toml b/polkadot/parachain/Cargo.toml index b849ba6724..3155e2b11a 100644 --- a/polkadot/parachain/Cargo.toml +++ b/polkadot/parachain/Cargo.toml @@ -3,14 +3,15 @@ name = "polkadot-parachain" version = "0.1.0" authors = ["Parity Technologies "] description = "Types and utilities for creating and working with parachains" +edition = "2018" [dependencies] -parity-codec = { version = "3.5", default-features = false } -parity-codec-derive = { version = "3.3", default-features = false } +codec = { package = "parity-codec", version = "3.5", default-features = false, features = [ "derive" ] } wasmi = { version = "0.4.3", optional = true } derive_more = { version = "0.14", optional = true } -serde = { version = "1.0", default-features = false } -serde_derive = { version = "1.0", optional = true } +serde = { version = "1.0", default-features = false, features = [ "derive" ] } + +rstd = { package = "sr-std", git = "https://github.com/paritytech/substrate", branch = "polkadot-master", default-features = false } [dev-dependencies] tiny-keccak = "1.4" @@ -18,4 +19,4 @@ tiny-keccak = "1.4" [features] default = ["std"] wasm-api = [] -std = ["parity-codec/std", "wasmi", "derive_more", "serde_derive", "serde/std"] +std = [ "codec/std", "wasmi", "derive_more", "serde/std", "rstd/std" ] diff --git a/polkadot/parachain/src/lib.rs b/polkadot/parachain/src/lib.rs index e42862c1d1..2662d5d309 100644 --- a/polkadot/parachain/src/lib.rs +++ b/polkadot/parachain/src/lib.rs @@ -43,29 +43,7 @@ #![cfg_attr(not(feature = "std"), no_std)] /// Re-export of parity-codec. -pub extern crate parity_codec as codec; - -#[macro_use] -extern crate parity_codec_derive; - -#[cfg(not(feature = "std"))] -extern crate alloc; - -#[cfg(feature = "std")] -extern crate core; - -#[cfg(feature = "std")] -extern crate wasmi; - -#[cfg(feature = "std")] -extern crate serde; - -#[cfg(feature = "std")] -#[macro_use] -extern crate serde_derive; - -#[cfg(not(feature = "std"))] -use alloc::vec::Vec; +pub use codec; #[cfg(feature = "std")] pub mod wasm_executor; @@ -75,6 +53,8 @@ pub mod wasm_api; use codec::{Encode, Decode}; +use rstd::vec::Vec; + struct TrailingZeroInput<'a>(&'a [u8]); impl<'a> codec::Input for TrailingZeroInput<'a> { fn read(&mut self, into: &mut [u8]) -> usize { @@ -112,7 +92,7 @@ pub struct ValidationResult { /// Unique identifier of a parachain. #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Default, Clone, Copy, Encode, Decode)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))] +#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize, Debug))] pub struct Id(u32); impl codec::CompactAs for Id { @@ -207,7 +187,7 @@ pub enum ParachainDispatchOrigin { Parachain, } -impl core::convert::TryFrom for ParachainDispatchOrigin { +impl rstd::convert::TryFrom for ParachainDispatchOrigin { type Error = (); fn try_from(x: u8) -> core::result::Result { const SIGNED: u8 = ParachainDispatchOrigin::Signed as u8; diff --git a/polkadot/parachain/src/wasm_api.rs b/polkadot/parachain/src/wasm_api.rs index ad7c14ecec..dec6b60b3d 100644 --- a/polkadot/parachain/src/wasm_api.rs +++ b/polkadot/parachain/src/wasm_api.rs @@ -17,9 +17,7 @@ //! Utilities for writing parachain WASM. use codec::{Encode, Decode}; -use super::{ - ValidationParams, ValidationResult, MessageRef, UpwardMessageRef, ParachainDispatchOrigin -}; +use super::{ValidationParams, ValidationResult, MessageRef, UpwardMessageRef}; mod ll { extern "C" { @@ -32,8 +30,8 @@ mod ll { /// /// Offset and length must have been provided by the validation /// function's entry point. -pub unsafe fn load_params(offset: usize, len: usize) -> ValidationParams { - let mut slice = ::core::slice::from_raw_parts(offset as *const u8, len); +pub unsafe fn load_params(params: *const u8, len: usize) -> ValidationParams { + let mut slice = rstd::slice::from_raw_parts(params, len); ValidationParams::decode(&mut slice).expect("Invalid input data") } @@ -53,7 +51,7 @@ pub fn write_result(result: ValidationResult) -> usize { let end_ptr = &encoded[len] as *const u8 as usize; // leak so it doesn't get zeroed. - ::core::mem::forget(encoded); + rstd::mem::forget(encoded); end_ptr } @@ -67,8 +65,8 @@ pub fn post_message(message: MessageRef) { /// Post a message to this parachain's relay chain. pub fn post_upward_message(message: UpwardMessageRef) { - let data_ptr = message.as_ptr(); - let data_len = message.len(); + let data_ptr = message.data.as_ptr(); + let data_len = message.data.len(); - unsafe { ll::ext_post_upward_message(message.origin as u8 as u32, data_ptr, data_len as u32) } + unsafe { ll::ext_post_upward_message(u32::from(message.origin as u8), data_ptr, data_len as u32) } } diff --git a/polkadot/parachain/src/wasm_executor.rs b/polkadot/parachain/src/wasm_executor.rs index 0e1277ba9b..cbdccabf8b 100644 --- a/polkadot/parachain/src/wasm_executor.rs +++ b/polkadot/parachain/src/wasm_executor.rs @@ -21,7 +21,7 @@ //! a WASM VM for re-execution of a parachain candidate. use std::{cell::RefCell, fmt, convert::TryInto}; -use codec::{Decode, Encode}; +use crate::codec::{Decode, Encode}; use wasmi::{ self, Module, ModuleInstance, Trap, MemoryInstance, MemoryDescriptor, MemoryRef, ModuleImportResolver, RuntimeValue, Externals, Error as WasmError, ValueType, @@ -306,16 +306,15 @@ pub fn validate_candidate( }; let output = module.invoke_export( - "validate", + "validate_block", &[RuntimeValue::I32(offset as i32), RuntimeValue::I32(len as i32)], &mut externals, - ) - .map_err(|e| -> Error { - e.as_host_error() - .and_then(|he| he.downcast_ref::()) - .map(|ee| Error::Externalities(ee.clone())) - .unwrap_or_else(move || e.into()) - })?; + ).map_err(|e| -> Error { + e.as_host_error() + .and_then(|he| he.downcast_ref::()) + .map(|ee| Error::Externalities(ee.clone())) + .unwrap_or_else(move || e.into()) + })?; match output { Some(RuntimeValue::I32(len_offset)) => { @@ -344,6 +343,6 @@ pub fn validate_candidate( .map_err(Into::into) }) } - _ => return Err(Error::BadReturn), + _ => Err(Error::BadReturn), } } diff --git a/polkadot/parachain/tests/adder.rs b/polkadot/parachain/tests/adder.rs index ac6005ffb3..6ef3510a88 100644 --- a/polkadot/parachain/tests/adder.rs +++ b/polkadot/parachain/tests/adder.rs @@ -16,14 +16,12 @@ //! Basic parachain that adds a number as part of its state. -#[macro_use] -extern crate parity_codec_derive; -extern crate parity_codec as codec; -extern crate polkadot_parachain as parachain; -extern crate tiny_keccak; +use polkadot_parachain as parachain; -use parachain::{MessageRef, UpwardMessageRef, IncomingMessage, ValidationParams}; -use parachain::wasm_executor::{Externalities, ExternalitiesError}; +use crate::parachain::{ + MessageRef, UpwardMessageRef, IncomingMessage, ValidationParams, + wasm_executor::{Externalities, ExternalitiesError}, +}; use codec::{Decode, Encode}; /// Head data for this parachain. @@ -65,11 +63,11 @@ impl Externalities for DummyExt { const TEST_CODE: &[u8] = include_bytes!("res/adder.wasm"); fn hash_state(state: u64) -> [u8; 32] { - ::tiny_keccak::keccak256(state.encode().as_slice()) + tiny_keccak::keccak256(state.encode().as_slice()) } fn hash_head(head: &HeadData) -> [u8; 32] { - ::tiny_keccak::keccak256(head.encode().as_slice()) + tiny_keccak::keccak256(head.encode().as_slice()) } #[test] diff --git a/polkadot/parachain/tests/res/adder.wasm b/polkadot/parachain/tests/res/adder.wasm index 0e1aee6a82..687dc0d757 100644 Binary files a/polkadot/parachain/tests/res/adder.wasm and b/polkadot/parachain/tests/res/adder.wasm differ diff --git a/polkadot/runtime/wasm/Cargo.lock b/polkadot/runtime/wasm/Cargo.lock index f0e4345376..8bb74cf347 100644 --- a/polkadot/runtime/wasm/Cargo.lock +++ b/polkadot/runtime/wasm/Cargo.lock @@ -1701,9 +1701,8 @@ version = "0.1.0" dependencies = [ "derive_more 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", "parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "parity-codec-derive 3.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", + "sr-std 2.0.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", "wasmi 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/polkadot/test-parachains/adder/wasm/Cargo.toml b/polkadot/test-parachains/adder/wasm/Cargo.toml index 296786e4e4..8c46c3787c 100644 --- a/polkadot/test-parachains/adder/wasm/Cargo.toml +++ b/polkadot/test-parachains/adder/wasm/Cargo.toml @@ -8,12 +8,15 @@ edition = "2018" adder = { path = ".." } parachain = { package = "polkadot-parachain", path = "../../../parachain", default-features = false, features = ["wasm-api"] } tiny-keccak = "1.4" -dlmalloc = { version = "0.1.2", features = ["global"] } +dlmalloc = { version = "0.1.3", features = ["global"] } + +# We need to make sure the global allocator is disabled until we have support of full substrate externalities +rstd = { package = "sr-std", git = "https://github.com/paritytech/substrate", branch = "polkadot-master", default-features = false, features = [ "no_global_allocator" ] } [lib] crate-type = ["cdylib"] -[target.release] +[profile.release] panic = "abort" lto = true diff --git a/polkadot/test-parachains/adder/wasm/src/lib.rs b/polkadot/test-parachains/adder/wasm/src/lib.rs index 53c55de430..458d4f4d14 100644 --- a/polkadot/test-parachains/adder/wasm/src/lib.rs +++ b/polkadot/test-parachains/adder/wasm/src/lib.rs @@ -18,9 +18,7 @@ #![no_std] -#![feature( - alloc, core_intrinsics, lang_items, core_panic_info, alloc_error_handler -)] +#![feature(core_intrinsics, lang_items, core_panic_info, alloc_error_handler)] #[global_allocator] static ALLOC: dlmalloc::GlobalDlmalloc = dlmalloc::GlobalDlmalloc; @@ -40,30 +38,30 @@ pub fn panic(_info: &panic::PanicInfo) -> ! { #[alloc_error_handler] #[no_mangle] -pub fn oom(_: ::core::alloc::Layout) -> ! { +pub fn oom(_: core::alloc::Layout) -> ! { unsafe { intrinsics::abort(); } } #[no_mangle] -pub extern fn validate(offset: usize, len: usize) -> usize { - let params = unsafe { ::parachain::wasm_api::load_params(offset, len) }; +pub extern fn validate_block(params: *const u8, len: usize) -> usize { + let params = unsafe { parachain::wasm_api::load_params(params, len) }; let parent_head = HeadData::decode(&mut ¶ms.parent_head[..]) .expect("invalid parent head format."); let block_data = BlockData::decode(&mut ¶ms.block_data[..]) .expect("invalid block data format."); - let parent_hash = ::tiny_keccak::keccak256(¶ms.parent_head[..]); + let parent_hash = tiny_keccak::keccak256(¶ms.parent_head[..]); // we also add based on incoming data from messages. ignoring unknown message // kinds. - let from_messages = ::adder::process_messages( + let from_messages = adder::process_messages( params.ingress.iter().map(|incoming| &incoming.data[..]) ); - match ::adder::execute(parent_hash, parent_head, &block_data, from_messages) { + match adder::execute(parent_hash, parent_head, &block_data, from_messages) { Ok(new_head) => parachain::wasm_api::write_result( ValidationResult { head_data: new_head.encode() } ), diff --git a/polkadot/test-parachains/build.sh b/polkadot/test-parachains/build.sh index 307b3e6748..f5961e7180 100755 --- a/polkadot/test-parachains/build.sh +++ b/polkadot/test-parachains/build.sh @@ -4,10 +4,16 @@ set -e # Make LLD produce a binary that imports memory from the outside environment. export RUSTFLAGS="-C link-arg=--import-memory -C link-arg=--export-table -C panic=abort" +if cargo --version | grep -q "nightly"; then + CARGO_CMD="cargo" +else + CARGO_CMD="cargo +nightly" +fi + for i in adder do cd $i/wasm - cargo +nightly build --target=wasm32-unknown-unknown --release --no-default-features --target-dir target "$@" + $CARGO_CMD build --target=wasm32-unknown-unknown --release --no-default-features --target-dir target "$@" wasm-gc target/wasm32-unknown-unknown/release/$i'_'wasm.wasm target/wasm32-unknown-unknown/release/$i.wasm cp target/wasm32-unknown-unknown/release/$i.wasm ../../../parachain/tests/res/ rm -rf target