-
Notifications
You must be signed in to change notification settings - Fork 49
Remove without_storage_info for the grants pallet V3 #642
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
Merged
Merged
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
7b68846
remove without_storage_info
a3e90e8
Added default test case for Relese enum.
simonsso 995ffa0
Update pallets/grants/src/benchmarking.rs
rajesh-nodle aeeca4d
Update pallets/grants/src/benchmarking.rs
rajesh-nodle ffa3bb0
Update pallets/grants/src/benchmarking.rs
rajesh-nodle cab356e
Update pallets/grants/src/mock.rs
rajesh-nodle 3497e2f
Update pallets/grants/src/tests.rs
rajesh-nodle 37e503b
Update pallets/grants/src/tests.rs
rajesh-nodle 6d5002e
review closure
d149366
Merge branch 'shamb0/v2_bounded_grants' of https://github.com/NodleCo…
c491f0e
Remove syntax error
simonsso 33b8798
Merge branch 'shamb0/v2_bounded_grants' of https://github.com/NodleCo…
59dc05d
Merge remote-tracking branch 'origin/master' into shamb0/v2_bounded_g…
simonsso c789812
remove without_storage_info
595b1e6
Added default test case for Relese enum.
simonsso 06439ac
Update pallets/grants/src/benchmarking.rs
rajesh-nodle 6b95f78
Update pallets/grants/src/benchmarking.rs
rajesh-nodle bcc102a
Update pallets/grants/src/benchmarking.rs
rajesh-nodle 957deb2
Update pallets/grants/src/mock.rs
rajesh-nodle e8e81dd
review closure
f9ba4e1
Update pallets/grants/src/tests.rs
rajesh-nodle 1341c1f
Update pallets/grants/src/tests.rs
rajesh-nodle 7a833d0
Merge branch 'shamb0/v2_bounded_grants' of https://github.com/NodleCo…
6a3e968
Clarified test case cancel_tolerates_corrupted_state
simonsso 5946bd8
Merge branch 'shamb0/v2_bounded_grants' of github.com:NodleCode/chain…
simonsso b76cc73
Remove benchmarking code from codecov
simonsso 4dffaf6
unittest improvements
924e046
Merge branch 'shamb0/v2_bounded_grants' of https://github.com/NodleCo…
b4090ee
Merge branch 'shamb0/v2_bounded_grants' into shamb0/v3_bounded_grants
dbbc8fb
codecov check without migrations
9b02c78
bounded grants
8c1cda6
bounded grants
8d4e87e
remove tarpaulin ignore
rajesh-nodle dc45d1b
remove tarpaulin ignore
rajesh-nodle a7ad59a
bounded grants
280f23c
runtime migration updates
42f3df9
runtime migration updates
cc7136e
runtime migration updates
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,10 +26,14 @@ mod mock; | |
| #[cfg(test)] | ||
| mod tests; | ||
|
|
||
| mod migrations; | ||
|
|
||
| use codec::{Decode, Encode}; | ||
| use frame_support::{ | ||
| ensure, | ||
| pallet_prelude::*, | ||
| traits::{Currency, ExistenceRequirement, LockIdentifier, LockableCurrency, WithdrawReasons}, | ||
| BoundedVec, | ||
| }; | ||
| use sp_runtime::{ | ||
| traits::{AtLeast32Bit, BlockNumberProvider, CheckedAdd, Saturating, StaticLookup, Zero}, | ||
|
|
@@ -48,6 +52,21 @@ pub use weights::WeightInfo; | |
|
|
||
| pub use pallet::*; | ||
|
|
||
| // A value placed in storage that represents the current version of the POA storage. | ||
| // This value is used by the `on_runtime_upgrade` logic to determine whether we run storage | ||
| // migration logic. This should match directly with the semantic versions of the Rust crate. | ||
| #[derive(Encode, MaxEncodedLen, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo)] | ||
| enum Releases { | ||
| V0, // Legacy version | ||
| V1, // Adds storage info | ||
| } | ||
|
|
||
| impl Default for Releases { | ||
| fn default() -> Self { | ||
| Releases::V0 | ||
| } | ||
| } | ||
|
|
||
| pub type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance; | ||
| pub type VestingScheduleOf<T> = VestingSchedule<<T as frame_system::Config>::BlockNumber, BalanceOf<T>>; | ||
| pub type ListVestingScheduleOf<T> = Vec<VestingScheduleOf<T>>; | ||
|
|
@@ -63,7 +82,7 @@ pub type ScheduledItem<T> = (<T as frame_system::Config>::AccountId, Vec<Schedul | |
| /// | ||
| /// Benefits would be granted gradually, `per_period` amount every `period` of blocks | ||
| /// after `start`. | ||
| #[derive(Clone, Encode, Decode, PartialEq, Eq, RuntimeDebug, scale_info::TypeInfo)] | ||
| #[derive(Clone, Encode, Decode, PartialEq, Eq, RuntimeDebug, MaxEncodedLen, scale_info::TypeInfo)] | ||
| pub struct VestingSchedule<BlockNumber, Balance> { | ||
| pub start: BlockNumber, | ||
| pub period: BlockNumber, | ||
|
|
@@ -103,7 +122,6 @@ impl<BlockNumber: AtLeast32Bit + Copy, Balance: AtLeast32Bit + Copy> VestingSche | |
| #[frame_support::pallet] | ||
| pub mod pallet { | ||
| use super::*; | ||
| use frame_support::pallet_prelude::*; | ||
| use frame_system::pallet_prelude::*; | ||
|
|
||
| #[pallet::config] | ||
|
|
@@ -112,6 +130,9 @@ pub mod pallet { | |
| type Currency: LockableCurrency<Self::AccountId, Moment = Self::BlockNumber>; | ||
| type CancelOrigin: EnsureOrigin<Self::Origin>; | ||
| type ForceOrigin: EnsureOrigin<Self::Origin>; | ||
| /// The maximum number of vesting schedule. | ||
| #[pallet::constant] | ||
| type MaxSchedule: Get<u32>; | ||
| /// Weight information for extrinsics in this pallet. | ||
| type WeightInfo: WeightInfo; | ||
| // The block number provider | ||
|
|
@@ -120,11 +141,24 @@ pub mod pallet { | |
|
|
||
| #[pallet::pallet] | ||
| #[pallet::generate_store(pub(super) trait Store)] | ||
| #[pallet::without_storage_info] | ||
| pub struct Pallet<T>(PhantomData<T>); | ||
|
|
||
| #[pallet::hooks] | ||
| impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {} | ||
| impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> { | ||
| #[cfg(feature = "try-runtime")] | ||
| fn pre_upgrade() -> Result<(), &'static str> { | ||
| migrations::v1::pre_upgrade::<T>() | ||
| } | ||
|
|
||
| fn on_runtime_upgrade() -> frame_support::weights::Weight { | ||
| migrations::v1::on_runtime_upgrade::<T>() | ||
| } | ||
|
|
||
| #[cfg(feature = "try-runtime")] | ||
| fn post_upgrade() -> Result<(), &'static str> { | ||
| migrations::v1::post_upgrade::<T>() | ||
| } | ||
| } | ||
|
|
||
| #[pallet::call] | ||
| impl<T: Config> Pallet<T> { | ||
|
|
@@ -136,7 +170,7 @@ pub mod pallet { | |
|
|
||
| if locked_amount.is_zero() { | ||
| // No more claimable, clear | ||
| VestingSchedules::<T>::remove(who.clone()); | ||
| <VestingSchedules<T>>::remove(who.clone()); | ||
| } | ||
|
|
||
| Self::deposit_event(Event::Claimed(who, locked_amount)); | ||
|
|
@@ -186,7 +220,7 @@ pub mod pallet { | |
| collectable_funds, | ||
| ExistenceRequirement::AllowDeath, | ||
| )?; | ||
| VestingSchedules::<T>::remove(account_with_schedule.clone()); | ||
| <VestingSchedules<T>>::remove(account_with_schedule.clone()); | ||
|
|
||
| Self::deposit_event(Event::VestingSchedulesCanceled(account_with_schedule)); | ||
|
|
||
|
|
@@ -213,12 +247,21 @@ pub mod pallet { | |
| InsufficientBalanceToLock, | ||
| EmptySchedules, | ||
| VestingToSelf, | ||
| MaxScheduleOverflow, | ||
| } | ||
|
|
||
| #[pallet::storage] | ||
| #[pallet::getter(fn vesting_schedules)] | ||
| pub type VestingSchedules<T: Config> = | ||
| StorageMap<_, Blake2_128Concat, T::AccountId, Vec<VestingScheduleOf<T>>, ValueQuery>; | ||
| pub type VestingSchedules<T: Config> = CountedStorageMap< | ||
| _, | ||
| Blake2_128Concat, | ||
| T::AccountId, | ||
| BoundedVec<VestingScheduleOf<T>, T::MaxSchedule>, | ||
| ValueQuery, | ||
| >; | ||
|
|
||
| #[pallet::storage] | ||
| pub(crate) type StorageVersion<T: Config> = StorageValue<_, Releases, ValueQuery>; | ||
|
|
||
| #[pallet::genesis_config] | ||
| pub struct GenesisConfig<T: Config> { | ||
|
|
@@ -237,34 +280,26 @@ pub mod pallet { | |
| #[pallet::genesis_build] | ||
| impl<T: Config> GenesisBuild<T> for GenesisConfig<T> { | ||
| fn build(&self) { | ||
| let grants = self | ||
| .vesting | ||
| .iter() | ||
| .map(|(ref who, schedules)| { | ||
| ( | ||
| who.clone(), | ||
| schedules | ||
| .iter() | ||
| .map(|&(start, period, period_count, per_period)| VestingSchedule { | ||
| start, | ||
| period, | ||
| period_count, | ||
| per_period, | ||
| }) | ||
| .collect::<Vec<_>>(), | ||
| ) | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| // Create the required coins at genesis and add to storage | ||
| grants.iter().for_each(|(ref who, schedules)| { | ||
| let total_grants = schedules.iter().fold(Zero::zero(), |acc: BalanceOf<T>, s| { | ||
| self.vesting.iter().for_each(|(ref who, schedules)| { | ||
| let vesting_schedule: BoundedVec<VestingScheduleOf<T>, T::MaxSchedule> = schedules | ||
| .iter() | ||
| .map(|&(start, period, period_count, per_period)| VestingSchedule { | ||
| start, | ||
| period, | ||
| period_count, | ||
| per_period, | ||
| }) | ||
| .collect::<Vec<_>>() | ||
| .try_into() | ||
| .expect("Genesis Init Failed Vesting Schedules Overflow"); | ||
|
|
||
| let total_grants = vesting_schedule.iter().fold(Zero::zero(), |acc: BalanceOf<T>, s| { | ||
| acc.saturating_add(s.locked_amount(Zero::zero())) | ||
| }); | ||
|
|
||
| T::Currency::resolve_creating(who, T::Currency::issue(total_grants)); | ||
| T::Currency::set_lock(VESTING_LOCK_ID, who, total_grants, WithdrawReasons::all()); | ||
| <VestingSchedules<T>>::insert(who, schedules); | ||
| <VestingSchedules<T>>::insert(who, vesting_schedule); | ||
| }); | ||
| } | ||
| } | ||
|
|
@@ -303,13 +338,11 @@ impl<T: Config> Pallet<T> { | |
| /// Returns locked balance based on current block number. | ||
| fn locked_balance(who: &T::AccountId) -> BalanceOf<T> { | ||
| let now = T::BlockNumberProvider::current_block_number(); | ||
| Self::vesting_schedules(who) | ||
| .iter() | ||
| .fold(Zero::zero(), |acc, s| { | ||
| acc.checked_add(&s.locked_amount(now)).expect( | ||
| "locked amount is a balance and can't be higher than the total balance stored inside the same integer type; qed", | ||
| ) | ||
| }) | ||
| Self::vesting_schedules(who).iter().fold(Zero::zero(), |acc, s| { | ||
| acc.checked_add(&s.locked_amount(now)).expect( | ||
| "locked amount is a balance and can't be higher than the total balance stored inside the same integer type; qed", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo check if this code is only present in tryruntime sand never will make it into runtime. |
||
| ) | ||
| }) | ||
| } | ||
|
|
||
| fn do_add_vesting_schedule( | ||
|
|
@@ -324,9 +357,16 @@ impl<T: Config> Pallet<T> { | |
| .checked_add(&schedule_amount) | ||
| .ok_or(Error::<T>::NumOverflow)?; | ||
|
|
||
| T::Currency::transfer(from, to, schedule_amount, ExistenceRequirement::AllowDeath)?; | ||
| T::Currency::set_lock(VESTING_LOCK_ID, to, total_amount, WithdrawReasons::all()); | ||
| <VestingSchedules<T>>::mutate(to, |v| (*v).push(schedule)); | ||
| <VestingSchedules<T>>::try_mutate(to, |vesting_schedules| -> DispatchResult { | ||
| vesting_schedules | ||
| .try_push(schedule) | ||
| .map_err(|_| <Error<T>>::MaxScheduleOverflow)?; | ||
|
|
||
| T::Currency::transfer(from, to, schedule_amount, ExistenceRequirement::AllowDeath)?; | ||
| T::Currency::set_lock(VESTING_LOCK_ID, to, total_amount, WithdrawReasons::all()); | ||
|
|
||
| Ok(()) | ||
| })?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
todo check if this is done only in try-runtime and never will make it to the runtime