Skip to content

Commit c2d45e7

Browse files
arrudagatesbkchr
andauthored
pallet-vesting: Configurable block number provider (#2403)
This PR makes the block number provider configurable through the Config trait in pallet-vesting, this gives parachains the option to use the relay chain block number provider from ParachainSystem. --------- Co-authored-by: Bastian Köcher <git@kchr.de>
1 parent 50b7b6f commit c2d45e7

11 files changed

Lines changed: 33 additions & 9 deletions

File tree

polkadot/runtime/common/src/claims.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,7 @@ mod tests {
801801
type MinVestedTransfer = MinVestedTransfer;
802802
type WeightInfo = ();
803803
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
804+
type BlockNumberProvider = System;
804805
const MAX_VESTING_SCHEDULES: u32 = 28;
805806
}
806807

polkadot/runtime/common/src/purchase.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ mod tests {
573573
type MinVestedTransfer = MinVestedTransfer;
574574
type WeightInfo = ();
575575
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
576+
type BlockNumberProvider = System;
576577
const MAX_VESTING_SCHEDULES: u32 = 28;
577578
}
578579

polkadot/runtime/rococo/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ impl pallet_vesting::Config for Runtime {
744744
type MinVestedTransfer = MinVestedTransfer;
745745
type WeightInfo = weights::pallet_vesting::WeightInfo<Runtime>;
746746
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
747+
type BlockNumberProvider = System;
747748
const MAX_VESTING_SCHEDULES: u32 = 28;
748749
}
749750

polkadot/runtime/test-runtime/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ impl pallet_vesting::Config for Runtime {
463463
type MinVestedTransfer = MinVestedTransfer;
464464
type WeightInfo = ();
465465
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
466+
type BlockNumberProvider = System;
466467
const MAX_VESTING_SCHEDULES: u32 = 28;
467468
}
468469

polkadot/runtime/westend/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,7 @@ impl pallet_vesting::Config for Runtime {
948948
type MinVestedTransfer = MinVestedTransfer;
949949
type WeightInfo = weights::pallet_vesting::WeightInfo<Runtime>;
950950
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
951+
type BlockNumberProvider = System;
951952
const MAX_VESTING_SCHEDULES: u32 = 28;
952953
}
953954

prdoc/pr_2403.prdoc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
title: Configurable block number provider in pallet-vesting
2+
3+
doc:
4+
- audience: Runtime Dev
5+
description: |
6+
Adds `BlockNumberProvider` type to pallet-vesting Config trait, allowing for custom providers instead of hardcoding frame-system.
7+
This is particularly useful for parachains wanting to use `cumulus_pallet_parachain_system::RelaychainDataProvider` with `pallet-vesting`.
8+
9+
crates: [ ]

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,7 @@ impl pallet_vesting::Config for Runtime {
15521552
type MinVestedTransfer = MinVestedTransfer;
15531553
type WeightInfo = pallet_vesting::weights::SubstrateWeight<Runtime>;
15541554
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
1555+
type BlockNumberProvider = System;
15551556
// `VestingInfo` encode length is 36bytes. 28 schedules gets encoded as 1009 bytes, which is the
15561557
// highest number of schedules that encodes less than 2^10.
15571558
const MAX_VESTING_SCHEDULES: u32 = 28;

substrate/frame/system/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,6 +1919,11 @@ impl<T: Config> BlockNumberProvider for Pallet<T> {
19191919
fn current_block_number() -> Self::BlockNumber {
19201920
Pallet::<T>::block_number()
19211921
}
1922+
1923+
#[cfg(feature = "runtime-benchmarks")]
1924+
fn set_block_number(n: BlockNumberFor<T>) {
1925+
Self::set_block_number(n)
1926+
}
19221927
}
19231928

19241929
/// Implement StoredMap for a simple single-item, provide-when-not-default system. This works fine

substrate/frame/vesting/src/benchmarking.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn add_vesting_schedules<T: Config>(
5555
let source_lookup = T::Lookup::unlookup(source.clone());
5656
T::Currency::make_free_balance_be(&source, BalanceOf::<T>::max_value());
5757

58-
System::<T>::set_block_number(BlockNumberFor::<T>::zero());
58+
T::BlockNumberProvider::set_block_number(BlockNumberFor::<T>::zero());
5959

6060
let mut total_locked: BalanceOf<T> = Zero::zero();
6161
for _ in 0..n {
@@ -116,7 +116,7 @@ benchmarks! {
116116
add_vesting_schedules::<T>(caller_lookup, s)?;
117117

118118
// At block 21, everything is unlocked.
119-
System::<T>::set_block_number(21u32.into());
119+
T::BlockNumberProvider::set_block_number(21u32.into());
120120
assert_eq!(
121121
Vesting::<T>::vesting_balance(&caller),
122122
Some(BalanceOf::<T>::zero()),
@@ -173,7 +173,7 @@ benchmarks! {
173173
add_locks::<T>(&other, l as u8);
174174
add_vesting_schedules::<T>(other_lookup.clone(), s)?;
175175
// At block 21 everything is unlocked.
176-
System::<T>::set_block_number(21u32.into());
176+
T::BlockNumberProvider::set_block_number(21u32.into());
177177

178178
assert_eq!(
179179
Vesting::<T>::vesting_balance(&other),
@@ -335,7 +335,7 @@ benchmarks! {
335335
let total_transferred = add_vesting_schedules::<T>(caller_lookup, s)?;
336336

337337
// Go to about half way through all the schedules duration. (They all start at 1, and have a duration of 20 or 21).
338-
System::<T>::set_block_number(11u32.into());
338+
T::BlockNumberProvider::set_block_number(11u32.into());
339339
// We expect half the original locked balance (+ any remainder that vests on the last block).
340340
let expected_balance = total_transferred / 2u32.into();
341341
assert_eq!(

substrate/frame/vesting/src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ use frame_system::pallet_prelude::BlockNumberFor;
7171
use scale_info::TypeInfo;
7272
use sp_runtime::{
7373
traits::{
74-
AtLeast32BitUnsigned, Bounded, Convert, MaybeSerializeDeserialize, One, Saturating,
75-
StaticLookup, Zero,
74+
AtLeast32BitUnsigned, BlockNumberProvider, Bounded, Convert, MaybeSerializeDeserialize,
75+
One, Saturating, StaticLookup, Zero,
7676
},
7777
DispatchError, RuntimeDebug,
7878
};
@@ -176,6 +176,9 @@ pub mod pallet {
176176
/// the unvested amount.
177177
type UnvestedFundsAllowedWithdrawReasons: Get<WithdrawReasons>;
178178

179+
/// Provider for the block number.
180+
type BlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;
181+
179182
/// Maximum number of vesting schedules an account may have at a given moment.
180183
const MAX_VESTING_SCHEDULES: u32;
181184
}
@@ -565,7 +568,7 @@ impl<T: Config> Pallet<T> {
565568
schedules: Vec<VestingInfo<BalanceOf<T>, BlockNumberFor<T>>>,
566569
action: VestingAction,
567570
) -> (Vec<VestingInfo<BalanceOf<T>, BlockNumberFor<T>>>, BalanceOf<T>) {
568-
let now = <frame_system::Pallet<T>>::block_number();
571+
let now = T::BlockNumberProvider::current_block_number();
569572

570573
let mut total_locked_now: BalanceOf<T> = Zero::zero();
571574
let filtered_schedules = action
@@ -649,7 +652,7 @@ impl<T: Config> Pallet<T> {
649652
let (mut schedules, mut locked_now) =
650653
Self::report_schedule_updates(schedules.to_vec(), action);
651654

652-
let now = <frame_system::Pallet<T>>::block_number();
655+
let now = T::BlockNumberProvider::current_block_number();
653656
if let Some(new_schedule) = Self::merge_vesting_info(now, schedule1, schedule2) {
654657
// Merging created a new schedule so we:
655658
// 1) need to add it to the accounts vesting schedule collection,
@@ -685,7 +688,7 @@ where
685688
/// Get the amount that is currently being vested and cannot be transferred out of this account.
686689
fn vesting_balance(who: &T::AccountId) -> Option<BalanceOf<T>> {
687690
if let Some(v) = Self::vesting(who) {
688-
let now = <frame_system::Pallet<T>>::block_number();
691+
let now = T::BlockNumberProvider::current_block_number();
689692
let total_locked_now = v.iter().fold(Zero::zero(), |total, schedule| {
690693
schedule.locked_at::<T::BlockNumberToBalance>(now).saturating_add(total)
691694
});

0 commit comments

Comments
 (0)