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 071d7a11c4
commit c9aaddf667
30 changed files with 1037 additions and 391 deletions
+53 -20
View File
@@ -864,10 +864,14 @@ name = "cumulus-message-broker"
version = "0.1.0"
dependencies = [
"cumulus-primitives",
"cumulus-upward-message",
"frame-support",
"frame-system",
"parity-scale-codec",
"sp-inherents",
"sp-io",
"sp-runtime",
"sp-std",
]
[[package]]
@@ -923,6 +927,7 @@ version = "0.1.0"
dependencies = [
"impl-trait-for-tuples",
"parity-scale-codec",
"polkadot-core-primitives",
"polkadot-parachain",
"polkadot-primitives",
"sp-inherents",
@@ -947,7 +952,6 @@ dependencies = [
"sp-blockchain",
"sp-consensus",
"sp-core",
"sp-inherents",
"sp-io",
"sp-keyring",
"sp-runtime",
@@ -981,6 +985,7 @@ dependencies = [
"cumulus-collator",
"cumulus-consensus",
"cumulus-network",
"cumulus-primitives",
"cumulus-test-parachain-runtime",
"derive_more 0.15.0",
"exit-future 0.1.4",
@@ -997,6 +1002,7 @@ dependencies = [
"parking_lot 0.9.0",
"polkadot-cli",
"polkadot-collator",
"polkadot-parachain",
"polkadot-primitives",
"polkadot-runtime",
"polkadot-runtime-common",
@@ -1030,18 +1036,21 @@ dependencies = [
name = "cumulus-test-parachain-runtime"
version = "0.1.0"
dependencies = [
"cumulus-message-broker",
"cumulus-parachain-upgrade",
"cumulus-primitives",
"cumulus-runtime",
"cumulus-upward-message",
"frame-executive",
"frame-support",
"frame-system",
"pallet-balances",
"pallet-indices",
"pallet-randomness-collective-flip",
"pallet-sudo",
"pallet-timestamp",
"pallet-transaction-payment",
"parity-scale-codec",
"polkadot-parachain",
"serde",
"sp-api",
"sp-block-builder",
@@ -1066,6 +1075,18 @@ dependencies = [
"substrate-wasm-builder-runner 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cumulus-upward-message"
version = "0.1.0"
dependencies = [
"kusama-runtime",
"polkadot-core-primitives",
"polkadot-parachain",
"polkadot-runtime",
"sp-std",
"westend-runtime",
]
[[package]]
name = "curve25519-dalek"
version = "2.1.0"
@@ -2483,7 +2504,7 @@ dependencies = [
[[package]]
name = "kusama-runtime"
version = "0.8.8"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#abc1db331ac1fc4f73138bb059c107ee4011a069"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#91f3e68f5750dff7bddedeb846ff55447e8cf8f8"
dependencies = [
"bitvec",
"frame-executive",
@@ -4320,7 +4341,7 @@ checksum = "feb3b2b1033b8a60b4da6ee470325f887758c95d5320f52f9ce0df055a55940e"
[[package]]
name = "polkadot-availability-store"
version = "0.8.8"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#abc1db331ac1fc4f73138bb059c107ee4011a069"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#91f3e68f5750dff7bddedeb846ff55447e8cf8f8"
dependencies = [
"derive_more 0.99.7",
"exit-future 0.2.0",
@@ -4347,7 +4368,7 @@ dependencies = [
[[package]]
name = "polkadot-cli"
version = "0.8.8"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#abc1db331ac1fc4f73138bb059c107ee4011a069"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#91f3e68f5750dff7bddedeb846ff55447e8cf8f8"
dependencies = [
"frame-benchmarking-cli",
"futures 0.3.5",
@@ -4369,7 +4390,7 @@ dependencies = [
[[package]]
name = "polkadot-collator"
version = "0.8.8"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#abc1db331ac1fc4f73138bb059c107ee4011a069"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#91f3e68f5750dff7bddedeb846ff55447e8cf8f8"
dependencies = [
"futures 0.3.5",
"futures-timer 2.0.2",
@@ -4393,10 +4414,21 @@ dependencies = [
"tokio 0.2.21",
]
[[package]]
name = "polkadot-core-primitives"
version = "0.7.30"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#91f3e68f5750dff7bddedeb846ff55447e8cf8f8"
dependencies = [
"parity-scale-codec",
"sp-core",
"sp-runtime",
"sp-std",
]
[[package]]
name = "polkadot-erasure-coding"
version = "0.8.8"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#abc1db331ac1fc4f73138bb059c107ee4011a069"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#91f3e68f5750dff7bddedeb846ff55447e8cf8f8"
dependencies = [
"derive_more 0.15.0",
"parity-scale-codec",
@@ -4409,7 +4441,7 @@ dependencies = [
[[package]]
name = "polkadot-network"
version = "0.8.8"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#abc1db331ac1fc4f73138bb059c107ee4011a069"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#91f3e68f5750dff7bddedeb846ff55447e8cf8f8"
dependencies = [
"arrayvec 0.4.12",
"bytes 0.5.4",
@@ -4436,19 +4468,19 @@ dependencies = [
[[package]]
name = "polkadot-parachain"
version = "0.8.8"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#abc1db331ac1fc4f73138bb059c107ee4011a069"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#91f3e68f5750dff7bddedeb846ff55447e8cf8f8"
dependencies = [
"derive_more 0.99.7",
"log 0.4.8",
"parity-scale-codec",
"parking_lot 0.10.2",
"polkadot-core-primitives",
"sc-executor",
"serde",
"shared_memory",
"sp-core",
"sp-externalities",
"sp-io",
"sp-runtime-interface",
"sp-std",
"sp-wasm-interface",
]
@@ -4456,11 +4488,12 @@ dependencies = [
[[package]]
name = "polkadot-primitives"
version = "0.8.8"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#abc1db331ac1fc4f73138bb059c107ee4011a069"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#91f3e68f5750dff7bddedeb846ff55447e8cf8f8"
dependencies = [
"bitvec",
"frame-system",
"parity-scale-codec",
"polkadot-core-primitives",
"polkadot-parachain",
"serde",
"sp-api",
@@ -4477,7 +4510,7 @@ dependencies = [
[[package]]
name = "polkadot-rpc"
version = "0.8.8"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#abc1db331ac1fc4f73138bb059c107ee4011a069"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#91f3e68f5750dff7bddedeb846ff55447e8cf8f8"
dependencies = [
"jsonrpc-core",
"pallet-transaction-payment-rpc",
@@ -4503,7 +4536,7 @@ dependencies = [
[[package]]
name = "polkadot-runtime"
version = "0.8.8"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#abc1db331ac1fc4f73138bb059c107ee4011a069"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#91f3e68f5750dff7bddedeb846ff55447e8cf8f8"
dependencies = [
"bitvec",
"frame-executive",
@@ -4567,7 +4600,7 @@ dependencies = [
[[package]]
name = "polkadot-runtime-common"
version = "0.8.8"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#abc1db331ac1fc4f73138bb059c107ee4011a069"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#91f3e68f5750dff7bddedeb846ff55447e8cf8f8"
dependencies = [
"bitvec",
"frame-support",
@@ -4599,7 +4632,7 @@ dependencies = [
[[package]]
name = "polkadot-service"
version = "0.8.8"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#abc1db331ac1fc4f73138bb059c107ee4011a069"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#91f3e68f5750dff7bddedeb846ff55447e8cf8f8"
dependencies = [
"frame-benchmarking",
"frame-system-rpc-runtime-api",
@@ -4657,7 +4690,7 @@ dependencies = [
[[package]]
name = "polkadot-statement-table"
version = "0.8.8"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#abc1db331ac1fc4f73138bb059c107ee4011a069"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#91f3e68f5750dff7bddedeb846ff55447e8cf8f8"
dependencies = [
"parity-scale-codec",
"polkadot-primitives",
@@ -4667,7 +4700,7 @@ dependencies = [
[[package]]
name = "polkadot-test-runtime"
version = "0.8.8"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#abc1db331ac1fc4f73138bb059c107ee4011a069"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#91f3e68f5750dff7bddedeb846ff55447e8cf8f8"
dependencies = [
"bitvec",
"frame-executive",
@@ -4717,7 +4750,7 @@ dependencies = [
[[package]]
name = "polkadot-test-runtime-client"
version = "2.0.0"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#abc1db331ac1fc4f73138bb059c107ee4011a069"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#91f3e68f5750dff7bddedeb846ff55447e8cf8f8"
dependencies = [
"futures 0.3.5",
"pallet-timestamp",
@@ -4740,7 +4773,7 @@ dependencies = [
[[package]]
name = "polkadot-validation"
version = "0.8.8"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#abc1db331ac1fc4f73138bb059c107ee4011a069"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#91f3e68f5750dff7bddedeb846ff55447e8cf8f8"
dependencies = [
"bitvec",
"derive_more 0.14.1",
@@ -8638,7 +8671,7 @@ dependencies = [
[[package]]
name = "westend-runtime"
version = "0.8.8"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#abc1db331ac1fc4f73138bb059c107ee4011a069"
source = "git+https://github.com/paritytech/polkadot?branch=cumulus-branch#91f3e68f5750dff7bddedeb846ff55447e8cf8f8"
dependencies = [
"bitvec",
"frame-executive",
+1
View File
@@ -10,6 +10,7 @@ members = [
"test/client",
"test/parachain/runtime",
"test/parachain/",
"upward-message",
]
[profile.release]
+100 -92
View File
@@ -20,39 +20,39 @@ use cumulus_network::{
DelayedBlockAnnounceValidator, JustifiedBlockAnnounceValidator, WaitToAnnounce,
};
use cumulus_primitives::{
HeadData, inherents::VALIDATION_FUNCTION_PARAMS_IDENTIFIER as VFP_IDENT,
validation_function_params::ValidationFunctionParams,
inherents::{VALIDATION_FUNCTION_PARAMS_IDENTIFIER as VFP_IDENT, DOWNWARD_MESSAGES_IDENTIFIER, DownwardMessagesType},
validation_function_params::ValidationFunctionParams, HeadData,
};
use cumulus_runtime::ParachainBlockData;
use sc_client_api::{BlockchainEvents, Finalizer, StateBackend, UsageProvider};
use sc_service::Configuration;
use sp_api::{ApiExt, ProvideRuntimeApi};
use sp_blockchain::HeaderBackend;
use sp_consensus::{
BlockImport, BlockImportParams, BlockOrigin, Environment, Error as ConsensusError,
ForkChoiceStrategy, Proposal, Proposer, RecordProof,
};
use sp_inherents::{InherentData, InherentDataProviders};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, HashFor};
use sp_api::{ApiExt, ProvideRuntimeApi};
use sc_client_api::{StateBackend, UsageProvider, Finalizer, BlockchainEvents};
use sc_service::Configuration;
use sp_runtime::traits::{Block as BlockT, HashFor, Header as HeaderT};
use polkadot_collator::{
BuildParachainContext, InvalidHead, Network as CollatorNetwork, ParachainContext,
RuntimeApiCollection,
};
use polkadot_primitives::{
parachain::{self, BlockData, GlobalValidationSchedule, LocalValidationData, Id as ParaId},
Block as PBlock, Hash as PHash,
parachain::{self, BlockData, GlobalValidationSchedule, Id as ParaId, LocalValidationData},
Block as PBlock, DownwardMessage, Hash as PHash,
};
use codec::{Decode, Encode};
use log::{error, trace};
use futures::task::Spawn;
use futures::prelude::*;
use futures::task::Spawn;
use std::{marker::PhantomData, sync::Arc, time::Duration, pin::Pin};
use std::{marker::PhantomData, pin::Pin, sync::Arc, time::Duration};
use parking_lot::Mutex;
@@ -98,29 +98,44 @@ impl<Block: BlockT, PF, BI> Collator<Block, PF, BI> {
inherent_providers: InherentDataProviders,
global_validation: GlobalValidationSchedule,
local_validation: LocalValidationData,
downward_messages: DownwardMessagesType,
) -> Result<InherentData, InvalidHead> {
let mut inherent_data = inherent_providers
.create_inherent_data()
let mut inherent_data = inherent_providers.create_inherent_data().map_err(|e| {
error!(
target: "cumulus-collator",
"Failed to create inherent data: {:?}",
e,
);
InvalidHead
})?;
inherent_data
.put_data(
VFP_IDENT,
&ValidationFunctionParams::from((global_validation, local_validation)),
)
.map_err(|e| {
error!(
target: "cumulus-collator",
"Failed to create inherent data: {:?}",
"Failed to put validation function params into inherent data: {:?}",
e,
);
InvalidHead
})?;
inherent_data.put_data(
VFP_IDENT,
&ValidationFunctionParams::from((global_validation, local_validation))
).map_err(|e| {
error!(
target: "cumulus-collator",
"Failed to inject validation function params into inherents: {:?}",
e,
);
InvalidHead
})?;
inherent_data
.put_data(
DOWNWARD_MESSAGES_IDENTIFIER,
&downward_messages,
)
.map_err(|e| {
error!(
target: "cumulus-collator",
"Failed to put downward messages into inherent data: {:?}",
e,
);
InvalidHead
})?;
Ok(inherent_data)
}
@@ -152,16 +167,15 @@ where
+ Sync
+ 'static,
{
type ProduceCandidate = Pin<Box<
dyn Future<Output=Result<(BlockData, parachain::HeadData), InvalidHead>>
+ Send,
>>;
type ProduceCandidate =
Pin<Box<dyn Future<Output = Result<(BlockData, parachain::HeadData), InvalidHead>> + Send>>;
fn produce_candidate(
&mut self,
relay_chain_parent: PHash,
global_validation: GlobalValidationSchedule,
local_validation: LocalValidationData,
downward_messages: Vec<DownwardMessage>,
) -> Self::ProduceCandidate {
let factory = self.proposer_factory.clone();
let inherent_providers = self.inherent_data_providers.clone();
@@ -177,27 +191,28 @@ where
}
};
let proposer_future = factory
.lock()
.init(&last_head.header);
let proposer_future = factory.lock().init(&last_head.header);
let wait_to_announce = self.wait_to_announce.clone();
Box::pin(async move {
let parent_state_root = *last_head.header.state_root();
let proposer = proposer_future
.await
.map_err(|e| {
error!(
target: "cumulus-collator",
"Could not create proposer: {:?}",
e,
);
InvalidHead
})?;
let proposer = proposer_future.await.map_err(|e| {
error!(
target: "cumulus-collator",
"Could not create proposer: {:?}",
e,
);
InvalidHead
})?;
let inherent_data = Self::inherent_data(inherent_providers, global_validation, local_validation)?;
let inherent_data = Self::inherent_data(
inherent_providers,
global_validation,
local_validation,
downward_messages,
)?;
let Proposal {
block,
@@ -221,14 +236,13 @@ where
InvalidHead
})?;
let proof = proof
.ok_or_else(|| {
error!(
target: "cumulus-collator",
"Proposer did not return the requested proof.",
);
InvalidHead
})?;
let proof = proof.ok_or_else(|| {
error!(
target: "cumulus-collator",
"Proposer did not return the requested proof.",
);
InvalidHead
})?;
let (header, extrinsics) = block.deconstruct();
@@ -262,20 +276,13 @@ where
let header = b.into_header();
let encoded_header = header.encode();
let hash = header.hash();
let head_data = HeadData::<Block> {
header,
};
let head_data = HeadData::<Block> { header };
let candidate = (
block_data,
parachain::HeadData(head_data.encode()),
);
let candidate = (block_data, parachain::HeadData(head_data.encode()));
wait_to_announce.lock().wait_to_announce(
hash,
relay_chain_parent,
encoded_header,
);
wait_to_announce
.lock()
.wait_to_announce(hash, relay_chain_parent, encoded_header);
trace!(target: "cumulus-collator", "Produced candidate: {:?}", candidate);
@@ -296,9 +303,7 @@ pub struct CollatorBuilder<Block: BlockT, PF, BI, Backend, Client> {
_marker: PhantomData<(Block, Backend)>,
}
impl<Block: BlockT, PF, BI, Backend, Client>
CollatorBuilder<Block, PF, BI, Backend, Client>
{
impl<Block: BlockT, PF, BI, Backend, Client> CollatorBuilder<Block, PF, BI, Backend, Client> {
/// Create a new instance of self.
pub fn new(
proposer_factory: PF,
@@ -334,7 +339,9 @@ where
+ Sync
+ 'static,
Backend: sc_client_api::Backend<Block> + 'static,
Client: Finalizer<Block, Backend> + UsageProvider<Block> + HeaderBackend<Block>
Client: Finalizer<Block, Backend>
+ UsageProvider<Block>
+ HeaderBackend<Block>
+ Send
+ Sync
+ 'static,
@@ -348,16 +355,22 @@ where
polkadot_network: impl CollatorNetwork + Clone + 'static,
) -> Result<Self::ParachainContext, ()>
where
PClient: ProvideRuntimeApi<PBlock> + BlockchainEvents<PBlock> + HeaderBackend<PBlock>
+ Send + Sync + 'static,
PClient: ProvideRuntimeApi<PBlock>
+ BlockchainEvents<PBlock>
+ HeaderBackend<PBlock>
+ Send
+ Sync
+ 'static,
PClient::Api: RuntimeApiCollection<Extrinsic>,
<PClient::Api as ApiExt<PBlock>>::StateBackend: StateBackend<HashFor<PBlock>>,
Spawner: Spawn + Clone + Send + Sync + 'static,
Extrinsic: codec::Codec + Send + Sync + 'static,
{
self.delayed_block_announce_validator.set(
Box::new(JustifiedBlockAnnounceValidator::new(polkadot_client.clone(), self.para_id)),
);
self.delayed_block_announce_validator
.set(Box::new(JustifiedBlockAnnounceValidator::new(
polkadot_client.clone(),
self.para_id,
)));
let follow =
match cumulus_consensus::follow_polkadot(self.para_id, self.client, polkadot_client) {
@@ -368,12 +381,7 @@ where
};
spawner
.spawn_obj(
Box::new(
follow.map(|_| ()),
)
.into(),
)
.spawn_obj(Box::new(follow.map(|_| ())).into())
.map_err(|_| error!("Could not spawn parachain server!"))?;
Ok(Collator::new(
@@ -415,12 +423,10 @@ mod tests {
use sp_state_machine::StorageProof;
use substrate_test_client::{NativeExecutor, WasmExecutionMethod::Interpreted};
use test_client::{
DefaultTestClientBuilderExt, TestClientBuilder, TestClientBuilderExt,
};
use test_client::{DefaultTestClientBuilderExt, TestClientBuilder, TestClientBuilderExt};
use test_runtime::{Block, Header};
use futures::{Stream, future};
use futures::{future, Stream};
#[derive(Debug)]
struct Error;
@@ -436,9 +442,9 @@ mod tests {
impl Environment<Block> for DummyFactory {
type Proposer = DummyProposer;
type Error = Error;
type CreateProposer = Pin<Box<
dyn Future<Output = Result<Self::Proposer, Self::Error>> + Send + Unpin + 'static
>>;
type CreateProposer = Pin<
Box<dyn Future<Output = Result<Self::Proposer, Self::Error>> + Send + Unpin + 'static>,
>;
fn init(&mut self, _: &Header) -> Self::CreateProposer {
Box::pin(future::ready(Ok(DummyProposer)))
@@ -479,7 +485,10 @@ mod tests {
struct DummyCollatorNetwork;
impl CollatorNetwork for DummyCollatorNetwork {
fn checked_statements(&self, _: PHash) -> Pin<Box<dyn Stream<Item = SignedStatement> + Send>> {
fn checked_statements(
&self,
_: PHash,
) -> Pin<Box<dyn Stream<Item = SignedStatement> + Send>> {
unimplemented!("Not required in tests")
}
}
@@ -526,13 +535,11 @@ mod tests {
Arc::new(
substrate_test_client::TestClientBuilder::<_, _, _, ()>::default()
.build_with_native_executor::<polkadot_service::polkadot_runtime::RuntimeApi, _>(
Some(
NativeExecutor::<polkadot_service::PolkadotExecutor>::new(
Interpreted,
None,
1,
)
)
Some(NativeExecutor::<polkadot_service::PolkadotExecutor>::new(
Interpreted,
None,
1,
)),
)
.0,
),
@@ -562,6 +569,7 @@ mod tests {
balance: 10,
code_upgrade_allowed: None,
},
Vec::new(),
context,
Arc::new(Sr25519Keyring::Alice.pair().into()),
);
+8
View File
@@ -9,12 +9,16 @@ edition = "2018"
frame-support = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
sp-inherents = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
# Other dependencies
codec = { package = "parity-scale-codec", version = "1.3.0", features = [ "derive" ], default-features = false }
# Cumulus dependencies
cumulus-primitives = { path = "../primitives", default-features = false }
cumulus-upward-message = { path = "../upward-message", default-features = false }
[features]
default = [ "std" ]
@@ -22,6 +26,10 @@ std = [
"frame-support/std",
"frame-system/std",
"sp-inherents/std",
"sp-io/std",
"sp-std/std",
"sp-runtime/std",
"codec/std",
"cumulus-primitives/std",
"cumulus-upward-message/std",
]
+85 -9
View File
@@ -16,52 +16,128 @@
//! Cumulus message broker pallet.
//!
//! This pallet provides support for handling downward and upward messages.
//! This pallet provides support for handling downward, upward messages and
//! XMCP messages.
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Encode, Decode};
use cumulus_primitives::{
inherents::{DownwardMessagesType, DOWNWARD_MESSAGES_IDENTIFIER},
well_known_keys, DownwardMessageHandler, UpwardMessageSender,
well_known_keys,
xcmp::{RawXCMPMessage, XCMPMessageHandler, XCMPMessageSender},
DownwardMessage, DownwardMessageHandler, GenericUpwardMessage, ParaId, UpwardMessageOrigin,
UpwardMessageSender,
};
use cumulus_upward_message::XCMPMessage;
use frame_support::{
decl_module, storage,
decl_event, decl_module, storage,
traits::Get,
weights::{DispatchClass, Weight},
};
use frame_system::ensure_none;
use sp_inherents::{InherentData, InherentIdentifier, MakeFatalError, ProvideInherent};
use sp_runtime::traits::Hash;
use sp_std::vec::Vec;
/// Configuration trait of this pallet.
pub trait Trait: frame_system::Trait {
/// Event type used by the runtime.
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
/// 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 + XCMPMessage;
/// The XCMP message handlers that will be informed when a XCMP message is received.
type XCMPMessageHandlers: XCMPMessageHandler<Self::XCMPMessage>;
/// The XCMP message type used by the Parachain runtime.
type XCMPMessage: codec::Codec;
/// The Id of the parachain.
type ParachainId: Get<ParaId>;
}
decl_event! {
pub enum Event<T> where Hash = <T as frame_system::Trait>::Hash {
/// An upward message was sent to the relay chain.
///
/// The hash corresponds to the hash of the encoded upward message.
UpwardMessageSent(Hash),
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system = frame_system {
/// Executes the given downward messages by calling the message handlers.
///
/// The origin of this call needs to be `None` as this is an inherent.
#[weight = (10, DispatchClass::Mandatory)]
fn execute_downward_messages(origin, messages: Vec<()>) {
fn execute_downward_messages(origin, messages: Vec<DownwardMessage>) {
ensure_none(origin)?;
messages.iter().for_each(T::DownwardMessageHandlers::handle_downward_message);
//TODO: max messages should not be hardcoded. It should be determined based on the
// weight used by the handlers.
let max_messages = 10;
messages.iter().take(max_messages).for_each(|msg| {
match msg {
DownwardMessage::XCMPMessage(msg) => {
if let Ok(msg) = RawXCMPMessage::decode(&mut &msg[..]) {
if let Ok(xcmp_msg) = T::XCMPMessage::decode(&mut &msg.data[..]) {
T::XCMPMessageHandlers::handle_xcmp_message(msg.from, &xcmp_msg);
}
}
},
msg => T::DownwardMessageHandlers::handle_downward_message(msg),
}
});
let processed = sp_std::cmp::min(messages.len(), max_messages) as u32;
storage::unhashed::put(well_known_keys::PROCESSED_DOWNWARD_MESSAGES, &processed);
}
fn on_initialize() -> Weight {
storage::unhashed::kill(well_known_keys::UPWARD_MESSAGES);
0
T::DbWeight::get().writes(1)
}
fn deposit_event() = default;
}
}
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 data = msg.encode();
let data_hash = T::Hashing::hash(&data);
let msg = GenericUpwardMessage { origin, data };
sp_io::storage::append(well_known_keys::UPWARD_MESSAGES, msg.encode());
Self::deposit_event(RawEvent::UpwardMessageSent(data_hash));
Ok(())
}
}
impl<T: Trait> XCMPMessageSender<T::XCMPMessage> for Module<T> {
fn send_xcmp_message(dest: ParaId, msg: &T::XCMPMessage) -> Result<(), ()> {
let message = RawXCMPMessage {
from: T::ParachainId::get(),
data: msg.encode(),
};
Self::send_upward_message(
&T::UpwardMessage::send_message(dest, message.encode()),
UpwardMessageOrigin::Parachain,
)
}
}
impl<T: Trait> ProvideInherent for Module<T> {
type Call = Call<T>;
type Error = MakeFatalError<()>;
+4
View File
@@ -439,6 +439,10 @@ sp_api::mock_impl_runtime_apis! {
parent_hash: Default::default(),
}
}
fn downward_messages(_: ParaId) -> Vec<polkadot_primitives::DownwardMessage> {
Vec::new()
}
}
}
+12 -11
View File
@@ -14,23 +14,24 @@ cumulus-runtime = { path = "../runtime", default-features = false }
parachain = { package = "polkadot-parachain", git = "https://github.com/paritytech/polkadot", branch = "cumulus-branch", default-features = false }
# Substrate dependencies
frame-support = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", version = "2.0.0-dev", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", version = "2.0.0-dev", default-features = false }
sp-inherents = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
system = { package = "frame-system", git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate.git", branch = "cumulus-branch", default-features = false }
pallet-balances = { git = "https://github.com/paritytech/substrate.git", branch = "cumulus-branch", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate.git", branch = "cumulus-branch", version = "2.0.0-dev", default-features = false }
sp-inherents = { git = "https://github.com/paritytech/substrate.git", branch = "cumulus-branch", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate.git", branch = "cumulus-branch", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate.git", branch = "cumulus-branch", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate.git", branch = "cumulus-branch", default-features = false }
sp-version = { git = "https://github.com/paritytech/substrate.git", branch = "cumulus-branch", default-features = false }
system = { package = "frame-system", git = "https://github.com/paritytech/substrate.git", branch = "cumulus-branch", default-features = false }
# Other Dependencies
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"]}
serde = { version = "1.0.101", optional = true, features = ["derive"] }
[dev-dependencies]
sp-externalities = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
substrate-test-runtime-client = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
sp-version = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
sp-externalities = { git = "https://github.com/paritytech/substrate.git", branch = "cumulus-branch", default-features = false }
substrate-test-runtime-client = { git = "https://github.com/paritytech/substrate.git", branch = "cumulus-branch", default-features = false }
sp-version = { git = "https://github.com/paritytech/substrate.git", branch = "cumulus-branch", default-features = false }
[features]
default = ['std']
+4
View File
@@ -18,6 +18,9 @@ polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch =
codec = { package = "parity-scale-codec", version = "1.0.5", default-features = false, features = [ "derive" ] }
impl-trait-for-tuples = "0.1.3"
# Polkadot dependencies
polkadot-core-primitives = { git = "https://github.com/paritytech/polkadot", branch = "cumulus-branch", default-features = false }
[features]
default = [ "std" ]
std = [
@@ -26,5 +29,6 @@ std = [
"polkadot-primitives/std",
"polkadot-parachain/std",
"sp-inherents/std",
"polkadot-core-primitives/std",
"sp-runtime/std",
]
+23 -7
View File
@@ -18,7 +18,18 @@
#![cfg_attr(not(feature = "std"), no_std)]
pub use polkadot_core_primitives as relay_chain;
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 use polkadot_parachain::primitives::Id as ParaId;
pub mod validation_function_params;
pub mod xcmp;
use codec::{Decode, Encode};
use sp_runtime::traits::Block as BlockT;
@@ -31,7 +42,7 @@ pub mod inherents {
pub const DOWNWARD_MESSAGES_IDENTIFIER: InherentIdentifier = *b"cumdownm";
/// The type of the inherent downward messages.
pub type DownwardMessagesType = sp_std::vec::Vec<()>;
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";
@@ -43,29 +54,34 @@ 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";
/// The storage key for the processed downward messages.
///
/// The value is stored as SCALE encoded `u32`.
pub const PROCESSED_DOWNWARD_MESSAGES: &'static [u8] = b":cumulus_processed_downward_messages:";
}
/// Something that should be called when a downward message is received.
#[impl_trait_for_tuples::impl_for_tuples(30)]
pub trait DownwardMessageHandler {
/// Handle the given downward message.
fn handle_downward_message(msg: &());
fn handle_downward_message(msg: &DownwardMessage);
}
/// Something that can send upward messages.
pub trait UpwardMessageSender {
pub trait UpwardMessageSender<UpwardMessage> {
/// Send an upward message to the relay chain.
///
/// Returns an error if sending failed.
fn send_upward_message(msg: &()) -> Result<(), ()>;
fn send_upward_message(msg: &UpwardMessage, origin: UpwardMessageOrigin) -> Result<(), ()>;
}
/// The head data of the parachain, stored in the relay chain.
+42
View File
@@ -0,0 +1,42 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Cumulus.
// Substrate 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.
// Substrate 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 Cumulus. If not, see <http://www.gnu.org/licenses/>.
//! XMCP related primitives
use polkadot_primitives::parachain::Id as ParaId;
use sp_std::vec::Vec;
/// A raw XCMP message that is being send between two Parachain's.
#[derive(codec::Encode, codec::Decode)]
pub struct RawXCMPMessage {
/// Parachain sending the message.
pub from: ParaId,
/// SCALE encoded message.
pub data: Vec<u8>,
}
/// Something that can handle XCMP messages.
#[impl_trait_for_tuples::impl_for_tuples(30)]
pub trait XCMPMessageHandler<Message: codec::Decode> {
/// Handle a XCMP message.
fn handle_xcmp_message(src: ParaId, msg: &Message);
}
/// Something that can send XCMP messages.
pub trait XCMPMessageSender<Message: codec::Encode> {
/// Send a XCMP message to the given parachain.
fn send_xcmp_message(dest: ParaId, msg: &Message) -> Result<(), ()>;
}
-1
View File
@@ -22,7 +22,6 @@ sp-core = { git = "https://github.com/paritytech/substrate", default-features =
sp-io = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "cumulus-branch" }
frame-executive = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "cumulus-branch" }
sp-trie = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "cumulus-branch" }
sp-inherents = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "cumulus-branch" }
# Polkadot dependencies
parachain = { package = "polkadot-parachain", git = "https://github.com/paritytech/polkadot", branch = "cumulus-branch", default-features = false, features = [ "wasm-api" ] }
@@ -33,7 +33,11 @@ use codec::{Decode, Encode, EncodeAppend};
use cumulus_primitives::{
validation_function_params::ValidationFunctionParams,
well_known_keys::{NEW_VALIDATION_CODE, VALIDATION_FUNCTION_PARAMS},
well_known_keys::{
NEW_VALIDATION_CODE, PROCESSED_DOWNWARD_MESSAGES, UPWARD_MESSAGES,
VALIDATION_FUNCTION_PARAMS,
},
GenericUpwardMessage,
};
/// Stores the global [`Storage`] instance.
@@ -92,11 +96,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");
// TODO: Use correct head data
let parent_head =
B::Header::decode(&mut &params.parent_head.0[..]).expect("Invalid parent head");
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(),
@@ -131,16 +135,30 @@ 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(),
};
let processed_downward_messages = storage()
.get(PROCESSED_DOWNWARD_MESSAGES)
.and_then(|v| Decode::decode(&mut &v[..]).ok())
.unwrap_or_default();
ValidationResult {
head_data,
new_validation_code,
upward_messages,
processed_downward_messages,
}
}
@@ -157,7 +175,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);
@@ -180,20 +202,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),
}
}
@@ -209,8 +230,11 @@ impl<B: BlockT> Storage for WitnessStorage<B> {
let root = delta_trie_root::<Layout<HashFor<B>>, _, _, _, _, _>(
&mut self.witness_data,
self.storage_root.clone(),
self.overlay.iter().map(|(k, v)| (k.as_ref(), v.as_ref().map(|v| v.as_ref()))),
).expect("Calculates storage root");
self.overlay
.iter()
.map(|(k, v)| (k.as_ref(), v.as_ref().map(|v| v.as_ref()))),
)
.expect("Calculates storage root");
root.encode()
}
@@ -222,8 +246,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!(),
};
@@ -239,10 +262,12 @@ impl<B: BlockT> Storage for WitnessStorage<B> {
let current_value = self.overlay.entry(key.to_vec()).or_default();
let item = current_value.take().unwrap_or_default();
*current_value = Some(match Vec::<EncodeOpaqueValue>::append_or_new(item, &value_vec) {
Ok(item) => item,
Err(_) => value_vec.encode(),
});
*current_value = Some(
match Vec::<EncodeOpaqueValue>::append_or_new(item, &value_vec) {
Ok(item) => item,
Err(_) => value_vec.encode(),
},
);
}
}
+2 -1
View File
@@ -26,7 +26,8 @@ mod tests;
#[doc(hidden)]
pub use parachain;
/// Register the `validate_block` function that is used by parachains to validate blocks on a validator.
/// Register the `validate_block` function that is used by parachains to validate blocks on a
/// validator.
///
/// Does *nothing* when `std` feature is enabled.
///
+3 -1
View File
@@ -44,9 +44,11 @@ sc-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch
cumulus-consensus = { path = "../../consensus" }
cumulus-collator = { path = "../../collator" }
cumulus-network = { path = "../../network" }
cumulus-primitives = { path = "../../primitives" }
# Polkadot dependencies
polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "cumulus-branch" }
polkadot-parachain = { git = "https://github.com/paritytech/polkadot", branch = "cumulus-branch" }
polkadot-collator = { git = "https://github.com/paritytech/polkadot", branch = "cumulus-branch" }
polkadot-service = { git = "https://github.com/paritytech/polkadot", branch = "cumulus-branch" }
polkadot-cli = { git = "https://github.com/paritytech/polkadot", branch = "cumulus-branch" }
@@ -70,8 +72,8 @@ polkadot-runtime = { git = "https://github.com/paritytech/polkadot", branch = "c
# Substrate dependencies
substrate-test-runtime-client = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
pallet-grandpa = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
pallet-transaction-payment = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
sp-arithmetic = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
sp-version = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
pallet-sudo = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
pallet-grandpa = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
File diff suppressed because one or more lines are too long
+38 -26
View File
@@ -25,7 +25,6 @@ frame-support = { git = "https://github.com/paritytech/substrate", default-featu
frame-executive = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "cumulus-branch" }
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "cumulus-branch" }
pallet-balances = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "cumulus-branch" }
pallet-indices = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "cumulus-branch" }
pallet-randomness-collective-flip = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "cumulus-branch" }
pallet-timestamp = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "cumulus-branch" }
pallet-sudo = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "cumulus-branch" }
@@ -34,35 +33,48 @@ pallet-transaction-payment = { git = "https://github.com/paritytech/substrate",
# Cumulus dependencies
cumulus-runtime = { path = "../../../runtime", default-features = false }
cumulus-parachain-upgrade = { path = "../../../parachain-upgrade", default-features = false }
cumulus-message-broker = { path = "../../../message-broker", default-features = false }
cumulus-upward-message = { path = "../../../upward-message", default-features = false }
cumulus-primitives = { path = "../../../primitives", default-features = false }
# Polkadot dependencies
polkadot-parachain = { git = "https://github.com/paritytech/polkadot", branch = "cumulus-branch", default-features = false }
[build-dependencies]
wasm-builder-runner = { package = "substrate-wasm-builder-runner", version = "1.0.6" }
[features]
default = ['std']
default = [ "std" ]
std = [
'codec/std',
'serde',
'sp-api/std',
'sp-std/std',
'sp-io/std',
'sp-core/std',
'sp-runtime/std',
'sp-version/std',
'sp-offchain/std',
'sp-session/std',
'sp-block-builder/std',
'sp-transaction-pool/std',
'sp-inherents/std',
'frame-support/std',
'frame-executive/std',
'frame-system/std',
'pallet-balances/std',
'pallet-indices/std',
'pallet-randomness-collective-flip/std',
'pallet-timestamp/std',
'pallet-sudo/std',
'pallet-transaction-payment/std',
'cumulus-runtime/std',
'cumulus-parachain-upgrade/std',
"codec/std",
"serde",
"sp-api/std",
"sp-std/std",
"sp-io/std",
"sp-core/std",
"sp-runtime/std",
"sp-version/std",
"sp-offchain/std",
"sp-session/std",
"sp-block-builder/std",
"sp-transaction-pool/std",
"sp-inherents/std",
"frame-support/std",
"frame-executive/std",
"frame-system/std",
"pallet-balances/std",
"pallet-randomness-collective-flip/std",
"pallet-timestamp/std",
"pallet-sudo/std",
"pallet-transaction-payment/std",
"cumulus-runtime/std",
"cumulus-parachain-upgrade/std",
"cumulus-message-broker/std",
"cumulus-upward-message/std",
"cumulus-primitives/std",
"polkadot-parachain/std",
]
# Will be enabled by the `wasm-builder` when building the runtime for WASM.
runtime-wasm = [
"cumulus-upward-message/runtime-wasm",
]
+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.
@@ -0,0 +1,179 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Cumulus.
// Cumulus 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.
// Cumulus 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 Cumulus. If not, see <http://www.gnu.org/licenses/>.
//! Example Pallet that shows how to send upward messages and how to receive
//! downward messages.
use frame_support::{
decl_event, decl_module, decl_storage,
traits::{Currency, ExistenceRequirement, WithdrawReason},
};
use frame_system::ensure_signed;
use crate::XCMPMessage;
use codec::{Decode, Encode};
use cumulus_primitives::{
relay_chain::DownwardMessage,
xcmp::{XCMPMessageHandler, XCMPMessageSender},
DownwardMessageHandler, ParaId, UpwardMessageOrigin, UpwardMessageSender,
};
use cumulus_upward_message::BalancesMessage;
use polkadot_parachain::primitives::AccountIdConversion;
use sp_runtime::DispatchResult;
type BalanceOf<T> =
<<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
/// Configuration trait of this pallet.
pub trait Trait: frame_system::Trait {
/// Event type used by the runtime.
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
/// The sender of upward messages.
type UpwardMessageSender: UpwardMessageSender<Self::UpwardMessage>;
/// The upward message type used by the Parachain runtime.
type UpwardMessage: codec::Codec + BalancesMessage<Self::AccountId, BalanceOf<Self>>;
/// Currency of the runtime.
type Currency: Currency<Self::AccountId>;
/// The sender of XCMP messages.
type XCMPMessageSender: XCMPMessageSender<XCMPMessage<Self::AccountId, BalanceOf<Self>>>;
}
// This pallet's storage items.
decl_storage! {
trait Store for Module<T: Trait> as ParachainUpgrade {}
add_extra_genesis {
config(parachain_id): ParaId;
build(|config: &Self| {
// This is basically a hack to make the parachain id easily configurable.
// Could also be done differently, but yeah..
crate::ParachainId::set(&config.parachain_id);
});
}
}
decl_event! {
pub enum Event<T> where
AccountId = <T as frame_system::Trait>::AccountId,
Balance = BalanceOf<T>
{
/// Transferred tokens to the account on the relay chain.
TransferredTokensToRelayChain(AccountId, Balance),
/// Transferred tokens to the account on request from the relay chain.
TransferredTokensFromRelayChain(AccountId, Balance),
/// Transferred tokens to the account from the given parachain account.
TransferredTokensViaXCMP(ParaId, AccountId, Balance, DispatchResult),
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system = frame_system {
/// Transfer `amount` of tokens on the relay chain from the Parachain account to
/// the given `dest` account.
#[weight = 10]
fn transfer_tokens_to_relay_chain(origin, dest: T::AccountId, amount: BalanceOf<T>) {
let who = ensure_signed(origin)?;
let _ = T::Currency::withdraw(
&who,
amount,
WithdrawReason::Transfer.into(),
ExistenceRequirement::AllowDeath,
)?;
let msg = <T as Trait>::UpwardMessage::transfer(dest.clone(), amount.clone());
<T as Trait>::UpwardMessageSender::send_upward_message(&msg, UpwardMessageOrigin::Signed)
.expect("Should not fail; qed");
Self::deposit_event(Event::<T>::TransferredTokensToRelayChain(dest, amount));
}
/// Transfer `amount` of tokens to another parachain.
#[weight = 10]
fn transfer_tokens_to_parachain_chain(
origin,
para_id: u32,
dest: T::AccountId,
amount: BalanceOf<T>,
) {
//TODO we don't make sure that the parachain has some tokens on the other parachain.
let who = ensure_signed(origin)?;
let _ = T::Currency::withdraw(
&who,
amount,
WithdrawReason::Transfer.into(),
ExistenceRequirement::AllowDeath,
)?;
T::XCMPMessageSender::send_xcmp_message(
para_id.into(),
&XCMPMessage::TransferToken(dest, amount),
).expect("Should not fail; qed");
}
fn deposit_event() = default;
}
}
/// This is a hack to convert from one generic type to another where we are sure that both are the
/// same type/use the same encoding.
fn convert_hack<O: Decode>(input: &impl Encode) -> O {
input.using_encoded(|e| Decode::decode(&mut &e[..]).expect("Must be compatible; qed"))
}
impl<T: Trait> DownwardMessageHandler for Module<T> {
fn handle_downward_message(msg: &DownwardMessage) {
match msg {
DownwardMessage::TransferInto(dest, amount, _) => {
let dest = convert_hack(&dest);
let amount: BalanceOf<T> = convert_hack(amount);
let _ = T::Currency::deposit_creating(&dest, amount.clone());
Self::deposit_event(Event::<T>::TransferredTokensFromRelayChain(dest, amount));
}
_ => {}
}
}
}
impl<T: Trait> XCMPMessageHandler<XCMPMessage<T::AccountId, BalanceOf<T>>> for Module<T> {
fn handle_xcmp_message(src: ParaId, msg: &XCMPMessage<T::AccountId, BalanceOf<T>>) {
match msg {
XCMPMessage::TransferToken(dest, amount) => {
let para_account = src.clone().into_account();
let res = T::Currency::transfer(
&para_account,
dest,
amount.clone(),
ExistenceRequirement::AllowDeath,
);
Self::deposit_event(Event::<T>::TransferredTokensViaXCMP(
src,
dest.clone(),
amount.clone(),
res,
));
}
}
}
}
+34 -37
View File
@@ -14,11 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
use cumulus_primitives::ParaId;
use parachain_runtime::{
AccountId, BalancesConfig, GenesisConfig, IndicesConfig, SudoConfig, SystemConfig, WASM_BINARY,
AccountId, BalancesConfig, GenesisConfig, Signature, SudoConfig, SystemConfig,
TokenDealerConfig, WASM_BINARY,
};
use sc_service;
use sp_core::{Pair, Public};
use sc_service::ChainType;
use sp_core::{sr25519, Pair, Public};
use sp_runtime::traits::{IdentifyAccount, Verify};
/// Specialized `ChainSpec`. This is a specialization of the general Substrate ChainSpec type.
pub type ChainSpec = sc_service::GenericChainSpec<GenesisConfig>;
@@ -30,42 +33,39 @@ pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Pu
.public()
}
/// Helper function to generate stash, controller and session key from seed
pub fn get_authority_keys_from_seed(seed: &str) -> (AccountId, AccountId) {
(
get_from_seed::<AccountId>(&format!("{}//stash", seed)),
get_from_seed::<AccountId>(seed),
)
type AccountPublic = <Signature as Verify>::Signer;
/// Helper function to generate an account ID from seed
pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId
where
AccountPublic: From<<TPublic::Pair as Pair>::Public>,
{
AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
}
/// Returns the chain spec.
pub fn get_chain_spec() -> ChainSpec {
pub fn get_chain_spec(id: ParaId) -> ChainSpec {
ChainSpec::from_genesis(
"Local Testnet",
"parachain_local_testnet",
sc_service::ChainType::Local,
|| {
"local_testnet",
ChainType::Local,
move || {
testnet_genesis(
get_account_id_from_seed::<sr25519::Public>("Alice"),
vec![
get_authority_keys_from_seed("Alice"),
get_authority_keys_from_seed("Bob"),
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_account_id_from_seed::<sr25519::Public>("Charlie"),
get_account_id_from_seed::<sr25519::Public>("Dave"),
get_account_id_from_seed::<sr25519::Public>("Eve"),
get_account_id_from_seed::<sr25519::Public>("Ferdie"),
get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),
get_account_id_from_seed::<sr25519::Public>("Dave//stash"),
get_account_id_from_seed::<sr25519::Public>("Eve//stash"),
get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),
],
get_from_seed::<AccountId>("Alice"),
vec![
get_from_seed::<AccountId>("Alice"),
get_from_seed::<AccountId>("Bob"),
get_from_seed::<AccountId>("Charlie"),
get_from_seed::<AccountId>("Dave"),
get_from_seed::<AccountId>("Eve"),
get_from_seed::<AccountId>("Ferdie"),
get_from_seed::<AccountId>("Alice//stash"),
get_from_seed::<AccountId>("Bob//stash"),
get_from_seed::<AccountId>("Charlie//stash"),
get_from_seed::<AccountId>("Dave//stash"),
get_from_seed::<AccountId>("Eve//stash"),
get_from_seed::<AccountId>("Ferdie//stash"),
],
true,
id,
)
},
vec![],
@@ -77,19 +77,15 @@ pub fn get_chain_spec() -> ChainSpec {
}
fn testnet_genesis(
_initial_authorities: Vec<(AccountId, AccountId)>,
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
_enable_println: bool,
id: ParaId,
) -> GenesisConfig {
GenesisConfig {
frame_system: Some(SystemConfig {
code: WASM_BINARY.to_vec(),
changes_trie_config: Default::default(),
}),
pallet_indices: Some(IndicesConfig {
indices: vec![],
}),
pallet_balances: Some(BalancesConfig {
balances: endowed_accounts
.iter()
@@ -98,5 +94,6 @@ fn testnet_genesis(
.collect(),
}),
pallet_sudo: Some(SudoConfig { key: root_key }),
message_example: Some(TokenDealerConfig { parachain_id: id }),
}
}
+23 -1
View File
@@ -42,6 +42,28 @@ pub struct ExportGenesisStateCommand {
/// Output file name or stdout if unspecified.
#[structopt(parse(from_os_str))]
pub output: Option<PathBuf>,
/// Id of the parachain this state is for.
#[structopt(long, default_value = "100")]
pub parachain_id: u32,
}
#[derive(Debug, StructOpt)]
pub struct RunCmd {
#[structopt(flatten)]
pub base: sc_cli::RunCmd,
/// Id of the parachain this collator collates for.
#[structopt(long, default_value = "100")]
pub parachain_id: u32,
}
impl std::ops::Deref for RunCmd {
type Target = sc_cli::RunCmd;
fn deref(&self) -> &Self::Target {
&self.base
}
}
#[derive(Debug, StructOpt)]
@@ -55,7 +77,7 @@ pub struct Cli {
pub subcommand: Option<Subcommand>,
#[structopt(flatten)]
pub run: sc_cli::RunCmd,
pub run: RunCmd,
/// Relaychain arguments
#[structopt(raw = true)]
+37 -25
View File
@@ -19,6 +19,7 @@ use crate::cli::{Cli, PolkadotCli, Subcommand};
use codec::Encode;
use log::info;
use parachain_runtime::Block;
use polkadot_parachain::primitives::AccountIdConversion;
use sc_cli::{
CliConfiguration, Error, ImportParams, KeystoreParams, NetworkParams, Result, SharedParams,
SubstrateCli,
@@ -30,8 +31,8 @@ use sp_runtime::{
traits::{Block as BlockT, Hash as HashT, Header as HeaderT, Zero},
BuildStorage,
};
use std::net::SocketAddr;
use std::sync::Arc;
use std::{net::SocketAddr, sync::Arc};
use cumulus_primitives::ParaId;
impl SubstrateCli for Cli {
fn impl_name() -> &'static str {
@@ -66,7 +67,8 @@ impl SubstrateCli for Cli {
}
fn load_spec(&self, _id: &str) -> std::result::Result<Box<dyn sc_service::ChainSpec>, String> {
Ok(Box::new(chain_spec::get_chain_spec()))
// Such a hack :(
Ok(Box::new(chain_spec::get_chain_spec(self.run.parachain_id.into())))
}
}
@@ -114,23 +116,21 @@ impl SubstrateCli for PolkadotCli {
}
}
fn generate_genesis_state() -> Result<Block> {
let storage = (&chain_spec::get_chain_spec()).build_storage()?;
fn generate_genesis_state(para_id: ParaId) -> Result<Block> {
let storage = (&chain_spec::get_chain_spec(para_id)).build_storage()?;
let child_roots = storage.children_default.iter().map(|(sk, child_content)| {
let state_root =
<<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
child_content.data.clone().into_iter().collect(),
);
let state_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
child_content.data.clone().into_iter().collect(),
);
(sk.clone(), state_root.encode())
});
let state_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
storage.top.clone().into_iter().chain(child_roots).collect(),
);
let extrinsics_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
Vec::new(),
);
let extrinsics_root =
<<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(Vec::new());
Ok(Block::new(
<<Block as BlockT>::Header as HeaderT>::new(
@@ -157,7 +157,7 @@ pub fn run() -> Result<()> {
Some(Subcommand::ExportGenesisState(params)) => {
sc_cli::init_logger("");
let block = generate_genesis_state()?;
let block = generate_genesis_state(params.parachain_id.into())?;
let header_hex = format!("0x{:?}", HexDisplay::from(&block.header().encode()));
if let Some(output) = &params.output {
@@ -174,13 +174,14 @@ pub fn run() -> Result<()> {
let grandpa_pause = if polkadot_cli.run.grandpa_pause.is_empty() {
None
} else {
Some((polkadot_cli.run.grandpa_pause[0], polkadot_cli.run.grandpa_pause[1]))
Some((
polkadot_cli.run.grandpa_pause[0],
polkadot_cli.run.grandpa_pause[1],
))
};
runner.run_node(
|config| {
polkadot_service::polkadot_new_light(config)
},
|config| polkadot_service::polkadot_new_light(config),
|config| {
polkadot_service::polkadot_new_full(
config,
@@ -190,22 +191,23 @@ pub fn run() -> Result<()> {
6000,
grandpa_pause,
None,
).map(|(s, _, _)| s)
)
.map(|(s, _, _)| s)
},
polkadot_service::PolkadotExecutor::native_version().runtime_version
polkadot_service::PolkadotExecutor::native_version().runtime_version,
)
},
}
Some(Subcommand::PolkadotValidationWorker(cmd)) => {
sc_cli::init_logger("");
polkadot_service::run_validation_worker(&cmd.mem_id)?;
Ok(())
},
}
None => {
let runner = cli.create_runner(&cli.run)?;
let runner = cli.create_runner(&*cli.run)?;
// TODO
let key = Arc::new(sp_core::Pair::from_seed(&[10; 32]));
let key = Arc::new(sp_core::Pair::generate().0);
let mut polkadot_cli = PolkadotCli::from_iter(
[PolkadotCli::executable_name().to_string()]
@@ -213,6 +215,14 @@ pub fn run() -> Result<()> {
.chain(cli.relaychain_args.iter()),
);
let id = ParaId::from(cli.run.parachain_id);
let parachain_account =
AccountIdConversion::<polkadot_primitives::AccountId>::into_account(&id);
let block = generate_genesis_state(id)?;
let genesis_state = format!("0x{:?}", HexDisplay::from(&block.header().encode()));
runner.run_full_node(
|config| {
polkadot_cli.base_path =
@@ -226,9 +236,11 @@ pub fn run() -> Result<()> {
)
.unwrap();
info!("Parachain id: {:?}", crate::PARA_ID);
info!("Parachain id: {:?}", id);
info!("Parachain Account: {}", parachain_account);
info!("Parachain genesis state: {}", genesis_state);
crate::service::run_collator(config, key, polkadot_config)
crate::service::run_collator(config, key, polkadot_config, id)
},
parachain_runtime::VERSION,
)
-5
View File
@@ -19,17 +19,12 @@
#![warn(missing_docs)]
#![warn(unused_extern_crates)]
use polkadot_primitives::parachain::Id as ParaId;
mod chain_spec;
#[macro_use]
mod service;
mod cli;
mod command;
/// The parachain id of this parachain.
pub const PARA_ID: ParaId = ParaId::new(100);
fn main() -> sc_cli::Result<()> {
command::run()
}
+21 -29
View File
@@ -15,15 +15,17 @@
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
use ansi_term::Color;
use std::sync::Arc;
use sc_executor::native_executor_instance;
use sc_service::{AbstractService, Configuration};
use sc_finality_grandpa::{FinalityProofProvider as GrandpaFinalityProofProvider, StorageAndProofProvider};
use polkadot_primitives::parachain::CollatorPair;
use cumulus_collator::{CollatorBuilder, prepare_collator_config};
use futures::FutureExt;
pub use sc_executor::NativeExecutor;
use cumulus_collator::{prepare_collator_config, CollatorBuilder};
use cumulus_network::DelayedBlockAnnounceValidator;
use futures::FutureExt;
use polkadot_primitives::parachain::CollatorPair;
use sc_executor::native_executor_instance;
pub use sc_executor::NativeExecutor;
use sc_finality_grandpa::{
FinalityProofProvider as GrandpaFinalityProofProvider, StorageAndProofProvider,
};
use sc_service::{AbstractService, Configuration};
use std::sync::Arc;
// Our native executor instance.
native_executor_instance!(
@@ -47,9 +49,8 @@ macro_rules! new_full_start {
>($config)?
.with_select_chain(|_config, backend| Ok(sc_consensus::LongestChain::new(backend.clone())))?
.with_transaction_pool(|builder| {
let pool_api = Arc::new(sc_transaction_pool::FullChainApi::new(
builder.client().clone(),
));
let client = builder.client();
let pool_api = Arc::new(sc_transaction_pool::FullChainApi::new(client.clone()));
let pool = sc_transaction_pool::BasicPool::new(
builder.config().transaction_pool.clone(),
pool_api,
@@ -57,19 +58,12 @@ macro_rules! new_full_start {
);
Ok(pool)
})?
.with_import_queue(|
_config,
client,
_,
_,
spawn_task_handle,
registry,
| {
.with_import_queue(|_config, client, _, _, spawner, registry| {
let import_queue = cumulus_consensus::import_queue::import_queue(
client.clone(),
client,
inherent_data_providers.clone(),
spawn_task_handle,
spawner,
registry,
)?;
@@ -87,6 +81,7 @@ pub fn run_collator(
parachain_config: Configuration,
key: Arc<CollatorPair>,
polkadot_config: polkadot_collator::Configuration,
id: polkadot_primitives::parachain::Id,
) -> sc_service::error::Result<impl AbstractService> {
let parachain_config = prepare_collator_config(parachain_config);
@@ -104,9 +99,7 @@ pub fn run_collator(
let provider = client as Arc<dyn StorageAndProofProvider<_, _>>;
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, provider)) as _)
})?
.with_block_announce_validator(|_client| {
Box::new(block_announce_validator_copy)
})?
.with_block_announce_validator(|_client| Box::new(block_announce_validator_copy))?
.build_full()?;
let registry = service.prometheus_registry();
@@ -120,14 +113,12 @@ pub fn run_collator(
let block_import = service.client();
let client = service.client();
let network = service.network();
let announce_block = Arc::new(move |hash, data| {
network.announce_block(hash, data)
});
let announce_block = Arc::new(move |hash, data| network.announce_block(hash, data));
let builder = CollatorBuilder::new(
proposer_factory,
inherent_data_providers,
block_import,
crate::PARA_ID,
id,
client,
announce_block,
block_announce_validator,
@@ -135,11 +126,12 @@ pub fn run_collator(
let polkadot_future = polkadot_collator::start_collator(
builder,
crate::PARA_ID,
id,
key,
polkadot_config,
Some(format!("[{}] ", Color::Blue.bold().paint("Relaychain"))),
).map(|_| ());
)
.map(|_| ());
service.spawn_essential_task("polkadot", polkadot_future);
Ok(service)
@@ -48,6 +48,6 @@ fn purge_chain_works() {
assert!(status.success());
// Make sure that the `parachain_local_testnet` chain folder exists, but the `db` is deleted.
assert!(PathBuf::from(base_path).join("chains/parachain_local_testnet/").exists());
assert!(!PathBuf::from(base_path).join("chains/parachain_local_testnet/db").exists());
assert!(PathBuf::from(base_path).join("chains/local_testnet/").exists());
assert!(!PathBuf::from(base_path).join("chains/local_testnet/db").exists());
}
+1 -1
View File
@@ -7,7 +7,7 @@ build = "build.rs"
[dependencies]
runtime = { package = "cumulus-runtime", path = "../../runtime", default-features = false }
substrate-test-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "cumulus-branch", version = "2.0.0-rc2" }
substrate-test-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "cumulus-branch" }
[build-dependencies]
wasm-builder-runner = { package = "substrate-wasm-builder-runner", version = " 1.0.6" }
+39
View File
@@ -0,0 +1,39 @@
[package]
name = "cumulus-upward-message"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
# Substrate dependencies
sp-std = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
# Polkadot deps
polkadot-core-primitives = { git = "https://github.com/paritytech/polkadot", branch = "cumulus-branch", default-features = false }
polkadot-parachain = { git = "https://github.com/paritytech/polkadot", branch = "cumulus-branch", default-features = false }
# All these should be optional dependenices, but given the perfect Cargo, it is not possible.
polkadot-runtime = { git = "https://github.com/paritytech/polkadot", branch = "cumulus-branch", default-features = false }
kusama-runtime = { git = "https://github.com/paritytech/polkadot", branch = "cumulus-branch", default-features = false }
westend-runtime = { git = "https://github.com/paritytech/polkadot", branch = "cumulus-branch", default-features = false }
[features]
default = [ "std" ]
std = [
"sp-std/std",
"polkadot-runtime/std",
"kusama-runtime/std",
"westend-runtime/std",
"polkadot-core-primitives/std",
"polkadot-parachain/std",
]
# Should be enabled by when compiling the runtime for WASM.
#
# This disables the runtime api of the Polkadot/Kusama/Westend
# runtimes to not clash with the runtime api of the Parachain when building
# for WASM.
runtime-wasm = [
"polkadot-runtime/disable-runtime-api",
"westend-runtime/disable-runtime-api",
"kusama-runtime/disable-runtime-api",
]
+37
View File
@@ -0,0 +1,37 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Cumulus.
// Substrate 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.
// Substrate 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 Cumulus. If not, see <http://www.gnu.org/licenses/>.
//! Kusama upward message
use crate::*;
use polkadot_core_primitives::{Balance, AccountId};
use kusama_runtime::{BalancesCall, ParachainsCall};
use sp_std::vec::Vec;
/// The Kusama upward message.
pub type UpwardMessage = kusama_runtime::Call;
impl BalancesMessage<AccountId, Balance> for UpwardMessage {
fn transfer(dest: AccountId, amount: Balance) -> Self {
BalancesCall::transfer(dest, amount).into()
}
}
impl XCMPMessage for UpwardMessage {
fn send_message(dest: ParaId, msg: Vec<u8>) -> Self {
ParachainsCall::send_xcmp_message(dest, msg).into()
}
}
+47
View File
@@ -0,0 +1,47 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Cumulus.
// Substrate 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.
// Substrate 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 Cumulus. If not, see <http://www.gnu.org/licenses/>.
//! Upward messages types and traits for Polkadot, Kusama and Westend.
//!
//! As Cumulus needs to suits multiple Polkadot-like runtimes the upward message
//! type is different for each of them. To support all of them, Cumulus provides
//! traits to write upward message generic code.
#![cfg_attr(not(feature = "std"), no_std)]
use polkadot_parachain::primitives::Id as ParaId;
use sp_std::vec::Vec;
mod polkadot;
mod kusama;
mod westend;
pub use polkadot::UpwardMessage as PolkadotUpwardMessage;
pub use kusama::UpwardMessage as KusamaUpwardMessage;
pub use westend::UpwardMessage as WestendUpwardMessage;
/// A `Balances` related upward message.
pub trait BalancesMessage<AccountId, Balance>: Sized {
/// Transfer the given `amount` from the parachain account to the given
/// `dest` account.
fn transfer(dest: AccountId, amount: Balance) -> Self;
}
/// A `XCMP` related upward message.
pub trait XCMPMessage: Sized {
/// Send the given XCMP message to given parachain.
fn send_message(dest: ParaId, msg: Vec<u8>) -> Self;
}
+37
View File
@@ -0,0 +1,37 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Cumulus.
// Substrate 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.
// Substrate 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 Cumulus. If not, see <http://www.gnu.org/licenses/>.
//! Polkadot upward message
use crate::*;
use polkadot_core_primitives::{Balance, AccountId};
use polkadot_runtime::{BalancesCall, ParachainsCall};
use sp_std::vec::Vec;
/// The Polkadot upward message.
pub type UpwardMessage = polkadot_runtime::Call;
impl BalancesMessage<AccountId, Balance> for UpwardMessage {
fn transfer(dest: AccountId, amount: Balance) -> Self {
BalancesCall::transfer(dest, amount).into()
}
}
impl XCMPMessage for UpwardMessage {
fn send_message(dest: ParaId, msg: Vec<u8>) -> Self {
ParachainsCall::send_xcmp_message(dest, msg).into()
}
}
+37
View File
@@ -0,0 +1,37 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Cumulus.
// Substrate 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.
// Substrate 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 Cumulus. If not, see <http://www.gnu.org/licenses/>.
//! Westend upward message
use crate::*;
use polkadot_core_primitives::{Balance, AccountId};
use westend_runtime::{BalancesCall, ParachainsCall};
use sp_std::vec::Vec;
/// The Westend upward message.
pub type UpwardMessage = westend_runtime::Call;
impl BalancesMessage<AccountId, Balance> for UpwardMessage {
fn transfer(dest: AccountId, amount: Balance) -> Self {
BalancesCall::transfer(dest, amount).into()
}
}
impl XCMPMessage for UpwardMessage {
fn send_message(dest: ParaId, msg: Vec<u8>) -> Self {
ParachainsCall::send_xcmp_message(dest, msg).into()
}
}