Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions frame/democracy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
21 changes: 15 additions & 6 deletions frame/democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -566,6 +564,7 @@ pub mod pallet {
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
/// Weight: see `begin_block`
#[cfg(not(feature = "disable-hooks"))]
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
Self::begin_block(n)
}
Expand Down Expand Up @@ -1536,6 +1535,7 @@ impl<T: Config> Pallet<T> {
}

/// Table the next waiting proposal for a vote.
#[cfg(not(feature = "disable-hooks"))]
fn launch_next(now: BlockNumberFor<T>) -> DispatchResult {
if LastTabledWasExternal::<T>::take() {
Self::launch_public(now).or_else(|_| Self::launch_external(now))
Expand All @@ -1546,6 +1546,7 @@ impl<T: Config> Pallet<T> {
}

/// Table the waiting external proposal for a vote, if there is one.
#[cfg(not(feature = "disable-hooks"))]
fn launch_external(now: BlockNumberFor<T>) -> DispatchResult {
if let Some((proposal, threshold)) = <NextExternal<T>>::take() {
LastTabledWasExternal::<T>::put(true);
Expand All @@ -1564,7 +1565,10 @@ impl<T: Config> Pallet<T> {
}

/// Table the waiting public proposal with the highest backing for a vote.
#[cfg(not(feature = "disable-hooks"))]
fn launch_public(now: BlockNumberFor<T>) -> 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
Expand Down Expand Up @@ -1596,11 +1600,15 @@ impl<T: Config> Pallet<T> {
}
}

#[cfg(not(feature = "disable-hooks"))]
fn bake_referendum(
now: BlockNumberFor<T>,
index: ReferendumIndex,
status: ReferendumStatus<BlockNumberFor<T>, BoundedCallOf<T>, BalanceOf<T>>,
) -> 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);

Expand Down Expand Up @@ -1634,6 +1642,7 @@ impl<T: Config> Pallet<T> {
/// ## 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<T>) -> Weight {
let max_block_weight = T::BlockWeights::get().max_block;
let mut weight = Weight::zero();
Expand Down
3 changes: 3 additions & 0 deletions frame/elections-phragmen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>) -> Weight {
let term_duration = T::TermDuration::get();
if !term_duration.is_zero() && (n % term_duration).is_zero() {
Expand All @@ -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.
Expand Down