mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-19 17:01:02 +00:00
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>
This commit is contained in:
committed by
GitHub
parent
50b7b6f399
commit
c2d45e7e47
@@ -801,6 +801,7 @@ mod tests {
|
|||||||
type MinVestedTransfer = MinVestedTransfer;
|
type MinVestedTransfer = MinVestedTransfer;
|
||||||
type WeightInfo = ();
|
type WeightInfo = ();
|
||||||
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
|
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
|
||||||
|
type BlockNumberProvider = System;
|
||||||
const MAX_VESTING_SCHEDULES: u32 = 28;
|
const MAX_VESTING_SCHEDULES: u32 = 28;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -573,6 +573,7 @@ mod tests {
|
|||||||
type MinVestedTransfer = MinVestedTransfer;
|
type MinVestedTransfer = MinVestedTransfer;
|
||||||
type WeightInfo = ();
|
type WeightInfo = ();
|
||||||
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
|
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
|
||||||
|
type BlockNumberProvider = System;
|
||||||
const MAX_VESTING_SCHEDULES: u32 = 28;
|
const MAX_VESTING_SCHEDULES: u32 = 28;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -744,6 +744,7 @@ impl pallet_vesting::Config for Runtime {
|
|||||||
type MinVestedTransfer = MinVestedTransfer;
|
type MinVestedTransfer = MinVestedTransfer;
|
||||||
type WeightInfo = weights::pallet_vesting::WeightInfo<Runtime>;
|
type WeightInfo = weights::pallet_vesting::WeightInfo<Runtime>;
|
||||||
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
|
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
|
||||||
|
type BlockNumberProvider = System;
|
||||||
const MAX_VESTING_SCHEDULES: u32 = 28;
|
const MAX_VESTING_SCHEDULES: u32 = 28;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -463,6 +463,7 @@ impl pallet_vesting::Config for Runtime {
|
|||||||
type MinVestedTransfer = MinVestedTransfer;
|
type MinVestedTransfer = MinVestedTransfer;
|
||||||
type WeightInfo = ();
|
type WeightInfo = ();
|
||||||
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
|
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
|
||||||
|
type BlockNumberProvider = System;
|
||||||
const MAX_VESTING_SCHEDULES: u32 = 28;
|
const MAX_VESTING_SCHEDULES: u32 = 28;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -948,6 +948,7 @@ impl pallet_vesting::Config for Runtime {
|
|||||||
type MinVestedTransfer = MinVestedTransfer;
|
type MinVestedTransfer = MinVestedTransfer;
|
||||||
type WeightInfo = weights::pallet_vesting::WeightInfo<Runtime>;
|
type WeightInfo = weights::pallet_vesting::WeightInfo<Runtime>;
|
||||||
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
|
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
|
||||||
|
type BlockNumberProvider = System;
|
||||||
const MAX_VESTING_SCHEDULES: u32 = 28;
|
const MAX_VESTING_SCHEDULES: u32 = 28;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
title: Configurable block number provider in pallet-vesting
|
||||||
|
|
||||||
|
doc:
|
||||||
|
- audience: Runtime Dev
|
||||||
|
description: |
|
||||||
|
Adds `BlockNumberProvider` type to pallet-vesting Config trait, allowing for custom providers instead of hardcoding frame-system.
|
||||||
|
This is particularly useful for parachains wanting to use `cumulus_pallet_parachain_system::RelaychainDataProvider` with `pallet-vesting`.
|
||||||
|
|
||||||
|
crates: [ ]
|
||||||
@@ -1552,6 +1552,7 @@ impl pallet_vesting::Config for Runtime {
|
|||||||
type MinVestedTransfer = MinVestedTransfer;
|
type MinVestedTransfer = MinVestedTransfer;
|
||||||
type WeightInfo = pallet_vesting::weights::SubstrateWeight<Runtime>;
|
type WeightInfo = pallet_vesting::weights::SubstrateWeight<Runtime>;
|
||||||
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
|
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
|
||||||
|
type BlockNumberProvider = System;
|
||||||
// `VestingInfo` encode length is 36bytes. 28 schedules gets encoded as 1009 bytes, which is the
|
// `VestingInfo` encode length is 36bytes. 28 schedules gets encoded as 1009 bytes, which is the
|
||||||
// highest number of schedules that encodes less than 2^10.
|
// highest number of schedules that encodes less than 2^10.
|
||||||
const MAX_VESTING_SCHEDULES: u32 = 28;
|
const MAX_VESTING_SCHEDULES: u32 = 28;
|
||||||
|
|||||||
@@ -1919,6 +1919,11 @@ impl<T: Config> BlockNumberProvider for Pallet<T> {
|
|||||||
fn current_block_number() -> Self::BlockNumber {
|
fn current_block_number() -> Self::BlockNumber {
|
||||||
Pallet::<T>::block_number()
|
Pallet::<T>::block_number()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "runtime-benchmarks")]
|
||||||
|
fn set_block_number(n: BlockNumberFor<T>) {
|
||||||
|
Self::set_block_number(n)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Implement StoredMap for a simple single-item, provide-when-not-default system. This works fine
|
/// Implement StoredMap for a simple single-item, provide-when-not-default system. This works fine
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ fn add_vesting_schedules<T: Config>(
|
|||||||
let source_lookup = T::Lookup::unlookup(source.clone());
|
let source_lookup = T::Lookup::unlookup(source.clone());
|
||||||
T::Currency::make_free_balance_be(&source, BalanceOf::<T>::max_value());
|
T::Currency::make_free_balance_be(&source, BalanceOf::<T>::max_value());
|
||||||
|
|
||||||
System::<T>::set_block_number(BlockNumberFor::<T>::zero());
|
T::BlockNumberProvider::set_block_number(BlockNumberFor::<T>::zero());
|
||||||
|
|
||||||
let mut total_locked: BalanceOf<T> = Zero::zero();
|
let mut total_locked: BalanceOf<T> = Zero::zero();
|
||||||
for _ in 0..n {
|
for _ in 0..n {
|
||||||
@@ -116,7 +116,7 @@ benchmarks! {
|
|||||||
add_vesting_schedules::<T>(caller_lookup, s)?;
|
add_vesting_schedules::<T>(caller_lookup, s)?;
|
||||||
|
|
||||||
// At block 21, everything is unlocked.
|
// At block 21, everything is unlocked.
|
||||||
System::<T>::set_block_number(21u32.into());
|
T::BlockNumberProvider::set_block_number(21u32.into());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Vesting::<T>::vesting_balance(&caller),
|
Vesting::<T>::vesting_balance(&caller),
|
||||||
Some(BalanceOf::<T>::zero()),
|
Some(BalanceOf::<T>::zero()),
|
||||||
@@ -173,7 +173,7 @@ benchmarks! {
|
|||||||
add_locks::<T>(&other, l as u8);
|
add_locks::<T>(&other, l as u8);
|
||||||
add_vesting_schedules::<T>(other_lookup.clone(), s)?;
|
add_vesting_schedules::<T>(other_lookup.clone(), s)?;
|
||||||
// At block 21 everything is unlocked.
|
// At block 21 everything is unlocked.
|
||||||
System::<T>::set_block_number(21u32.into());
|
T::BlockNumberProvider::set_block_number(21u32.into());
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Vesting::<T>::vesting_balance(&other),
|
Vesting::<T>::vesting_balance(&other),
|
||||||
@@ -335,7 +335,7 @@ benchmarks! {
|
|||||||
let total_transferred = add_vesting_schedules::<T>(caller_lookup, s)?;
|
let total_transferred = add_vesting_schedules::<T>(caller_lookup, s)?;
|
||||||
|
|
||||||
// Go to about half way through all the schedules duration. (They all start at 1, and have a duration of 20 or 21).
|
// Go to about half way through all the schedules duration. (They all start at 1, and have a duration of 20 or 21).
|
||||||
System::<T>::set_block_number(11u32.into());
|
T::BlockNumberProvider::set_block_number(11u32.into());
|
||||||
// We expect half the original locked balance (+ any remainder that vests on the last block).
|
// We expect half the original locked balance (+ any remainder that vests on the last block).
|
||||||
let expected_balance = total_transferred / 2u32.into();
|
let expected_balance = total_transferred / 2u32.into();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|||||||
@@ -71,8 +71,8 @@ use frame_system::pallet_prelude::BlockNumberFor;
|
|||||||
use scale_info::TypeInfo;
|
use scale_info::TypeInfo;
|
||||||
use sp_runtime::{
|
use sp_runtime::{
|
||||||
traits::{
|
traits::{
|
||||||
AtLeast32BitUnsigned, Bounded, Convert, MaybeSerializeDeserialize, One, Saturating,
|
AtLeast32BitUnsigned, BlockNumberProvider, Bounded, Convert, MaybeSerializeDeserialize,
|
||||||
StaticLookup, Zero,
|
One, Saturating, StaticLookup, Zero,
|
||||||
},
|
},
|
||||||
DispatchError, RuntimeDebug,
|
DispatchError, RuntimeDebug,
|
||||||
};
|
};
|
||||||
@@ -176,6 +176,9 @@ pub mod pallet {
|
|||||||
/// the unvested amount.
|
/// the unvested amount.
|
||||||
type UnvestedFundsAllowedWithdrawReasons: Get<WithdrawReasons>;
|
type UnvestedFundsAllowedWithdrawReasons: Get<WithdrawReasons>;
|
||||||
|
|
||||||
|
/// Provider for the block number.
|
||||||
|
type BlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;
|
||||||
|
|
||||||
/// Maximum number of vesting schedules an account may have at a given moment.
|
/// Maximum number of vesting schedules an account may have at a given moment.
|
||||||
const MAX_VESTING_SCHEDULES: u32;
|
const MAX_VESTING_SCHEDULES: u32;
|
||||||
}
|
}
|
||||||
@@ -565,7 +568,7 @@ impl<T: Config> Pallet<T> {
|
|||||||
schedules: Vec<VestingInfo<BalanceOf<T>, BlockNumberFor<T>>>,
|
schedules: Vec<VestingInfo<BalanceOf<T>, BlockNumberFor<T>>>,
|
||||||
action: VestingAction,
|
action: VestingAction,
|
||||||
) -> (Vec<VestingInfo<BalanceOf<T>, BlockNumberFor<T>>>, BalanceOf<T>) {
|
) -> (Vec<VestingInfo<BalanceOf<T>, BlockNumberFor<T>>>, BalanceOf<T>) {
|
||||||
let now = <frame_system::Pallet<T>>::block_number();
|
let now = T::BlockNumberProvider::current_block_number();
|
||||||
|
|
||||||
let mut total_locked_now: BalanceOf<T> = Zero::zero();
|
let mut total_locked_now: BalanceOf<T> = Zero::zero();
|
||||||
let filtered_schedules = action
|
let filtered_schedules = action
|
||||||
@@ -649,7 +652,7 @@ impl<T: Config> Pallet<T> {
|
|||||||
let (mut schedules, mut locked_now) =
|
let (mut schedules, mut locked_now) =
|
||||||
Self::report_schedule_updates(schedules.to_vec(), action);
|
Self::report_schedule_updates(schedules.to_vec(), action);
|
||||||
|
|
||||||
let now = <frame_system::Pallet<T>>::block_number();
|
let now = T::BlockNumberProvider::current_block_number();
|
||||||
if let Some(new_schedule) = Self::merge_vesting_info(now, schedule1, schedule2) {
|
if let Some(new_schedule) = Self::merge_vesting_info(now, schedule1, schedule2) {
|
||||||
// Merging created a new schedule so we:
|
// Merging created a new schedule so we:
|
||||||
// 1) need to add it to the accounts vesting schedule collection,
|
// 1) need to add it to the accounts vesting schedule collection,
|
||||||
@@ -685,7 +688,7 @@ where
|
|||||||
/// Get the amount that is currently being vested and cannot be transferred out of this account.
|
/// Get the amount that is currently being vested and cannot be transferred out of this account.
|
||||||
fn vesting_balance(who: &T::AccountId) -> Option<BalanceOf<T>> {
|
fn vesting_balance(who: &T::AccountId) -> Option<BalanceOf<T>> {
|
||||||
if let Some(v) = Self::vesting(who) {
|
if let Some(v) = Self::vesting(who) {
|
||||||
let now = <frame_system::Pallet<T>>::block_number();
|
let now = T::BlockNumberProvider::current_block_number();
|
||||||
let total_locked_now = v.iter().fold(Zero::zero(), |total, schedule| {
|
let total_locked_now = v.iter().fold(Zero::zero(), |total, schedule| {
|
||||||
schedule.locked_at::<T::BlockNumberToBalance>(now).saturating_add(total)
|
schedule.locked_at::<T::BlockNumberToBalance>(now).saturating_add(total)
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -96,6 +96,7 @@ impl Config for Test {
|
|||||||
type MinVestedTransfer = MinVestedTransfer;
|
type MinVestedTransfer = MinVestedTransfer;
|
||||||
type WeightInfo = ();
|
type WeightInfo = ();
|
||||||
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
|
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
|
||||||
|
type BlockNumberProvider = System;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ExtBuilder {
|
pub struct ExtBuilder {
|
||||||
|
|||||||
Reference in New Issue
Block a user