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
+13 -2
View File
@@ -72,7 +72,7 @@ pub mod wasm_executor;
pub mod wasm_api;
/// Validation parameters for evaluating the parachain validity function.
// TODO: consolidated ingress and balance downloads
// TODO: balance downloads
#[derive(PartialEq, Eq, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Encode))]
pub struct ValidationParams {
@@ -80,6 +80,8 @@ pub struct ValidationParams {
pub block_data: Vec<u8>,
/// Previous head-data.
pub parent_head: Vec<u8>,
/// Incoming messages.
pub ingress: Vec<IncomingMessage>,
}
/// The result of parachain validation.
@@ -91,8 +93,17 @@ pub struct ValidationResult {
pub head_data: Vec<u8>,
}
/// An incoming message.
#[derive(PartialEq, Eq, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Encode))]
pub struct IncomingMessage {
/// The source parachain.
pub source: u32,
/// The data of the message.
pub data: Vec<u8>,
}
/// A reference to a message.
#[cfg(feature = "std")]
pub struct MessageRef<'a> {
/// The target parachain.
pub target: u32,
+2 -2
View File
@@ -17,7 +17,7 @@
//! Utilities for writing parachain WASM.
use codec::{Encode, Decode};
use super::{ValidationParams, ValidationResult, Message};
use super::{ValidationParams, ValidationResult, MessageRef};
mod ll {
extern "C" {
@@ -55,7 +55,7 @@ pub fn write_result(result: ValidationResult) -> usize {
}
/// Post a message to another parachain.
pub fn post_message(message: &Message) {
pub fn post_message(message: MessageRef) {
let data_ptr = message.data.as_ptr();
let data_len = message.data.len();
+47 -1
View File
@@ -22,7 +22,7 @@ extern crate parity_codec as codec;
extern crate polkadot_parachain as parachain;
extern crate tiny_keccak;
use parachain::{MessageRef, ValidationParams};
use parachain::{MessageRef, IncomingMessage, ValidationParams};
use parachain::wasm_executor::{Externalities, ExternalitiesError};
use codec::{Decode, Encode};
@@ -46,6 +46,12 @@ struct BlockData {
add: u64,
}
#[derive(Encode, Decode)]
struct AddMessage {
/// amount to add.
amount: u64,
}
struct DummyExt;
impl Externalities for DummyExt {
fn post_message(&mut self, _message: MessageRef) -> Result<(), ExternalitiesError> {
@@ -81,6 +87,7 @@ fn execute_good_on_parent() {
ValidationParams {
parent_head: parent_head.encode(),
block_data: block_data.encode(),
ingress: Vec::new(),
},
&mut DummyExt,
).unwrap();
@@ -115,6 +122,7 @@ fn execute_good_chain_on_parent() {
ValidationParams {
parent_head: parent_head.encode(),
block_data: block_data.encode(),
ingress: Vec::new(),
},
&mut DummyExt,
).unwrap();
@@ -149,7 +157,45 @@ fn execute_bad_on_parent() {
ValidationParams {
parent_head: parent_head.encode(),
block_data: block_data.encode(),
ingress: Vec::new(),
},
&mut DummyExt,
).unwrap_err();
}
#[test]
fn processes_messages() {
let parent_head = HeadData {
number: 0,
parent_hash: [0; 32],
post_state: hash_state(0),
};
let block_data = BlockData {
state: 0,
add: 512,
};
let bad_message_data = vec![1];
assert!(AddMessage::decode(&mut &bad_message_data[..]).is_none());
let ret = parachain::wasm_executor::validate_candidate(
TEST_CODE,
ValidationParams {
parent_head: parent_head.encode(),
block_data: block_data.encode(),
ingress: vec![
IncomingMessage { source: 1, data: (AddMessage { amount: 256 }).encode() },
IncomingMessage { source: 2, data: bad_message_data },
IncomingMessage { source: 3, data: (AddMessage { amount: 256 }).encode() },
],
},
&mut DummyExt,
).unwrap();
let new_head = HeadData::decode(&mut &ret.head_data[..]).unwrap();
assert_eq!(new_head.number, 1);
assert_eq!(new_head.parent_hash, hash_head(&parent_head));
assert_eq!(new_head.post_state, hash_state(1024));
}
Binary file not shown.