Parachain validation moved to external process (#325)

* Improved execution & tests

* Style

* Made CLI arg const

* Moved Upwards message

* CLI subcommand for validation worker

* Build halting parachain

* Build halting parachain

* Made stuff private

* Reorganized parachain tests

* Comment

* Whitespace

* Apply suggestions from code review

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Fixed call data size check and introduced an enum

* Apply suggestions from code review

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Arkadiy Paronyan
2019-07-23 11:09:30 +02:00
committed by Bastian Köcher
parent 157cd9a217
commit f1fdb0cb83
19 changed files with 1076 additions and 417 deletions
@@ -18,10 +18,8 @@
use polkadot_parachain as parachain;
use crate::parachain::{
MessageRef, UpwardMessageRef, IncomingMessage, ValidationParams,
wasm_executor::{Externalities, ExternalitiesError},
};
use crate::parachain::{IncomingMessage, ValidationParams};
use crate::DummyExt;
use codec::{Decode, Encode};
/// Head data for this parachain.
@@ -50,16 +48,6 @@ struct AddMessage {
amount: u64,
}
struct DummyExt;
impl Externalities for DummyExt {
fn post_message(&mut self, _message: MessageRef) -> Result<(), ExternalitiesError> {
Ok(())
}
fn post_upward_message(&mut self, _message: UpwardMessageRef) -> Result<(), ExternalitiesError> {
Ok(())
}
}
const TEST_CODE: &[u8] = adder::WASM_BINARY;
fn hash_state(state: u64) -> [u8; 32] {
@@ -71,7 +59,7 @@ fn hash_head(head: &HeadData) -> [u8; 32] {
}
#[test]
fn execute_good_on_parent() {
pub fn execute_good_on_parent() {
let parent_head = HeadData {
number: 0,
parent_hash: [0; 32],
@@ -91,6 +79,7 @@ fn execute_good_on_parent() {
ingress: Vec::new(),
},
&mut DummyExt,
parachain::wasm_executor::ExecutionMode::RemoteTest,
).unwrap();
let new_head = HeadData::decode(&mut &ret.head_data[..]).unwrap();
@@ -126,6 +115,7 @@ fn execute_good_chain_on_parent() {
ingress: Vec::new(),
},
&mut DummyExt,
parachain::wasm_executor::ExecutionMode::RemoteTest,
).unwrap();
let new_head = HeadData::decode(&mut &ret.head_data[..]).unwrap();
@@ -161,6 +151,7 @@ fn execute_bad_on_parent() {
ingress: Vec::new(),
},
&mut DummyExt,
parachain::wasm_executor::ExecutionMode::RemoteTest,
).unwrap_err();
}
@@ -192,6 +183,7 @@ fn processes_messages() {
],
},
&mut DummyExt,
parachain::wasm_executor::ExecutionMode::RemoteTest,
).unwrap();
let new_head = HeadData::decode(&mut &ret.head_data[..]).unwrap();
+44
View File
@@ -0,0 +1,44 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
mod adder;
mod wasm_executor;
use polkadot_parachain as parachain;
use crate::parachain::{
MessageRef, UpwardMessageRef,
wasm_executor::{Externalities, ExternalitiesError, run_worker},
};
struct DummyExt;
impl Externalities for DummyExt {
fn post_message(&mut self, _message: MessageRef) -> Result<(), ExternalitiesError> {
Ok(())
}
fn post_upward_message(&mut self, _message: UpwardMessageRef) -> Result<(), ExternalitiesError> {
Ok(())
}
}
// This is not an actual test, but rather an entry point for out-of process WASM executor.
// When executing tests the executor spawns currently executing binary, which happens to be test binary.
// It then passes "validation_worker" on CLI effectivly making rust test executor to run this single test.
#[test]
fn validation_worker() {
if let Some(id) = std::env::args().find(|a| a.starts_with("/shmem_rs_")) {
run_worker(&id).unwrap()
}
}
@@ -0,0 +1,45 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Basic parachain that adds a number as part of its state.
use polkadot_parachain as parachain;
use crate::{adder, DummyExt};
use crate::parachain::ValidationParams;
// Code that exposes `validate_block` and loops infinitely
const INFINITE_LOOP_CODE: &[u8] = halt::WASM_BINARY;
#[test]
fn terminates_on_timeout() {
let result = parachain::wasm_executor::validate_candidate(
INFINITE_LOOP_CODE,
ValidationParams {
parent_head: Default::default(),
block_data: Vec::new(),
ingress: Vec::new(),
},
&mut DummyExt,
parachain::wasm_executor::ExecutionMode::RemoteTest,
);
match result {
Err(parachain::wasm_executor::Error::Timeout) => {},
r => panic!("{:?}", r),
}
// check that another parachain can validate normaly
adder::execute_good_on_parent();
}