Unverified Commit fe6b7c3f authored by Bastian Köcher's avatar Bastian Köcher Committed by GitHub
Browse files

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
parent d6fa479b
Pipeline #41368 passed with stages
in 12 minutes and 10 seconds
......@@ -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)",
]
......
......@@ -3,14 +3,15 @@ name = "polkadot-parachain"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
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" ]
......@@ -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<u8> for ParachainDispatchOrigin {
impl rstd::convert::TryFrom<u8> for ParachainDispatchOrigin {
type Error = ();
fn try_from(x: u8) -> core::result::Result<ParachainDispatchOrigin, ()> {
const SIGNED: u8 = ParachainDispatchOrigin::Signed as u8;
......
......@@ -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) }
}
......@@ -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<E: Externalities>(
};
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::<ExternalitiesError>())
.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::<ExternalitiesError>())
.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<E: Externalities>(
.map_err(Into::into)
})
}
_ => return Err(Error::BadReturn),
_ => Err(Error::BadReturn),
}
}
......@@ -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 parachain::{MessageRef, UpwardMessageRef, IncomingMessage, ValidationParams};
use parachain::wasm_executor::{Externalities, ExternalitiesError};
use polkadot_parachain as parachain;
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]
......
......@@ -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)",
]
......
......@@ -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
......
......@@ -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 &params.parent_head[..])
.expect("invalid parent head format.");
let block_data = BlockData::decode(&mut &params.block_data[..])
.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
// 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() }
),
......
......@@ -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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment