dir restructure to support more CGP (#1266)

* rerame res to chain-specs

* split polkadot-parachains dir

* rename dir parachains-common to common

Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>
Co-authored-by: Wilfried Kopp <wilfried@parity.io>
Co-authored-by: Chevdor <chevdor@users.noreply.github.com>
This commit is contained in:
Squirrel
2022-05-20 13:43:04 +01:00
committed by GitHub
parent 9d88e462f0
commit bc0c98884f
138 changed files with 197 additions and 151 deletions
@@ -0,0 +1,33 @@
[package]
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
name = "cumulus-ping"
version = "0.1.0"
[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
serde = { version = "1.0.137", optional = true, features = ["derive"] }
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
xcm = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
cumulus-primitives-core = { path = "../../../primitives/core", default-features = false }
cumulus-pallet-xcm = { path = "../../../pallets/xcm", default-features = false }
[features]
default = ["std"]
std = [
"codec/std",
"scale-info/std",
"serde",
"cumulus-primitives-core/std",
"sp-std/std",
"sp-runtime/std",
"frame-support/std",
"frame-system/std",
]
+206
View File
@@ -0,0 +1,206 @@
// Copyright 2020-2021 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/>.
//! Pallet to spam the XCM/UMP.
#![cfg_attr(not(feature = "std"), no_std)]
use cumulus_pallet_xcm::{ensure_sibling_para, Origin as CumulusOrigin};
use cumulus_primitives_core::ParaId;
use frame_system::Config as SystemConfig;
use sp_runtime::traits::Saturating;
use sp_std::prelude::*;
use xcm::latest::prelude::*;
pub use pallet::*;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);
/// The module configuration trait.
#[pallet::config]
pub trait Config: frame_system::Config {
/// The overarching event type.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
type Origin: From<<Self as SystemConfig>::Origin>
+ Into<Result<CumulusOrigin, <Self as Config>::Origin>>;
/// The overarching call type; we assume sibling chains use the same type.
type Call: From<Call<Self>> + Encode;
type XcmSender: SendXcm;
}
/// The target parachains to ping.
#[pallet::storage]
pub(super) type Targets<T: Config> = StorageValue<_, Vec<(ParaId, Vec<u8>)>, ValueQuery>;
/// The total number of pings sent.
#[pallet::storage]
pub(super) type PingCount<T: Config> = StorageValue<_, u32, ValueQuery>;
/// The sent pings.
#[pallet::storage]
pub(super) type Pings<T: Config> =
StorageMap<_, Blake2_128Concat, u32, T::BlockNumber, OptionQuery>;
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
PingSent(ParaId, u32, Vec<u8>),
Pinged(ParaId, u32, Vec<u8>),
PongSent(ParaId, u32, Vec<u8>),
Ponged(ParaId, u32, Vec<u8>, T::BlockNumber),
ErrorSendingPing(SendError, ParaId, u32, Vec<u8>),
ErrorSendingPong(SendError, ParaId, u32, Vec<u8>),
UnknownPong(ParaId, u32, Vec<u8>),
}
#[pallet::error]
pub enum Error<T> {}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_finalize(n: T::BlockNumber) {
for (para, payload) in Targets::<T>::get().into_iter() {
let seq = PingCount::<T>::mutate(|seq| {
*seq += 1;
*seq
});
match T::XcmSender::send_xcm(
(1, Junction::Parachain(para.into())),
Xcm(vec![Transact {
origin_type: OriginKind::Native,
require_weight_at_most: 1_000,
call: <T as Config>::Call::from(Call::<T>::ping {
seq,
payload: payload.clone(),
})
.encode()
.into(),
}]),
) {
Ok(()) => {
Pings::<T>::insert(seq, n);
Self::deposit_event(Event::PingSent(para, seq, payload));
},
Err(e) => {
Self::deposit_event(Event::ErrorSendingPing(e, para, seq, payload));
},
}
}
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(0)]
pub fn start(origin: OriginFor<T>, para: ParaId, payload: Vec<u8>) -> DispatchResult {
ensure_root(origin)?;
Targets::<T>::mutate(|t| t.push((para, payload)));
Ok(())
}
#[pallet::weight(0)]
pub fn start_many(
origin: OriginFor<T>,
para: ParaId,
count: u32,
payload: Vec<u8>,
) -> DispatchResult {
ensure_root(origin)?;
for _ in 0..count {
Targets::<T>::mutate(|t| t.push((para, payload.clone())));
}
Ok(())
}
#[pallet::weight(0)]
pub fn stop(origin: OriginFor<T>, para: ParaId) -> DispatchResult {
ensure_root(origin)?;
Targets::<T>::mutate(|t| {
if let Some(p) = t.iter().position(|(p, _)| p == &para) {
t.swap_remove(p);
}
});
Ok(())
}
#[pallet::weight(0)]
pub fn stop_all(origin: OriginFor<T>, maybe_para: Option<ParaId>) -> DispatchResult {
ensure_root(origin)?;
if let Some(para) = maybe_para {
Targets::<T>::mutate(|t| t.retain(|&(x, _)| x != para));
} else {
Targets::<T>::kill();
}
Ok(())
}
#[pallet::weight(0)]
pub fn ping(origin: OriginFor<T>, seq: u32, payload: Vec<u8>) -> DispatchResult {
// Only accept pings from other chains.
let para = ensure_sibling_para(<T as Config>::Origin::from(origin))?;
Self::deposit_event(Event::Pinged(para, seq, payload.clone()));
match T::XcmSender::send_xcm(
(1, Junction::Parachain(para.into())),
Xcm(vec![Transact {
origin_type: OriginKind::Native,
require_weight_at_most: 1_000,
call: <T as Config>::Call::from(Call::<T>::pong {
seq,
payload: payload.clone(),
})
.encode()
.into(),
}]),
) {
Ok(()) => Self::deposit_event(Event::PongSent(para, seq, payload)),
Err(e) => Self::deposit_event(Event::ErrorSendingPong(e, para, seq, payload)),
}
Ok(())
}
#[pallet::weight(0)]
pub fn pong(origin: OriginFor<T>, seq: u32, payload: Vec<u8>) -> DispatchResult {
// Only accept pings from other chains.
let para = ensure_sibling_para(<T as Config>::Origin::from(origin))?;
if let Some(sent_at) = Pings::<T>::take(seq) {
Self::deposit_event(Event::Ponged(
para,
seq,
payload,
frame_system::Pallet::<T>::block_number().saturating_sub(sent_at),
));
} else {
// Pong received for a ping we apparently didn't send?!
Self::deposit_event(Event::UnknownPong(para, seq, payload));
}
Ok(())
}
}
}