diff --git a/frame/democracy/Cargo.toml b/frame/democracy/Cargo.toml index f29af914295fe..c74621475acde 100644 --- a/frame/democracy/Cargo.toml +++ b/frame/democracy/Cargo.toml @@ -66,3 +66,6 @@ try-runtime = [ "pallet-scheduler/try-runtime", "sp-runtime/try-runtime" ] +# Disable hooks that would be automatically called by the runtime. +# Useful for migrating from Gov V1 to V2. +disable-hooks = [] diff --git a/frame/democracy/src/lib.rs b/frame/democracy/src/lib.rs index e538d31c6ad03..adf86edb198fe 100644 --- a/frame/democracy/src/lib.rs +++ b/frame/democracy/src/lib.rs @@ -157,17 +157,15 @@ use frame_support::{ ensure, error::BadOrigin, traits::{ - defensive_prelude::*, - schedule::{v3::Named as ScheduleNamed, DispatchTime}, - Bounded, Currency, EnsureOrigin, Get, Hash as PreimageHash, LockIdentifier, - LockableCurrency, OnUnbalanced, QueryPreimage, ReservableCurrency, StorePreimage, - WithdrawReasons, + schedule::v3::Named as ScheduleNamed, Bounded, Currency, EnsureOrigin, Get, + Hash as PreimageHash, LockIdentifier, LockableCurrency, OnUnbalanced, QueryPreimage, + ReservableCurrency, StorePreimage, WithdrawReasons, }, weights::Weight, }; use frame_system::pallet_prelude::{BlockNumberFor, OriginFor}; use sp_runtime::{ - traits::{Bounded as ArithBounded, One, Saturating, StaticLookup, Zero}, + traits::{Bounded as ArithBounded, Saturating, StaticLookup, Zero}, ArithmeticError, DispatchError, DispatchResult, }; use sp_std::prelude::*; @@ -566,6 +564,7 @@ pub mod pallet { #[pallet::hooks] impl Hooks> for Pallet { /// Weight: see `begin_block` + #[cfg(not(feature = "disable-hooks"))] fn on_initialize(n: BlockNumberFor) -> Weight { Self::begin_block(n) } @@ -1536,6 +1535,7 @@ impl Pallet { } /// Table the next waiting proposal for a vote. + #[cfg(not(feature = "disable-hooks"))] fn launch_next(now: BlockNumberFor) -> DispatchResult { if LastTabledWasExternal::::take() { Self::launch_public(now).or_else(|_| Self::launch_external(now)) @@ -1546,6 +1546,7 @@ impl Pallet { } /// Table the waiting external proposal for a vote, if there is one. + #[cfg(not(feature = "disable-hooks"))] fn launch_external(now: BlockNumberFor) -> DispatchResult { if let Some((proposal, threshold)) = >::take() { LastTabledWasExternal::::put(true); @@ -1564,7 +1565,10 @@ impl Pallet { } /// Table the waiting public proposal with the highest backing for a vote. + #[cfg(not(feature = "disable-hooks"))] fn launch_public(now: BlockNumberFor) -> DispatchResult { + use frame_support::traits::defensive_prelude::*; + let mut public_props = Self::public_props(); if let Some((winner_index, _)) = public_props.iter().enumerate().max_by_key( // defensive only: All current public proposals have an amount locked @@ -1596,11 +1600,15 @@ impl Pallet { } } + #[cfg(not(feature = "disable-hooks"))] fn bake_referendum( now: BlockNumberFor, index: ReferendumIndex, status: ReferendumStatus, BoundedCallOf, BalanceOf>, ) -> bool { + use frame_support::traits::schedule::DispatchTime; + use sp_runtime::traits::One; + let total_issuance = T::Currency::total_issuance(); let approved = status.threshold.approved(status.tally, total_issuance); @@ -1634,6 +1642,7 @@ impl Pallet { /// ## Complexity: /// If a referendum is launched or maturing, this will take full block weight if queue is not /// empty. Otherwise, `O(R)` where `R` is the number of unbaked referenda. + #[cfg(not(feature = "disable-hooks"))] fn begin_block(now: BlockNumberFor) -> Weight { let max_block_weight = T::BlockWeights::get().max_block; let mut weight = Weight::zero(); diff --git a/frame/elections-phragmen/Cargo.toml b/frame/elections-phragmen/Cargo.toml index ac4f2411dd280..14da127d7c9a3 100644 --- a/frame/elections-phragmen/Cargo.toml +++ b/frame/elections-phragmen/Cargo.toml @@ -52,6 +52,9 @@ std = [ "sp-staking/std", "sp-tracing/std" ] +# Disable hooks that would be automatically called by the runtime. +# Useful for migrating from Gov V1 to V2. +disable-hooks = [] runtime-benchmarks = [ "frame-benchmarking/runtime-benchmarks", "frame-support/runtime-benchmarks", diff --git a/frame/elections-phragmen/src/lib.rs b/frame/elections-phragmen/src/lib.rs index b23ddda4e8d1c..1da5486f88bb6 100644 --- a/frame/elections-phragmen/src/lib.rs +++ b/frame/elections-phragmen/src/lib.rs @@ -286,6 +286,7 @@ pub mod pallet { /// What to do at the end of each block. /// /// Checks if an election needs to happen or not. + #[cfg(not(feature = "disable-hooks"))] fn on_initialize(n: BlockNumberFor) -> Weight { let term_duration = T::TermDuration::get(); if !term_duration.is_zero() && (n % term_duration).is_zero() { @@ -295,6 +296,7 @@ pub mod pallet { } } + #[cfg(not(feature = "disable-hooks"))] fn integrity_test() { let block_weight = T::BlockWeights::get().max_block; // mind the order.