mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 07:37:57 +00:00
BREAKING - Try-runtime: Use proper error types (#13993)
* Try-state: DispatchResult as return type * try_state for the rest of the pallets * pre_upgrade * post_upgrade * try_runtime_upgrade * fixes * bags-list fix * fix * update test * warning fix * ... * final fixes 🤞 * warning.. * frame-support * warnings * Update frame/staking/src/migrations.rs Co-authored-by: Liam Aharon <liam.aharon@hotmail.com> * fix * fix warning * nit fix * merge fixes * small fix * should be good now * missed these ones * introduce TryRuntimeError and TryRuntimeResult * fixes * fix * removed TryRuntimeResult & made some fixes * fix testsg * tests passing * unnecessary imports * Update frame/assets/src/migration.rs Co-authored-by: Keith Yeung <kungfukeith11@gmail.com> --------- Co-authored-by: Liam Aharon <liam.aharon@hotmail.com> Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>
This commit is contained in:
@@ -23,6 +23,11 @@ use frame_support::{
|
||||
traits::OnRuntimeUpgrade,
|
||||
};
|
||||
|
||||
#[cfg(feature = "try-runtime")]
|
||||
use frame_support::ensure;
|
||||
#[cfg(feature = "try-runtime")]
|
||||
use sp_runtime::TryRuntimeError;
|
||||
|
||||
/// Used for release versioning upto v12.
|
||||
///
|
||||
/// Obsolete from v13. Keeping around to make encoding/decoding of old migration code easier.
|
||||
@@ -58,7 +63,7 @@ pub mod v13 {
|
||||
pub struct MigrateToV13<T>(sp_std::marker::PhantomData<T>);
|
||||
impl<T: Config> OnRuntimeUpgrade for MigrateToV13<T> {
|
||||
#[cfg(feature = "try-runtime")]
|
||||
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
|
||||
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
|
||||
frame_support::ensure!(
|
||||
StorageVersion::<T>::get() == ObsoleteReleases::V12_0_0,
|
||||
"Required v12 before upgrading to v13"
|
||||
@@ -84,7 +89,7 @@ pub mod v13 {
|
||||
}
|
||||
|
||||
#[cfg(feature = "try-runtime")]
|
||||
fn post_upgrade(_state: Vec<u8>) -> Result<(), &'static str> {
|
||||
fn post_upgrade(_state: Vec<u8>) -> Result<(), TryRuntimeError> {
|
||||
frame_support::ensure!(
|
||||
Pallet::<T>::on_chain_storage_version() == 13,
|
||||
"v13 not applied"
|
||||
@@ -114,7 +119,7 @@ pub mod v12 {
|
||||
pub struct MigrateToV12<T>(sp_std::marker::PhantomData<T>);
|
||||
impl<T: Config> OnRuntimeUpgrade for MigrateToV12<T> {
|
||||
#[cfg(feature = "try-runtime")]
|
||||
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
|
||||
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
|
||||
frame_support::ensure!(
|
||||
StorageVersion::<T>::get() == ObsoleteReleases::V11_0_0,
|
||||
"Expected v11 before upgrading to v12"
|
||||
@@ -146,7 +151,7 @@ pub mod v12 {
|
||||
}
|
||||
|
||||
#[cfg(feature = "try-runtime")]
|
||||
fn post_upgrade(_state: Vec<u8>) -> Result<(), &'static str> {
|
||||
fn post_upgrade(_state: Vec<u8>) -> Result<(), TryRuntimeError> {
|
||||
frame_support::ensure!(
|
||||
StorageVersion::<T>::get() == ObsoleteReleases::V12_0_0,
|
||||
"v12 not applied"
|
||||
@@ -170,7 +175,7 @@ pub mod v11 {
|
||||
for MigrateToV11<T, P, N>
|
||||
{
|
||||
#[cfg(feature = "try-runtime")]
|
||||
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
|
||||
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
|
||||
frame_support::ensure!(
|
||||
StorageVersion::<T>::get() == ObsoleteReleases::V10_0_0,
|
||||
"must upgrade linearly"
|
||||
@@ -217,7 +222,7 @@ pub mod v11 {
|
||||
}
|
||||
|
||||
#[cfg(feature = "try-runtime")]
|
||||
fn post_upgrade(_state: Vec<u8>) -> Result<(), &'static str> {
|
||||
fn post_upgrade(_state: Vec<u8>) -> Result<(), TryRuntimeError> {
|
||||
frame_support::ensure!(
|
||||
StorageVersion::<T>::get() == ObsoleteReleases::V11_0_0,
|
||||
"wrong version after the upgrade"
|
||||
@@ -332,7 +337,7 @@ pub mod v9 {
|
||||
}
|
||||
|
||||
#[cfg(feature = "try-runtime")]
|
||||
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
|
||||
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
|
||||
frame_support::ensure!(
|
||||
StorageVersion::<T>::get() == ObsoleteReleases::V8_0_0,
|
||||
"must upgrade linearly"
|
||||
@@ -343,17 +348,21 @@ pub mod v9 {
|
||||
}
|
||||
|
||||
#[cfg(feature = "try-runtime")]
|
||||
fn post_upgrade(prev_count: Vec<u8>) -> Result<(), &'static str> {
|
||||
fn post_upgrade(prev_count: Vec<u8>) -> Result<(), TryRuntimeError> {
|
||||
let prev_count: u32 = Decode::decode(&mut prev_count.as_slice()).expect(
|
||||
"the state parameter should be something that was generated by pre_upgrade",
|
||||
);
|
||||
let post_count = T::VoterList::count();
|
||||
let validators = Validators::<T>::count();
|
||||
assert!(post_count == prev_count + validators);
|
||||
ensure!(
|
||||
post_count == prev_count + validators,
|
||||
"`VoterList` count after the migration must equal to the sum of \
|
||||
previous count and the current number of validators"
|
||||
);
|
||||
|
||||
frame_support::ensure!(
|
||||
StorageVersion::<T>::get() == ObsoleteReleases::V9_0_0,
|
||||
"must upgrade "
|
||||
"must upgrade"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -51,6 +51,11 @@ use crate::{
|
||||
|
||||
use super::{pallet::*, STAKING_ID};
|
||||
|
||||
#[cfg(feature = "try-runtime")]
|
||||
use frame_support::ensure;
|
||||
#[cfg(any(test, feature = "try-runtime"))]
|
||||
use sp_runtime::TryRuntimeError;
|
||||
|
||||
/// The maximum number of iterations that we do whilst iterating over `T::VoterList` in
|
||||
/// `get_npos_voters`.
|
||||
///
|
||||
@@ -1467,7 +1472,7 @@ impl<T: Config> SortedListProvider<T::AccountId> for UseValidatorsMap<T> {
|
||||
0
|
||||
}
|
||||
#[cfg(feature = "try-runtime")]
|
||||
fn try_state() -> Result<(), &'static str> {
|
||||
fn try_state() -> Result<(), TryRuntimeError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1544,7 +1549,7 @@ impl<T: Config> SortedListProvider<T::AccountId> for UseNominatorsAndValidatorsM
|
||||
}
|
||||
|
||||
#[cfg(feature = "try-runtime")]
|
||||
fn try_state() -> Result<(), &'static str> {
|
||||
fn try_state() -> Result<(), TryRuntimeError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1713,7 +1718,7 @@ impl<T: Config> StakingInterface for Pallet<T> {
|
||||
|
||||
#[cfg(any(test, feature = "try-runtime"))]
|
||||
impl<T: Config> Pallet<T> {
|
||||
pub(crate) fn do_try_state(_: BlockNumberFor<T>) -> Result<(), &'static str> {
|
||||
pub(crate) fn do_try_state(_: BlockNumberFor<T>) -> Result<(), TryRuntimeError> {
|
||||
ensure!(
|
||||
T::VoterList::iter()
|
||||
.all(|x| <Nominators<T>>::contains_key(&x) || <Validators<T>>::contains_key(&x)),
|
||||
@@ -1726,7 +1731,7 @@ impl<T: Config> Pallet<T> {
|
||||
Self::check_count()
|
||||
}
|
||||
|
||||
fn check_count() -> Result<(), &'static str> {
|
||||
fn check_count() -> Result<(), TryRuntimeError> {
|
||||
ensure!(
|
||||
<T as Config>::VoterList::count() ==
|
||||
Nominators::<T>::count() + Validators::<T>::count(),
|
||||
@@ -1739,18 +1744,19 @@ impl<T: Config> Pallet<T> {
|
||||
ensure!(
|
||||
ValidatorCount::<T>::get() <=
|
||||
<T::ElectionProvider as frame_election_provider_support::ElectionProviderBase>::MaxWinners::get(),
|
||||
"validator count exceeded election max winners"
|
||||
Error::<T>::TooManyValidators
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_ledgers() -> Result<(), &'static str> {
|
||||
fn check_ledgers() -> Result<(), TryRuntimeError> {
|
||||
Bonded::<T>::iter()
|
||||
.map(|(_, ctrl)| Self::ensure_ledger_consistent(ctrl))
|
||||
.collect::<Result<_, _>>()
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_exposures() -> Result<(), &'static str> {
|
||||
fn check_exposures() -> Result<(), TryRuntimeError> {
|
||||
// a check per validator to ensure the exposure struct is always sane.
|
||||
let era = Self::active_era().unwrap().index;
|
||||
ErasStakers::<T>::iter_prefix_values(era)
|
||||
@@ -1766,10 +1772,10 @@ impl<T: Config> Pallet<T> {
|
||||
);
|
||||
Ok(())
|
||||
})
|
||||
.collect::<Result<_, _>>()
|
||||
.collect::<Result<(), TryRuntimeError>>()
|
||||
}
|
||||
|
||||
fn check_nominators() -> Result<(), &'static str> {
|
||||
fn check_nominators() -> Result<(), TryRuntimeError> {
|
||||
// a check per nominator to ensure their entire stake is correctly distributed. Will only
|
||||
// kick-in if the nomination was submitted before the current era.
|
||||
let era = Self::active_era().unwrap().index;
|
||||
@@ -1783,27 +1789,33 @@ impl<T: Config> Pallet<T> {
|
||||
}
|
||||
},
|
||||
)
|
||||
.map(|nominator| {
|
||||
.map(|nominator| -> Result<(), TryRuntimeError> {
|
||||
// must be bonded.
|
||||
Self::ensure_is_stash(&nominator)?;
|
||||
let mut sum = BalanceOf::<T>::zero();
|
||||
T::SessionInterface::validators()
|
||||
.iter()
|
||||
.map(|v| Self::eras_stakers(era, v))
|
||||
.map(|e| {
|
||||
.map(|e| -> Result<(), TryRuntimeError> {
|
||||
let individual =
|
||||
e.others.iter().filter(|e| e.who == nominator).collect::<Vec<_>>();
|
||||
let len = individual.len();
|
||||
match len {
|
||||
0 => { /* not supporting this validator at all. */ },
|
||||
1 => sum += individual[0].value,
|
||||
_ => return Err("nominator cannot back a validator more than once."),
|
||||
_ =>
|
||||
return Err(
|
||||
"nominator cannot back a validator more than once.".into()
|
||||
),
|
||||
};
|
||||
Ok(())
|
||||
})
|
||||
.collect::<Result<_, _>>()
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
Ok(())
|
||||
})
|
||||
.collect::<Result<_, _>>()
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn ensure_is_stash(who: &T::AccountId) -> Result<(), &'static str> {
|
||||
@@ -1811,7 +1823,7 @@ impl<T: Config> Pallet<T> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn ensure_ledger_consistent(ctrl: T::AccountId) -> Result<(), &'static str> {
|
||||
fn ensure_ledger_consistent(ctrl: T::AccountId) -> Result<(), TryRuntimeError> {
|
||||
// ensures ledger.total == ledger.active + sum(ledger.unlocking).
|
||||
let ledger = Self::ledger(ctrl.clone()).ok_or("Not a controller.")?;
|
||||
let real_total: BalanceOf<T> =
|
||||
|
||||
@@ -805,7 +805,7 @@ pub mod pallet {
|
||||
}
|
||||
|
||||
#[cfg(feature = "try-runtime")]
|
||||
fn try_state(n: BlockNumberFor<T>) -> Result<(), &'static str> {
|
||||
fn try_state(n: BlockNumberFor<T>) -> Result<(), sp_runtime::TryRuntimeError> {
|
||||
Self::do_try_state(n)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user