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 2 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
5 changes: 3 additions & 2 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,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 @@ -719,6 +719,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 @@ -2290,7 +2291,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
36 changes: 18 additions & 18 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,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 @@ -1020,11 +1021,12 @@ impl InstanceFilter<RuntimeCall> for ProxyType {
ProxyType::Staking => {
matches!(
c,
RuntimeCall::Staking(..) |
RuntimeCall::Session(..) | RuntimeCall::Utility(..) |
RuntimeCall::FastUnstake(..) |
RuntimeCall::VoterList(..) |
RuntimeCall::NominationPools(..)
RuntimeCall::Staking(..)
| RuntimeCall::Session(..)
| RuntimeCall::Utility(..)
| RuntimeCall::FastUnstake(..)
| RuntimeCall::VoterList(..)
| RuntimeCall::NominationPools(..)
)
},
ProxyType::NominationPools => {
Expand All @@ -1040,24 +1042,24 @@ impl InstanceFilter<RuntimeCall> for ProxyType {
ProxyType::Governance => matches!(
c,
// OpenGov calls
RuntimeCall::ConvictionVoting(..) |
RuntimeCall::Referenda(..) |
RuntimeCall::Whitelist(..)
RuntimeCall::ConvictionVoting(..)
| RuntimeCall::Referenda(..)
| RuntimeCall::Whitelist(..)
),
ProxyType::IdentityJudgement => matches!(
c,
RuntimeCall::Identity(pallet_identity::Call::provide_judgement { .. }) |
RuntimeCall::Utility(..)
RuntimeCall::Identity(pallet_identity::Call::provide_judgement { .. })
| RuntimeCall::Utility(..)
),
ProxyType::CancelProxy => {
matches!(c, RuntimeCall::Proxy(pallet_proxy::Call::reject_announcement { .. }))
},
ProxyType::Auction => matches!(
c,
RuntimeCall::Auctions(..) |
RuntimeCall::Crowdloan(..) |
RuntimeCall::Registrar(..) |
RuntimeCall::Slots(..)
RuntimeCall::Auctions(..)
| RuntimeCall::Crowdloan(..)
| RuntimeCall::Registrar(..)
| RuntimeCall::Slots(..)
),
}
}
Expand Down Expand Up @@ -2338,7 +2340,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 Expand Up @@ -2408,6 +2410,4 @@ mod clean_state_migration {
"State migration clean.",
);
Ok(())
}
}
}
arrudagates marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -95,6 +95,7 @@ impl Config for Test {
type MinVestedTransfer = MinVestedTransfer;
type WeightInfo = ();
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
type BlockNumberProvider = System;
}

pub struct ExtBuilder {
Expand Down
Loading