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
@@ -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,
@@ -280,7 +280,7 @@
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 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<
@@ -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)]
@@ -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.
@@ -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]
@@ -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]
@@ -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;
}
@@ -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();

@@ -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);
}
@@ -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

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);
@@ -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

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

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

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

mismatched types

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

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

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

mismatched types

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

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

mismatched types

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

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

mismatched types
}
}

@@ -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

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

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

Candidates::<T, I>::remove(&candidate);
@@ -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) => {
@@ -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
}
@@ -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()
}

Unchanged files with check annotations Beta

.map(|p| past_payouts[p].1)
.unwrap_or(Zero::zero());
match BoundedVec::try_from(payouts) {
Ok(payouts) => Payouts::<T, I>::insert(who, PayoutRecord { paid, payouts }),

Check failure on line 327 in substrate/frame/society/src/migrations.rs

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

the trait bound `PayoutRecord<<<T as pallet::Config<I>>::Currency as frame_support::traits::Currency<<T as frame_system::Config>::AccountId>>::Balance, frame_support::BoundedVec<(<<<T as frame_system::Config>::Block as sp_runtime::traits::Block>::Header as sp_runtime::traits::Header>::Number, <<T as pallet::Config<I>>::Currency as frame_support::traits::Currency<<T as frame_system::Config>::AccountId>>::Balance), _>>: EncodeLike<PayoutRecord<<<T as pallet::Config<I>>::Currency as frame_support::traits::Currency<<T as frame_system::Config>::AccountId>>::Balance, frame_support::BoundedVec<(<<T as pallet::Config<I>>::BlockNumberProvider as sp_runtime::traits::BlockNumberProvider>::BlockNumber, <<T as pallet::Config<I>>::Currency as frame_support::traits::Currency<<T as frame_system::Config>::AccountId>>::Balance), <T as pallet::Config<I>>::MaxPayouts>>>` is not satisfied
Err(_) => debug_assert!(false, "Truncation of Payouts ineffective??"),
}
}