Skip to content

Commit fddb6a2

Browse files
authored
Update pallet society to support Block Number Provider (#6623)
This PR introduces BlockNumberProvider config for pallet society. closes part of #6297
1 parent 3726493 commit fddb6a2

File tree

7 files changed

+50
-22
lines changed

7 files changed

+50
-22
lines changed

polkadot/runtime/rococo/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,7 @@ impl pallet_society::Config for Runtime {
821821
type MaxPayouts = ConstU32<8>;
822822
type MaxBids = ConstU32<512>;
823823
type PalletId = SocietyPalletId;
824+
type BlockNumberProvider = System;
824825
type WeightInfo = ();
825826
}
826827

prdoc/pr_6623.prdoc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
2+
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json
3+
4+
title: Update Society Pallet to Support Block Number Provider
5+
6+
doc:
7+
- audience: Runtime Dev
8+
description: |
9+
This PR makes the block number provider in the Society pallet configurable so that runtimes can choose between using the system block number or an alternative source like the relay chain’s block number.
10+
If you want to keep the existing behavior, simply set the provider to System. For scenarios that require a different notion of block number—such as using a relay chain number you can select another provider,
11+
ensuring flexibility in how the Society pallet references the current block.
12+
13+
crates:
14+
- name: pallet-society
15+
bump: major

substrate/bin/node/runtime/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,7 @@ impl pallet_society::Config for Runtime {
17191719
type ChallengePeriod = ChallengePeriod;
17201720
type MaxPayouts = MaxPayouts;
17211721
type MaxBids = MaxBids;
1722+
type BlockNumberProvider = System;
17221723
type WeightInfo = pallet_society::weights::SubstrateWeight<Runtime>;
17231724
}
17241725

substrate/frame/society/src/benchmarking.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ use sp_runtime::traits::Bounded;
2929

3030
use crate::Pallet as Society;
3131

32+
fn set_block_number<T: Config<I>, I: 'static>(n: BlockNumberFor<T, I>) {
33+
<T as Config<I>>::BlockNumberProvider::set_block_number(n);
34+
}
35+
3236
fn mock_balance_deposit<T: Config<I>, I: 'static>() -> BalanceOf<T, I> {
3337
T::Currency::minimum_balance().saturating_mul(1_000u32.into())
3438
}
@@ -352,9 +356,7 @@ mod benchmarks {
352356
let _ = Society::<T, I>::insert_member(&skeptic, 0u32.into());
353357
Skeptic::<T, I>::put(&skeptic);
354358
if let Period::Voting { more, .. } = Society::<T, I>::period() {
355-
frame_system::Pallet::<T>::set_block_number(
356-
frame_system::Pallet::<T>::block_number() + more,
357-
);
359+
set_block_number::<T, I>(T::BlockNumberProvider::current_block_number() + more)
358360
}
359361

360362
#[extrinsic_call]

substrate/frame/society/src/lib.rs

+26-18
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,9 @@ use frame_support::{
271271
},
272272
PalletId,
273273
};
274-
use frame_system::pallet_prelude::*;
274+
use frame_system::pallet_prelude::{
275+
ensure_signed, BlockNumberFor as SystemBlockNumberFor, OriginFor,
276+
};
275277
use rand_chacha::{
276278
rand_core::{RngCore, SeedableRng},
277279
ChaChaRng,
@@ -289,6 +291,10 @@ use sp_runtime::{
289291
pub use weights::WeightInfo;
290292

291293
pub use pallet::*;
294+
use sp_runtime::traits::BlockNumberProvider;
295+
296+
pub type BlockNumberFor<T, I> =
297+
<<T as Config<I>>::BlockNumberProvider as BlockNumberProvider>::BlockNumber;
292298

293299
type BalanceOf<T, I> =
294300
<<T as Config<I>>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
@@ -423,7 +429,7 @@ impl<AccountId: PartialEq, Balance> BidKind<AccountId, Balance> {
423429
}
424430

425431
pub type PayoutsFor<T, I> =
426-
BoundedVec<(BlockNumberFor<T>, BalanceOf<T, I>), <T as Config<I>>::MaxPayouts>;
432+
BoundedVec<(BlockNumberFor<T, I>, BalanceOf<T, I>), <T as Config<I>>::MaxPayouts>;
427433

428434
/// Information concerning a member.
429435
#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
@@ -443,7 +449,7 @@ pub struct PayoutRecord<Balance, PayoutsVec> {
443449

444450
pub type PayoutRecordFor<T, I> = PayoutRecord<
445451
BalanceOf<T, I>,
446-
BoundedVec<(BlockNumberFor<T>, BalanceOf<T, I>), <T as Config<I>>::MaxPayouts>,
452+
BoundedVec<(BlockNumberFor<T, I>, BalanceOf<T, I>), <T as Config<I>>::MaxPayouts>,
447453
>;
448454

449455
/// Record for an individual new member who was elevated from a candidate recently.
@@ -491,7 +497,7 @@ pub mod pallet {
491497
type Currency: ReservableCurrency<Self::AccountId>;
492498

493499
/// Something that provides randomness in the runtime.
494-
type Randomness: Randomness<Self::Hash, BlockNumberFor<Self>>;
500+
type Randomness: Randomness<Self::Hash, BlockNumberFor<Self, I>>;
495501

496502
/// The maximum number of strikes before a member gets funds slashed.
497503
#[pallet::constant]
@@ -504,23 +510,23 @@ pub mod pallet {
504510
/// The number of blocks on which new candidates should be voted on. Together with
505511
/// `ClaimPeriod`, this sums to the number of blocks between candidate intake periods.
506512
#[pallet::constant]
507-
type VotingPeriod: Get<BlockNumberFor<Self>>;
513+
type VotingPeriod: Get<BlockNumberFor<Self, I>>;
508514

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

514520
/// The maximum duration of the payout lock.
515521
#[pallet::constant]
516-
type MaxLockDuration: Get<BlockNumberFor<Self>>;
522+
type MaxLockDuration: Get<BlockNumberFor<Self, I>>;
517523

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

521527
/// The number of blocks between membership challenges.
522528
#[pallet::constant]
523-
type ChallengePeriod: Get<BlockNumberFor<Self>>;
529+
type ChallengePeriod: Get<SystemBlockNumberFor<Self>>;
524530

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

533539
/// Weight information for extrinsics in this pallet.
534540
type WeightInfo: WeightInfo;
541+
/// Provider for the block number. Normally this is the `frame_system` pallet.
542+
type BlockNumberProvider: BlockNumberProvider;
535543
}
536544

537545
#[pallet::error]
@@ -757,8 +765,8 @@ pub mod pallet {
757765
StorageDoubleMap<_, Twox64Concat, RoundIndex, Twox64Concat, T::AccountId, Vote>;
758766

759767
#[pallet::hooks]
760-
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
761-
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
768+
impl<T: Config<I>, I: 'static> Hooks<SystemBlockNumberFor<T>> for Pallet<T, I> {
769+
fn on_initialize(n: SystemBlockNumberFor<T>) -> Weight {
762770
let mut weight = Weight::zero();
763771
let weights = T::BlockWeights::get();
764772

@@ -1018,9 +1026,9 @@ pub mod pallet {
10181026
Error::<T, I>::NoPayout
10191027
);
10201028
let mut record = Payouts::<T, I>::get(&who);
1021-
1029+
let block_number = T::BlockNumberProvider::current_block_number();
10221030
if let Some((when, amount)) = record.payouts.first() {
1023-
if when <= &<frame_system::Pallet<T>>::block_number() {
1031+
if when <= &block_number {
10241032
record.paid = record.paid.checked_add(amount).ok_or(Overflow)?;
10251033
T::Currency::transfer(&Self::payouts(), &who, *amount, AllowDeath)?;
10261034
record.payouts.remove(0);
@@ -1397,11 +1405,11 @@ pub enum Period<BlockNumber> {
13971405

13981406
impl<T: Config<I>, I: 'static> Pallet<T, I> {
13991407
/// Get the period we are currently in.
1400-
fn period() -> Period<BlockNumberFor<T>> {
1408+
fn period() -> Period<BlockNumberFor<T, I>> {
14011409
let claim_period = T::ClaimPeriod::get();
14021410
let voting_period = T::VotingPeriod::get();
14031411
let rotation_period = voting_period + claim_period;
1404-
let now = frame_system::Pallet::<T>::block_number();
1412+
let now = T::BlockNumberProvider::current_block_number();
14051413
let phase = now % rotation_period;
14061414
if phase < voting_period {
14071415
Period::Voting { elapsed: phase, more: voting_period - phase }
@@ -1728,7 +1736,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
17281736
});
17291737
NextHead::<T, I>::put(next_head);
17301738

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

@@ -1890,7 +1898,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
18901898
candidate: &T::AccountId,
18911899
value: BalanceOf<T, I>,
18921900
kind: BidKind<T::AccountId, BalanceOf<T, I>>,
1893-
maturity: BlockNumberFor<T>,
1901+
maturity: BlockNumberFor<T, I>,
18941902
) {
18951903
let value = match kind {
18961904
BidKind::Deposit(deposit) => {
@@ -1927,7 +1935,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
19271935
///
19281936
/// It is the caller's duty to ensure that `who` is already a member. This does nothing if `who`
19291937
/// is not a member or if `value` is zero.
1930-
fn bump_payout(who: &T::AccountId, when: BlockNumberFor<T>, value: BalanceOf<T, I>) {
1938+
fn bump_payout(who: &T::AccountId, when: BlockNumberFor<T, I>, value: BalanceOf<T, I>) {
19311939
if value.is_zero() {
19321940
return
19331941
}
@@ -2010,7 +2018,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
20102018
///
20112019
/// This is a rather opaque calculation based on the formula here:
20122020
/// https://www.desmos.com/calculator/9itkal1tce
2013-
fn lock_duration(x: u32) -> BlockNumberFor<T> {
2021+
fn lock_duration(x: u32) -> BlockNumberFor<T, I> {
20142022
let lock_pc = 100 - 50_000 / (x + 500);
20152023
Percent::from_percent(lock_pc as u8) * T::MaxLockDuration::get()
20162024
}

substrate/frame/society/src/migrations.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ pub(crate) mod v0 {
170170
Pallet<T, I>,
171171
Twox64Concat,
172172
<T as frame_system::Config>::AccountId,
173-
Vec<(frame_system::pallet_prelude::BlockNumberFor<T>, BalanceOf<T, I>)>,
173+
Vec<(BlockNumberFor<T, I>, BalanceOf<T, I>)>,
174174
ValueQuery,
175175
>;
176176
#[storage_alias]

substrate/frame/society/src/mock.rs

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ impl Config for Test {
8383
type MaxPayouts = MaxPayouts;
8484
type MaxBids = MaxBids;
8585
type WeightInfo = ();
86+
type BlockNumberProvider = System;
8687
}
8788

8889
pub struct EnvBuilder {

0 commit comments

Comments
 (0)