Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Tracable defensive errors (#11532)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
Szegoo and KiChjang authored Jun 3, 2022
1 parent 5f0aa1f commit 2274363
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions frame/nomination-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1329,13 +1329,31 @@ pub mod pallet {
MetadataExceedsMaxLen,
/// Some error occurred that should never happen. This should be reported to the
/// maintainers.
DefensiveError,
Defensive(DefensiveError),
/// Not enough points. Ty unbonding less.
NotEnoughPointsToUnbond,
/// Partial unbonding now allowed permissionlessly.
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]
impl<T: Config> Pallet<T> {
/// Stake funds with a pool. The amount to bond is transferred from the member to the
Expand Down Expand Up @@ -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
// because we are not adjusting its points (all other values can calculated virtual).
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()?;
let points_issued = bonded_pool.try_bond_funds(&who, amount, BondType::Later)?;
Expand Down Expand Up @@ -1530,14 +1548,16 @@ pub mod pallet {
.try_insert(unbond_era, UnbondPool::default())
// The above call to `maybe_merge_pools` should ensure there is
// always enough space to insert.
.defensive_map_err(|_| Error::<T>::DefensiveError)?;
.defensive_map_err::<Error<T>, _>(|_| {
DefensiveError::NotEnoughSpaceInUnbondPool.into()
})?;
}

sub_pools
.with_era
.get_mut(&unbond_era)
// 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);

Self::deposit_event(Event::<T>::Unbonded {
Expand Down Expand Up @@ -1607,9 +1627,9 @@ pub mod pallet {
let current_era = T::StakingInterface::current_era();

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)
.defensive_ok_or_else(|| Error::<T>::SubPoolsNotFound)?;
.defensive_ok_or::<Error<T>>(DefensiveError::SubPoolsNotFound.into())?;

bonded_pool.ok_to_withdraw_unbonded_with(
&caller,
Expand Down Expand Up @@ -2027,9 +2047,9 @@ impl<T: Config> Pallet<T> {
) -> Result<(PoolMember<T>, BondedPool<T>, RewardPool<T>), Error<T>> {
let member = PoolMembers::<T>::get(&who).ok_or(Error::<T>::PoolMemberNotFound)?;
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 =
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))
}

Expand Down

0 comments on commit 2274363

Please sign in to comment.