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
32 commits
Select commit Hold shift + click to select a range
cab6466
Start by replacing branch names and set `DownwardMessage`
bkchr Apr 30, 2020
20b7977
Add the upward-message crate
bkchr May 5, 2020
1b7065d
Add Kusama & Polkadot
bkchr May 7, 2020
0af3395
Merge remote-tracking branch 'origin/master' into bkchr-upward-downwa…
bkchr May 8, 2020
c1bcce8
More work on getting the upward messages working
bkchr May 14, 2020
495088a
Fix build
bkchr May 15, 2020
12d4f43
Merge remote-tracking branch 'origin/master' into bkchr-upward-downwa…
bkchr May 15, 2020
6f55f3a
Merge remote-tracking branch 'origin/master' into bkchr-upward-downwa…
bkchr May 15, 2020
bb23735
Begin to integrate it into the test Parachain
bkchr May 18, 2020
131fc35
Merge remote-tracking branch 'origin/master' into bkchr-upward-downwa…
bkchr May 19, 2020
8a0e3b0
Update
bkchr May 20, 2020
204097b
Make everything compile again
bkchr May 20, 2020
3ba4e80
Switch to westend and print parachain account on startup
bkchr May 20, 2020
c787fbf
Use MultiSignature etc
bkchr May 20, 2020
ca1c222
Fix validate block
bkchr May 22, 2020
3d96d81
Merge remote-tracking branch 'origin/master' into bkchr-upward-downwa…
bkchr Jun 5, 2020
f3c14c2
Some downward messages work
bkchr Jun 9, 2020
ec8f352
Update git reference
bkchr Jun 9, 2020
ab4d96f
More downward messages integration
bkchr Jun 9, 2020
95e0e3c
Update test runtime for downward messages
bkchr Jun 9, 2020
a3a4df1
Enable downward message handler and withdraw send tokens
bkchr Jun 9, 2020
9fde313
Add some docs
bkchr Jun 10, 2020
215444c
Begin to implement simple XCMP
bkchr Jun 11, 2020
348e688
More work
bkchr Jun 11, 2020
fcb66c4
Merge remote-tracking branch 'origin/master' into bkchr-upward-downwa…
bkchr Jun 11, 2020
d451f10
Fixes and make parachain id configurable
bkchr Jun 11, 2020
cd1a9e4
Make parachain ID be part of the genesis
bkchr Jun 11, 2020
7c987ce
Finishing the XCMP message demo
bkchr Jun 11, 2020
b52f9d6
Update and fixes tests
bkchr Jun 15, 2020
5df2315
Update branch
bkchr Jun 15, 2020
fe2b7ca
Merge remote-tracking branch 'origin/master' into bkchr-upward-downwa…
bkchr Jun 15, 2020
2a15c6f
Merge remote-tracking branch 'origin/master' into bkchr-upward-downwa…
bkchr Jun 18, 2020
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
More work on getting the upward messages working
  • Loading branch information
bkchr committed May 14, 2020
commit c1bcce865576589c55b90de21029ddaa55ce4dac
34 changes: 17 additions & 17 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions message-broker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2018"
frame-support = { git = "https://github.com/paritytech/substrate", branch = "bkchr-cumulus-branch", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "bkchr-cumulus-branch", default-features = false }
sp-inherents = { git = "https://github.com/paritytech/substrate", branch = "bkchr-cumulus-branch", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "bkchr-cumulus-branch", default-features = false }

# Other dependencies
codec = { package = "parity-scale-codec", version = "1.3.0", features = [ "derive" ], default-features = false }
Expand All @@ -22,6 +23,7 @@ std = [
"frame-support/std",
"frame-system/std",
"sp-inherents/std",
"sp-io/std",
"codec/std",
"cumulus-primitives/std",
]
18 changes: 13 additions & 5 deletions message-broker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,24 @@

use cumulus_primitives::{
inherents::{DownwardMessagesType, DOWNWARD_MESSAGES_IDENTIFIER},
well_known_keys, DownwardMessage, DownwardMessageHandler, UpwardMessageSender,
well_known_keys, DownwardMessage, DownwardMessageHandler, GenericUpwardMessage,
UpwardMessageOrigin, UpwardMessageSender,
};
use frame_support::{
decl_module, storage,
decl_module, storage, traits::Get,
weights::{DispatchClass, Weight},
};
use frame_system::ensure_none;
use sp_inherents::{InherentData, InherentIdentifier, MakeFatalError, ProvideInherent};
use codec::Encode;

/// Configuration trait of this pallet.
pub trait Trait: frame_system::Trait {
/// The downward message handlers that will be informed when a message is received.
type DownwardMessageHandlers: DownwardMessageHandler;

/// The upward message type used by the Parachain runtime.
type UpwardMessage: codec::Codec;
}

decl_module! {
Expand All @@ -51,13 +56,16 @@ decl_module! {
fn on_initialize() -> Weight {
storage::unhashed::kill(well_known_keys::UPWARD_MESSAGES);

0
T::DbWeight::get().writes(1)
}
}
}

impl<T: Trait> UpwardMessageSender for Module<T> {
fn send_upward_message(_: &()) -> Result<(), ()> {
impl<T: Trait> UpwardMessageSender<T::UpwardMessage> for Module<T> {
fn send_upward_message(msg: &T::UpwardMessage, origin: UpwardMessageOrigin) -> Result<(), ()> {
//TODO: check fee schedule
let msg = GenericUpwardMessage { origin, data: msg.encode() };
sp_io::storage::append(well_known_keys::UPWARD_MESSAGES, msg.encode());
Ok(())
}
}
Expand Down
16 changes: 11 additions & 5 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub use polkadot_core_primitives::DownwardMessage;
/// A generic upward message from a Parachain to the Relay Chain.
///
/// It is "generic" in such a way, that the actual message is encoded in the `data` field.
/// Besides the `data` it also holds the `origin` of the message.
pub use polkadot_parachain::primitives::UpwardMessage as GenericUpwardMessage;
pub use polkadot_parachain::primitives::ParachainDispatchOrigin as UpwardMessageOrigin;

pub mod validation_function_params;

Expand All @@ -30,7 +36,7 @@ pub mod inherents {
pub const DOWNWARD_MESSAGES_IDENTIFIER: InherentIdentifier = *b"cumdownm";

/// The type of the inherent downward messages.
pub type DownwardMessagesType = Vec<crate::DownwardMessage>;
pub type DownwardMessagesType = sp_std::vec::Vec<crate::DownwardMessage>;

/// The identifier for the `validation_function_params` inherent.
pub const VALIDATION_FUNCTION_PARAMS_IDENTIFIER: InherentIdentifier = *b"valfunp0";
Expand All @@ -42,14 +48,14 @@ pub mod inherents {
pub mod well_known_keys {
/// The storage key for the upward messages.
///
/// The upward messages are stored as SCALE encoded `Vec<()>`.
/// The upward messages are stored as SCALE encoded `Vec<GenericUpwardMessage>`.
pub const UPWARD_MESSAGES: &'static [u8] = b":cumulus_upward_messages:";

/// Current validation function parameters.
pub const VALIDATION_FUNCTION_PARAMS: &'static [u8] = b":validation_function_params";
pub const VALIDATION_FUNCTION_PARAMS: &'static [u8] = b":cumulus_validation_function_params";

/// Code upgarde (set as appropriate by a pallet).
pub const NEW_VALIDATION_CODE: &'static [u8] = b":new_validation_code";
pub const NEW_VALIDATION_CODE: &'static [u8] = b":cumulus_new_validation_code";
}

/// Something that should be called when a downward message is received.
Expand All @@ -64,5 +70,5 @@ pub trait UpwardMessageSender<UpwardMessage> {
/// Send an upward message to the relay chain.
///
/// Returns an error if sending failed.
fn send_upward_message(msg: &UpwardMessage) -> Result<(), ()>;
fn send_upward_message(msg: &UpwardMessage, origin: UpwardMessageOrigin) -> Result<(), ()>;
}
62 changes: 37 additions & 25 deletions runtime/src/validate_block/implementation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ use sp_trie::{delta_trie_root, read_trie_value, Layout, MemoryDB};

use hash_db::{HashDB, EMPTY_PREFIX};

use trie_db::{Trie, TrieDB, TrieDBIterator};
use trie_db::{TrieDB, TrieDBIterator};

use parachain::primitives::{HeadData, ValidationCode, ValidationParams, ValidationResult};

use codec::{Decode, Encode};

use cumulus_primitives::{
validation_function_params::ValidationFunctionParams,
well_known_keys::{NEW_VALIDATION_CODE, VALIDATION_FUNCTION_PARAMS},
well_known_keys::{NEW_VALIDATION_CODE, UPWARD_MESSAGES, VALIDATION_FUNCTION_PARAMS},
GenericUpwardMessage,
};

/// Stores the global [`Storage`] instance.
Expand Down Expand Up @@ -80,11 +81,11 @@ pub fn validate_block<B: BlockT, E: ExecuteBlock<B>>(params: ValidationParams) -
let block_data = crate::ParachainBlockData::<B>::decode(&mut &params.block_data.0[..])
.expect("Invalid parachain block data");

let parent_head = B::Header::decode(&mut &params.parent_head.0[..]).expect("Invalid parent head");
let parent_head =
B::Header::decode(&mut &params.parent_head.0[..]).expect("Invalid parent head");
// TODO: Use correct head data
let head_data = HeadData(block_data.header.encode());

// TODO: Add `PolkadotInherent`.
let block = B::new(block_data.header, block_data.extrinsics);
assert!(
parent_head.hash() == *block.header().parent_hash(),
Expand Down Expand Up @@ -118,16 +119,24 @@ pub fn validate_block<B: BlockT, E: ExecuteBlock<B>>(params: ValidationParams) -

E::execute_block(block);

// if in the course of block execution new validation code was set, insert
// its scheduled upgrade so we can validate that block number later
// If in the course of block execution new validation code was set, insert
// its scheduled upgrade so we can validate that block number later.
let new_validation_code = storage().get(NEW_VALIDATION_CODE).map(ValidationCode);
if new_validation_code.is_some() && validation_function_params.code_upgrade_allowed.is_none() {
panic!("attempt to upgrade validation function when not permitted");
panic!("Attempt to upgrade validation function when not permitted!");
}

// Extract potential upward messages from the storage.
let upward_messages = match storage().get(UPWARD_MESSAGES) {
Some(encoded) => Vec::<GenericUpwardMessage>::decode(&mut &encoded[..])
.expect("Upward messages vec is not correctly encoded in the storage!"),
None => Vec::new(),
};

ValidationResult {
head_data,
new_validation_code,
upward_messages,
}
}

Expand All @@ -144,7 +153,11 @@ impl<B: BlockT> WitnessStorage<B> {
/// Initialize from the given witness data and storage root.
///
/// Returns an error if given storage root was not found in the witness data.
fn new(data: WitnessData, storage_root: B::Hash, params: ValidationFunctionParams) -> Result<Self, &'static str> {
fn new(
data: WitnessData,
storage_root: B::Hash,
params: ValidationFunctionParams,
) -> Result<Self, &'static str> {
let mut db = MemoryDB::default();
data.into_iter().for_each(|i| {
db.insert(EMPTY_PREFIX, &i);
Expand All @@ -167,20 +180,19 @@ impl<B: BlockT> Storage for WitnessStorage<B> {
fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
match key {
VALIDATION_FUNCTION_PARAMS => Some(self.params.encode()),
key => {
self.overlay
.get(key)
.cloned()
.or_else(|| {
read_trie_value::<Layout<HashFor<B>>, _>(
&self.witness_data,
&self.storage_root,
key,
)
.ok()
})
.unwrap_or(None)
}
key => self
.overlay
.get(key)
.cloned()
.or_else(|| {
read_trie_value::<Layout<HashFor<B>>, _>(
&self.witness_data,
&self.storage_root,
key,
)
.ok()
})
.unwrap_or(None),
}
}

Expand All @@ -197,7 +209,8 @@ impl<B: BlockT> Storage for WitnessStorage<B> {
&mut self.witness_data,
self.storage_root.clone(),
self.overlay.drain(),
).expect("Calculates storage root");
)
.expect("Calculates storage root");

root.encode()
}
Expand All @@ -209,8 +222,7 @@ impl<B: BlockT> Storage for WitnessStorage<B> {
}
});

let trie = match TrieDB::<Layout<HashFor<B>>>::new(&self.witness_data, &self.storage_root)
{
let trie = match TrieDB::<Layout<HashFor<B>>>::new(&self.witness_data, &self.storage_root) {
Ok(r) => r,
Err(_) => panic!(),
};
Expand Down
Loading