Separate ParachainId injection to its own pallet (#183)

* Separate paraid injection to own pallet

* Move token dealer to a crate

* Move to rococo-parachains

* Remove parameter_types hack

* Fix chainspec

* fix build

* remove commented code

* Update contracts runtime to match other runtime

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Alphebetize workspace members

* Parachain info to own crate

* prune system = frame_system

Co-authored-by: Ricardo Rius <ricardo@parity.io>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Joshy Orndorff
2020-08-07 15:52:15 -04:00
committed by GitHub
parent 8a6e29eef9
commit d8aabf0c32
13 changed files with 165 additions and 224 deletions
@@ -8,6 +8,9 @@ edition = '2018'
serde = { version = "1.0.101", optional = true, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "1.3.0", default-features = false, features = ["derive"] }
cumulus-token-dealer = { path = "../pallets/token-dealer", default-features = false}
parachain-info = { path = "../pallets/parachain-info", default-features = false}
# Substrate dependencies
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "rococo-branch" }
sp-api = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "rococo-branch" }
@@ -75,12 +78,14 @@ std = [
"pallet-timestamp/std",
"pallet-sudo/std",
"pallet-transaction-payment/std",
"parachain-info/std",
"cumulus-runtime/std",
"cumulus-parachain-upgrade/std",
"cumulus-message-broker/std",
"cumulus-upward-message/std",
"cumulus-primitives/std",
"polkadot-parachain/std",
"cumulus-token-dealer/std",
]
# Will be enabled by the `wasm-builder` when building the runtime for WASM.
runtime-wasm = [
@@ -36,8 +36,6 @@ use sp_std::prelude::*;
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,
@@ -244,20 +242,16 @@ 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::RococoUpwardMessage;
type ParachainId = ParachainId;
type XCMPMessage = XCMPMessage<AccountId, Balance>;
type ParachainId = ParachainInfo;
type XCMPMessage = cumulus_token_dealer::XCMPMessage<AccountId, Balance>;
type XCMPMessageHandlers = TokenDealer;
}
impl message_example::Trait for Runtime {
impl cumulus_token_dealer::Trait for Runtime {
type Event = Event;
type UpwardMessageSender = MessageBroker;
type UpwardMessage = cumulus_upward_message::RococoUpwardMessage;
@@ -265,6 +259,8 @@ impl message_example::Trait for Runtime {
type XCMPMessageSender = MessageBroker;
}
impl parachain_info::Trait for Runtime {}
// We disable the rent system for easier testing.
parameter_types! {
pub const TombstoneDeposit: Balance = 0;
@@ -307,8 +303,9 @@ construct_runtime! {
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},
TokenDealer: cumulus_token_dealer::{Module, Call, Event<T>},
TransactionPayment: pallet_transaction_payment::{Module, Storage},
ParachainInfo: parachain_info::{Module, Storage, Config},
}
}
@@ -0,0 +1,24 @@
[package]
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
name = "parachain-info"
version = "0.1.0"
[dependencies]
codec = { package = "parity-scale-codec", version = "1.3.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.101", optional = true, features = ["derive"] }
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "rococo-branch" }
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "rococo-branch" }
cumulus-primitives = { path = "../../../primitives", default-features = false }
[features]
default = ["std"]
std = [
"codec/std",
"serde",
"cumulus-primitives/std",
"frame-support/std",
"frame-system/std",
]
@@ -0,0 +1,42 @@
// 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/>.
//! Minimal Pallet that injects a ParachainId into Runtime storage from
#![cfg_attr(not(feature = "std"), no_std)]
use frame_support::{decl_module, decl_storage, traits::Get};
use cumulus_primitives::ParaId;
/// Configuration trait of this pallet.
pub trait Trait: frame_system::Trait {}
impl <T: Trait> Get<ParaId> for Module<T> {
fn get() -> ParaId {
Self::parachain_id()
}
}
decl_storage! {
trait Store for Module<T: Trait> as ParachainUpgrade {
ParachainId get(fn parachain_id) config(): ParaId = 100.into();
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
}
@@ -0,0 +1,29 @@
[package]
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
name = "cumulus-token-dealer"
version = "0.1.0"
[dependencies]
codec = { package = "parity-scale-codec", version = "1.3.0", default-features = false, features = ["derive"] }
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "rococo-branch" }
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "rococo-branch" }
# Cumulus dependencies
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 = "rococo-branch", default-features = false }
[features]
default = ["std"]
std = [
"codec/std",
"cumulus-upward-message/std",
"cumulus-primitives/std",
"frame-support/std",
"frame-system/std",
"polkadot-parachain/std",
]
@@ -14,16 +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/>.
//! Example Pallet that shows how to send upward messages and how to receive
//! downward messages.
#![cfg_attr(not(feature = "std"), no_std)]
use frame_support::{
decl_event, decl_module, decl_storage,
decl_event, decl_module, dispatch::DispatchResult,
traits::{Currency, ExistenceRequirement, WithdrawReason},
};
use frame_system::ensure_signed;
use crate::XCMPMessage;
use codec::{Decode, Encode};
use cumulus_primitives::{
relay_chain::DownwardMessage,
@@ -32,7 +30,12 @@ use cumulus_primitives::{
};
use cumulus_upward_message::BalancesMessage;
use polkadot_parachain::primitives::AccountIdConversion;
use sp_runtime::DispatchResult;
#[derive(Encode, Decode)]
pub enum XCMPMessage<XAccountId, XBalance> {
/// Transfer tokens to the given account from the Parachain account.
TransferToken(XAccountId, XBalance),
}
type BalanceOf<T> =
<<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
@@ -55,19 +58,6 @@ pub trait Trait: frame_system::Trait {
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,
@@ -83,7 +73,7 @@ decl_event! {
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system = frame_system {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
/// Transfer `amount` of tokens on the relay chain from the Parachain account to
/// the given `dest` account.
#[weight = 10]
+5 -1
View File
@@ -8,6 +8,9 @@ edition = '2018'
serde = { version = "1.0.101", optional = true, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "1.3.0", default-features = false, features = ["derive"] }
cumulus-token-dealer = { path = "../pallets/token-dealer", default-features = false}
parachain-info = { path = "../pallets/parachain-info", default-features = false}
# Substrate dependencies
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "rococo-branch" }
sp-api = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "rococo-branch" }
@@ -67,12 +70,13 @@ std = [
"pallet-timestamp/std",
"pallet-sudo/std",
"pallet-transaction-payment/std",
"parachain-info/std",
"cumulus-runtime/std",
"cumulus-parachain-upgrade/std",
"cumulus-message-broker/std",
"cumulus-upward-message/std",
"cumulus-primitives/std",
"polkadot-parachain/std",
"cumulus-token-dealer/std",
]
# Will be enabled by the `wasm-builder` when building the runtime for WASM.
runtime-wasm = [
+9 -9
View File
@@ -35,7 +35,8 @@ use sp_std::prelude::*;
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
mod message_example;
/// Import the token dealer pallet.
pub use cumulus_token_dealer;
// A few exports that help ease life for downstream crates.
pub use frame_support::{
@@ -243,20 +244,16 @@ 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::RococoUpwardMessage;
type ParachainId = ParachainId;
type XCMPMessage = XCMPMessage<AccountId, Balance>;
type ParachainId = ParachainInfo;
type XCMPMessage = cumulus_token_dealer::XCMPMessage<AccountId, Balance>;
type XCMPMessageHandlers = TokenDealer;
}
impl message_example::Trait for Runtime {
impl cumulus_token_dealer::Trait for Runtime {
type Event = Event;
type UpwardMessageSender = MessageBroker;
type UpwardMessage = cumulus_upward_message::RococoUpwardMessage;
@@ -264,6 +261,8 @@ impl message_example::Trait for Runtime {
type XCMPMessageSender = MessageBroker;
}
impl parachain_info::Trait for Runtime {}
construct_runtime! {
pub enum Runtime where
Block = Block,
@@ -277,8 +276,9 @@ construct_runtime! {
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},
TokenDealer: cumulus_token_dealer::{Module, Call, Event<T>},
TransactionPayment: pallet_transaction_payment::{Module, Storage},
ParachainInfo: parachain_info::{Module, Storage, Config},
}
}
@@ -1,179 +0,0 @@
// 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,
));
}
}
}
}
+2 -2
View File
@@ -17,7 +17,7 @@
use cumulus_primitives::ParaId;
use parachain_runtime::{
AccountId, BalancesConfig, GenesisConfig, Signature, SudoConfig, SystemConfig,
TokenDealerConfig, WASM_BINARY,
ParachainInfoConfig, WASM_BINARY,
};
use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup};
use sc_service::ChainType;
@@ -140,7 +140,7 @@ fn testnet_genesis(
.collect(),
}),
pallet_sudo: Some(SudoConfig { key: root_key }),
message_example: Some(TokenDealerConfig { parachain_id: id }),
parachain_info: Some(ParachainInfoConfig { parachain_id: id }),
// TODO: add contracts genesis for the contracts runtime
// pallet_contracts: Some(parachain_runtime::ContractsConfig { current_schedule: Default::default() }),
}