Collator for the "adder" (formerly basic-add) parachain and various small fixes (#438)

* update basic_add wasm

* wasm feature and collator feature

* move test parachains around a little

* fix wasm build for basic_add

* move basic_add to adder, introduce README

* minimal basic_add collator

* ensure collator messages are sent in the right order

* more logging

* route consensus statements to all peers

* minor bugfixes for parachains

* genesis builder accounts for parachain heads

* fix parachains tests

* targets for txpool

* tweak runtime + collator

* fix version in adder-collator

* consistency for overflowing

* adjust comment

* fix stable test run

* remove dummy registration test

* final grumbles
This commit is contained in:
Robert Habermeier
2018-08-01 17:04:04 +02:00
committed by GitHub
parent f5aa4f6f79
commit f4cd995558
27 changed files with 569 additions and 333 deletions
+2
View File
@@ -0,0 +1,2 @@
target/
Cargo.lock
+5
View File
@@ -0,0 +1,5 @@
# Test Parachains
Each parachain consists of three parts: a `#![no_std]` library with the main execution logic, a WASM crate which wraps this logic, and a collator node.
Run `build.sh` in this directory to build all registered test parachains and copy the generated WASM to the `parachain/tests/res` folder.
@@ -0,0 +1,9 @@
[package]
name = "adder"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Test parachain which adds to a number as its state transition"
[dependencies]
polkadot-parachain = { path = "../../parachain/", default-features = false }
tiny-keccak = "1.4"
@@ -0,0 +1,15 @@
[package]
name = "adder-collator"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
adder = { path = ".." }
polkadot-parachain = { path = "../../../parachain" }
polkadot-collator = { path = "../../../collator" }
polkadot-primitives = { path = "../../../primitives" }
ed25519 = { path = "../../../../substrate/ed25519" }
parking_lot = "0.4"
ctrlc = { git = "https://github.com/paritytech/rust-ctrlc.git" }
futures = "0.1"
exit-future = "0.1.2"
@@ -0,0 +1,145 @@
// Copyright 2018 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/>.
//! Collator for polkadot
extern crate adder;
extern crate polkadot_parachain as parachain;
extern crate polkadot_primitives as primitives;
extern crate polkadot_collator as collator;
extern crate ed25519;
extern crate parking_lot;
extern crate ctrlc;
extern crate futures;
extern crate exit_future;
use std::cell::RefCell;
use std::collections::HashMap;
use std::sync::Arc;
use adder::{HeadData as AdderHead, BlockData as AdderBody};
use ed25519::Pair;
use parachain::codec::{Encode, Decode};
use primitives::parachain::{HeadData, BlockData, Id as ParaId, Message};
use collator::{InvalidHead, ParachainContext, VersionInfo};
use parking_lot::Mutex;
const GENESIS: AdderHead = AdderHead {
number: 0,
parent_hash: [0; 32],
post_state: [1, 27, 77, 3, 221, 140, 1, 241, 4, 145, 67, 207, 156, 76, 129, 126, 75, 22, 127, 29, 27, 131, 229, 198, 240, 241, 13, 137, 186, 30, 123, 206],
};
const GENESIS_BODY: AdderBody = AdderBody {
state: 0,
add: 0,
};
#[derive(Clone)]
struct AdderContext {
db: Arc<Mutex<HashMap<AdderHead, AdderBody>>>,
}
/// The parachain context.
impl ParachainContext for AdderContext {
fn produce_candidate<I: IntoIterator<Item=(ParaId, Message)>>(
&self,
last_head: HeadData,
_ingress: I,
) -> Result<(BlockData, HeadData), InvalidHead>
{
let adder_head = AdderHead::decode(&mut &last_head.0[..])
.ok_or(InvalidHead)?;
let mut db = self.db.lock();
let last_body = if adder_head == GENESIS {
GENESIS_BODY
} else {
db.get(&adder_head)
.expect("All past bodies stored since this is the only collator")
.clone()
};
let next_body = AdderBody {
state: last_body.state.overflowing_add(last_body.add).0,
add: adder_head.number % 100,
};
let next_head = ::adder::execute(adder_head.hash(), adder_head, &next_body)
.expect("good execution params; qed");
let encoded_head = HeadData(next_head.encode());
let encoded_body = BlockData(next_body.encode());
println!("Created collation for #{}, post-state={}",
next_head.number, next_body.state.overflowing_add(next_body.add).0);
db.insert(next_head.clone(), next_body);
Ok((encoded_body, encoded_head))
}
}
fn main() {
let key = Arc::new(Pair::from_seed(&[1; 32]));
let id: ParaId = 100.into();
println!("Starting adder collator with genesis: ");
{
let encoded = GENESIS.encode();
println!("Dec: {:?}", encoded);
print!("Hex: 0x");
for byte in encoded {
print!("{:02x}", byte);
}
println!();
}
// can't use signal directly here because CtrlC takes only `Fn`.
let (exit_send, exit) = exit_future::signal();
let exit_send_cell = RefCell::new(Some(exit_send));
ctrlc::CtrlC::set_handler(move || {
if let Some(exit_send) = exit_send_cell.try_borrow_mut().expect("signal handler not reentrant; qed").take() {
exit_send.fire();
}
});
let context = AdderContext {
db: Arc::new(Mutex::new(HashMap::new())),
};
let res = ::collator::run_collator(
context,
id,
exit,
key,
::std::env::args(),
VersionInfo {
version: "<unknown>",
commit: "<unknown>",
executable_name: "adder-collator",
description: "collator for adder parachain",
author: "parity technologies",
}
);
if let Err(e) = res {
println!("{}", e);
}
}
+110
View File
@@ -0,0 +1,110 @@
// Copyright 2017 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.
#![no_std]
extern crate polkadot_parachain as parachain;
extern crate tiny_keccak;
use parachain::codec::{Decode, Encode, Input, Output};
/// Head data for this parachain.
#[derive(Default, Clone, Hash, Eq, PartialEq)]
pub struct HeadData {
/// Block number
pub number: u64,
/// parent block keccak256
pub parent_hash: [u8; 32],
/// hash of post-execution state.
pub post_state: [u8; 32],
}
impl HeadData {
pub fn hash(&self) -> [u8; 32] {
::tiny_keccak::keccak256(&self.encode())
}
}
impl Encode for HeadData {
fn encode_to<T: Output>(&self, dest: &mut T) {
dest.push(&self.number);
dest.push(&self.parent_hash);
dest.push(&self.post_state);
}
}
impl Decode for HeadData {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Some(HeadData {
number: Decode::decode(input)?,
parent_hash: Decode::decode(input)?,
post_state: Decode::decode(input)?,
})
}
}
/// Block data for this parachain.
#[derive(Default, Clone)]
pub struct BlockData {
/// State to begin from.
pub state: u64,
/// Amount to add (overflowing)
pub add: u64,
}
impl Encode for BlockData {
fn encode_to<T: Output>(&self, dest: &mut T) {
dest.push(&self.state);
dest.push(&self.add);
}
}
impl Decode for BlockData {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Some(BlockData {
state: Decode::decode(input)?,
add: Decode::decode(input)?,
})
}
}
pub fn hash_state(state: u64) -> [u8; 32] {
::tiny_keccak::keccak256(state.encode().as_slice())
}
/// Start state mismatched with parent header's state hash.
#[derive(Debug)]
pub struct StateMismatch;
/// Execute a block body on top of given parent head, producing new parent head
/// if valid.
pub fn execute(parent_hash: [u8; 32], parent_head: HeadData, block_data: &BlockData) -> Result<HeadData, StateMismatch> {
debug_assert_eq!(parent_hash, parent_head.hash());
if hash_state(block_data.state) != parent_head.post_state {
return Err(StateMismatch);
}
let new_state = block_data.state.overflowing_add(block_data.add).0;
Ok(HeadData {
number: parent_head.number + 1,
parent_hash,
post_state: hash_state(new_state),
})
}
+1
View File
@@ -0,0 +1 @@
src
@@ -0,0 +1,20 @@
[package]
name = "adder-wasm"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
adder = { path = ".." }
polkadot-parachain = { path = "../../../parachain", default-features = false }
wee_alloc = { version = "0.4.1" }
pwasm-libc = { version = "0.2" }
tiny-keccak = "1.4"
[lib]
crate-type = ["cdylib"]
[target.release]
panic = "abort"
lto = true
[workspace]
@@ -0,0 +1,73 @@
// Copyright 2017 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/>.
//! WASM validation for adder parachain.
#![no_std]
#![feature(
alloc, core_intrinsics, lang_items, panic_implementation, core_panic_info,
alloc_error_handler
)]
extern crate alloc;
extern crate wee_alloc;
extern crate pwasm_libc;
extern crate adder;
extern crate polkadot_parachain as parachain;
extern crate tiny_keccak;
// Define global allocator.
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
use core::{intrinsics, panic};
use parachain::ValidationResult;
use parachain::codec::{Encode, Decode};
use adder::{HeadData, BlockData};
#[panic_implementation]
#[no_mangle]
pub fn panic(_info: &panic::PanicInfo) -> ! {
unsafe {
intrinsics::abort()
}
}
#[alloc_error_handler]
#[no_mangle]
pub fn oom(_: ::core::alloc::Layout) -> ! {
unsafe {
intrinsics::abort();
}
}
#[no_mangle]
pub extern fn validate(offset: usize, len: usize) -> usize {
let params = unsafe { ::parachain::load_params(offset, 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[..]);
match ::adder::execute(parent_hash, parent_head, &block_data) {
Ok(new_head) => parachain::write_result(ValidationResult { head_data: new_head.encode() }),
Err(_) => panic!("execution failure"),
}
}
+15
View File
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -e
# Make LLD produce a binary that imports memory from the outside environment.
export RUSTFLAGS="-C link-arg=--import-memory -C lto=fat -C panic=abort"
for i in adder
do
cd $i/wasm
cargo +nightly 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
cd ../..
done