Skip to content
Merged
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions pallets/relay-migration/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
[package]
authors = ["KILT <info@kilt.io>"]
description = "Initiate migration to another relay chain"
edition = "2021"
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"]

[dependencies]
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"}
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"}
scale-info = {version = "2.1.1", default-features = false, features = ["derive"]}
xcm = {git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.27"}

# 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"}

[features]
default = ["std"]
std = [
"codec/std",
"cumulus-pallet-parachain-system/std",
"cumulus-primitives-core/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"kilt-support/std",
"log/std",
"pallet-xcm/std",
"polkadot-core-primitives/std",
"polkadot-runtime-common/std",
"scale-info/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",
]
144 changes: 144 additions & 0 deletions pallets/relay-migration/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// 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 <https://www.gnu.org/licenses/>.

// If you feel like getting in touch with us, you can do so at info@botlabs.org

//! # Relay migration initialization pallet
//!
//! 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`]

#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet::*;

#[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]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);

#[pallet::config]
pub trait Config: frame_system::Config + pallet_xcm::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

/// Origin from which calls of this pallet can be made.
type ApproveOrigin: EnsureOrigin<<Self as frame_system::Config>::Origin>;

/// The Call builder for communicating with RelayChain via XCM
/// messaging.
type RelayChainCallBuilder: RelayCallBuilder<
AccountId = Self::AccountId,
Balance = polkadot_core_primitives::Balance,
>;
}

/// Switch between RelayNumberStrictlyIncreases and AnyRelayNumber.
#[pallet::storage]
#[pallet::getter(fn relay_block_number_strictly_increases)]
pub(crate) type RelayNumberStrictlyIncreases<T: Config> = StorageValue<_, bool, ValueQuery>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// 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<T> {}

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Set an XCM call to the relay chain.
///
/// Has to be done pre migration.
#[pallet::weight(1_000_000 + T::DbWeight::get().reads_writes(10, 10))]
pub fn send_swap_call_bytes(
origin: OriginFor<T>,
relay_call: Vec<u8>,
relay_balance: u128,
max_weight: u64,
) -> DispatchResult {
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::<T>::send_xcm(Here, Parent, xcm_message);
log::debug!("Sending XCM with result: {:?}", result);

Self::deposit_event(Event::LeaseSwapInitiated);

Ok(())
}

/// Set the associated relay block number to be
/// RelayNumberStrictlyIncreases.
///
/// Has to be done post migration.
#[pallet::weight(100_000 + T::DbWeight::get().reads_writes(1, 1))]
pub fn enable_strict_relay_number_check(origin: OriginFor<T>) -> DispatchResult {
T::ApproveOrigin::ensure_origin(origin)?;
RelayNumberStrictlyIncreases::<T>::put(true);

Self::deposit_event(Event::RelayNumberCheckSet { strict: true });

Ok(())
}

/// Set the associated relay block number to be AnyRelayNumber.
///
/// Has to be done pre migration.
#[pallet::weight(100_000 + T::DbWeight::get().reads_writes(1, 1))]
pub fn disable_strict_relay_number_check(origin: OriginFor<T>) -> DispatchResult {
T::ApproveOrigin::ensure_origin(origin)?;
RelayNumberStrictlyIncreases::<T>::put(false);

Self::deposit_event(Event::RelayNumberCheckSet { strict: false });

Ok(())
}
}

impl<T: Config> cumulus_pallet_parachain_system::CheckAssociatedRelayNumber for Pallet<T> {
fn check_associated_relay_number(
current: polkadot_core_primitives::BlockNumber,
previous: polkadot_core_primitives::BlockNumber,
) {
if RelayNumberStrictlyIncreases::<T>::get() {
cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases::check_associated_relay_number(
current, previous,
)
} else {
cumulus_pallet_parachain_system::AnyRelayNumber::check_associated_relay_number(current, previous)
}
}
}
}
5 changes: 3 additions & 2 deletions runtimes/peregrine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
12 changes: 11 additions & 1 deletion runtimes/peregrine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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 {}
Expand Down Expand Up @@ -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<Runtime, ParachainInfo>;
}

impl pallet_utility::Config for Runtime {
type Event = Event;
type Call = Call;
Expand Down Expand Up @@ -881,6 +890,7 @@ construct_runtime! {
// placeholder: parachain council election = 33,
TechnicalMembership: pallet_membership::<Instance1> = 34,
Treasury: pallet_treasury = 35,
RelayMigration: pallet_relay_migration::{Pallet, Call, Storage, Event<T>} = 36,

// Utility module.
Utility: pallet_utility = 40,
Expand Down
6 changes: 4 additions & 2 deletions runtimes/spiritnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Loading