Refactoring Checkpoint: (WIP)
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
[package]
|
||||
authors.workspace = true
|
||||
edition.workspace = true
|
||||
name = "pezcumulus-ping"
|
||||
version = "0.7.0"
|
||||
license = "Apache-2.0"
|
||||
description = "Ping Pallet for Pezcumulus XCM/UMP testing."
|
||||
homepage.workspace = true
|
||||
repository.workspace = true
|
||||
documentation = "https://docs.rs/pezcumulus-ping"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
codec = { features = ["derive"], workspace = true }
|
||||
scale-info = { features = ["derive"], workspace = true }
|
||||
|
||||
pezframe-support = { workspace = true }
|
||||
pezframe-system = { workspace = true }
|
||||
pezsp-runtime = { workspace = true }
|
||||
|
||||
xcm = { workspace = true }
|
||||
|
||||
pezcumulus-pezpallet-xcm = { workspace = true }
|
||||
pezcumulus-primitives-core = { workspace = true }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"codec/std",
|
||||
"pezcumulus-pezpallet-xcm/std",
|
||||
"pezcumulus-primitives-core/std",
|
||||
"pezframe-support/std",
|
||||
"pezframe-system/std",
|
||||
"scale-info/std",
|
||||
"pezsp-runtime/std",
|
||||
"xcm/std",
|
||||
]
|
||||
try-runtime = [
|
||||
"pezcumulus-pezpallet-xcm/try-runtime",
|
||||
"pezframe-support/try-runtime",
|
||||
"pezframe-system/try-runtime",
|
||||
"pezsp-runtime/try-runtime",
|
||||
]
|
||||
runtime-benchmarks = [
|
||||
"pezcumulus-pezpallet-xcm/runtime-benchmarks",
|
||||
"pezcumulus-primitives-core/runtime-benchmarks",
|
||||
"pezframe-support/runtime-benchmarks",
|
||||
"pezframe-system/runtime-benchmarks",
|
||||
"pezsp-runtime/runtime-benchmarks",
|
||||
"xcm/runtime-benchmarks",
|
||||
]
|
||||
@@ -0,0 +1,249 @@
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// This file is part of Pezcumulus.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Pallet to spam the XCM/UMP.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::{vec, vec::Vec};
|
||||
use pezcumulus_pezpallet_xcm::{ensure_sibling_para, Origin as CumulusOrigin};
|
||||
use pezcumulus_primitives_core::ParaId;
|
||||
use pezframe_support::{parameter_types, BoundedVec};
|
||||
use pezframe_system::Config as SystemConfig;
|
||||
use pezsp_runtime::traits::Saturating;
|
||||
use xcm::latest::prelude::*;
|
||||
|
||||
pub use pallet::*;
|
||||
|
||||
parameter_types! {
|
||||
const MaxTeyrchains: u32 = 100;
|
||||
const MaxPayloadSize: u32 = 1024;
|
||||
}
|
||||
|
||||
#[pezframe_support::pallet]
|
||||
pub mod pallet {
|
||||
use super::*;
|
||||
use pezframe_support::pezpallet_prelude::*;
|
||||
use pezframe_system::pezpallet_prelude::*;
|
||||
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(_);
|
||||
|
||||
/// The module configuration trait.
|
||||
#[pallet::config]
|
||||
pub trait Config: pezframe_system::Config {
|
||||
/// The overarching event type.
|
||||
#[allow(deprecated)]
|
||||
type RuntimeEvent: From<Event<Self>> + IsType<<Self as pezframe_system::Config>::RuntimeEvent>;
|
||||
|
||||
type RuntimeOrigin: From<<Self as SystemConfig>::RuntimeOrigin>
|
||||
+ Into<Result<CumulusOrigin, <Self as Config>::RuntimeOrigin>>;
|
||||
|
||||
/// The overarching call type; we assume sibling chains use the same type.
|
||||
type RuntimeCall: From<Call<Self>> + Encode;
|
||||
|
||||
type XcmSender: SendXcm;
|
||||
}
|
||||
|
||||
/// The target teyrchains to ping.
|
||||
#[pallet::storage]
|
||||
pub(super) type Targets<T: Config> = StorageValue<
|
||||
_,
|
||||
BoundedVec<(ParaId, BoundedVec<u8, MaxPayloadSize>), MaxTeyrchains>,
|
||||
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, BlockNumberFor<T>, OptionQuery>;
|
||||
|
||||
#[pallet::event]
|
||||
#[pallet::generate_deposit(pub(super) fn deposit_event)]
|
||||
pub enum Event<T: Config> {
|
||||
PingSent(ParaId, u32, Vec<u8>, XcmHash, Assets),
|
||||
Pinged(ParaId, u32, Vec<u8>),
|
||||
PongSent(ParaId, u32, Vec<u8>, XcmHash, Assets),
|
||||
Ponged(ParaId, u32, Vec<u8>, BlockNumberFor<T>),
|
||||
ErrorSendingPing(SendError, ParaId, u32, Vec<u8>),
|
||||
ErrorSendingPong(SendError, ParaId, u32, Vec<u8>),
|
||||
UnknownPong(ParaId, u32, Vec<u8>),
|
||||
}
|
||||
|
||||
#[pallet::error]
|
||||
pub enum Error<T> {
|
||||
/// Too many teyrchains have been added as a target.
|
||||
TooManyTargets,
|
||||
/// The payload provided is too large, limit is 1024 bytes.
|
||||
PayloadTooLarge,
|
||||
}
|
||||
|
||||
#[pallet::hooks]
|
||||
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
|
||||
fn on_finalize(n: BlockNumberFor<T>) {
|
||||
for (para, payload) in Targets::<T>::get().into_iter() {
|
||||
let seq = PingCount::<T>::mutate(|seq| {
|
||||
*seq += 1;
|
||||
*seq
|
||||
});
|
||||
match send_xcm::<T::XcmSender>(
|
||||
(Parent, Junction::Teyrchain(para.into())).into(),
|
||||
Xcm(vec![Transact {
|
||||
origin_kind: OriginKind::Native,
|
||||
call: <T as Config>::RuntimeCall::from(Call::<T>::ping {
|
||||
seq,
|
||||
payload: payload.clone().to_vec(),
|
||||
})
|
||||
.encode()
|
||||
.into(),
|
||||
fallback_max_weight: None,
|
||||
}]),
|
||||
) {
|
||||
Ok((hash, cost)) => {
|
||||
Pings::<T>::insert(seq, n);
|
||||
Self::deposit_event(Event::PingSent(
|
||||
para,
|
||||
seq,
|
||||
payload.to_vec(),
|
||||
hash,
|
||||
cost,
|
||||
));
|
||||
},
|
||||
Err(e) => {
|
||||
Self::deposit_event(Event::ErrorSendingPing(
|
||||
e,
|
||||
para,
|
||||
seq,
|
||||
payload.to_vec(),
|
||||
));
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[pallet::call]
|
||||
impl<T: Config> Pallet<T> {
|
||||
#[pallet::call_index(0)]
|
||||
#[pallet::weight({0})]
|
||||
pub fn start(origin: OriginFor<T>, para: ParaId, payload: Vec<u8>) -> DispatchResult {
|
||||
ensure_root(origin)?;
|
||||
let payload = BoundedVec::<u8, MaxPayloadSize>::try_from(payload)
|
||||
.map_err(|_| Error::<T>::PayloadTooLarge)?;
|
||||
Targets::<T>::try_mutate(|t| {
|
||||
t.try_push((para, payload)).map_err(|_| Error::<T>::TooManyTargets)
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[pallet::call_index(1)]
|
||||
#[pallet::weight({0})]
|
||||
pub fn start_many(
|
||||
origin: OriginFor<T>,
|
||||
para: ParaId,
|
||||
count: u32,
|
||||
payload: Vec<u8>,
|
||||
) -> DispatchResult {
|
||||
ensure_root(origin)?;
|
||||
let bounded_payload = BoundedVec::<u8, MaxPayloadSize>::try_from(payload)
|
||||
.map_err(|_| Error::<T>::PayloadTooLarge)?;
|
||||
for _ in 0..count {
|
||||
Targets::<T>::try_mutate(|t| {
|
||||
t.try_push((para, bounded_payload.clone()))
|
||||
.map_err(|_| Error::<T>::TooManyTargets)
|
||||
})?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[pallet::call_index(2)]
|
||||
#[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 == ¶) {
|
||||
t.swap_remove(p);
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[pallet::call_index(3)]
|
||||
#[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::call_index(4)]
|
||||
#[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>::RuntimeOrigin::from(origin))?;
|
||||
|
||||
Self::deposit_event(Event::Pinged(para, seq, payload.clone()));
|
||||
match send_xcm::<T::XcmSender>(
|
||||
(Parent, Junction::Teyrchain(para.into())).into(),
|
||||
Xcm(vec![Transact {
|
||||
origin_kind: OriginKind::Native,
|
||||
call: <T as Config>::RuntimeCall::from(Call::<T>::pong {
|
||||
seq,
|
||||
payload: payload.clone(),
|
||||
})
|
||||
.encode()
|
||||
.into(),
|
||||
fallback_max_weight: None,
|
||||
}]),
|
||||
) {
|
||||
Ok((hash, cost)) =>
|
||||
Self::deposit_event(Event::PongSent(para, seq, payload, hash, cost)),
|
||||
Err(e) => Self::deposit_event(Event::ErrorSendingPong(e, para, seq, payload)),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[pallet::call_index(5)]
|
||||
#[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>::RuntimeOrigin::from(origin))?;
|
||||
|
||||
if let Some(sent_at) = Pings::<T>::take(seq) {
|
||||
Self::deposit_event(Event::Ponged(
|
||||
para,
|
||||
seq,
|
||||
payload,
|
||||
pezframe_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(())
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user