Implement basic upward & downward messages (#118)

* Start by replacing branch names and set `DownwardMessage`

* Add the upward-message crate

* Add Kusama & Polkadot

* More work on getting the upward messages working

* Fix build

* Begin to integrate it into the test Parachain

* Update

* Make everything compile again

* Switch to westend and print parachain account on startup

* Use MultiSignature etc

* Fix validate block

* Some downward messages work

* Update git reference

* More downward messages integration

* Update test runtime for downward messages

* Enable downward message handler and withdraw send tokens

* Add some docs

* Begin to implement simple XCMP

* More work

* Fixes and make parachain id configurable

* Make parachain ID be part of the genesis

* Finishing the XCMP message demo

* Update and fixes tests

* Update branch
This commit is contained in:
Bastian Köcher
2020-06-18 12:10:20 +02:00
committed by GitHub
parent b082e7f815
commit 62c22b88b9
30 changed files with 1037 additions and 391 deletions
+41 -21
View File
@@ -26,20 +26,22 @@ use sp_api::impl_runtime_apis;
use sp_core::OpaqueMetadata;
use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
traits::{BlakeTwo256, Block as BlockT, Saturating, StaticLookup, Verify},
traits::{BlakeTwo256, Block as BlockT, IdentifyAccount, IdentityLookup, Saturating, Verify},
transaction_validity::{TransactionSource, TransactionValidity},
AnySignature, ApplyExtrinsicResult,
ApplyExtrinsicResult, MultiSignature,
};
use sp_std::prelude::*;
#[cfg(feature = "std")]
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
mod message_example;
// A few exports that help ease life for downstream crates.
pub use frame_support::{
construct_runtime, parameter_types,
traits::Randomness,
weights::{IdentityFee, Weight},
weights::{constants::WEIGHT_PER_SECOND, IdentityFee, Weight},
StorageValue,
};
pub use pallet_balances::Call as BalancesCall;
@@ -52,11 +54,11 @@ pub use sp_runtime::{Perbill, Permill};
pub type BlockNumber = u32;
/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
pub type Signature = AnySignature;
pub type Signature = MultiSignature;
/// Some way of identifying an account on the chain. We intentionally make it equivalent
/// to the public key of our transaction signing scheme.
pub type AccountId = <Signature as Verify>::Signer;
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;
/// The type for looking up accounts. We don't expect more than 4 billion of them, but you
/// never know...
@@ -122,6 +124,12 @@ pub const DAYS: BlockNumber = HOURS * 24;
// 1 in 4 blocks (on average, not counting collisions) will be primary babe blocks.
pub const PRIMARY_PROBABILITY: (u64, u64) = (1, 4);
#[derive(codec::Encode, codec::Decode)]
pub enum XCMPMessage<XAccountId, XBalance> {
/// Transfer tokens to the given account from the Parachain account.
TransferToken(XAccountId, XBalance),
}
/// The version infromation used to identify this runtime when compiled natively.
#[cfg(feature = "std")]
pub fn native_version() -> NativeVersion {
@@ -133,7 +141,7 @@ pub fn native_version() -> NativeVersion {
parameter_types! {
pub const BlockHashCount: BlockNumber = 250;
pub const MaximumBlockWeight: Weight = 1_000_000;
pub const MaximumBlockWeight: Weight = 2 * WEIGHT_PER_SECOND;
/// Assume 10% of weight for average on_initialize calls.
pub MaximumExtrinsicWeight: Weight = AvailableBlockRatio::get()
.saturating_sub(Perbill::from_percent(10)) * MaximumBlockWeight::get();
@@ -149,7 +157,7 @@ impl frame_system::Trait for Runtime {
/// The aggregated dispatch type that is available for extrinsics.
type Call = Call;
/// The lookup mechanism to get account ID from whatever is passed in dispatchers.
type Lookup = Indices;
type Lookup = IdentityLookup<AccountId>;
/// The index type for storing how many extrinsics an account has signed.
type Index = Index;
/// The index type for blocks.
@@ -178,24 +186,13 @@ impl frame_system::Trait for Runtime {
type ModuleToIndex = ModuleToIndex;
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = Balances;
type OnKilledAccount = ();
type DbWeight = ();
type ExtrinsicBaseWeight = ExtrinsicBaseWeight;
type BlockExecutionWeight = ();
type MaximumExtrinsicWeight = MaximumExtrinsicWeight;
}
parameter_types! {
pub const IndexDeposit: Balance = 1;
}
impl pallet_indices::Trait for Runtime {
type AccountIndex = u32;
type Event = Event;
type Currency = Balances;
type Deposit = IndexDeposit;
}
parameter_types! {
pub const MinimumPeriod: u64 = SLOT_DURATION / 2;
}
@@ -242,6 +239,27 @@ impl cumulus_parachain_upgrade::Trait for Runtime {
type OnValidationFunctionParams = ();
}
parameter_types! {
pub storage ParachainId: cumulus_primitives::ParaId = 100.into();
}
impl cumulus_message_broker::Trait for Runtime {
type Event = Event;
type DownwardMessageHandlers = TokenDealer;
type UpwardMessage = cumulus_upward_message::WestendUpwardMessage;
type ParachainId = ParachainId;
type XCMPMessage = XCMPMessage<AccountId, Balance>;
type XCMPMessageHandlers = TokenDealer;
}
impl message_example::Trait for Runtime {
type Event = Event;
type UpwardMessageSender = MessageBroker;
type UpwardMessage = cumulus_upward_message::WestendUpwardMessage;
type Currency = Balances;
type XCMPMessageSender = MessageBroker;
}
construct_runtime! {
pub enum Runtime where
Block = Block,
@@ -250,16 +268,18 @@ construct_runtime! {
{
System: frame_system::{Module, Call, Storage, Config, Event<T>},
Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent},
Indices: pallet_indices::{Module, Call, Storage, Config<T>, Event<T>},
Balances: pallet_balances::{Module, Call, Storage, Config<T>, Event<T>},
Sudo: pallet_sudo::{Module, Call, Storage, Config<T>, Event<T>},
RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Module, Call, Storage},
ParachainUpgrade: cumulus_parachain_upgrade::{Module, Call, Storage, Inherent, Event},
MessageBroker: cumulus_message_broker::{Module, Call, Inherent, Event<T>},
TokenDealer: message_example::{Module, Call, Event<T>, Config},
TransactionPayment: pallet_transaction_payment::{Module, Storage},
}
}
/// The address format for describing accounts.
pub type Address = <Indices as StaticLookup>::Source;
pub type Address = AccountId;
/// Block header type as expected by this runtime.
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
/// Block type as expected by this runtime.