Tracable defensive errors (#11532)

* Tracable defensive errors

* small fixes

* fix

* refactored

* switched to defensive_ok_or

* Remove unnecessary type annotations and conversions

* cargo fmt

* Fixes

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>
This commit is contained in:
Sergej Sakac
2022-06-03 16:25:55 +02:00
committed by GitHub
parent 6b1e871137
commit d09e835753
+28 -8
View File
@@ -1329,13 +1329,31 @@ pub mod pallet {
MetadataExceedsMaxLen, MetadataExceedsMaxLen,
/// Some error occurred that should never happen. This should be reported to the /// Some error occurred that should never happen. This should be reported to the
/// maintainers. /// maintainers.
DefensiveError, Defensive(DefensiveError),
/// Not enough points. Ty unbonding less. /// Not enough points. Ty unbonding less.
NotEnoughPointsToUnbond, NotEnoughPointsToUnbond,
/// Partial unbonding now allowed permissionlessly. /// Partial unbonding now allowed permissionlessly.
PartialUnbondNotAllowedPermissionlessly, PartialUnbondNotAllowedPermissionlessly,
} }
#[derive(Encode, Decode, PartialEq, TypeInfo, frame_support::PalletError)]
pub enum DefensiveError {
/// There isn't enough space in the unbond pool.
NotEnoughSpaceInUnbondPool,
/// A (bonded) pool id does not exist.
PoolNotFound,
/// A reward pool does not exist. In all cases this is a system logic error.
RewardPoolNotFound,
/// A sub pool does not exist.
SubPoolsNotFound,
}
impl<T> From<DefensiveError> for Error<T> {
fn from(e: DefensiveError) -> Error<T> {
Error::<T>::Defensive(e)
}
}
#[pallet::call] #[pallet::call]
impl<T: Config> Pallet<T> { impl<T: Config> Pallet<T> {
/// Stake funds with a pool. The amount to bond is transferred from the member to the /// Stake funds with a pool. The amount to bond is transferred from the member to the
@@ -1366,7 +1384,7 @@ pub mod pallet {
// We just need its total earnings at this point in time, but we don't need to write it // We just need its total earnings at this point in time, but we don't need to write it
// because we are not adjusting its points (all other values can calculated virtual). // because we are not adjusting its points (all other values can calculated virtual).
let reward_pool = RewardPool::<T>::get_and_update(pool_id) let reward_pool = RewardPool::<T>::get_and_update(pool_id)
.defensive_ok_or_else(|| Error::<T>::RewardPoolNotFound)?; .defensive_ok_or::<Error<T>>(DefensiveError::RewardPoolNotFound.into())?;
bonded_pool.try_inc_members()?; bonded_pool.try_inc_members()?;
let points_issued = bonded_pool.try_bond_funds(&who, amount, BondType::Later)?; let points_issued = bonded_pool.try_bond_funds(&who, amount, BondType::Later)?;
@@ -1530,14 +1548,16 @@ pub mod pallet {
.try_insert(unbond_era, UnbondPool::default()) .try_insert(unbond_era, UnbondPool::default())
// The above call to `maybe_merge_pools` should ensure there is // The above call to `maybe_merge_pools` should ensure there is
// always enough space to insert. // always enough space to insert.
.defensive_map_err(|_| Error::<T>::DefensiveError)?; .defensive_map_err::<Error<T>, _>(|_| {
DefensiveError::NotEnoughSpaceInUnbondPool.into()
})?;
} }
sub_pools sub_pools
.with_era .with_era
.get_mut(&unbond_era) .get_mut(&unbond_era)
// The above check ensures the pool exists. // The above check ensures the pool exists.
.defensive_ok_or_else(|| Error::<T>::DefensiveError)? .defensive_ok_or::<Error<T>>(DefensiveError::PoolNotFound.into())?
.issue(unbonding_balance); .issue(unbonding_balance);
Self::deposit_event(Event::<T>::Unbonded { Self::deposit_event(Event::<T>::Unbonded {
@@ -1607,9 +1627,9 @@ pub mod pallet {
let current_era = T::StakingInterface::current_era(); let current_era = T::StakingInterface::current_era();
let bonded_pool = BondedPool::<T>::get(member.pool_id) let bonded_pool = BondedPool::<T>::get(member.pool_id)
.defensive_ok_or_else(|| Error::<T>::PoolNotFound)?; .defensive_ok_or::<Error<T>>(DefensiveError::PoolNotFound.into())?;
let mut sub_pools = SubPoolsStorage::<T>::get(member.pool_id) let mut sub_pools = SubPoolsStorage::<T>::get(member.pool_id)
.defensive_ok_or_else(|| Error::<T>::SubPoolsNotFound)?; .defensive_ok_or::<Error<T>>(DefensiveError::SubPoolsNotFound.into())?;
bonded_pool.ok_to_withdraw_unbonded_with( bonded_pool.ok_to_withdraw_unbonded_with(
&caller, &caller,
@@ -2027,9 +2047,9 @@ impl<T: Config> Pallet<T> {
) -> Result<(PoolMember<T>, BondedPool<T>, RewardPool<T>), Error<T>> { ) -> Result<(PoolMember<T>, BondedPool<T>, RewardPool<T>), Error<T>> {
let member = PoolMembers::<T>::get(&who).ok_or(Error::<T>::PoolMemberNotFound)?; let member = PoolMembers::<T>::get(&who).ok_or(Error::<T>::PoolMemberNotFound)?;
let bonded_pool = let bonded_pool =
BondedPool::<T>::get(member.pool_id).defensive_ok_or(Error::<T>::PoolNotFound)?; BondedPool::<T>::get(member.pool_id).defensive_ok_or(DefensiveError::PoolNotFound)?;
let reward_pool = let reward_pool =
RewardPools::<T>::get(member.pool_id).defensive_ok_or(Error::<T>::PoolNotFound)?; RewardPools::<T>::get(member.pool_id).defensive_ok_or(DefensiveError::PoolNotFound)?;
Ok((member, bonded_pool, reward_pool)) Ok((member, bonded_pool, reward_pool))
} }