Skip to content

Commit

Permalink
Fix try-runtime compilation. Implement Staking migration for custody …
Browse files Browse the repository at this point in the history
…stake inclusion in Exposure
  • Loading branch information
bernardo-xxnet committed Jul 7, 2022
1 parent 2159f74 commit 0ba345b
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 10 deletions.
1 change: 0 additions & 1 deletion bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ std = [
"sp-session/std",
"pallet-sudo/std",
"frame-support/std",
"frame-benchmarking/std",
"frame-system-rpc-runtime-api/std",
"frame-system/std",
"pallet-election-provider-multi-phase/std",
Expand Down
2 changes: 1 addition & 1 deletion frame/offences/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ fn create_offender<T: Config>(n: u32, nominators: u32) -> Result<Offender<T>, &'
}

let exposure =
Exposure { total: amount.clone() * n.into(), own: amount, others: individual_exposures };
Exposure { total: amount.clone() * n.into(), custody: Default::default(), own: amount, others: individual_exposures };
let current_era = 0u32;
Staking::<T>::add_era_stakers(current_era.into(), stash.clone().into(), exposure);

Expand Down
1 change: 1 addition & 0 deletions frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,7 @@ enum Releases {
V5_0_0, // blockable validators.
V6_0_0, // removal of all storage associated with offchain phragmen.
V7_0_0, // keep track of number of nominators / validators in map
V7_5_0, // track custody stake in Exposure.
V8_0_0, // populate `SortedListProvider`.
}

Expand Down
100 changes: 98 additions & 2 deletions frame/staking/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub mod v8 {
#[cfg(feature = "try-runtime")]
pub fn pre_migrate<T: Config>() -> Result<(), &'static str> {
frame_support::ensure!(
StorageVersion::<T>::get() == crate::Releases::V7_0_0,
StorageVersion::<T>::get() == crate::Releases::V7_5_0,
"must upgrade linearly"
);

Expand All @@ -37,7 +37,7 @@ pub mod v8 {

/// Migration to sorted [`SortedListProvider`].
pub fn migrate<T: Config>() -> Weight {
if StorageVersion::<T>::get() == crate::Releases::V7_0_0 {
if StorageVersion::<T>::get() == crate::Releases::V7_5_0 {
crate::log!(info, "migrating staking to Releases::V8_0_0");

let migrated = T::SortedListProvider::regenerate(
Expand Down Expand Up @@ -68,6 +68,102 @@ pub mod v8 {
}
}

pub mod v7dot5 {
use super::*;

#[derive(Decode)]
struct OldExposure<AccountId, Balance: HasCompact> {
/// The total balance backing this validator.
#[codec(compact)]
pub total: Balance,
/// The validator's own stash that is exposed.
#[codec(compact)]
pub own: Balance,
/// The portions of nominators stashes that are exposed.
pub others: Vec<IndividualExposure<AccountId, Balance>>,
}

type OldExposureData<T> = OldExposure<<T as frame_system::Config>::AccountId, BalanceOf<T>>;

#[cfg(feature = "try-runtime")]
pub fn pre_migrate<T: Config>() -> Result<(), &'static str> {
frame_support::ensure!(
StorageVersion::<T>::get() == crate::Releases::V7_0_0,
"must upgrade linearly"
);

crate::log!(info, "👜 staking custody-rewards migration passes PRE migrate checks ✅",);
Ok(())
}

// Migration of Exposure including custody stake
pub fn migrate<T: Config>() -> Weight {
if StorageVersion::<T>::get() == crate::Releases::V7_0_0 {
crate::log!(info, "migrating staking to Releases::V7_5_0");
let mut reads_writes = 0;

ErasStakers::<T>::translate_values::<OldExposureData<T>, _>(|old| {
reads_writes += 1;
let exposure = Exposure {
total: old.total,
custody: Default::default(),
own: old.own,
others: old.others,
};
Some(exposure)
});

ErasStakersClipped::<T>::translate_values::<OldExposureData<T>, _>(|old| {
reads_writes += 1;
let exposure = Exposure {
total: old.total,
custody: Default::default(),
own: old.own,
others: old.others,
};
Some(exposure)
});

StorageVersion::<T>::put(crate::Releases::V7_5_0);
crate::log!(
info,
"👜 completed staking migration to Releases::V7_5_0 with {} Exposure structs modified",
reads_writes,
);

T::DbWeight::get().reads_writes(1+reads_writes, 1+reads_writes)
} else {
T::DbWeight::get().reads(1)
}
}

#[cfg(feature = "try-runtime")]
pub fn post_migrate<T: Config>() -> Result<(), &'static str> {
// Check all exposures have custody stake set to 0
let mut res = ErasStakers::<T>::iter_values().try_for_each(|new| {
frame_support::ensure!(
new.custody == Default::default(),
"custody value is not zero"
);
Ok(())
});
if res.is_err() {
return res
};
res = ErasStakersClipped::<T>::iter_values().try_for_each(|new| {
frame_support::ensure!(
new.custody == Default::default(),
"custody value is not zero"
);
Ok(())
});
if res.is_ok() {
crate::log!(info, "👜 staking custody-rewards migration passes POST migrate checks ✅",);
}
res
}
}

pub mod v7 {
use super::*;

Expand Down
21 changes: 15 additions & 6 deletions frame/staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ pub mod pallet {
/// True if network has been upgraded to this version.
/// Storage version of the pallet.
///
/// This is set to v7.0.0 for new networks.
/// This is set to v7.5.0 for new networks.
#[pallet::storage]
pub(crate) type StorageVersion<T: Config> = StorageValue<_, Releases, ValueQuery>;

Expand Down Expand Up @@ -533,7 +533,7 @@ pub mod pallet {
ForceEra::<T>::put(self.force_era);
CanceledSlashPayout::<T>::put(self.canceled_payout);
SlashRewardFraction::<T>::put(self.slash_reward_fraction);
StorageVersion::<T>::put(Releases::V7_0_0);
StorageVersion::<T>::put(Releases::V7_5_0);
MinNominatorBond::<T>::put(self.min_nominator_bond);
MinValidatorBond::<T>::put(self.min_validator_bond);
MinValidatorCommission::<T>::put(self.min_validator_commission);
Expand Down Expand Up @@ -694,17 +694,26 @@ pub mod pallet {
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> Weight {
if StorageVersion::<T>::get() == Releases::V6_0_0 {
migrations::v7::migrate::<T>()
if StorageVersion::<T>::get() == Releases::V7_0_0 {
migrations::v7dot5::migrate::<T>()
} else {
T::DbWeight::get().reads(1)
}
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<(), &'static str> {
if StorageVersion::<T>::get() == Releases::V6_0_0 {
migrations::v7::pre_migrate::<T>()
if StorageVersion::<T>::get() == Releases::V7_0_0 {
migrations::v7dot5::pre_migrate::<T>()
} else {
Ok(())
}
}

#[cfg(feature = "try-runtime")]
fn post_upgrade() -> Result<(), &'static str> {
if StorageVersion::<T>::get() == Releases::V7_5_0 {
migrations::v7dot5::post_migrate::<T>()
} else {
Ok(())
}
Expand Down

0 comments on commit 0ba345b

Please sign in to comment.