Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Girazoki xcm queues to maintenance mode #1031

Merged
merged 33 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
da236a5
wip
girazoki Nov 23, 2021
d814ee6
WIP
girazoki Nov 23, 2021
f2a8692
WIP
girazoki Nov 24, 2021
b1d42dd
Add to runtimes
girazoki Nov 25, 2021
17b96fc
Merge remote-tracking branch 'upstream/master' into girazoki-xcm-rece…
girazoki Nov 25, 2021
c0e06df
wip
girazoki Nov 25, 2021
0873568
Add tests
girazoki Nov 26, 2021
f4b74b7
Remove commented
girazoki Nov 26, 2021
f6a0681
Update moonriver and moonbase too
girazoki Nov 26, 2021
287e3bd
Remove it.only
girazoki Nov 26, 2021
84c7990
Add types
girazoki Nov 26, 2021
da3682c
Test hooks on maintenance and normal
girazoki Nov 26, 2021
fcb18c3
EditorConfig
girazoki Nov 26, 2021
5f57175
DoNothingOnIdle
girazoki Nov 26, 2021
5e2b804
XcmTranactor also diasbled
girazoki Nov 26, 2021
98a9753
more editorconfig
girazoki Nov 26, 2021
96eb25e
Toml checker
girazoki Nov 26, 2021
3ec392e
Fix moonbeam and moonriver
girazoki Nov 26, 2021
a49d962
A forgotten DoNOtIdle
girazoki Nov 26, 2021
0b4f0b1
Prevent dead code warning
girazoki Nov 26, 2021
9098f9f
Remove maintenance mode from xcm parachain mock
girazoki Nov 26, 2021
d20bf06
non-default OnIdle
girazoki Nov 29, 2021
e4c8b3a
Remove comment from pallet-maintenance pallet
girazoki Nov 29, 2021
f2df86a
Fix DoNothingOnIdle
girazoki Nov 29, 2021
541f12f
Comments and import back cumulus
girazoki Nov 29, 2021
22c72a5
Fmt
girazoki Nov 29, 2021
1b708fc
Order hooks by execution timeline
girazoki Nov 29, 2021
3f6b3af
xcm-support feature to pallet-maintenance-mode
girazoki Nov 29, 2021
e9c6062
Add feature in all runtimes as one of them already incorporates it
girazoki Nov 29, 2021
730ca42
change to single associated type with all hooks
girazoki Nov 29, 2021
1246424
Comments indicating that people should use AllPallets if they dont wa…
girazoki Nov 29, 2021
ea43855
Disable Assets in maintenance
girazoki Nov 29, 2021
3a65632
Alphabetical order
girazoki Nov 29, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions pallets/maintenance-mode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ frame-system = { git = "https://github.com/purestake/substrate", branch = "moonb
parity-scale-codec = { version = "2.2", default-features = false }
scale-info = { version = "1.0", default-features = false, features = [ "derive" ] }
sp-runtime = { git = "https://github.com/purestake/substrate", branch = "moonbeam-polkadot-v0.9.12", default-features = false }
sp-std = { git = "https://github.com/purestake/substrate", branch = "moonbeam-polkadot-v0.9.12", default-features = false }

cumulus-primitives-core = { git = "https://github.com/purestake/cumulus", branch = "moonbeam-polkadot-v0.9.12", default-features = false }
JoshOrndorff marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
sp-core = { git = "https://github.com/purestake/substrate", branch = "moonbeam-polkadot-v0.9.12" }
Expand All @@ -26,5 +29,7 @@ std = [
"frame-system/std",
"scale-info/std",
"sp-runtime/std",
"cumulus-primitives-core/std",
"sp-std/std",
]
try-runtime = [ "frame-support/try-runtime" ]
65 changes: 63 additions & 2 deletions pallets/maintenance-mode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,24 @@ mod mock;
#[cfg(test)]
mod tests;

mod types;
pub use types::*;

use frame_support::pallet;

pub use pallet::*;

#[pallet]
pub mod pallet {
use cumulus_primitives_core::{
relay_chain::BlockNumber as RelayBlockNumber, DmpMessageHandler, ParaId, XcmpMessageHandler,
};
use frame_support::pallet_prelude::*;
use frame_support::traits::{Contains, EnsureOrigin};
use frame_support::traits::{
Contains, EnsureOrigin, OffchainWorker, OnFinalize, OnIdle, OnInitialize, OnRuntimeUpgrade,
};
use frame_system::pallet_prelude::*;

use sp_std::vec::Vec;
/// Pallet for migrations
#[pallet::pallet]
#[pallet::generate_storage_info]
Expand All @@ -81,6 +89,34 @@ pub mod pallet {
/// able to return to normal mode. For example, if your MaintenanceOrigin is a council, make
/// sure that your councilors can still cast votes.
type MaintenanceOrigin: EnsureOrigin<Self::Origin>;
/// The DMP handler to be used in normal operating mode
type NormalDmpHandler: DmpMessageHandler;
/// The DMP handler to be used in maintenance mode
type MaintenanceDmpHandler: DmpMessageHandler;
/// The XCMP handler to be used in normal operating mode
type NormalXcmpHandler: XcmpMessageHandler;
/// The XCMP handler to be used in maintenance mode
type MaintenanceXcmpHandler: XcmpMessageHandler;
/// The OnIdle hooks that should be called on normal operating mode
type NormalOnIdle: OnIdle<Self::BlockNumber>;
JoshOrndorff marked this conversation as resolved.
Show resolved Hide resolved
/// The OnIdle hooks that should be called on maintenance mode
type MaintenanceOnIdle: OnIdle<Self::BlockNumber>;
/// The OnInitialize hooks that should be called on normal operating mode
type NormalOnInitialize: OnInitialize<Self::BlockNumber>;
/// The OnInitialize hooks that should be called on maintenance mode
type MaintenanceOnInitialize: OnInitialize<Self::BlockNumber>;
/// The OnFinalize hooks that should be called on normal operating mode
type NormalOnFinalize: OnFinalize<Self::BlockNumber>;
/// The OnFinalize hooks that should be called on maintenance mode
type MaintenanceOnFinalize: OnFinalize<Self::BlockNumber>;
/// The OffchainWorker hooks that should be called on normal operating mode
type NormalOffchainWorker: OffchainWorker<Self::BlockNumber>;
/// The OffchainWorker hooks that should be called on maintenance mode
type MaintenanceOffchainWorker: OffchainWorker<Self::BlockNumber>;
/// The OnRuntimeUpgrade hooks that should be called on normal operating mode
type NormalOnRuntimeUpgrade: OnRuntimeUpgrade;
/// The OnRuntimeUpgrade hook that should be called on maintenance mode
type MaintenanceOnRuntimeUpgrade: OnRuntimeUpgrade;
}

#[pallet::event]
Expand Down Expand Up @@ -189,4 +225,29 @@ pub mod pallet {
}
}
}
impl<T: Config> DmpMessageHandler for Pallet<T> {
fn handle_dmp_messages(
iter: impl Iterator<Item = (RelayBlockNumber, Vec<u8>)>,
limit: Weight,
) -> Weight {
if MaintenanceMode::<T>::get() {
T::MaintenanceDmpHandler::handle_dmp_messages(iter, limit)
} else {
T::NormalDmpHandler::handle_dmp_messages(iter, limit)
}
}
}

impl<T: Config> XcmpMessageHandler for Pallet<T> {
fn handle_xcmp_messages<'a, I: Iterator<Item = (ParaId, RelayBlockNumber, &'a [u8])>>(
iter: I,
limit: Weight,
) -> Weight {
if MaintenanceMode::<T>::get() {
T::MaintenanceXcmpHandler::handle_xcmp_messages(iter, limit)
} else {
T::NormalXcmpHandler::handle_xcmp_messages(iter, limit)
}
}
}
}
111 changes: 111 additions & 0 deletions pallets/maintenance-mode/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright 2019-2021 PureStake Inc.
// This file is part of Moonbeam.

// Moonbeam 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.

// Moonbeam 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 Moonbeam. If not, see <http://www.gnu.org/licenses/>.

// This file contains the ExecutiveHooks type which is intended to be used
// with frame_executive::Executive. This instructs which pallets execute
// hooks in each of the normal and maintenance modes.
use super::*;
use frame_support::{
traits::{OffchainWorker, OnFinalize, OnIdle, OnInitialize, OnRuntimeUpgrade},
weights::Weight,
};
use sp_std::marker::PhantomData;

pub struct ExecutiveHooks<T>(PhantomData<T>);
type BlockNumberOf<T> = <T as frame_system::Config>::BlockNumber;

impl<T> OnIdle<BlockNumberOf<T>> for ExecutiveHooks<T>
where
T: Config,
{
fn on_idle(n: BlockNumberOf<T>, remaining_weight: Weight) -> Weight {
if Pallet::<T>::maintenance_mode() {
T::MaintenanceOnIdle::on_idle(n, remaining_weight)
} else {
T::NormalOnIdle::on_idle(n, remaining_weight)
}
}
}

impl<T> OnInitialize<BlockNumberOf<T>> for ExecutiveHooks<T>
where
T: Config,
{
fn on_initialize(n: BlockNumberOf<T>) -> Weight {
if Pallet::<T>::maintenance_mode() {
T::MaintenanceOnInitialize::on_initialize(n)
} else {
T::NormalOnInitialize::on_initialize(n)
}
}
}

impl<T> OnFinalize<BlockNumberOf<T>> for ExecutiveHooks<T>
where
T: Config,
{
fn on_finalize(n: BlockNumberOf<T>) {
if Pallet::<T>::maintenance_mode() {
T::MaintenanceOnFinalize::on_finalize(n)
} else {
T::NormalOnFinalize::on_finalize(n)
}
}
}

impl<T> OffchainWorker<BlockNumberOf<T>> for ExecutiveHooks<T>
where
T: Config,
{
fn offchain_worker(n: BlockNumberOf<T>) {
if Pallet::<T>::maintenance_mode() {
T::MaintenanceOffchainWorker::offchain_worker(n)
} else {
T::NormalOffchainWorker::offchain_worker(n)
}
}
}

impl<T> OnRuntimeUpgrade for ExecutiveHooks<T>
where
T: Config,
{
fn on_runtime_upgrade() -> Weight {
if Pallet::<T>::maintenance_mode() {
T::MaintenanceOnRuntimeUpgrade::on_runtime_upgrade()
} else {
T::NormalOnRuntimeUpgrade::on_runtime_upgrade()
}
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<(), &'static str> {
if Pallet::<T>::maintenance_mode() {
T::MaintenanceOnRuntimeUpgrade::pre_upgrade()
} else {
T::NormalOnRuntimeUpgrade::pre_upgrade()
}
}

#[cfg(feature = "try-runtime")]
fn post_upgrade() -> Result<(), &'static str> {
if Pallet::<T>::maintenance_mode() {
T::MaintenanceOnRuntimeUpgrade::post_upgrade()
} else {
T::NormalOnRuntimeUpgrade::post_upgrade()
}
}
}
56 changes: 53 additions & 3 deletions runtime/moonbase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,10 +691,10 @@ impl cumulus_pallet_parachain_system::Config for Runtime {
type Event = Event;
type OnValidationData = ();
type SelfParaId = ParachainInfo;
type DmpMessageHandler = DmpQueue;
type DmpMessageHandler = MaintenanceMode;
type ReservedDmpWeight = ReservedDmpWeight;
type OutboundXcmpMessageSource = XcmpQueue;
type XcmpMessageHandler = XcmpQueue;
type XcmpMessageHandler = MaintenanceMode;
type ReservedXcmpWeight = ReservedXcmpWeight;
}

Expand Down Expand Up @@ -1425,12 +1425,62 @@ impl Contains<Call> for NormalFilter {
}
}

use cumulus_primitives_core::{
relay_chain::BlockNumber as RelayBlockNumber, DmpMessageHandler, ParaId, XcmpMessageHandler,
};
pub struct MaintenanceDmpHandler;
impl DmpMessageHandler for MaintenanceDmpHandler {
// This implementation makes messages be queued
// Since the limit is 0, messages are queued for next iteration
fn handle_dmp_messages(
iter: impl Iterator<Item = (RelayBlockNumber, Vec<u8>)>,
_limit: Weight,
) -> Weight {
DmpQueue::handle_dmp_messages(iter, 0)
}
}

pub struct MaintenanceXcmpHandler;
impl XcmpMessageHandler for MaintenanceXcmpHandler {
// This implementation makes messages be queued
// Since the limit is 0, messages are queued for next iteration
fn handle_xcmp_messages<'a, I: Iterator<Item = (ParaId, RelayBlockNumber, &'a [u8])>>(
iter: I,
_limit: Weight,
) -> Weight {
XcmpQueue::handle_xcmp_messages(iter, 0)
}
}

// Default implementation already returns 0
// For some reason using empty tuple () isnt working
pub struct NoOnIdle;
librelois marked this conversation as resolved.
Show resolved Hide resolved
impl frame_support::traits::OnIdle<BlockNumber> for NoOnIdle {}

impl pallet_maintenance_mode::Config for Runtime {
type Event = Event;
type NormalCallFilter = NormalFilter;
type MaintenanceCallFilter = MaintenanceFilter;
type MaintenanceOrigin =
pallet_collective::EnsureProportionAtLeast<_2, _3, AccountId, TechCommitteeInstance>;
type NormalDmpHandler = DmpQueue;
type MaintenanceDmpHandler = MaintenanceDmpHandler;
type NormalXcmpHandler = XcmpQueue;
type MaintenanceXcmpHandler = MaintenanceXcmpHandler;
type NormalOnIdle = AllPallets;
// There exist only two pallets that use onIdle and these are xcmp and dmp queues
// For some reason putting an empty tumple does not work (transaction never finishes)
// We use an empty onIdle, if on the future we want one of the pallets to execute it
// we need to provide it here
type MaintenanceOnIdle = NoOnIdle;
type NormalOnInitialize = AllPallets;
type MaintenanceOnInitialize = AllPallets;
type NormalOnFinalize = AllPallets;
type MaintenanceOnFinalize = AllPallets;
type NormalOffchainWorker = AllPallets;
type MaintenanceOffchainWorker = AllPallets;
type NormalOnRuntimeUpgrade = AllPallets;
type MaintenanceOnRuntimeUpgrade = AllPallets;
}

impl pallet_proxy_genesis_companion::Config for Runtime {
Expand Down Expand Up @@ -1513,7 +1563,7 @@ pub type Executive = frame_executive::Executive<
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPallets,
pallet_maintenance_mode::ExecutiveHooks<Runtime>,
>;

// All of our runtimes share most of their Runtime API implementations.
Expand Down
31 changes: 30 additions & 1 deletion runtime/moonbase/tests/xcm_mock/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,35 @@ impl xcm_transactor::Config for Runtime {
type BaseXcmWeight = BaseXcmWeight;
}

pub struct NormalFilter;
impl frame_support::traits::Contains<Call> for NormalFilter {
fn contains(c: &Call) -> bool {
match c {
_ => true,
}
}
}

pub struct MaintenanceFilter;
impl frame_support::traits::Contains<Call> for MaintenanceFilter {
fn contains(c: &Call) -> bool {
match c {
Call::Balances(_) => false,
Call::XTokens(_) => false,
_ => true,
}
}
}

impl pallet_maintenance_mode::Config for Runtime {
type Event = Event;
type NormalCallFilter = NormalFilter;
type MaintenanceCallFilter = MaintenanceFilter;
type MaintenanceOrigin = EnsureRoot<AccountId>;
type NormalDmpHandler = MsgQueue;
type MaintenanceDmpHandler = ();
}

// We need to use the encoding from the relay mock runtime
#[derive(Encode, Decode)]
pub enum RelayCall {
Expand Down Expand Up @@ -691,7 +720,7 @@ construct_runtime!(
XTokens: orml_xtokens::{Pallet, Call, Storage, Event<T>},
AssetManager: pallet_asset_manager::{Pallet, Call, Storage, Event<T>},
XcmTransactor: xcm_transactor::{Pallet, Call, Storage, Event<T>},

MaintenanceMode: pallet_maintenance_mode::{Pallet, Call, Config, Storage, Event}
}
);

Expand Down
16 changes: 15 additions & 1 deletion runtime/moonbeam/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,20 @@ impl pallet_maintenance_mode::Config for Runtime {
type MaintenanceCallFilter = MaintenanceFilter;
type MaintenanceOrigin =
pallet_collective::EnsureProportionAtLeast<_2, _3, AccountId, TechCommitteeInstance>;
type NormalDmpHandler = ();
type MaintenanceDmpHandler = ();
type NormalXcmpHandler = ();
type MaintenanceXcmpHandler = ();
type NormalOnIdle = AllPallets;
type MaintenanceOnIdle = AllPallets;
type NormalOnInitialize = AllPallets;
type MaintenanceOnInitialize = AllPallets;
type NormalOnFinalize = AllPallets;
type MaintenanceOnFinalize = AllPallets;
type NormalOffchainWorker = AllPallets;
type MaintenanceOffchainWorker = AllPallets;
type NormalOnRuntimeUpgrade = AllPallets;
type MaintenanceOnRuntimeUpgrade = AllPallets;
}

impl pallet_proxy_genesis_companion::Config for Runtime {
Expand Down Expand Up @@ -1005,7 +1019,7 @@ pub type Executive = frame_executive::Executive<
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPallets,
MaintenanceMode::ExecutiveHooks,
>;

// All of our runtimes share most of their Runtime API implementations.
Expand Down
Loading