Make polkadot-parachain call validate_block instead of validate (#297)

* Make `polkadot-parachain` call `validate_block` instead of `validate`

Also switch to rust 2018 in the crate

* Use `rstd`

* Make `load_params` a pointer
This commit is contained in:
Bastian Köcher
2019-06-25 21:08:56 +02:00
committed by GitHub
parent 23432bb043
commit 9004fb3f97
11 changed files with 55 additions and 74 deletions
+1 -2
View File
@@ -2358,9 +2358,8 @@ version = "0.1.0"
dependencies = [ dependencies = [
"derive_more 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", "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 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 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)", "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)", "wasmi 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
+6 -5
View File
@@ -3,14 +3,15 @@ name = "polkadot-parachain"
version = "0.1.0" version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
description = "Types and utilities for creating and working with parachains" description = "Types and utilities for creating and working with parachains"
edition = "2018"
[dependencies] [dependencies]
parity-codec = { version = "3.5", default-features = false } codec = { package = "parity-codec", version = "3.5", default-features = false, features = [ "derive" ] }
parity-codec-derive = { version = "3.3", default-features = false }
wasmi = { version = "0.4.3", optional = true } wasmi = { version = "0.4.3", optional = true }
derive_more = { version = "0.14", optional = true } derive_more = { version = "0.14", optional = true }
serde = { version = "1.0", default-features = false } serde = { version = "1.0", default-features = false, features = [ "derive" ] }
serde_derive = { version = "1.0", optional = true }
rstd = { package = "sr-std", git = "https://github.com/paritytech/substrate", branch = "polkadot-master", default-features = false }
[dev-dependencies] [dev-dependencies]
tiny-keccak = "1.4" tiny-keccak = "1.4"
@@ -18,4 +19,4 @@ tiny-keccak = "1.4"
[features] [features]
default = ["std"] default = ["std"]
wasm-api = [] wasm-api = []
std = ["parity-codec/std", "wasmi", "derive_more", "serde_derive", "serde/std"] std = [ "codec/std", "wasmi", "derive_more", "serde/std", "rstd/std" ]
+5 -25
View File
@@ -43,29 +43,7 @@
#![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(not(feature = "std"), no_std)]
/// Re-export of parity-codec. /// Re-export of parity-codec.
pub extern crate parity_codec as codec; pub use 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;
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub mod wasm_executor; pub mod wasm_executor;
@@ -75,6 +53,8 @@ pub mod wasm_api;
use codec::{Encode, Decode}; use codec::{Encode, Decode};
use rstd::vec::Vec;
struct TrailingZeroInput<'a>(&'a [u8]); struct TrailingZeroInput<'a>(&'a [u8]);
impl<'a> codec::Input for TrailingZeroInput<'a> { impl<'a> codec::Input for TrailingZeroInput<'a> {
fn read(&mut self, into: &mut [u8]) -> usize { fn read(&mut self, into: &mut [u8]) -> usize {
@@ -112,7 +92,7 @@ pub struct ValidationResult {
/// Unique identifier of a parachain. /// Unique identifier of a parachain.
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Default, Clone, Copy, Encode, Decode)] #[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); pub struct Id(u32);
impl codec::CompactAs for Id { impl codec::CompactAs for Id {
@@ -207,7 +187,7 @@ pub enum ParachainDispatchOrigin {
Parachain, Parachain,
} }
impl core::convert::TryFrom<u8> for ParachainDispatchOrigin { impl rstd::convert::TryFrom<u8> for ParachainDispatchOrigin {
type Error = (); type Error = ();
fn try_from(x: u8) -> core::result::Result<ParachainDispatchOrigin, ()> { fn try_from(x: u8) -> core::result::Result<ParachainDispatchOrigin, ()> {
const SIGNED: u8 = ParachainDispatchOrigin::Signed as u8; const SIGNED: u8 = ParachainDispatchOrigin::Signed as u8;
+7 -9
View File
@@ -17,9 +17,7 @@
//! Utilities for writing parachain WASM. //! Utilities for writing parachain WASM.
use codec::{Encode, Decode}; use codec::{Encode, Decode};
use super::{ use super::{ValidationParams, ValidationResult, MessageRef, UpwardMessageRef};
ValidationParams, ValidationResult, MessageRef, UpwardMessageRef, ParachainDispatchOrigin
};
mod ll { mod ll {
extern "C" { extern "C" {
@@ -32,8 +30,8 @@ mod ll {
/// ///
/// Offset and length must have been provided by the validation /// Offset and length must have been provided by the validation
/// function's entry point. /// function's entry point.
pub unsafe fn load_params(offset: usize, len: usize) -> ValidationParams { pub unsafe fn load_params(params: *const u8, len: usize) -> ValidationParams {
let mut slice = ::core::slice::from_raw_parts(offset as *const u8, len); let mut slice = rstd::slice::from_raw_parts(params, len);
ValidationParams::decode(&mut slice).expect("Invalid input data") 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; let end_ptr = &encoded[len] as *const u8 as usize;
// leak so it doesn't get zeroed. // leak so it doesn't get zeroed.
::core::mem::forget(encoded); rstd::mem::forget(encoded);
end_ptr end_ptr
} }
@@ -67,8 +65,8 @@ pub fn post_message(message: MessageRef) {
/// Post a message to this parachain's relay chain. /// Post a message to this parachain's relay chain.
pub fn post_upward_message(message: UpwardMessageRef) { pub fn post_upward_message(message: UpwardMessageRef) {
let data_ptr = message.as_ptr(); let data_ptr = message.data.as_ptr();
let data_len = message.len(); 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) }
} }
+9 -10
View File
@@ -21,7 +21,7 @@
//! a WASM VM for re-execution of a parachain candidate. //! a WASM VM for re-execution of a parachain candidate.
use std::{cell::RefCell, fmt, convert::TryInto}; use std::{cell::RefCell, fmt, convert::TryInto};
use codec::{Decode, Encode}; use crate::codec::{Decode, Encode};
use wasmi::{ use wasmi::{
self, Module, ModuleInstance, Trap, MemoryInstance, MemoryDescriptor, MemoryRef, self, Module, ModuleInstance, Trap, MemoryInstance, MemoryDescriptor, MemoryRef,
ModuleImportResolver, RuntimeValue, Externals, Error as WasmError, ValueType, ModuleImportResolver, RuntimeValue, Externals, Error as WasmError, ValueType,
@@ -306,16 +306,15 @@ pub fn validate_candidate<E: Externalities>(
}; };
let output = module.invoke_export( let output = module.invoke_export(
"validate", "validate_block",
&[RuntimeValue::I32(offset as i32), RuntimeValue::I32(len as i32)], &[RuntimeValue::I32(offset as i32), RuntimeValue::I32(len as i32)],
&mut externals, &mut externals,
) ).map_err(|e| -> Error {
.map_err(|e| -> Error { e.as_host_error()
e.as_host_error() .and_then(|he| he.downcast_ref::<ExternalitiesError>())
.and_then(|he| he.downcast_ref::<ExternalitiesError>()) .map(|ee| Error::Externalities(ee.clone()))
.map(|ee| Error::Externalities(ee.clone())) .unwrap_or_else(move || e.into())
.unwrap_or_else(move || e.into()) })?;
})?;
match output { match output {
Some(RuntimeValue::I32(len_offset)) => { Some(RuntimeValue::I32(len_offset)) => {
@@ -344,6 +343,6 @@ pub fn validate_candidate<E: Externalities>(
.map_err(Into::into) .map_err(Into::into)
}) })
} }
_ => return Err(Error::BadReturn), _ => Err(Error::BadReturn),
} }
} }
+7 -9
View File
@@ -16,14 +16,12 @@
//! Basic parachain that adds a number as part of its state. //! Basic parachain that adds a number as part of its state.
#[macro_use] use polkadot_parachain as parachain;
extern crate parity_codec_derive;
extern crate parity_codec as codec;
extern crate polkadot_parachain as parachain;
extern crate tiny_keccak;
use parachain::{MessageRef, UpwardMessageRef, IncomingMessage, ValidationParams}; use crate::parachain::{
use parachain::wasm_executor::{Externalities, ExternalitiesError}; MessageRef, UpwardMessageRef, IncomingMessage, ValidationParams,
wasm_executor::{Externalities, ExternalitiesError},
};
use codec::{Decode, Encode}; use codec::{Decode, Encode};
/// Head data for this parachain. /// Head data for this parachain.
@@ -65,11 +63,11 @@ impl Externalities for DummyExt {
const TEST_CODE: &[u8] = include_bytes!("res/adder.wasm"); const TEST_CODE: &[u8] = include_bytes!("res/adder.wasm");
fn hash_state(state: u64) -> [u8; 32] { 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] { fn hash_head(head: &HeadData) -> [u8; 32] {
::tiny_keccak::keccak256(head.encode().as_slice()) tiny_keccak::keccak256(head.encode().as_slice())
} }
#[test] #[test]
Binary file not shown.
+1 -2
View File
@@ -1701,9 +1701,8 @@ version = "0.1.0"
dependencies = [ dependencies = [
"derive_more 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", "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 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 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)", "wasmi 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@@ -8,12 +8,15 @@ edition = "2018"
adder = { path = ".." } adder = { path = ".." }
parachain = { package = "polkadot-parachain", path = "../../../parachain", default-features = false, features = ["wasm-api"] } parachain = { package = "polkadot-parachain", path = "../../../parachain", default-features = false, features = ["wasm-api"] }
tiny-keccak = "1.4" 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] [lib]
crate-type = ["cdylib"] crate-type = ["cdylib"]
[target.release] [profile.release]
panic = "abort" panic = "abort"
lto = true lto = true
@@ -18,9 +18,7 @@
#![no_std] #![no_std]
#![feature( #![feature(core_intrinsics, lang_items, core_panic_info, alloc_error_handler)]
alloc, core_intrinsics, lang_items, core_panic_info, alloc_error_handler
)]
#[global_allocator] #[global_allocator]
static ALLOC: dlmalloc::GlobalDlmalloc = dlmalloc::GlobalDlmalloc; static ALLOC: dlmalloc::GlobalDlmalloc = dlmalloc::GlobalDlmalloc;
@@ -40,30 +38,30 @@ pub fn panic(_info: &panic::PanicInfo) -> ! {
#[alloc_error_handler] #[alloc_error_handler]
#[no_mangle] #[no_mangle]
pub fn oom(_: ::core::alloc::Layout) -> ! { pub fn oom(_: core::alloc::Layout) -> ! {
unsafe { unsafe {
intrinsics::abort(); intrinsics::abort();
} }
} }
#[no_mangle] #[no_mangle]
pub extern fn validate(offset: usize, len: usize) -> usize { pub extern fn validate_block(params: *const u8, len: usize) -> usize {
let params = unsafe { ::parachain::wasm_api::load_params(offset, len) }; let params = unsafe { parachain::wasm_api::load_params(params, len) };
let parent_head = HeadData::decode(&mut &params.parent_head[..]) let parent_head = HeadData::decode(&mut &params.parent_head[..])
.expect("invalid parent head format."); .expect("invalid parent head format.");
let block_data = BlockData::decode(&mut &params.block_data[..]) let block_data = BlockData::decode(&mut &params.block_data[..])
.expect("invalid block data format."); .expect("invalid block data format.");
let parent_hash = ::tiny_keccak::keccak256(&params.parent_head[..]); let parent_hash = tiny_keccak::keccak256(&params.parent_head[..]);
// we also add based on incoming data from messages. ignoring unknown message // we also add based on incoming data from messages. ignoring unknown message
// kinds. // kinds.
let from_messages = ::adder::process_messages( let from_messages = adder::process_messages(
params.ingress.iter().map(|incoming| &incoming.data[..]) 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( Ok(new_head) => parachain::wasm_api::write_result(
ValidationResult { head_data: new_head.encode() } ValidationResult { head_data: new_head.encode() }
), ),
+7 -1
View File
@@ -4,10 +4,16 @@ set -e
# Make LLD produce a binary that imports memory from the outside environment. # 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" 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 for i in adder
do do
cd $i/wasm 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 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/ cp target/wasm32-unknown-unknown/release/$i.wasm ../../../parachain/tests/res/
rm -rf target rm -rf target