-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Adding try_state
hook(tips & vesting)
#13515
base: master
Are you sure you want to change the base?
Changes from 10 commits
fcaf225
70e6c00
7f01672
34800d8
e3194a1
bed7f69
4b30985
2d742fa
786f375
d9e0f3a
834a28c
42410fe
6a5a764
3c539a9
8eb23d0
dc502ee
52d40d4
fda9110
b235a18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -192,6 +192,11 @@ pub mod pallet { | |
fn integrity_test() { | ||
assert!(T::MAX_VESTING_SCHEDULES > 0, "`MaxVestingSchedules` must ge greater than 0"); | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn try_state(_n: BlockNumberFor<T>) -> Result<(), &'static str> { | ||
Self::do_try_state() | ||
} | ||
} | ||
|
||
/// Information regarding the vesting of a given account. | ||
|
@@ -416,7 +421,7 @@ pub mod pallet { | |
) -> DispatchResult { | ||
let who = ensure_signed(origin)?; | ||
if schedule1_index == schedule2_index { | ||
return Ok(()) | ||
return Ok(()); | ||
}; | ||
let schedule1_index = schedule1_index as usize; | ||
let schedule2_index = schedule2_index as usize; | ||
|
@@ -494,7 +499,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)?; | ||
|
@@ -642,12 +647,48 @@ impl<T: Config> Pallet<T> { | |
}; | ||
|
||
debug_assert!( | ||
locked_now > Zero::zero() && schedules.len() > 0 || | ||
locked_now == Zero::zero() && schedules.len() == 0 | ||
locked_now > Zero::zero() && schedules.len() > 0 | ||
|| locked_now == Zero::zero() && schedules.len() == 0 | ||
); | ||
|
||
Ok((schedules, locked_now)) | ||
} | ||
|
||
/// Ensure the correctness of the state of this pallet. | ||
/// | ||
/// This should be valid before or after each state transition of this pallet. | ||
/// | ||
/// ## Invariants: | ||
/// | ||
/// For each account currently vesting: | ||
/// * The `per_block` amount left to be claimed is equal to the | ||
/// total balance currently locked. | ||
/// | ||
/// *`total_to_vest` the total amount left to vest over all remaining blocks. | ||
/// *`total_locked` the total amount locked. | ||
/// | ||
/// * `one_extra` accounts for the extra block that | ||
/// will be added to the vesting schedule if `per_block` is bigger than `locked`. | ||
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. Please add documentation for variables the line above where the variable is declared, rather than in the doc comment |
||
#[cfg(any(feature = "try-runtime", test))] | ||
pub fn do_try_state() -> Result<(), &'static str> { | ||
Doordashcon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Vesting::<T>::iter().for_each(|(_, d)| { | ||
let vesting = d.to_vec(); | ||
let mut total_to_vest: BalanceOf<T> = Zero::zero(); | ||
let mut total_locked: BalanceOf<T> = Zero::zero(); | ||
for info in vesting.iter() { | ||
// get the number of remaining vesting blocks<>balance | ||
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. I don't understand this comment 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. the number of blocks remaining are converted to balance 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. Please update this comment with this clarification |
||
let amount_left: BalanceOf<T> = | ||
info.ending_block_as_balance::<T::BlockNumberToBalance>(); | ||
total_to_vest += info.per_block().saturating_mul(amount_left); | ||
// get the total block<>balance still locked. | ||
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. Could you please replace this |
||
total_locked += info | ||
.locked_at::<T::BlockNumberToBalance>(<frame_system::Pallet<T>>::block_number()); | ||
} | ||
let one_extra = total_to_vest.saturating_sub(total_locked); | ||
assert_eq!(total_to_vest, total_locked.saturating_add(one_extra)); | ||
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. So what this is checking is: if or more abstractly if of course it will always be true, you're asserting that |
||
}); | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<T: Config> VestingSchedule<T::AccountId> for Pallet<T> | ||
|
@@ -689,13 +730,13 @@ where | |
starting_block: T::BlockNumber, | ||
) -> 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(); | ||
|
@@ -723,7 +764,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!( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -153,7 +153,10 @@ impl ExtBuilder { | |
.assimilate_storage(&mut t) | ||
.unwrap(); | ||
let mut ext = sp_io::TestExternalities::new(t); | ||
ext.execute_with(|| System::set_block_number(1)); | ||
ext.execute_with(|| { | ||
System::set_block_number(1); | ||
Vesting::do_try_state().unwrap(); | ||
}); | ||
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. I think this doesn't really test much because it runs before there's any state set up to test? I suggest that you
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. But i still think it's all the same and there's no need to pepper existing tests, the current setup in 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. Let me know if I'm missing something, but I think we should test all functionality not just the genesis state? Like 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. Hello @liamaharon , I have made updates to this. Please review 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. Please add tests ensuring that the try-state hooks detects the issues they are intended to. They’d have picked up a bug in the assert (see other comment) |
||
ext | ||
} | ||
} |
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.
Outdated variable name in comment