diff --git a/substrate/frame/society/src/lib.rs b/substrate/frame/society/src/lib.rs index b893bb6fba7d..fb2c0abab809 100644 --- a/substrate/frame/society/src/lib.rs +++ b/substrate/frame/society/src/lib.rs @@ -271,7 +271,7 @@ use frame_support::{ }, PalletId, }; -use frame_system::pallet_prelude::*; +use frame_system::pallet_prelude::{BlockNumberFor as SystemBlockNumberFor, *}; use rand_chacha::{ rand_core::{RngCore, SeedableRng}, ChaChaRng, @@ -280,7 +280,7 @@ use scale_info::TypeInfo; use sp_runtime::{ traits::{ AccountIdConversion, CheckedAdd, CheckedSub, Hash, Saturating, StaticLookup, - TrailingZeroInput, Zero, + TrailingZeroInput, Zero, BlockNumberProvider, }, ArithmeticError::Overflow, Percent, RuntimeDebug, @@ -290,6 +290,8 @@ pub use weights::WeightInfo; pub use pallet::*; +pub type BlockNumberFor = +<>::BlockNumberProvider as BlockNumberProvider>::BlockNumber; type BalanceOf = <>::Currency as Currency<::AccountId>>::Balance; type NegativeImbalanceOf = <>::Currency as Currency< @@ -423,7 +425,7 @@ impl BidKind { } pub type PayoutsFor = - BoundedVec<(BlockNumberFor, BalanceOf), >::MaxPayouts>; + BoundedVec<(BlockNumberFor, BalanceOf), >::MaxPayouts>; /// Information concerning a member. #[derive(Encode, Decode, Copy, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] @@ -443,7 +445,7 @@ pub struct PayoutRecord { pub type PayoutRecordFor = PayoutRecord< BalanceOf, - BoundedVec<(BlockNumberFor, BalanceOf), >::MaxPayouts>, + BoundedVec<(BlockNumberFor, BalanceOf), >::MaxPayouts>, >; /// Record for an individual new member who was elevated from a candidate recently. @@ -491,7 +493,7 @@ pub mod pallet { type Currency: ReservableCurrency; /// Something that provides randomness in the runtime. - type Randomness: Randomness>; + type Randomness: Randomness>; /// The maximum number of strikes before a member gets funds slashed. #[pallet::constant] @@ -504,23 +506,23 @@ pub mod pallet { /// The number of blocks on which new candidates should be voted on. Together with /// `ClaimPeriod`, this sums to the number of blocks between candidate intake periods. #[pallet::constant] - type VotingPeriod: Get>; + type VotingPeriod: Get>; /// The number of blocks on which new candidates can claim their membership and be the /// named head. #[pallet::constant] - type ClaimPeriod: Get>; + type ClaimPeriod: Get>; /// The maximum duration of the payout lock. #[pallet::constant] - type MaxLockDuration: Get>; + type MaxLockDuration: Get>; /// The origin that is allowed to call `found`. type FounderSetOrigin: EnsureOrigin; /// The number of blocks between membership challenges. #[pallet::constant] - type ChallengePeriod: Get>; + type ChallengePeriod: Get>; /// The maximum number of payouts a member may have waiting unclaimed. #[pallet::constant] @@ -530,6 +532,9 @@ pub mod pallet { #[pallet::constant] type MaxBids: Get; + /// Provider for the block number, normally this is the frame_system. + type BlockNumberProvider: BlockNumberProvider; + /// Weight information for extrinsics in this pallet. type WeightInfo: WeightInfo; } @@ -757,8 +762,9 @@ pub mod pallet { StorageDoubleMap<_, Twox64Concat, RoundIndex, Twox64Concat, T::AccountId, Vote>; #[pallet::hooks] - impl, I: 'static> Hooks> for Pallet { - fn on_initialize(n: BlockNumberFor) -> Weight { + impl, I: 'static> Hooks> for Pallet { + fn on_initialize(_n: SystemBlockNumberFor) -> Weight { + let now = T::BlockNumberProvider::current_block_number(); let mut weight = Weight::zero(); let weights = T::BlockWeights::get(); @@ -782,7 +788,7 @@ pub mod pallet { } // Run a challenge rotation - if (n % T::ChallengePeriod::get()).is_zero() { + if (now % T::ChallengePeriod::get()).is_zero() { Self::rotate_challenge(&mut rng); weight.saturating_accrue(weights.max_block / 20); } @@ -1397,7 +1403,7 @@ pub enum Period { impl, I: 'static> Pallet { /// Get the period we are currently in. - fn period() -> Period> { + fn period() -> Period> { let claim_period = T::ClaimPeriod::get(); let voting_period = T::VotingPeriod::get(); let rotation_period = voting_period + claim_period; @@ -1890,7 +1896,7 @@ impl, I: 'static> Pallet { candidate: &T::AccountId, value: BalanceOf, kind: BidKind>, - maturity: BlockNumberFor, + maturity: BlockNumberFor, ) { let value = match kind { BidKind::Deposit(deposit) => { @@ -1927,7 +1933,7 @@ impl, I: 'static> Pallet { /// /// It is the caller's duty to ensure that `who` is already a member. This does nothing if `who` /// is not a member or if `value` is zero. - fn bump_payout(who: &T::AccountId, when: BlockNumberFor, value: BalanceOf) { + fn bump_payout(who: &T::AccountId, when: BlockNumberFor, value: BalanceOf) { if value.is_zero() { return } @@ -2010,7 +2016,7 @@ impl, I: 'static> Pallet { /// /// This is a rather opaque calculation based on the formula here: /// https://www.desmos.com/calculator/9itkal1tce - fn lock_duration(x: u32) -> BlockNumberFor { + fn lock_duration(x: u32) -> BlockNumberFor { let lock_pc = 100 - 50_000 / (x + 500); Percent::from_percent(lock_pc as u8) * T::MaxLockDuration::get() }