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 #6623

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@ impl pallet_society::Config for Runtime {
type MaxPayouts = ConstU32<8>;
type MaxBids = ConstU32<512>;
type PalletId = SocietyPalletId;
type BlockNumberProvider = System;
type WeightInfo = ();
}

Expand Down
14 changes: 14 additions & 0 deletions prdoc/pr_6623.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: Update Society Pallet to Support Block Number Provider

doc:
- audience: Runtime Dev
description: |
This PR makes the society pallet uses the relay chain as a block provider for a parachain on a regular schedule.
To migrate existing society implementations, simply add `type BlockNumberProvider = System` to have the same behavior as before.

crates:
- name: pallet-society
bump: minor
1 change: 1 addition & 0 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,7 @@ impl pallet_society::Config for Runtime {
type ChallengePeriod = ChallengePeriod;
type MaxPayouts = MaxPayouts;
type MaxBids = MaxBids;
type BlockNumberProvider = System;
type WeightInfo = pallet_society::weights::SubstrateWeight<Runtime>;
}

Expand Down
8 changes: 5 additions & 3 deletions substrate/frame/society/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ use sp_runtime::traits::Bounded;

use crate::Pallet as Society;

fn set_block_number<T: Config<I>, I: 'static>(n: BlockNumberFor<T, I>) {
<T as Config<I>>::BlockNumberProvider::set_block_number(n);
}

fn mock_balance_deposit<T: Config<I>, I: 'static>() -> BalanceOf<T, I> {
T::Currency::minimum_balance().saturating_mul(1_000u32.into())
}
Expand Down Expand Up @@ -352,9 +356,7 @@ mod benchmarks {
let _ = Society::<T, I>::insert_member(&skeptic, 0u32.into());
Skeptic::<T, I>::put(&skeptic);
if let Period::Voting { more, .. } = Society::<T, I>::period() {
frame_system::Pallet::<T>::set_block_number(
frame_system::Pallet::<T>::block_number() + more,
);
set_block_number::<T, I>(T::BlockNumberProvider::current_block_number() + more)
}

#[extrinsic_call]
Expand Down
44 changes: 26 additions & 18 deletions substrate/frame/society/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ use frame_support::{
},
PalletId,
};
use frame_system::pallet_prelude::*;
use frame_system::pallet_prelude::{
ensure_signed, BlockNumberFor as SystemBlockNumberFor, OriginFor,
};
use rand_chacha::{
rand_core::{RngCore, SeedableRng},
ChaChaRng,
Expand All @@ -289,6 +291,10 @@ use sp_runtime::{
pub use weights::WeightInfo;

pub use pallet::*;
use sp_runtime::traits::BlockNumberProvider;

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;
Expand Down Expand Up @@ -423,7 +429,7 @@ impl<AccountId: PartialEq, Balance> BidKind<AccountId, Balance> {
}

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 +449,7 @@ pub struct PayoutRecord<Balance, PayoutsVec> {

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 +497,7 @@ pub mod pallet {
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 +510,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<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<SystemBlockNumberFor<Self>>;

/// The maximum number of payouts a member may have waiting unclaimed.
#[pallet::constant]
Expand All @@ -532,6 +538,8 @@ pub mod pallet {

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
/// Provider for the block number. Normally this is the `frame_system` pallet.
type BlockNumberProvider: BlockNumberProvider;
}

#[pallet::error]
Expand Down Expand Up @@ -757,8 +765,8 @@ pub mod pallet {
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 mut weight = Weight::zero();
let weights = T::BlockWeights::get();

Expand Down Expand Up @@ -1018,9 +1026,9 @@ pub mod pallet {
Error::<T, I>::NoPayout
);
let mut record = Payouts::<T, I>::get(&who);

let block_number = T::BlockNumberProvider::current_block_number();
if let Some((when, amount)) = record.payouts.first() {
if when <= &<frame_system::Pallet<T>>::block_number() {
if when <= &block_number {
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,11 +1405,11 @@ pub enum Period<BlockNumber> {

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 now = T::BlockNumberProvider::current_block_number();
let phase = now % rotation_period;
if phase < voting_period {
Period::Voting { elapsed: phase, more: voting_period - phase }
Expand Down Expand Up @@ -1728,7 +1736,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
});
NextHead::<T, I>::put(next_head);

let now = <frame_system::Pallet<T>>::block_number();
let now = T::BlockNumberProvider::current_block_number();
let maturity = now + Self::lock_duration(MemberCount::<T, I>::get());
Self::reward_bidder(&candidate, candidacy.bid, candidacy.kind, maturity);

Expand Down Expand Up @@ -1890,7 +1898,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
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 +1935,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
///
/// 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 +2018,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
///
/// 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
2 changes: 1 addition & 1 deletion substrate/frame/society/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ pub(crate) mod v0 {
Pallet<T, I>,
Twox64Concat,
<T as frame_system::Config>::AccountId,
Vec<(frame_system::pallet_prelude::BlockNumberFor<T>, BalanceOf<T, I>)>,
Vec<(BlockNumberFor<T, I>, BalanceOf<T, I>)>,
ValueQuery,
>;
#[storage_alias]
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/society/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl Config for Test {
type MaxPayouts = MaxPayouts;
type MaxBids = MaxBids;
type WeightInfo = ();
type BlockNumberProvider = System;
}

pub struct EnvBuilder {
Expand Down
Loading