Skip to content

Commit

Permalink
changed: using index instead of iterating in subscription vector for …
Browse files Browse the repository at this point in the history
…unsubscribe()
  • Loading branch information
guillaumebogard committed Aug 2, 2022
1 parent fe60de6 commit d2151f8
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub mod pallet {
pub enum Error<T> {
InvalidSubscription,
UnknownUnsubscription,
InvalidUnsubscription
}

#[pallet::hooks]
Expand Down Expand Up @@ -155,23 +156,26 @@ pub mod pallet {
subscriber: T::AccountId,
subscription: Subscription<T::BlockNumber, BalanceOf<T>, T::AccountId>,
when: T::BlockNumber,
index: u32
) -> DispatchResult {
ensure_signed(origin)?;

<Subscriptions<T>>::mutate(when, |wrapped_current_subscriptions| {
if let Some(current_subscriptions) = wrapped_current_subscriptions {
let old_subscriptions_len = current_subscriptions.len();
let index = index as usize;

current_subscriptions.retain(|current_subscription| {
!(current_subscription.0 == subscription
&& current_subscription.1 == subscriber)
});
if index >= current_subscriptions.len() {
return Err(Error::<T>::InvalidUnsubscription);
}

let desired_subscription = &(current_subscriptions[index]);

if old_subscriptions_len >= current_subscriptions.len() {
Err(Error::<T>::UnknownUnsubscription)
} else {
Ok(())
if !((*desired_subscription).0 == subscription && (*desired_subscription).1 == subscriber) {
return Err(Error::<T>::InvalidUnsubscription);
}

current_subscriptions.remove(index);
Ok(())
} else {
Err(Error::<T>::UnknownUnsubscription)
}
Expand Down

0 comments on commit d2151f8

Please sign in to comment.