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

pallet-vesting: Configurable block number provider #2403

Merged
merged 20 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
d5db28a
pallet-vesting: Configurable block number provider
arrudagates Nov 19, 2023
8608fa7
Changed pallet-vesting impl in rococo and westend
arrudagates Nov 19, 2023
155630f
Fixing fmt issue
arrudagates Nov 20, 2023
21b1ea5
Missing changes to pallet-vesting impls
arrudagates Nov 20, 2023
b117f5e
typo fix
arrudagates Nov 20, 2023
9b4673f
Merge branch 'master' into gabriel-vesting_block_provider
bkchr Nov 20, 2023
4f2ecbf
another missing impl
arrudagates Nov 20, 2023
6f3fa28
Change benchmarks to use BlockNumberProvider
arrudagates Nov 24, 2023
81876fe
Merge branch 'master' into gabriel-vesting_block_provider
arrudagates Nov 24, 2023
b197d24
Merge branch 'master' into gabriel-vesting_block_provider
bkchr Nov 24, 2023
b711d68
Merge branch 'master' into gabriel-vesting_block_provider
arrudagates Dec 8, 2023
6f7079e
Merge branch 'master' into gabriel-vesting_block_provider
bkchr Dec 8, 2023
1687c09
Merge branch 'master' into gabriel-vesting_block_provider
bkchr Dec 11, 2023
5d7297d
Added PRDoc file
arrudagates Dec 11, 2023
e940dbd
Undo some auto formatting
arrudagates Dec 11, 2023
215977c
Merge branch 'master' into gabriel-vesting_block_provider
arrudagates Dec 11, 2023
491596b
Update prdoc/pr_2403.prdoc
bkchr Dec 11, 2023
2d86aea
Implement set_block_number for frame-system
arrudagates Dec 11, 2023
aeeb282
Merge branch 'gabriel-vesting_block_provider' of https://github.com/a…
arrudagates Dec 11, 2023
3bce87a
Merge branch 'master' into gabriel-vesting_block_provider
arrudagates Dec 11, 2023
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
3 changes: 2 additions & 1 deletion polkadot/runtime/common/src/claims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ impl<T: Config> Pallet<T> {

let vesting = Vesting::<T>::get(&signer);
if vesting.is_some() && T::VestingSchedule::vesting_balance(&dest).is_some() {
return Err(Error::<T>::VestedBalanceExists.into())
return Err(Error::<T>::VestedBalanceExists.into());
}

// We first need to deposit the balance to ensure that the account exists.
Expand Down Expand Up @@ -801,6 +801,7 @@ mod tests {
type MinVestedTransfer = MinVestedTransfer;
type WeightInfo = ();
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
type BlockNumberProvider = System;
const MAX_VESTING_SCHEDULES: u32 = 28;
}

Expand Down
1 change: 1 addition & 0 deletions polkadot/runtime/common/src/purchase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ mod tests {
type MinVestedTransfer = MinVestedTransfer;
type WeightInfo = ();
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
type BlockNumberProvider = System;
const MAX_VESTING_SCHEDULES: u32 = 28;
}

Expand Down
5 changes: 3 additions & 2 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ pub struct OriginPrivilegeCmp;
impl PrivilegeCmp<OriginCaller> for OriginPrivilegeCmp {
fn cmp_privilege(left: &OriginCaller, right: &OriginCaller) -> Option<Ordering> {
if left == right {
return Some(Ordering::Equal)
return Some(Ordering::Equal);
}

match (left, right) {
Expand Down Expand Up @@ -744,6 +744,7 @@ impl pallet_vesting::Config for Runtime {
type MinVestedTransfer = MinVestedTransfer;
type WeightInfo = weights::pallet_vesting::WeightInfo<Runtime>;
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
type BlockNumberProvider = System;
const MAX_VESTING_SCHEDULES: u32 = 28;
}

Expand Down Expand Up @@ -2406,7 +2407,7 @@ mod remote_tests {
#[tokio::test]
async fn run_migrations() {
if var("RUN_MIGRATION_TESTS").is_err() {
return
return;
}

sp_tracing::try_init_simple();
Expand Down
1 change: 1 addition & 0 deletions polkadot/runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ impl pallet_vesting::Config for Runtime {
type MinVestedTransfer = MinVestedTransfer;
type WeightInfo = ();
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
type BlockNumberProvider = System;
const MAX_VESTING_SCHEDULES: u32 = 28;
}

Expand Down
3 changes: 2 additions & 1 deletion polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,7 @@ impl pallet_vesting::Config for Runtime {
type MinVestedTransfer = MinVestedTransfer;
type WeightInfo = weights::pallet_vesting::WeightInfo<Runtime>;
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
type BlockNumberProvider = System;
const MAX_VESTING_SCHEDULES: u32 = 28;
}

Expand Down Expand Up @@ -2460,7 +2461,7 @@ mod remote_tests {
#[tokio::test]
async fn run_migrations() {
if var("RUN_MIGRATION_TESTS").is_err() {
return
return;
}

sp_tracing::try_init_simple();
Expand Down
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 @@ -1552,6 +1552,7 @@ impl pallet_vesting::Config for Runtime {
type MinVestedTransfer = MinVestedTransfer;
type WeightInfo = pallet_vesting::weights::SubstrateWeight<Runtime>;
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
type BlockNumberProvider = System;
// `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.
const MAX_VESTING_SCHEDULES: u32 = 28;
Expand Down
8 changes: 4 additions & 4 deletions substrate/frame/vesting/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn add_vesting_schedules<T: Config>(
let source_lookup = T::Lookup::unlookup(source.clone());
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();
for _ in 0..n {
Expand Down Expand Up @@ -116,7 +116,7 @@ benchmarks! {
add_vesting_schedules::<T>(caller_lookup, s)?;

// At block 21, everything is unlocked.
System::<T>::set_block_number(21u32.into());
T::BlockNumberProvider::set_block_number(21u32.into());
assert_eq!(
Vesting::<T>::vesting_balance(&caller),
Some(BalanceOf::<T>::zero()),
Expand Down Expand Up @@ -173,7 +173,7 @@ benchmarks! {
add_locks::<T>(&other, l as u8);
add_vesting_schedules::<T>(other_lookup.clone(), s)?;
// At block 21 everything is unlocked.
System::<T>::set_block_number(21u32.into());
T::BlockNumberProvider::set_block_number(21u32.into());

assert_eq!(
Vesting::<T>::vesting_balance(&other),
Expand Down Expand Up @@ -335,7 +335,7 @@ benchmarks! {
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).
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).
let expected_balance = total_transferred / 2u32.into();
assert_eq!(
Expand Down
23 changes: 13 additions & 10 deletions substrate/frame/vesting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ use frame_system::pallet_prelude::BlockNumberFor;
use scale_info::TypeInfo;
use sp_runtime::{
traits::{
AtLeast32BitUnsigned, Bounded, Convert, MaybeSerializeDeserialize, One, Saturating,
StaticLookup, Zero,
AtLeast32BitUnsigned, BlockNumberProvider, Bounded, Convert, MaybeSerializeDeserialize,
One, Saturating, StaticLookup, Zero,
},
DispatchError, RuntimeDebug,
};
Expand Down Expand Up @@ -176,6 +176,9 @@ pub mod pallet {
/// the unvested amount.
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.
const MAX_VESTING_SCHEDULES: u32;
}
Expand Down Expand Up @@ -411,7 +414,7 @@ pub mod pallet {
) -> DispatchResult {
let who = ensure_signed(origin)?;
if schedule1_index == schedule2_index {
return Ok(())
return Ok(());
arrudagates marked this conversation as resolved.
Show resolved Hide resolved
};
let schedule1_index = schedule1_index as usize;
let schedule2_index = schedule2_index as usize;
Expand Down Expand Up @@ -519,7 +522,7 @@ impl<T: Config> Pallet<T> {
// Validate user inputs.
ensure!(schedule.locked() >= T::MinVestedTransfer::get(), Error::<T>::AmountLow);
if !schedule.is_valid() {
return Err(Error::<T>::InvalidScheduleParams.into())
return Err(Error::<T>::InvalidScheduleParams.into());
};
let target = T::Lookup::lookup(target)?;
let source = T::Lookup::lookup(source)?;
Expand Down Expand Up @@ -565,7 +568,7 @@ impl<T: Config> Pallet<T> {
schedules: Vec<VestingInfo<BalanceOf<T>, BlockNumberFor<T>>>,
action: VestingAction,
) -> (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 filtered_schedules = action
Expand Down Expand Up @@ -649,7 +652,7 @@ impl<T: Config> Pallet<T> {
let (mut schedules, mut locked_now) =
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) {
// Merging created a new schedule so we:
// 1) need to add it to the accounts vesting schedule collection,
Expand Down Expand Up @@ -685,7 +688,7 @@ where
/// 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>> {
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| {
schedule.locked_at::<T::BlockNumberToBalance>(now).saturating_add(total)
});
Expand Down Expand Up @@ -714,13 +717,13 @@ where
starting_block: BlockNumberFor<T>,
) -> DispatchResult {
if locked.is_zero() {
return Ok(())
return Ok(());
}

let vesting_schedule = VestingInfo::new(locked, per_block, starting_block);
// Check for `per_block` or `locked` of 0.
if !vesting_schedule.is_valid() {
return Err(Error::<T>::InvalidScheduleParams.into())
return Err(Error::<T>::InvalidScheduleParams.into());
};

let mut schedules = Self::vesting(who).unwrap_or_default();
Expand Down Expand Up @@ -748,7 +751,7 @@ where
) -> DispatchResult {
// Check for `per_block` or `locked` of 0.
if !VestingInfo::new(locked, per_block, starting_block).is_valid() {
return Err(Error::<T>::InvalidScheduleParams.into())
return Err(Error::<T>::InvalidScheduleParams.into());
}

ensure!(
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/vesting/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ impl Config for Test {
type MinVestedTransfer = MinVestedTransfer;
type WeightInfo = ();
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
type BlockNumberProvider = System;
}

pub struct ExtBuilder {
Expand Down
Loading