From 795185c8e34b73ea4aff53b86f2bcbdb429b3004 Mon Sep 17 00:00:00 2001 From: weichweich Date: Mon, 22 Aug 2022 10:47:50 +0200 Subject: [PATCH 01/11] feat: clear up runtime selection --- nodes/parachain/src/command.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/nodes/parachain/src/command.rs b/nodes/parachain/src/command.rs index 8db9f9e17e..5f70a14424 100644 --- a/nodes/parachain/src/command.rs +++ b/nodes/parachain/src/command.rs @@ -52,7 +52,7 @@ impl IdentifyChain for dyn sc_service::ChainSpec { self.id().contains("spiritnet") || self.id().eq("kilt_westend") || self.id().eq("kilt_rococo") } fn is_clone(&self) -> bool { - self.id().to_lowercase().contains("cln_kilt") + self.id().to_lowercase().contains("cln_kilt") || self.id().to_lowercase().contains("clone") } } @@ -68,7 +68,18 @@ impl IdentifyChain for T { } } -fn load_spec(id: &str, runtime: &str) -> std::result::Result, String> { +fn load_spec(id: &str) -> std::result::Result, String> { + let runtime = if id.contains("peregrine") || id.eq("kilt_parachain_testnet") { + "spiritnet" + } else if id.to_lowercase().contains("cln_kilt") || id.to_lowercase().contains("clone") { + "clone" + } else { + "peregrine" + }; + + log::info!("Load spec id: {}", id); + log::info!("The following runtime was chosen based on the spec id: {}", runtime); + match (id, runtime) { ("dev", _) => Ok(Box::new(chain_spec::peregrine::make_dev_spec()?)), ("spiritnet-dev", _) => Ok(Box::new(chain_spec::spiritnet::get_chain_spec_dev()?)), @@ -122,7 +133,7 @@ impl SubstrateCli for Cli { } fn load_spec(&self, id: &str) -> std::result::Result, String> { - load_spec(id, &self.runtime) + load_spec(id) } fn native_runtime_version(spec: &Box) -> &'static RuntimeVersion { From 16a299539e1420b3d063eb57ef200692f8e70043 Mon Sep 17 00:00:00 2001 From: weichweich Date: Mon, 22 Aug 2022 11:47:38 +0200 Subject: [PATCH 02/11] feat: s2p --- Cargo.lock | 18 ++++++++++++++++++ runtimes/clone/Cargo.toml | 2 ++ runtimes/clone/src/lib.rs | 8 +++++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 1e49f0ea04..b377154716 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -955,6 +955,7 @@ dependencies = [ "cumulus-pallet-dmp-queue", "cumulus-pallet-parachain-system", "cumulus-pallet-session-benchmarking", + "cumulus-pallet-solo-to-para", "cumulus-pallet-xcm", "cumulus-pallet-xcmp-queue", "cumulus-primitives-core", @@ -1627,6 +1628,23 @@ dependencies = [ "sp-std", ] +[[package]] +name = "cumulus-pallet-solo-to-para" +version = "0.1.0" +source = "git+https://github.com/paritytech/cumulus?branch=polkadot-v0.9.27#66b684f88eba6c755651b8c47dccad2c2d9ac3db" +dependencies = [ + "cumulus-pallet-parachain-system", + "cumulus-primitives-core", + "frame-support", + "frame-system", + "pallet-sudo", + "parity-scale-codec", + "polkadot-primitives", + "scale-info", + "sp-runtime", + "sp-std", +] + [[package]] name = "cumulus-pallet-xcm" version = "0.1.0" diff --git a/runtimes/clone/Cargo.toml b/runtimes/clone/Cargo.toml index f66fb9c7e4..225f0cf7c1 100644 --- a/runtimes/clone/Cargo.toml +++ b/runtimes/clone/Cargo.toml @@ -58,6 +58,7 @@ pallet-utility = {git = "https://github.com/paritytech/substrate", default-featu cumulus-pallet-aura-ext = {git = "https://github.com/paritytech/cumulus", default-features = false, branch = "polkadot-v0.9.27"} cumulus-pallet-dmp-queue = {git = "https://github.com/paritytech/cumulus", default-features = false, branch = "polkadot-v0.9.27"} cumulus-pallet-parachain-system = {git = "https://github.com/paritytech/cumulus", default-features = false, branch = "polkadot-v0.9.27"} +cumulus-pallet-solo-to-para = {git = "https://github.com/paritytech/cumulus", default-features = false, branch = "polkadot-v0.9.27"} cumulus-pallet-xcm = {git = "https://github.com/paritytech/cumulus", default-features = false, branch = "polkadot-v0.9.27"} cumulus-pallet-xcmp-queue = {git = "https://github.com/paritytech/cumulus", default-features = false, branch = "polkadot-v0.9.27"} cumulus-primitives-core = {git = "https://github.com/paritytech/cumulus", default-features = false, branch = "polkadot-v0.9.27"} @@ -101,6 +102,7 @@ runtime-benchmarks = [ "xcm-builder/runtime-benchmarks", ] std = [ + "cumulus-pallet-solo-to-para/std", "pallet-sudo/std", "pallet-collator-selection/std", "codec/std", diff --git a/runtimes/clone/src/lib.rs b/runtimes/clone/src/lib.rs index 9895c5cf74..7d591eeff9 100644 --- a/runtimes/clone/src/lib.rs +++ b/runtimes/clone/src/lib.rs @@ -201,7 +201,7 @@ parameter_types! { impl cumulus_pallet_parachain_system::Config for Runtime { type Event = Event; - type OnSystemEvent = (); + type OnSystemEvent = cumulus_pallet_solo_to_para::Pallet; type SelfParaId = parachain_info::Pallet; type OutboundXcmpMessageSource = XcmpQueue; type DmpMessageHandler = DmpQueue; @@ -215,6 +215,10 @@ impl parachain_info::Config for Runtime {} impl cumulus_pallet_aura_ext::Config for Runtime {} +impl cumulus_pallet_solo_to_para::Config for Runtime { + type Event = Event; +} + impl cumulus_pallet_xcmp_queue::Config for Runtime { type Event = Event; type XcmExecutor = XcmExecutor; @@ -331,6 +335,7 @@ construct_runtime! { Session: pallet_session = 23, Aura: pallet_aura = 24, AuraExt: cumulus_pallet_aura_ext = 25, + SoloToPara: cumulus_pallet_solo_to_para::{Pallet, Call, Storage, Event} = 26, // Governance stuff // Democracy: pallet_democracy = 30, @@ -405,6 +410,7 @@ pub type SignedExtra = ( frame_system::CheckNonce, frame_system::CheckWeight, pallet_transaction_payment::ChargeTransactionPayment, + cumulus_pallet_solo_to_para::CheckSudo, ); /// Unchecked extrinsic type as expected by this runtime. pub type UncheckedExtrinsic = generic::UncheckedExtrinsic; From 73027986d8d0cf7ef4f9812466926da321c710b7 Mon Sep 17 00:00:00 2001 From: weichweich Date: Mon, 22 Aug 2022 14:20:26 +0200 Subject: [PATCH 03/11] fix: runtime selection --- nodes/parachain/src/command.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/nodes/parachain/src/command.rs b/nodes/parachain/src/command.rs index 5f70a14424..7392ed1c09 100644 --- a/nodes/parachain/src/command.rs +++ b/nodes/parachain/src/command.rs @@ -69,7 +69,10 @@ impl IdentifyChain for T { } fn load_spec(id: &str) -> std::result::Result, String> { - let runtime = if id.contains("peregrine") || id.eq("kilt_parachain_testnet") { + let runtime = if id.to_lowercase().contains("spiritnet") + || id.to_lowercase().contains("wilt") + || id.to_lowercase().contains("rilt") + { "spiritnet" } else if id.to_lowercase().contains("cln_kilt") || id.to_lowercase().contains("clone") { "clone" From 9573e3698ead6bed2b4b791213b36007ad82a733 Mon Sep 17 00:00:00 2001 From: William Freudenberger Date: Wed, 24 Aug 2022 16:03:51 +0200 Subject: [PATCH 04/11] feat: add relay call builder --- Cargo.lock | 34 ++++++++++++++ support/Cargo.toml | 6 +++ support/src/lib.rs | 1 + support/src/relay.rs | 101 ++++++++++++++++++++++++++++++++++++++++++ support/src/traits.rs | 34 ++++++++++++++ 5 files changed, 176 insertions(+) create mode 100644 support/src/relay.rs diff --git a/Cargo.lock b/Cargo.lock index b377154716..e17c30fe6e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3647,15 +3647,18 @@ dependencies = [ name = "kilt-support" version = "1.7.1" dependencies = [ + "cumulus-primitives-core", "frame-support", "frame-system", "parity-scale-codec", + "polkadot-core-primitives", "scale-info", "serde", "serde_json", "sp-core", "sp-runtime", "sp-std", + "xcm", ] [[package]] @@ -5873,6 +5876,37 @@ dependencies = [ "sp-std", ] +[[package]] +name = "pallet-relay-fork" +version = "1.7.1" +dependencies = [ + "cumulus-primitives-core", + "frame-benchmarking", + "frame-support", + "frame-system", + "hex", + "impl-serde", + "kilt-support", + "libsecp256k1", + "log", + "pallet-balances", + "pallet-xcm", + "parachain-info", + "parity-scale-codec", + "polkadot-core-primitives", + "polkadot-runtime-common", + "rand 0.8.5", + "scale-info", + "serde", + "sha3 0.10.1", + "sp-core", + "sp-io", + "sp-keystore", + "sp-runtime", + "sp-std", + "xcm", +] + [[package]] name = "pallet-scheduler" version = "4.0.0-dev" diff --git a/support/Cargo.toml b/support/Cargo.toml index a5f21fd402..487c61d6f9 100644 --- a/support/Cargo.toml +++ b/support/Cargo.toml @@ -11,11 +11,14 @@ codec = {package = "parity-scale-codec", version = "3.1.5", default-features = f scale-info = {version = "2.1.1", default-features = false, features = ["derive"]} serde = {version = "1.0.136", optional = true, features = ["derive"]} +cumulus-primitives-core = {branch = "polkadot-v0.9.27", default-features = false, git = "https://github.com/paritytech/cumulus"} frame-support = {branch = "polkadot-v0.9.27", default-features = false, git = "https://github.com/paritytech/substrate"} frame-system = {branch = "polkadot-v0.9.27", default-features = false, git = "https://github.com/paritytech/substrate"} +polkadot-core-primitives = {branch = "release-v0.9.27", default-features = false, git = "https://github.com/paritytech/polkadot"} sp-core = {branch = "polkadot-v0.9.27", default-features = false, git = "https://github.com/paritytech/substrate"} sp-runtime = {branch = "polkadot-v0.9.27", default-features = false, git = "https://github.com/paritytech/substrate"} sp-std = {branch = "polkadot-v0.9.27", default-features = false, git = "https://github.com/paritytech/substrate"} +xcm = {branch = "release-v0.9.27", default-features = false, git = "https://github.com/paritytech/polkadot"} [dev-dependencies] serde_json = "1.0.83" @@ -34,12 +37,15 @@ runtime-benchmarks = [ std = [ "serde", "codec/std", + "cumulus-primitives-core/std", "frame-support/std", "frame-system/std", + "polkadot-core-primitives/std", "scale-info/std", "sp-core/std", "sp-runtime/std", "sp-std/std", + "xcm/std", ] try-runtime = [ "frame-support/try-runtime", diff --git a/support/src/lib.rs b/support/src/lib.rs index 7a58831778..1cb723b63c 100644 --- a/support/src/lib.rs +++ b/support/src/lib.rs @@ -24,6 +24,7 @@ use sp_runtime::traits::Zero; pub mod deposit; #[cfg(any(feature = "runtime-benchmarks", feature = "mock"))] pub mod mock; +pub mod relay; pub mod signature; pub mod traits; diff --git a/support/src/relay.rs b/support/src/relay.rs new file mode 100644 index 0000000000..43114d6bd6 --- /dev/null +++ b/support/src/relay.rs @@ -0,0 +1,101 @@ +// KILT Blockchain – https://botlabs.org +// Copyright (C) 2019-2022 BOTLabs GmbH + +// The KILT Blockchain 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. + +// The KILT Blockchain 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 this program. If not, see . + +// If you feel like getting in touch with us, you can do so at info@botlabs.org + +//! # RelayChain Support Module +//! +//! Provides means of of handling relaychain related utilities and +//! business logic such as finalizing XCM calls. + +#![cfg_attr(not(feature = "std"), no_std)] +#![allow(clippy::unused_unit)] + +use codec::{Decode, Encode, EncodeLike, FullCodec}; +use frame_support::{traits::Get, weights::Weight, RuntimeDebug}; +use frame_system::Config; +use scale_info::TypeInfo; +use sp_std::{boxed::Box, marker::PhantomData, prelude::*}; +use xcm::latest::prelude::*; + +pub use cumulus_primitives_core::ParaId; + +use crate::traits::RelayCallBuilder; + +#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo)] +pub enum UtilityCall { + #[codec(index = 1)] + AsDerivative(u16, RelayChainCall), + #[codec(index = 2)] + BatchAll(Vec), +} + +/// The encoded index correspondes to Kusama's Runtime module configuration. +/// https://github.com/paritytech/polkadot/blob/444e96ae34bcec8362f0f947a07bd912b32ca48f/runtime/kusama/src/lib.rs#L1379 +#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo)] +pub enum RelayChainCall { + #[codec(index = 24)] + Utility(Box>), +} + +#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo)] +pub struct RelayChainCallBuilder>(PhantomData<(T, ParachainId)>); + +impl> RelayCallBuilder for RelayChainCallBuilder +where + T::AccountId: FullCodec, + RelayChainCall: FullCodec, +{ + type AccountId = T::AccountId; + type Balance = polkadot_core_primitives::Balance; + type RelayChainCall = RelayChainCall; + + fn utility_batch_call(calls: Vec) -> Self::RelayChainCall { + RelayChainCall::Utility(Box::new(UtilityCall::BatchAll(calls))) + } + + fn utility_as_derivative_call(call: Self::RelayChainCall, index: u16) -> Self::RelayChainCall { + RelayChainCall::Utility(Box::new(UtilityCall::AsDerivative(index, call))) + } + + fn finalize_call_into_xcm_message(call: Self::RelayChainCall, extra_fee: Self::Balance, weight: Weight) -> Xcm<()> { + let asset = MultiAsset { + id: Concrete(MultiLocation::here()), + fun: Fungibility::Fungible(extra_fee), + }; + Xcm(vec![ + WithdrawAsset(asset.clone().into()), + BuyExecution { + fees: asset, + weight_limit: Unlimited, + }, + Transact { + origin_type: OriginKind::SovereignAccount, + require_weight_at_most: weight, + call: call.encode().into(), + }, + RefundSurplus, + DepositAsset { + assets: All.into(), + max_assets: u32::max_value(), + beneficiary: MultiLocation { + parents: 0, + interior: X1(Parachain(ParachainId::get().into())), + }, + }, + ]) + } +} diff --git a/support/src/traits.rs b/support/src/traits.rs index 07448b8a99..13c6d9f1f4 100644 --- a/support/src/traits.rs +++ b/support/src/traits.rs @@ -16,6 +16,12 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org +use codec::{EncodeLike, FullCodec}; +use frame_support::weights::Weight; +use scale_info::TypeInfo; +use sp_std::vec::Vec; +use xcm::latest::Xcm; + /// The sources of a call struct. /// /// This trait allows to differentiate between the sender of a call and the @@ -68,3 +74,31 @@ pub trait VersionMigratorTrait: Sized { pub trait GenerateBenchmarkOrigin { fn generate_origin(sender: AccountId, subject: SubjectId) -> OuterOrigin; } + +// TODO: Docs + +pub trait RelayCallBuilder { + type AccountId: FullCodec; + type Balance: FullCodec; + type RelayChainCall: FullCodec + EncodeLike + sp_std::fmt::Debug + Clone + PartialEq + Eq + TypeInfo; + + /// Execute multiple calls in a batch. + /// Param: + /// - calls: List of calls to be executed + fn utility_batch_call(calls: Vec) -> Self::RelayChainCall; + + /// Execute a call, replacing the `Origin` with a sub-account. + /// params: + /// - call: The call to be executed. Can be nested with `utility_batch_call` + /// - index: The index of sub-account to be used as the new origin. + fn utility_as_derivative_call(call: Self::RelayChainCall, index: u16) -> Self::RelayChainCall; + + /// Wrap the final calls into the Xcm format. + /// params: + /// - call: The call to be executed + /// - extra_fee: Extra fee (in staking currency) used for buy the `weight` + /// and `debt`. + /// - weight: the weight limit used for XCM. + /// - debt: the weight limit used to process the `call`. + fn finalize_call_into_xcm_message(call: Self::RelayChainCall, extra_fee: Self::Balance, weight: Weight) -> Xcm<()>; +} From ae3a94eb11ab07b7f7b444c53eac9636698ec235 Mon Sep 17 00:00:00 2001 From: William Freudenberger Date: Wed, 24 Aug 2022 16:04:13 +0200 Subject: [PATCH 05/11] wip: add migraton init extrinsics --- pallets/relay-fork/Cargo.toml | 93 +++++++++++++++++++++ pallets/relay-fork/src/lib.rs | 151 ++++++++++++++++++++++++++++++++++ 2 files changed, 244 insertions(+) create mode 100644 pallets/relay-fork/Cargo.toml create mode 100644 pallets/relay-fork/src/lib.rs diff --git a/pallets/relay-fork/Cargo.toml b/pallets/relay-fork/Cargo.toml new file mode 100644 index 0000000000..7dd5d8ce35 --- /dev/null +++ b/pallets/relay-fork/Cargo.toml @@ -0,0 +1,93 @@ +[package] +authors = ["KILT "] +description = "Initiate fork to another relay chain" +edition = "2021" +name = "pallet-relay-fork" +repository = "https://github.com/KILTprotocol/kilt-node" +version = "1.7.1" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dev-dependencies] +kilt-support = {features = ["mock"], path = "../../support"} + +pallet-balances = {branch = "polkadot-v0.9.27", git = "https://github.com/paritytech/substrate"} +rand = "0.8" +sp-core = {branch = "polkadot-v0.9.27", git = "https://github.com/paritytech/substrate"} +sp-io = {branch = "polkadot-v0.9.27", git = "https://github.com/paritytech/substrate"} +sp-keystore = {branch = "polkadot-v0.9.27", git = "https://github.com/paritytech/substrate"} + +[dependencies] + +# actively used +cumulus-primitives-core = {git = "https://github.com/paritytech/cumulus", default-features = false, branch = "polkadot-v0.9.27"} +pallet-xcm = {git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.27"} +parachain-info = {git = "https://github.com/paritytech/cumulus", default-features = false, branch = "polkadot-v0.9.27"} +polkadot-core-primitives = {git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.27"} +polkadot-runtime-common = {git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.27"} +xcm = {git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.27"} + + +codec = {package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive"]} +hex = {version = "0.4", default-features = false} +libsecp256k1 = {version = "0.7", default-features = false, features = ["hmac"]} +log = "0.4.17" +scale-info = {version = "2.1.1", default-features = false, features = ["derive"]} +sha3 = {version = "0.10", default-features = false} + +# KILT +kilt-support = {default-features = false, path = "../../support"} + +# Substrate dependencies +frame-support = {branch = "polkadot-v0.9.27", default-features = false, git = "https://github.com/paritytech/substrate"} +frame-system = {branch = "polkadot-v0.9.27", default-features = false, git = "https://github.com/paritytech/substrate"} +sp-core = {branch = "polkadot-v0.9.27", default-features = false, git = "https://github.com/paritytech/substrate"} +sp-io = {branch = "polkadot-v0.9.27", default-features = false, git = "https://github.com/paritytech/substrate"} +sp-runtime = {branch = "polkadot-v0.9.27", default-features = false, git = "https://github.com/paritytech/substrate"} +sp-std = {branch = "polkadot-v0.9.27", default-features = false, git = "https://github.com/paritytech/substrate"} + +# benchmarking +frame-benchmarking = {branch = "polkadot-v0.9.27", optional = true, default-features = false, git = "https://github.com/paritytech/substrate"} + +# optional dependencies +impl-serde = {version = "0.3.1", optional = true} +serde = {version = "1.0.101", optional = true} + +[features] +default = ["std"] + +runtime-benchmarks = [ + "frame-benchmarking", + "kilt-support/runtime-benchmarks", + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", +] + +std = [ + "cumulus-primitives-core/std", + "pallet-xcm/std", + "polkadot-core-primitives/std", + "polkadot-runtime-common/std", + "parachain-info/std", + "xcm/std", + "codec/std", + "frame-benchmarking/std", + "frame-support/std", + "frame-system/std", + "impl-serde", + "kilt-support/std", + "log/std", + "scale-info/std", + "serde", + "sha3/std", + "sp-core/std", + "sp-io/std", + "sp-runtime/std", + "sp-std/std", +] + +try-runtime = [ + "frame-support/try-runtime", + "kilt-support/try-runtime", +] diff --git a/pallets/relay-fork/src/lib.rs b/pallets/relay-fork/src/lib.rs new file mode 100644 index 0000000000..0ba9e75df8 --- /dev/null +++ b/pallets/relay-fork/src/lib.rs @@ -0,0 +1,151 @@ +// KILT Blockchain – https://botlabs.org +// Copyright (C) 2019-2022 BOTLabs GmbH + +// The KILT Blockchain 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. + +// The KILT Blockchain 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 this program. If not, see . + +// If you feel like getting in touch with us, you can do so at info@botlabs.org + +//! # Relay migration initialization pallet +//! +//! This pallet changes the para ID and sends an XCM message to swap the +//! parachain lease. +//! +//! - [`Pallet`] + +#![cfg_attr(not(feature = "std"), no_std)] + +pub use pallet::*; + +#[cfg(test)] +mod mock; + +#[cfg(test)] +mod tests; + +#[cfg(feature = "runtime-benchmarks")] +mod benchmarking; + +#[frame_support::pallet] +pub mod pallet { + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; + use kilt_support::traits::RelayCallBuilder; + use xcm::v2::{Junctions::Here, Parent}; + + #[pallet::pallet] + #[pallet::generate_store(pub(super) trait Store)] + pub struct Pallet(_); + + #[pallet::config] + // TODO: Check whether tight coupling is fine + pub trait Config: + frame_system::Config + + parachain_info::Config + + pallet_xcm::Config + + polkadot_runtime_common::paras_registrar::Config + { + type Event: From> + IsType<::Event>; + type AdminOrigin: EnsureOrigin<::Origin>; + /// The Call builder for communicating with RelayChain via XCM + /// messaging. + type RelayChainCallBuilder: RelayCallBuilder< + AccountId = Self::AccountId, + Balance = polkadot_core_primitives::Balance, + >; + } + + #[pallet::storage] + #[pallet::getter(fn para_id_changed)] + pub type ParaIdChanged = StorageValue<_, bool, ValueQuery>; + + #[pallet::storage] + #[pallet::getter(fn lease_swap_pending)] + pub type LeaseSwapPending = StorageValue<_, bool, ValueQuery>; + + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + pub enum Event { + //// The parachain id was changed. + ParaIdChanged { + /// The old parachain id. + old_id: u32, + /// The new parachain id. + new_id: u32, + }, + /// The parachain lease swap was initiated. + LeaseSwapInitiated, + } + + #[pallet::error] + pub enum Error { + /// The lease swap has already been initiated. + LeaseSwapAlreadyInitiated, + /// The para id has already been changed throughout the lifetime of the + /// parachain. + ParaIdAlreadyChanged, + /// The origin is not authorized to initiate the migration. + Unauthorized, + } + + #[pallet::call] + impl Pallet { + #[pallet::weight(10_000 + T::DbWeight::get().writes(1))] + pub fn send_swap_call( + origin: OriginFor, + relay_call: <::RelayChainCallBuilder as RelayCallBuilder>::RelayChainCall, + relay_balance: u128, + max_weight: u64, + ) -> DispatchResult { + // FIXME: Check for authorization + let who = ensure_signed(origin)?; + ensure!(!LeaseSwapPending::::get(), Error::::LeaseSwapAlreadyInitiated); + + let xcm_message = + T::RelayChainCallBuilder::finalize_call_into_xcm_message(relay_call, relay_balance, max_weight); + + let result = pallet_xcm::Pallet::::send_xcm(Here, Parent, xcm_message); + log::debug!("Sending XCM to swap para lease with result: {:?}", result); + + // TODO: Should probably just remove this storage entirely + if result.is_ok() { + LeaseSwapPending::::put(true); + } + + Self::deposit_event(Event::LeaseSwapInitiated); + + Ok(()) + } + // TODO: Docs + #[pallet::weight(10_000 + T::DbWeight::get().writes(1))] + pub fn initiate_relay_migration(origin: OriginFor, new_para_id: u32) -> DispatchResult { + // FIXME: Check for authorization + let who = ensure_signed(origin)?; + + ensure!(!ParaIdChanged::::get(), Error::::ParaIdAlreadyChanged); + + // FIXME: Add raw writing of parachain ID (is declared as private 🥲) + // ::Pallet::ParachainId::insert(new_para_id); + + let old_para_id = parachain_info::Pallet::::parachain_id(); + + ParaIdChanged::::put(true); + + Self::deposit_event(Event::ParaIdChanged { + old_id: old_para_id.into(), + new_id: new_para_id, + }); + Ok(()) + } + } +} From 2ab478aa0c502d92b78efcdc72259bfad9071f2f Mon Sep 17 00:00:00 2001 From: William Freudenberger Date: Fri, 26 Aug 2022 11:50:24 +0200 Subject: [PATCH 06/11] feat: add relay block check setters, xcm --- Cargo.lock | 13 +- .../Cargo.toml | 50 ++------ .../src/lib.rs | 121 ++++++++++-------- runtimes/spiritnet/Cargo.toml | 6 +- runtimes/spiritnet/src/lib.rs | 12 +- support/src/relay.rs | 20 ++- support/src/traits.rs | 40 +++--- 7 files changed, 136 insertions(+), 126 deletions(-) rename pallets/{relay-fork => relay-migration}/Cargo.toml (71%) rename pallets/{relay-fork => relay-migration}/src/lib.rs (53%) diff --git a/Cargo.lock b/Cargo.lock index e17c30fe6e..f3c5a03a98 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5877,31 +5877,23 @@ dependencies = [ ] [[package]] -name = "pallet-relay-fork" +name = "pallet-relay-migration" version = "1.7.1" dependencies = [ + "cumulus-pallet-parachain-system", "cumulus-primitives-core", "frame-benchmarking", "frame-support", "frame-system", - "hex", - "impl-serde", "kilt-support", - "libsecp256k1", "log", - "pallet-balances", "pallet-xcm", - "parachain-info", "parity-scale-codec", "polkadot-core-primitives", "polkadot-runtime-common", - "rand 0.8.5", "scale-info", - "serde", - "sha3 0.10.1", "sp-core", "sp-io", - "sp-keystore", "sp-runtime", "sp-std", "xcm", @@ -10799,6 +10791,7 @@ dependencies = [ "pallet-preimage", "pallet-proxy", "pallet-randomness-collective-flip", + "pallet-relay-migration", "pallet-scheduler", "pallet-session", "pallet-timestamp", diff --git a/pallets/relay-fork/Cargo.toml b/pallets/relay-migration/Cargo.toml similarity index 71% rename from pallets/relay-fork/Cargo.toml rename to pallets/relay-migration/Cargo.toml index 7dd5d8ce35..4bfe94e355 100644 --- a/pallets/relay-fork/Cargo.toml +++ b/pallets/relay-migration/Cargo.toml @@ -1,40 +1,24 @@ [package] authors = ["KILT "] -description = "Initiate fork to another relay chain" +description = "Initiate migration to another relay chain" edition = "2021" -name = "pallet-relay-fork" +name = "pallet-relay-migration" repository = "https://github.com/KILTprotocol/kilt-node" version = "1.7.1" [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] -[dev-dependencies] -kilt-support = {features = ["mock"], path = "../../support"} - -pallet-balances = {branch = "polkadot-v0.9.27", git = "https://github.com/paritytech/substrate"} -rand = "0.8" -sp-core = {branch = "polkadot-v0.9.27", git = "https://github.com/paritytech/substrate"} -sp-io = {branch = "polkadot-v0.9.27", git = "https://github.com/paritytech/substrate"} -sp-keystore = {branch = "polkadot-v0.9.27", git = "https://github.com/paritytech/substrate"} - [dependencies] - -# actively used +codec = {package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive"]} +cumulus-pallet-parachain-system = {git = "https://github.com/paritytech/cumulus", default-features = false, branch = "polkadot-v0.9.27"} cumulus-primitives-core = {git = "https://github.com/paritytech/cumulus", default-features = false, branch = "polkadot-v0.9.27"} +log = "0.4.17" pallet-xcm = {git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.27"} -parachain-info = {git = "https://github.com/paritytech/cumulus", default-features = false, branch = "polkadot-v0.9.27"} polkadot-core-primitives = {git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.27"} polkadot-runtime-common = {git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.27"} -xcm = {git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.27"} - - -codec = {package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive"]} -hex = {version = "0.4", default-features = false} -libsecp256k1 = {version = "0.7", default-features = false, features = ["hmac"]} -log = "0.4.17" scale-info = {version = "2.1.1", default-features = false, features = ["derive"]} -sha3 = {version = "0.10", default-features = false} +xcm = {git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.27"} # KILT kilt-support = {default-features = false, path = "../../support"} @@ -50,43 +34,33 @@ sp-std = {branch = "polkadot-v0.9.27", default-features = false, git = "https:// # benchmarking frame-benchmarking = {branch = "polkadot-v0.9.27", optional = true, default-features = false, git = "https://github.com/paritytech/substrate"} -# optional dependencies -impl-serde = {version = "0.3.1", optional = true} -serde = {version = "1.0.101", optional = true} - [features] default = ["std"] - runtime-benchmarks = [ "frame-benchmarking", "kilt-support/runtime-benchmarks", "frame-support/runtime-benchmarks", "frame-system/runtime-benchmarks", ] - std = [ - "cumulus-primitives-core/std", - "pallet-xcm/std", - "polkadot-core-primitives/std", - "polkadot-runtime-common/std", - "parachain-info/std", - "xcm/std", "codec/std", + "cumulus-pallet-parachain-system/std", + "cumulus-primitives-core/std", "frame-benchmarking/std", "frame-support/std", "frame-system/std", - "impl-serde", "kilt-support/std", "log/std", + "pallet-xcm/std", + "polkadot-core-primitives/std", + "polkadot-runtime-common/std", "scale-info/std", - "serde", - "sha3/std", "sp-core/std", "sp-io/std", "sp-runtime/std", "sp-std/std", + "xcm/std", ] - try-runtime = [ "frame-support/try-runtime", "kilt-support/try-runtime", diff --git a/pallets/relay-fork/src/lib.rs b/pallets/relay-migration/src/lib.rs similarity index 53% rename from pallets/relay-fork/src/lib.rs rename to pallets/relay-migration/src/lib.rs index 0ba9e75df8..059b516fd2 100644 --- a/pallets/relay-fork/src/lib.rs +++ b/pallets/relay-migration/src/lib.rs @@ -27,20 +27,22 @@ pub use pallet::*; -#[cfg(test)] -mod mock; +// TODO: Create modules and enable before merging! +// #[cfg(test)] +// mod mock; -#[cfg(test)] -mod tests; +// #[cfg(test)] +// mod tests; -#[cfg(feature = "runtime-benchmarks")] -mod benchmarking; +// #[cfg(feature = "runtime-benchmarks")] +// mod benchmarking; #[frame_support::pallet] pub mod pallet { use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; use kilt_support::traits::RelayCallBuilder; + use sp_std::vec::Vec; use xcm::v2::{Junctions::Here, Parent}; #[pallet::pallet] @@ -48,15 +50,12 @@ pub mod pallet { pub struct Pallet(_); #[pallet::config] - // TODO: Check whether tight coupling is fine - pub trait Config: - frame_system::Config - + parachain_info::Config - + pallet_xcm::Config - + polkadot_runtime_common::paras_registrar::Config - { + pub trait Config: frame_system::Config + pallet_xcm::Config { type Event: From> + IsType<::Event>; - type AdminOrigin: EnsureOrigin<::Origin>; + + /// Origin from which calls of this pallet can be made. + type ApproveOrigin: EnsureOrigin<::Origin>; + /// The Call builder for communicating with RelayChain via XCM /// messaging. type RelayChainCallBuilder: RelayCallBuilder< @@ -65,87 +64,97 @@ pub mod pallet { >; } + /// Switch between RelayNumberStrictlyIncreases and AnyRelayNumber. #[pallet::storage] - #[pallet::getter(fn para_id_changed)] - pub type ParaIdChanged = StorageValue<_, bool, ValueQuery>; - - #[pallet::storage] - #[pallet::getter(fn lease_swap_pending)] - pub type LeaseSwapPending = StorageValue<_, bool, ValueQuery>; + #[pallet::getter(fn relay_block_number_strictly_increases)] + pub(crate) type RelayNumberStrictlyIncreases = StorageValue<_, bool, ValueQuery>; #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { - //// The parachain id was changed. - ParaIdChanged { - /// The old parachain id. - old_id: u32, - /// The new parachain id. - new_id: u32, - }, /// The parachain lease swap was initiated. LeaseSwapInitiated, + /// The requirement for associated relay block numbers was set + RelayNumberCheckSet { + /// Reflects setting to RelayNumberStrictlyIncreases + strict: bool, + }, } #[pallet::error] pub enum Error { - /// The lease swap has already been initiated. - LeaseSwapAlreadyInitiated, /// The para id has already been changed throughout the lifetime of the /// parachain. ParaIdAlreadyChanged, - /// The origin is not authorized to initiate the migration. - Unauthorized, } #[pallet::call] impl Pallet { - #[pallet::weight(10_000 + T::DbWeight::get().writes(1))] - pub fn send_swap_call( + #[pallet::weight(100_000 + T::DbWeight::get().writes(1))] + /// Set an XCM call to the relay chain. + /// + /// Has to be done pre migration. + // TODO: Weights + pub fn send_swap_call_bytes( origin: OriginFor, - relay_call: <::RelayChainCallBuilder as RelayCallBuilder>::RelayChainCall, + relay_call: Vec, relay_balance: u128, max_weight: u64, ) -> DispatchResult { - // FIXME: Check for authorization - let who = ensure_signed(origin)?; - ensure!(!LeaseSwapPending::::get(), Error::::LeaseSwapAlreadyInitiated); - + T::ApproveOrigin::ensure_origin(origin)?; let xcm_message = T::RelayChainCallBuilder::finalize_call_into_xcm_message(relay_call, relay_balance, max_weight); let result = pallet_xcm::Pallet::::send_xcm(Here, Parent, xcm_message); log::debug!("Sending XCM to swap para lease with result: {:?}", result); - // TODO: Should probably just remove this storage entirely - if result.is_ok() { - LeaseSwapPending::::put(true); - } - Self::deposit_event(Event::LeaseSwapInitiated); Ok(()) } - // TODO: Docs + + /// Set the associated relay block number to be + /// RelayNumberStrictlyIncreases. + /// + /// Has to be done post migration. + // TODO: Weights #[pallet::weight(10_000 + T::DbWeight::get().writes(1))] - pub fn initiate_relay_migration(origin: OriginFor, new_para_id: u32) -> DispatchResult { - // FIXME: Check for authorization - let who = ensure_signed(origin)?; + pub fn enable_strict_relay_number_check(origin: OriginFor) -> DispatchResult { + T::ApproveOrigin::ensure_origin(origin)?; + RelayNumberStrictlyIncreases::::put(true); - ensure!(!ParaIdChanged::::get(), Error::::ParaIdAlreadyChanged); + Self::deposit_event(Event::RelayNumberCheckSet { strict: true }); - // FIXME: Add raw writing of parachain ID (is declared as private 🥲) - // ::Pallet::ParachainId::insert(new_para_id); + Ok(()) + } - let old_para_id = parachain_info::Pallet::::parachain_id(); + /// Set the associated relay block number to be AnyRelayNumber. + /// + /// Has to be done pre migration. + // TODO: Weights + #[pallet::weight(10_000 + T::DbWeight::get().writes(1))] + pub fn disable_strict_relay_number_check(origin: OriginFor) -> DispatchResult { + T::ApproveOrigin::ensure_origin(origin)?; + RelayNumberStrictlyIncreases::::put(false); - ParaIdChanged::::put(true); + Self::deposit_event(Event::RelayNumberCheckSet { strict: false }); - Self::deposit_event(Event::ParaIdChanged { - old_id: old_para_id.into(), - new_id: new_para_id, - }); Ok(()) } } + + impl cumulus_pallet_parachain_system::CheckAssociatedRelayNumber for Pallet { + fn check_associated_relay_number( + current: polkadot_core_primitives::BlockNumber, + previous: polkadot_core_primitives::BlockNumber, + ) { + if RelayNumberStrictlyIncreases::::get() { + cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases::check_associated_relay_number( + current, previous, + ) + } else { + cumulus_pallet_parachain_system::AnyRelayNumber::check_associated_relay_number(current, previous) + } + } + } } diff --git a/runtimes/spiritnet/Cargo.toml b/runtimes/spiritnet/Cargo.toml index 2cc39c760f..e60d7049c2 100644 --- a/runtimes/spiritnet/Cargo.toml +++ b/runtimes/spiritnet/Cargo.toml @@ -27,9 +27,10 @@ attestation = {path = "../../pallets/attestation", default-features = false} ctype = {path = "../../pallets/ctype", default-features = false} delegation = {path = "../../pallets/delegation", default-features = false} did = {path = "../../pallets/did", default-features = false} -kilt-support = {path = "../../support", default-features = false, optional = true} +kilt-support = {path = "../../support", default-features = false} pallet-did-lookup = {path = "../../pallets/pallet-did-lookup", default-features = false} pallet-inflation = {path = "../../pallets/pallet-inflation", default-features = false} +pallet-relay-migration = {path = "../../pallets/relay-migration", default-features = false} pallet-web3-names = {path = "../../pallets/pallet-web3-names", default-features = false} parachain-staking = {path = "../../pallets/parachain-staking", default-features = false} runtime-common = {path = "../../runtimes/common", default-features = false} @@ -112,7 +113,6 @@ runtime-benchmarks = [ "frame-system-benchmarking", "frame-system/runtime-benchmarks", "hex-literal", - "kilt-support", "kilt-support/runtime-benchmarks", "pallet-balances/runtime-benchmarks", "pallet-collective/runtime-benchmarks", @@ -155,6 +155,7 @@ std = [ "frame-system-rpc-runtime-api/std", "frame-system/std", "frame-try-runtime/std", + "kilt-support/std", "log/std", "pallet-aura/std", "pallet-authorship/std", @@ -165,6 +166,7 @@ std = [ "pallet-indices/std", "pallet-inflation/std", "pallet-membership/std", + "pallet-relay-migration/std", "pallet-preimage/std", "pallet-proxy/std", "pallet-randomness-collective-flip/std", diff --git a/runtimes/spiritnet/src/lib.rs b/runtimes/spiritnet/src/lib.rs index 79c85fd895..c1dfd0aa54 100644 --- a/runtimes/spiritnet/src/lib.rs +++ b/runtimes/spiritnet/src/lib.rs @@ -32,6 +32,7 @@ use frame_support::{ weights::{constants::RocksDbWeight, ConstantMultiplier, Weight}, }; use frame_system::EnsureRoot; +use kilt_support::relay::RelayChainCallBuilder; use sp_api::impl_runtime_apis; use sp_core::OpaqueMetadata; use sp_runtime::{ @@ -213,7 +214,9 @@ impl cumulus_pallet_parachain_system::Config for Runtime { type ReservedDmpWeight = ReservedDmpWeight; type XcmpMessageHandler = XcmpQueue; type ReservedXcmpWeight = ReservedXcmpWeight; - type CheckAssociatedRelayNumber = cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; + // TODO: Put in ForkPallet which impls trait s.t. we can switch between Strictly + // and Any + type CheckAssociatedRelayNumber = RelayMigration; } impl parachain_info::Config for Runtime {} @@ -657,6 +660,12 @@ impl parachain_staking::Config for Runtime { const BLOCKS_PER_YEAR: Self::BlockNumber = constants::BLOCKS_PER_YEAR; } +impl pallet_relay_migration::Config for Runtime { + type Event = Event; + type ApproveOrigin = ApproveOrigin; + type RelayChainCallBuilder = RelayChainCallBuilder; +} + impl pallet_utility::Config for Runtime { type Event = Event; type Call = Call; @@ -874,6 +883,7 @@ construct_runtime! { // placeholder: parachain council election = 33, TechnicalMembership: pallet_membership:: = 34, Treasury: pallet_treasury = 35, + RelayMigration: pallet_relay_migration::{Pallet, Call, Storage, Event} = 36, // Utility module. Utility: pallet_utility = 40, diff --git a/support/src/relay.rs b/support/src/relay.rs index 43114d6bd6..7c08cbfb2f 100644 --- a/support/src/relay.rs +++ b/support/src/relay.rs @@ -24,8 +24,8 @@ #![cfg_attr(not(feature = "std"), no_std)] #![allow(clippy::unused_unit)] -use codec::{Decode, Encode, EncodeLike, FullCodec}; -use frame_support::{traits::Get, weights::Weight, RuntimeDebug}; +use codec::{Decode, Encode, FullCodec}; +use frame_support::{traits::Get, weights::Weight}; use frame_system::Config; use scale_info::TypeInfo; use sp_std::{boxed::Box, marker::PhantomData, prelude::*}; @@ -43,12 +43,20 @@ pub enum UtilityCall { BatchAll(Vec), } +#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo)] +pub enum RegistrarCall { + #[codec(index = 4)] + Swap(ParaId, ParaId), +} + /// The encoded index correspondes to Kusama's Runtime module configuration. /// https://github.com/paritytech/polkadot/blob/444e96ae34bcec8362f0f947a07bd912b32ca48f/runtime/kusama/src/lib.rs#L1379 #[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo)] pub enum RelayChainCall { #[codec(index = 24)] Utility(Box>), + #[codec(index = 70)] + Registrar(RegistrarCall), } #[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo)] @@ -71,7 +79,11 @@ where RelayChainCall::Utility(Box::new(UtilityCall::AsDerivative(index, call))) } - fn finalize_call_into_xcm_message(call: Self::RelayChainCall, extra_fee: Self::Balance, weight: Weight) -> Xcm<()> { + fn swap_call(id: ParaId, other: ParaId) -> Self::RelayChainCall { + RelayChainCall::Registrar(RegistrarCall::Swap(id, other)) + } + + fn finalize_call_into_xcm_message(call: Vec, extra_fee: Self::Balance, weight: Weight) -> Xcm<()> { let asset = MultiAsset { id: Concrete(MultiLocation::here()), fun: Fungibility::Fungible(extra_fee), @@ -85,7 +97,7 @@ where Transact { origin_type: OriginKind::SovereignAccount, require_weight_at_most: weight, - call: call.encode().into(), + call: call.into(), }, RefundSurplus, DepositAsset { diff --git a/support/src/traits.rs b/support/src/traits.rs index 13c6d9f1f4..99ae91ce31 100644 --- a/support/src/traits.rs +++ b/support/src/traits.rs @@ -17,6 +17,7 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org use codec::{EncodeLike, FullCodec}; +use cumulus_primitives_core::ParaId; use frame_support::weights::Weight; use scale_info::TypeInfo; use sp_std::vec::Vec; @@ -75,30 +76,39 @@ pub trait GenerateBenchmarkOrigin { fn generate_origin(sender: AccountId, subject: SubjectId) -> OuterOrigin; } -// TODO: Docs - +/// Trait to reflect calls to the relaychain which we support on the pallet +/// level. pub trait RelayCallBuilder { type AccountId: FullCodec; type Balance: FullCodec; type RelayChainCall: FullCodec + EncodeLike + sp_std::fmt::Debug + Clone + PartialEq + Eq + TypeInfo; /// Execute multiple calls in a batch. - /// Param: - /// - calls: List of calls to be executed + /// + /// * calls: The list of calls to be executed. fn utility_batch_call(calls: Vec) -> Self::RelayChainCall; /// Execute a call, replacing the `Origin` with a sub-account. - /// params: - /// - call: The call to be executed. Can be nested with `utility_batch_call` - /// - index: The index of sub-account to be used as the new origin. + /// + /// * call: The call to be executed. Can be nested with + /// `utility_batch_call`. + /// * index: The index of the sub-account to be used as the new origin. fn utility_as_derivative_call(call: Self::RelayChainCall, index: u16) -> Self::RelayChainCall; - /// Wrap the final calls into the Xcm format. - /// params: - /// - call: The call to be executed - /// - extra_fee: Extra fee (in staking currency) used for buy the `weight` - /// and `debt`. - /// - weight: the weight limit used for XCM. - /// - debt: the weight limit used to process the `call`. - fn finalize_call_into_xcm_message(call: Self::RelayChainCall, extra_fee: Self::Balance, weight: Weight) -> Xcm<()>; + /// Execute a parachain lease swap call. + /// + /// * id: One of the two para ids. Typically, this should be the one of the + /// parachain that executes this XCM call, e.g. the source. + /// * other: The target para id with which the lease swap should be + /// executed. + fn swap_call(id: ParaId, other: ParaId) -> Self::RelayChainCall; + + /// Wrap the final calls into the latest Xcm format. + /// + /// * call: The relaychain call to be executed + /// * extra_fee: The extra fee (in relaychain currency) used for buying the + /// `weight` and `debt`. + /// * weight: The weight limit used for XCM. + /// * debt: The weight limit used to process the call. + fn finalize_call_into_xcm_message(call: Vec, extra_fee: Self::Balance, weight: Weight) -> Xcm<()>; } From c202d47cca7e1e9c6c38b911831c931dc5346075 Mon Sep 17 00:00:00 2001 From: William Freudenberger Date: Fri, 26 Aug 2022 11:52:51 +0200 Subject: [PATCH 07/11] docs: update pallet description --- pallets/relay-migration/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pallets/relay-migration/src/lib.rs b/pallets/relay-migration/src/lib.rs index 059b516fd2..8fd5f78d24 100644 --- a/pallets/relay-migration/src/lib.rs +++ b/pallets/relay-migration/src/lib.rs @@ -18,8 +18,9 @@ //! # Relay migration initialization pallet //! -//! This pallet changes the para ID and sends an XCM message to swap the -//! parachain lease. +//! This pallet provides means of sending an XCM messages to the relay chain by +//! a configurable origin and switching the associated relay number block checks +//! between strictly and any. //! //! - [`Pallet`] From eda78774506437bd339d6c34cd34f6c00920ee2c Mon Sep 17 00:00:00 2001 From: William Freudenberger Date: Fri, 26 Aug 2022 11:58:37 +0200 Subject: [PATCH 08/11] docs: add comment to CheckAssociatedRelayNumber --- runtimes/spiritnet/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtimes/spiritnet/src/lib.rs b/runtimes/spiritnet/src/lib.rs index c1dfd0aa54..b7e56b06c5 100644 --- a/runtimes/spiritnet/src/lib.rs +++ b/runtimes/spiritnet/src/lib.rs @@ -214,8 +214,8 @@ impl cumulus_pallet_parachain_system::Config for Runtime { type ReservedDmpWeight = ReservedDmpWeight; type XcmpMessageHandler = XcmpQueue; type ReservedXcmpWeight = ReservedXcmpWeight; - // TODO: Put in ForkPallet which impls trait s.t. we can switch between Strictly - // and Any + // We temporarily control this via the RelayMigration pallet which can toggle + // between strict and any. type CheckAssociatedRelayNumber = RelayMigration; } From 219378a2f04b3e5634a795ca310cb5e09b77b630 Mon Sep 17 00:00:00 2001 From: weichweich Date: Fri, 26 Aug 2022 15:55:18 +0200 Subject: [PATCH 09/11] fix: native origin --- Cargo.lock | 1 + pallets/relay-migration/src/lib.rs | 10 +++------- runtimes/peregrine/Cargo.toml | 4 ++-- runtimes/peregrine/src/lib.rs | 12 +++++++++++- runtimes/spiritnet/src/lib.rs | 14 +++++++------- support/src/relay.rs | 4 ++-- 6 files changed, 26 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 37a7d6b4c6..295849a364 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6460,6 +6460,7 @@ dependencies = [ "pallet-preimage", "pallet-proxy", "pallet-randomness-collective-flip", + "pallet-relay-migration", "pallet-scheduler", "pallet-session", "pallet-sudo", diff --git a/pallets/relay-migration/src/lib.rs b/pallets/relay-migration/src/lib.rs index 8fd5f78d24..3d5c4df628 100644 --- a/pallets/relay-migration/src/lib.rs +++ b/pallets/relay-migration/src/lib.rs @@ -83,19 +83,15 @@ pub mod pallet { } #[pallet::error] - pub enum Error { - /// The para id has already been changed throughout the lifetime of the - /// parachain. - ParaIdAlreadyChanged, - } + pub enum Error {} #[pallet::call] impl Pallet { - #[pallet::weight(100_000 + T::DbWeight::get().writes(1))] /// Set an XCM call to the relay chain. /// /// Has to be done pre migration. // TODO: Weights + #[pallet::weight(100_000 + T::DbWeight::get().writes(1))] pub fn send_swap_call_bytes( origin: OriginFor, relay_call: Vec, @@ -107,7 +103,7 @@ pub mod pallet { T::RelayChainCallBuilder::finalize_call_into_xcm_message(relay_call, relay_balance, max_weight); let result = pallet_xcm::Pallet::::send_xcm(Here, Parent, xcm_message); - log::debug!("Sending XCM to swap para lease with result: {:?}", result); + log::debug!("Sending XCM with result: {:?}", result); Self::deposit_event(Event::LeaseSwapInitiated); diff --git a/runtimes/peregrine/Cargo.toml b/runtimes/peregrine/Cargo.toml index 1ae901e4a9..7b95c69c61 100644 --- a/runtimes/peregrine/Cargo.toml +++ b/runtimes/peregrine/Cargo.toml @@ -27,9 +27,10 @@ attestation = {path = "../../pallets/attestation", default-features = false} ctype = {path = "../../pallets/ctype", default-features = false} delegation = {path = "../../pallets/delegation", default-features = false} did = {path = "../../pallets/did", default-features = false} -kilt-support = {path = "../../support", default-features = false, optional = true} +kilt-support = {path = "../../support", default-features = false} pallet-did-lookup = {path = "../../pallets/pallet-did-lookup", default-features = false} pallet-inflation = {path = "../../pallets/pallet-inflation", default-features = false} +pallet-relay-migration = {path = "../../pallets/relay-migration", default-features = false} pallet-web3-names = {path = "../../pallets/pallet-web3-names", default-features = false} parachain-staking = {path = "../../pallets/parachain-staking", default-features = false} runtime-common = {path = "../../runtimes/common", default-features = false} @@ -113,7 +114,6 @@ runtime-benchmarks = [ "frame-system-benchmarking", "frame-system/runtime-benchmarks", "hex-literal", - "kilt-support", "kilt-support/runtime-benchmarks", "pallet-balances/runtime-benchmarks", "pallet-collective/runtime-benchmarks", diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index 4038289362..61acd0a3d6 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -48,6 +48,7 @@ use delegation::DelegationAc; use pallet_did_lookup::{linkable_account::LinkableAccountId, migrations::EthereumMigration}; pub use parachain_staking::InflationInfo; +use kilt_support::relay::RelayChainCallBuilder; use runtime_common::{ authorization::{AuthorizationId, PalletAuthorize}, constants::{self, EXISTENTIAL_DEPOSIT, KILT}, @@ -218,7 +219,9 @@ impl cumulus_pallet_parachain_system::Config for Runtime { type ReservedDmpWeight = ReservedDmpWeight; type XcmpMessageHandler = XcmpQueue; type ReservedXcmpWeight = ReservedXcmpWeight; - type CheckAssociatedRelayNumber = cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; + // We temporarily control this via the RelayMigration pallet which can toggle + // between strict and any. + type CheckAssociatedRelayNumber = RelayMigration; } impl parachain_info::Config for Runtime {} @@ -662,6 +665,12 @@ impl parachain_staking::Config for Runtime { const BLOCKS_PER_YEAR: Self::BlockNumber = constants::BLOCKS_PER_YEAR; } +impl pallet_relay_migration::Config for Runtime { + type Event = Event; + type ApproveOrigin = ApproveOrigin; + type RelayChainCallBuilder = RelayChainCallBuilder; +} + impl pallet_utility::Config for Runtime { type Event = Event; type Call = Call; @@ -881,6 +890,7 @@ construct_runtime! { // placeholder: parachain council election = 33, TechnicalMembership: pallet_membership:: = 34, Treasury: pallet_treasury = 35, + RelayMigration: pallet_relay_migration::{Pallet, Call, Storage, Event} = 36, // Utility module. Utility: pallet_utility = 40, diff --git a/runtimes/spiritnet/src/lib.rs b/runtimes/spiritnet/src/lib.rs index b7e56b06c5..5207d15553 100644 --- a/runtimes/spiritnet/src/lib.rs +++ b/runtimes/spiritnet/src/lib.rs @@ -32,7 +32,6 @@ use frame_support::{ weights::{constants::RocksDbWeight, ConstantMultiplier, Weight}, }; use frame_system::EnsureRoot; -use kilt_support::relay::RelayChainCallBuilder; use sp_api::impl_runtime_apis; use sp_core::OpaqueMetadata; use sp_runtime::{ @@ -49,6 +48,7 @@ use delegation::DelegationAc; use pallet_did_lookup::{linkable_account::LinkableAccountId, migrations::EthereumMigration}; pub use parachain_staking::InflationInfo; +use kilt_support::relay::RelayChainCallBuilder; use runtime_common::{ authorization::{AuthorizationId, PalletAuthorize}, constants::{self, EXISTENTIAL_DEPOSIT, KILT}, @@ -917,16 +917,16 @@ construct_runtime! { // Parachains pallets. Start indices at 80 to leave room. - // Many other things but also: Send and receive DMP and XCMP messages + // Among others: Send and receive DMP and XCMP messages. ParachainSystem: cumulus_pallet_parachain_system = 80, ParachainInfo: parachain_info::{Pallet, Storage, Config} = 81, // Wrap and unwrap XCMP messages to send and receive them. Queue them for later processing. XcmpQueue: cumulus_pallet_xcmp_queue::{Pallet, Call, Storage, Event} = 82, - // Build XCM scripts + // Build XCM scripts. PolkadotXcm: pallet_xcm::{Pallet, Call, Event, Origin, Config} = 83, - // Does nothing cool. Provides an origin... + // Does nothing cool, just provides an origin. CumulusXcm: cumulus_pallet_xcm::{Pallet, Event, Origin} = 84, - // Queue and DMP messages and pass them on to be executed + // Queue and pass DMP messages on to be executed. DmpQueue: cumulus_pallet_dmp_queue::{Pallet, Call, Storage, Event} = 85, } } @@ -1012,7 +1012,7 @@ pub type Executive = frame_executive::Executive< // Executes pallet hooks in reverse order of definition in construct_runtime // If we want to switch to AllPalletsWithSystem, we need to reorder the staking pallets AllPalletsReversedWithSystemFirst, - (EthereumMigration,), + EthereumMigration, >; impl_runtime_apis! { @@ -1143,7 +1143,7 @@ impl_runtime_apis! { did::Did::::get(&owner_info.owner).map(|details| (owner_info, details)) }) .map(|(owner_info, details)| { - let accounts: Vec = pallet_did_lookup::ConnectedAccounts::::iter_key_prefix(&owner_info.owner).collect(); + let accounts = pallet_did_lookup::ConnectedAccounts::::iter_key_prefix(&owner_info.owner).collect(); let service_endpoints = did::ServiceEndpoints::::iter_prefix(&owner_info.owner).map(|e| From::from(e.1)).collect(); did_rpc_runtime_api::RawDidLinkedInfo { diff --git a/support/src/relay.rs b/support/src/relay.rs index 7c08cbfb2f..7bd0187ba0 100644 --- a/support/src/relay.rs +++ b/support/src/relay.rs @@ -95,14 +95,14 @@ where weight_limit: Unlimited, }, Transact { - origin_type: OriginKind::SovereignAccount, + origin_type: OriginKind::Native, require_weight_at_most: weight, call: call.into(), }, RefundSurplus, DepositAsset { assets: All.into(), - max_assets: u32::max_value(), + max_assets: 1, beneficiary: MultiLocation { parents: 0, interior: X1(Parachain(ParachainId::get().into())), From ac572ccfbe7da0ff2eb67a375c3e794746fff554 Mon Sep 17 00:00:00 2001 From: William Freudenberger Date: Tue, 30 Aug 2022 15:41:50 +0200 Subject: [PATCH 10/11] bench: add extrinsic weight estimates --- pallets/relay-migration/src/lib.rs | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/pallets/relay-migration/src/lib.rs b/pallets/relay-migration/src/lib.rs index 3d5c4df628..8c458c2718 100644 --- a/pallets/relay-migration/src/lib.rs +++ b/pallets/relay-migration/src/lib.rs @@ -28,16 +28,6 @@ pub use pallet::*; -// TODO: Create modules and enable before merging! -// #[cfg(test)] -// mod mock; - -// #[cfg(test)] -// mod tests; - -// #[cfg(feature = "runtime-benchmarks")] -// mod benchmarking; - #[frame_support::pallet] pub mod pallet { use frame_support::pallet_prelude::*; @@ -90,8 +80,7 @@ pub mod pallet { /// Set an XCM call to the relay chain. /// /// Has to be done pre migration. - // TODO: Weights - #[pallet::weight(100_000 + T::DbWeight::get().writes(1))] + #[pallet::weight(1_000_000 + T::DbWeight::get().reads_writes(10, 10))] pub fn send_swap_call_bytes( origin: OriginFor, relay_call: Vec, @@ -114,8 +103,7 @@ pub mod pallet { /// RelayNumberStrictlyIncreases. /// /// Has to be done post migration. - // TODO: Weights - #[pallet::weight(10_000 + T::DbWeight::get().writes(1))] + #[pallet::weight(100_000 + T::DbWeight::get().reads_writes(1, 1))] pub fn enable_strict_relay_number_check(origin: OriginFor) -> DispatchResult { T::ApproveOrigin::ensure_origin(origin)?; RelayNumberStrictlyIncreases::::put(true); @@ -128,8 +116,7 @@ pub mod pallet { /// Set the associated relay block number to be AnyRelayNumber. /// /// Has to be done pre migration. - // TODO: Weights - #[pallet::weight(10_000 + T::DbWeight::get().writes(1))] + #[pallet::weight(100_000 + T::DbWeight::get().reads_writes(1, 1))] pub fn disable_strict_relay_number_check(origin: OriginFor) -> DispatchResult { T::ApproveOrigin::ensure_origin(origin)?; RelayNumberStrictlyIncreases::::put(false); From 2ce62a3ce36c44bcd2c8d30694249c68a36e3a0d Mon Sep 17 00:00:00 2001 From: William Freudenberger Date: Wed, 31 Aug 2022 14:12:32 +0200 Subject: [PATCH 11/11] fix: deps --- pallets/relay-migration/Cargo.toml | 6 ------ runtimes/peregrine/Cargo.toml | 1 + 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/pallets/relay-migration/Cargo.toml b/pallets/relay-migration/Cargo.toml index 4bfe94e355..d85c075aca 100644 --- a/pallets/relay-migration/Cargo.toml +++ b/pallets/relay-migration/Cargo.toml @@ -36,12 +36,6 @@ frame-benchmarking = {branch = "polkadot-v0.9.27", optional = true, default-feat [features] default = ["std"] -runtime-benchmarks = [ - "frame-benchmarking", - "kilt-support/runtime-benchmarks", - "frame-support/runtime-benchmarks", - "frame-system/runtime-benchmarks", -] std = [ "codec/std", "cumulus-pallet-parachain-system/std", diff --git a/runtimes/peregrine/Cargo.toml b/runtimes/peregrine/Cargo.toml index 7b95c69c61..a88079e65d 100644 --- a/runtimes/peregrine/Cargo.toml +++ b/runtimes/peregrine/Cargo.toml @@ -166,6 +166,7 @@ std = [ "pallet-indices/std", "pallet-inflation/std", "pallet-membership/std", + "pallet-relay-migration/std", "pallet-preimage/std", "pallet-proxy/std", "pallet-randomness-collective-flip/std",