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

Update pallet-society to support Block Number Provider #7094

Closed
Closed
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
38 changes: 22 additions & 16 deletions substrate/frame/society/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@
},
PalletId,
};
use frame_system::pallet_prelude::*;
use frame_system::pallet_prelude::{BlockNumberFor as SystemBlockNumberFor, *};
use rand_chacha::{
rand_core::{RngCore, SeedableRng},
ChaChaRng,
Expand All @@ -280,7 +280,7 @@
use sp_runtime::{
traits::{
AccountIdConversion, CheckedAdd, CheckedSub, Hash, Saturating, StaticLookup,
TrailingZeroInput, Zero,
TrailingZeroInput, Zero, BlockNumberProvider,
},
ArithmeticError::Overflow,
Percent, RuntimeDebug,
Expand All @@ -290,6 +290,8 @@

pub use pallet::*;

pub type BlockNumberFor<T, I> =
<<T as Config<I>>::BlockNumberProvider as BlockNumberProvider>::BlockNumber;
type BalanceOf<T, I> =
<<T as Config<I>>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
type NegativeImbalanceOf<T, I> = <<T as Config<I>>::Currency as Currency<
Expand Down Expand Up @@ -423,7 +425,7 @@
}

pub type PayoutsFor<T, I> =
BoundedVec<(BlockNumberFor<T>, BalanceOf<T, I>), <T as Config<I>>::MaxPayouts>;
BoundedVec<(BlockNumberFor<T, I>, BalanceOf<T, I>), <T as Config<I>>::MaxPayouts>;

/// Information concerning a member.
#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
Expand All @@ -443,7 +445,7 @@

pub type PayoutRecordFor<T, I> = PayoutRecord<
BalanceOf<T, I>,
BoundedVec<(BlockNumberFor<T>, BalanceOf<T, I>), <T as Config<I>>::MaxPayouts>,
BoundedVec<(BlockNumberFor<T, I>, BalanceOf<T, I>), <T as Config<I>>::MaxPayouts>,
>;

/// Record for an individual new member who was elevated from a candidate recently.
Expand Down Expand Up @@ -491,7 +493,7 @@
type Currency: ReservableCurrency<Self::AccountId>;

/// Something that provides randomness in the runtime.
type Randomness: Randomness<Self::Hash, BlockNumberFor<Self>>;
type Randomness: Randomness<Self::Hash, BlockNumberFor<Self, I>>;

/// The maximum number of strikes before a member gets funds slashed.
#[pallet::constant]
Expand All @@ -504,23 +506,23 @@
/// 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<BlockNumberFor<Self>>;
type VotingPeriod: Get<BlockNumberFor<Self, I>>;

/// The number of blocks on which new candidates can claim their membership and be the
/// named head.
#[pallet::constant]
type ClaimPeriod: Get<BlockNumberFor<Self>>;
type ClaimPeriod: Get<BlockNumberFor<Self, I>>;

/// The maximum duration of the payout lock.
#[pallet::constant]
type MaxLockDuration: Get<BlockNumberFor<Self>>;
type MaxLockDuration: Get<BlockNumberFor<Self, I>>;

/// The origin that is allowed to call `found`.
type FounderSetOrigin: EnsureOrigin<Self::RuntimeOrigin>;

/// The number of blocks between membership challenges.
#[pallet::constant]
type ChallengePeriod: Get<BlockNumberFor<Self>>;
type ChallengePeriod: Get<BlockNumberFor<Self, I>>;

/// The maximum number of payouts a member may have waiting unclaimed.
#[pallet::constant]
Expand All @@ -530,6 +532,9 @@
#[pallet::constant]
type MaxBids: Get<u32>;

/// Provider for the block number, normally this is the frame_system.
type BlockNumberProvider: BlockNumberProvider;

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -757,8 +762,9 @@
StorageDoubleMap<_, Twox64Concat, RoundIndex, Twox64Concat, T::AccountId, Vote>;

#[pallet::hooks]
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
impl<T: Config<I>, I: 'static> Hooks<SystemBlockNumberFor<T>> for Pallet<T, I> {
fn on_initialize(_n: SystemBlockNumberFor<T>) -> Weight {
let now = T::BlockNumberProvider::current_block_number();
let mut weight = Weight::zero();
let weights = T::BlockWeights::get();

Expand All @@ -782,7 +788,7 @@
}

// 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);
}
Expand Down Expand Up @@ -1020,7 +1026,7 @@
let mut record = Payouts::<T, I>::get(&who);

if let Some((when, amount)) = record.payouts.first() {
if when <= &<frame_system::Pallet<T>>::block_number() {

Check failure on line 1029 in substrate/frame/society/src/lib.rs

View workflow job for this annotation

GitHub Actions / cargo-check-all-crate-macos

mismatched types
record.paid = record.paid.checked_add(amount).ok_or(Overflow)?;
T::Currency::transfer(&Self::payouts(), &who, *amount, AllowDeath)?;
record.payouts.remove(0);
Expand Down Expand Up @@ -1397,16 +1403,16 @@

impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Get the period we are currently in.
fn period() -> Period<BlockNumberFor<T>> {
fn period() -> Period<BlockNumberFor<T, I>> {
let claim_period = T::ClaimPeriod::get();
let voting_period = T::VotingPeriod::get();
let rotation_period = voting_period + claim_period;
let now = frame_system::Pallet::<T>::block_number();
let phase = now % rotation_period;

Check failure on line 1411 in substrate/frame/society/src/lib.rs

View workflow job for this annotation

GitHub Actions / cargo-check-all-crate-macos

mismatched types
if phase < voting_period {

Check failure on line 1412 in substrate/frame/society/src/lib.rs

View workflow job for this annotation

GitHub Actions / cargo-check-all-crate-macos

mismatched types
Period::Voting { elapsed: phase, more: voting_period - phase }

Check failure on line 1413 in substrate/frame/society/src/lib.rs

View workflow job for this annotation

GitHub Actions / cargo-check-all-crate-macos

mismatched types

Check failure on line 1413 in substrate/frame/society/src/lib.rs

View workflow job for this annotation

GitHub Actions / cargo-check-all-crate-macos

mismatched types
} else {
Period::Claim { elapsed: phase - voting_period, more: rotation_period - phase }

Check failure on line 1415 in substrate/frame/society/src/lib.rs

View workflow job for this annotation

GitHub Actions / cargo-check-all-crate-macos

mismatched types

Check failure on line 1415 in substrate/frame/society/src/lib.rs

View workflow job for this annotation

GitHub Actions / cargo-check-all-crate-macos

mismatched types

Check failure on line 1415 in substrate/frame/society/src/lib.rs

View workflow job for this annotation

GitHub Actions / cargo-check-all-crate-macos

mismatched types
}
}

Expand Down Expand Up @@ -1729,7 +1735,7 @@
NextHead::<T, I>::put(next_head);

let now = <frame_system::Pallet<T>>::block_number();
let maturity = now + Self::lock_duration(MemberCount::<T, I>::get());

Check failure on line 1738 in substrate/frame/society/src/lib.rs

View workflow job for this annotation

GitHub Actions / cargo-check-all-crate-macos

mismatched types
Self::reward_bidder(&candidate, candidacy.bid, candidacy.kind, maturity);

Candidates::<T, I>::remove(&candidate);
Expand Down Expand Up @@ -1890,7 +1896,7 @@
candidate: &T::AccountId,
value: BalanceOf<T, I>,
kind: BidKind<T::AccountId, BalanceOf<T, I>>,
maturity: BlockNumberFor<T>,
maturity: BlockNumberFor<T, I>,
) {
let value = match kind {
BidKind::Deposit(deposit) => {
Expand Down Expand Up @@ -1927,7 +1933,7 @@
///
/// 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<T>, value: BalanceOf<T, I>) {
fn bump_payout(who: &T::AccountId, when: BlockNumberFor<T, I>, value: BalanceOf<T, I>) {
if value.is_zero() {
return
}
Expand Down Expand Up @@ -2010,7 +2016,7 @@
///
/// This is a rather opaque calculation based on the formula here:
/// https://www.desmos.com/calculator/9itkal1tce
fn lock_duration(x: u32) -> BlockNumberFor<T> {
fn lock_duration(x: u32) -> BlockNumberFor<T, I> {
let lock_pc = 100 - 50_000 / (x + 500);
Percent::from_percent(lock_pc as u8) * T::MaxLockDuration::get()
}
Expand Down
Loading