-
Notifications
You must be signed in to change notification settings - Fork 835
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
Initialize TotalValueUnbonding
#2514
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 |
---|---|---|
|
@@ -27,6 +27,16 @@ use sp_runtime::TryRuntimeError; | |
pub mod versioned { | ||
use super::*; | ||
|
||
/// V7 to V8 migration, wrapped in [`frame_support::migrations::VersionedMigration`] to ensure | ||
/// it is only performed when on-chain version is 7. | ||
pub type V7ToV8<T> = frame_support::migrations::VersionedMigration< | ||
7, | ||
8, | ||
v8::MigrateToV8<T>, | ||
crate::pallet::Pallet<T>, | ||
<T as frame_system::Config>::DbWeight, | ||
>; | ||
|
||
/// Migration V6 to V7 wrapped in a [`frame_support::migrations::VersionedMigration`], ensuring | ||
/// the migration is only performed when on-chain version is 6. | ||
pub type V6ToV7<T> = frame_support::migrations::VersionedMigration< | ||
|
@@ -47,6 +57,70 @@ pub mod versioned { | |
>; | ||
} | ||
|
||
mod v8 { | ||
use super::*; | ||
|
||
pub struct MigrateToV8<T>(sp_std::marker::PhantomData<T>); | ||
|
||
impl<T: Config> MigrateToV8<T> { | ||
fn calculate_tvu() -> BalanceOf<T> { | ||
BondedPools::<T>::iter() | ||
.map(|(id, inner)| { | ||
let bonded_account = BondedPool { id, inner: inner.clone() }.bonded_account(); | ||
let total_stake = T::Staking::total_stake(&bonded_account).unwrap_or_default(); | ||
let active_stake = | ||
T::Staking::active_stake(&bonded_account).unwrap_or_default(); | ||
total_stake - active_stake | ||
}) | ||
.reduce(|acc, balance| acc + balance) | ||
.unwrap_or_default() | ||
} | ||
} | ||
|
||
impl<T: Config> OnRuntimeUpgrade for MigrateToV8<T> { | ||
fn on_runtime_upgrade() -> Weight { | ||
let pool_count = BondedPools::<T>::count(); | ||
|
||
// the sum of all funds that are in the unbonding process of each pool | ||
let tvu = Self::calculate_tvu(); | ||
TotalValueUnbonding::<T>::set(tvu); | ||
|
||
log!(info, "Migrating {} pools with TotalValueUnbonding of {:?}", pool_count, tvu); | ||
|
||
// reads: pool_count * (BondedPool + total_stake + active_stake) | ||
// + pool_count + storage_version | ||
// writes: new storage_version + TotalValueUnbonding | ||
T::DbWeight::get() | ||
.reads_writes(pool_count.saturating_mul(3).saturating_add(2).into(), 2) | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn post_upgrade(_data: Vec<u8>) -> Result<(), TryRuntimeError> { | ||
ensure!( | ||
Pallet::<T>::on_chain_storage_version() >= 8, | ||
"nomination-pools::migration::v8: storage version must be no less than 8", | ||
); | ||
|
||
ensure!( | ||
TotalValueUnbonding::<T>::get() == Self::calculate_tvu(), | ||
"TotalValueUnbonding must equal the sum of unbonding balance of all pools.", | ||
); | ||
|
||
let members_balance_unbonding = PoolMembers::<T>::iter() | ||
.map(|(_, member)| member.unbonding_balance()) | ||
.reduce(|acc, balance| acc + balance) | ||
.unwrap_or_default(); | ||
|
||
ensure!( | ||
TotalValueUnbonding::<T>::get() <= members_balance_unbonding, | ||
"TotalValueUnbonding cannot surpass the sum of unbonding funds of members across all pools.", | ||
); | ||
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. Why's this 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. TBH, I'm not sure. :)) I'm just following the pattern above: |
||
|
||
Ok(()) | ||
} | ||
} | ||
} | ||
|
||
/// This migration accumulates and initializes the [`TotalValueLocked`] for all pools. | ||
/// | ||
/// WARNING: This migration works under the assumption that the [`BondedPools`] cannot be inflated | ||
|
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.
Should use safe math here I think
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.
active_stake <= total_stake && active_stake >= 0
should always hold right? If that's the contract, I would rather let the migration fail than carry on with the wrong states in this case.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.
It wouldn't fail, it would silently underflow :P