Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
4f03019
update basic_add wasm
rphmeier Jul 18, 2018
82def8c
Merge remote-tracking branch 'upstream/master' into basic-add-collator
rphmeier Jul 18, 2018
6d7a112
wasm feature and collator feature
rphmeier Jul 18, 2018
7a2b86a
move test parachains around a little
rphmeier Jul 20, 2018
78c0226
fix wasm build for basic_add
rphmeier Jul 20, 2018
18dfc62
move basic_add to adder, introduce README
rphmeier Jul 20, 2018
2bdcaf9
minimal basic_add collator
rphmeier Jul 23, 2018
9083847
ensure collator messages are sent in the right order
rphmeier Jul 23, 2018
c157c4c
more logging
rphmeier Jul 23, 2018
6b5f5ff
route consensus statements to all peers
rphmeier Jul 23, 2018
957f69e
minor bugfixes for parachains
rphmeier Jul 23, 2018
d9e1c77
genesis builder accounts for parachain heads
rphmeier Jul 23, 2018
53889d7
Merge remote-tracking branch 'upstream/master' into rh-simple-parachain
rphmeier Jul 24, 2018
d628c3d
fix parachains tests
rphmeier Jul 24, 2018
5653984
targets for txpool
rphmeier Jul 24, 2018
6888a5b
tweak runtime + collator
rphmeier Jul 25, 2018
beded4c
Merge remote-tracking branch 'upstream/master' into rh-simple-parachain
rphmeier Jul 26, 2018
ec68919
fix version in adder-collator
rphmeier Jul 27, 2018
5c633ea
Merge remote-tracking branch 'upstream/master' into rh-simple-parachain
rphmeier Jul 27, 2018
5625a81
consistency for overflowing
rphmeier Jul 27, 2018
b8e9dfc
Merge branch 'master' into rh-simple-parachain
gavofyork Jul 29, 2018
2e7d5c8
Merge remote-tracking branch 'upstream/master' into rh-simple-parachain
rphmeier Jul 30, 2018
55cbde1
Merge branch 'rh-simple-parachain' of github.com:paritytech/polkadot …
rphmeier Jul 30, 2018
1ebdd12
adjust comment
rphmeier Jul 30, 2018
65112b3
fix stable test run
rphmeier Jul 30, 2018
5e50c6a
remove dummy registration test
rphmeier Jul 30, 2018
b393ae9
final grumbles
rphmeier Jul 31, 2018
b1e3250
Merge remote-tracking branch 'upstream/master' into rh-simple-parachain
rphmeier Aug 1, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
minimal basic_add collator
  • Loading branch information
rphmeier committed Jul 23, 2018
commit 2bdcaf94a20d165ed6949bcad79756c2e8f5e6d4
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ members = [
"polkadot/service",

"polkadot/test-parachains/adder",
"polkadot/test-parachains/adder/collator",
"polkadot/test-parachains/adder/wasm",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this in members but other wasm projects in exclude?


"substrate/bft",
Expand Down
37 changes: 30 additions & 7 deletions polkadot/collator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ extern crate polkadot_primitives;
extern crate log;

use std::collections::{BTreeSet, BTreeMap, HashSet};
use std::fmt;
use std::sync::Arc;
use std::time::{Duration, Instant};

Expand All @@ -74,6 +75,28 @@ use tokio::timer::Deadline;

const COLLATION_TIMEOUT: Duration = Duration::from_secs(30);

/// Error to return when the head data was invalid.
#[derive(Clone, Copy, Debug)]
pub struct InvalidHead;

/// Collation errors.
#[derive(Debug)]
pub enum Error<R> {
/// Error on the relay-chain side of things.
Polkadot(R),
/// Error on the collator side of things.
Collator(InvalidHead),
}

impl<R: fmt::Display> fmt::Display for Error<R> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Polkadot(ref err) => write!(f, "Polkadot node error: {}", err),
Error::Collator(_) => write!(f, "Collator node error: Invalid head data"),
}
}
}

/// Parachain context needed for collation.
///
/// This can be implemented through an externally attached service or a stub.
Expand All @@ -84,7 +107,7 @@ pub trait ParachainContext: Clone {
&self,
last_head: HeadData,
ingress: I,
) -> (BlockData, HeadData);
) -> Result<(BlockData, HeadData), InvalidHead>;
}

/// Relay chain context needed to collate.
Expand Down Expand Up @@ -154,18 +177,18 @@ pub fn collate<'a, R, P>(
para_context: P,
key: Arc<ed25519::Pair>,
)
-> impl Future<Item=parachain::Collation, Error=R::Error> + 'a
-> impl Future<Item=parachain::Collation, Error=Error<R::Error>> + 'a
where
R: RelayChainContext + 'a,
R::Error: 'a,
R::FutureEgress: 'a,
P: ParachainContext + 'a,
{
collate_ingress(relay_context).map(move |ingress| {
collate_ingress(relay_context).map_err(Error::Polkadot).and_then(move |ingress| {
let (block_data, head_data) = para_context.produce_candidate(
last_head,
ingress.0.iter().flat_map(|&(id, ref msgs)| msgs.iter().cloned().map(move |msg| (id, msg)))
);
).map_err(Error::Collator)?;

let block_data_hash = block_data.hash();
let signature = key.sign(&block_data_hash.0[..]).into();
Expand All @@ -181,10 +204,10 @@ pub fn collate<'a, R, P>(
block_data_hash,
};

parachain::Collation {
Ok(parachain::Collation {
receipt,
block_data,
}
})
})
}

Expand Down Expand Up @@ -243,7 +266,7 @@ impl<P, E> Worker for CollationNode<P, E> where
($e:expr) => {
match $e {
Ok(x) => x,
Err(e) => return future::Either::A(future::err(e)),
Err(e) => return future::Either::A(future::err(Error::Polkadot(e))),
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions polkadot/test-parachains/adder/collator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "adder-collator"
version = "0.1.0"
authors = ["Parity Technologies <[email protected]>"]

[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"
125 changes: 125 additions & 0 deletions polkadot/test-parachains/adder/collator/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// 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;

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};
use futures::sync::oneshot;
use futures::Future;
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 + last_body.add,
Copy link
Contributor

@pepyakin pepyakin Jul 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this isn't saturating (like in adder::execute)?

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());

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();

// can't use signal directly here because CtrlC takes only `Fn`.
let (exit_send, exit) = oneshot::channel();

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.send(()).expect("Error sending exit notification");
}
});

let on_exit = exit.map_err(drop);
let context = AdderContext {
db: Arc::new(Mutex::new(HashMap::new())),
};

let args: Vec<_> = ::std::env::args().into_iter().map(Into::into).collect();
let res = ::collator::run_collator(
context,
id,
on_exit,
key,
args,
);

if let Err(e) = res {
println!("{}", e);
}
}
31 changes: 0 additions & 31 deletions polkadot/test-parachains/adder/src/collator.rs

This file was deleted.

11 changes: 9 additions & 2 deletions polkadot/test-parachains/adder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extern crate tiny_keccak;
use parachain::codec::{Decode, Encode, Input, Output};

/// Head data for this parachain.
#[derive(Default, Clone)]
#[derive(Default, Clone, Hash, Eq, PartialEq)]
pub struct HeadData {
/// Block number
pub number: u64,
Expand All @@ -34,6 +34,12 @@ pub struct HeadData {
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);
Expand Down Expand Up @@ -82,12 +88,13 @@ pub fn hash_state(state: u64) -> [u8; 32] {
}

/// Start state
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, did you mean to write something different? : )

#[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, ::tiny_keccak::keccak256(&parent_head.encode()));
debug_assert_eq!(parent_hash, parent_head.hash());

if hash_state(block_data.state) != parent_head.post_state {
return Err(StateMismatch);
Expand Down