Interchain message-passing (#117)

* compute ingress and routing in polkadot runtime

* extract parent candidates from block when beginning consensus

* fetch incoming messages when validating

* fix consensus tests

* parachain wasm execution uses messages

* update parachain tests to check if messages are executed

* abstract out network service to make room for network tests

* skeleton for incoming data fetch

* collate ingress from consensus-gossip

* keep track of validated candidates in the shared-table

* add some shared_table tests for new behavior

* broadcast egress messages on gossip

* test compute_ingress

* move network tests to module folder

* dummy network for consensus-network tests

* make consensus network generic over executor

* test egress broadcast and ingress fetch

* fix test compilation

* address some grumbles

* address grumbles and fix parachain shuffle

* remove broadcast parameter from consensus network trait
This commit is contained in:
Robert Habermeier
2019-02-19 13:59:29 -03:00
committed by GitHub
parent da409f6c9e
commit f8af277006
26 changed files with 1733 additions and 368 deletions
+25 -2
View File
@@ -24,7 +24,7 @@ extern crate parity_codec;
extern crate polkadot_parachain as parachain;
extern crate tiny_keccak;
use parity_codec::Encode;
use parity_codec::{Encode, Decode};
/// Head data for this parachain.
#[derive(Default, Clone, Hash, Eq, PartialEq, Encode, Decode)]
@@ -56,13 +56,35 @@ pub fn hash_state(state: u64) -> [u8; 32] {
::tiny_keccak::keccak256(state.encode().as_slice())
}
#[derive(Default, Encode, Decode)]
pub struct AddMessage {
/// The amount to add based on this message.
pub amount: u64,
}
/// Start state mismatched with parent header's state hash.
#[derive(Debug)]
pub struct StateMismatch;
/// Process all incoming messages, yielding the amount of addition from messages.
///
/// Ignores unknown message kinds.
pub fn process_messages<I, T>(iterable: I) -> u64
where I: IntoIterator<Item=T>, T: AsRef<[u8]>
{
iterable.into_iter()
.filter_map(|data| AddMessage::decode(&mut data.as_ref()))
.fold(0u64, |a, c| a.overflowing_add(c.amount).0)
}
/// 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> {
pub fn execute(
parent_hash: [u8; 32],
parent_head: HeadData,
block_data: &BlockData,
from_messages: u64,
) -> Result<HeadData, StateMismatch> {
debug_assert_eq!(parent_hash, parent_head.hash());
if hash_state(block_data.state) != parent_head.post_state {
@@ -70,6 +92,7 @@ pub fn execute(parent_hash: [u8; 32], parent_head: HeadData, block_data: &BlockD
}
let new_state = block_data.state.overflowing_add(block_data.add).0;
let new_state = new_state.overflowing_add(from_messages).0;
Ok(HeadData {
number: parent_head.number + 1,