Skip to content

Commit

Permalink
fix: 🐛 Delete payment stream when executing stop storing for insolven…
Browse files Browse the repository at this point in the history
…t user
  • Loading branch information
ffarall committed Oct 10, 2024
1 parent 30c56ea commit 26fc867
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 36 deletions.
24 changes: 24 additions & 0 deletions pallets/file-system/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,18 @@ where
// Update root of the BSP.
<T::Providers as shp_traits::MutateProvidersInterface>::update_root(sp_id, new_root)?;

// Delete payment stream between this BSP and this user (also charge it for all the owed funds
// of all files that were stored by this BSP).
if <T::PaymentStreams as PaymentStreamsInterface>::get_dynamic_rate_payment_stream_info(
&sp_id, &owner,
)
.is_some()
{
<T::PaymentStreams as PaymentStreamsInterface>::delete_dynamic_rate_payment_stream(
&sp_id, &owner,
)?;
}

new_root
} else {
// If the Provider is a MSP, the proof is verified against the Bucket's root.
Expand Down Expand Up @@ -1780,6 +1792,18 @@ where
bucket_id, new_root,
)?;

// Delete payment stream between this MSP and this user (also charge it for all the owed funds
// of all files that were stored by this MSP).
if <T::PaymentStreams as PaymentStreamsInterface>::get_fixed_rate_payment_stream_info(
&sp_id, &owner,
)
.is_some()
{
<T::PaymentStreams as PaymentStreamsInterface>::delete_fixed_rate_payment_stream(
&sp_id, &owner,
)?;
}

new_root
};

Expand Down
80 changes: 44 additions & 36 deletions pallets/payment-streams/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,26 +254,30 @@ where
});
}

// Release the deposit of this payment stream to the User
let deposit = FixedRatePaymentStreams::<T>::get(provider_id, user_account)
.ok_or(Error::<T>::PaymentStreamNotFound)?
.user_deposit;
T::NativeBalance::release(
&HoldReason::PaymentStreamDeposit.into(),
&user_account,
deposit,
Precision::Exact,
)?;
// The payment stream may have been deleted when charged if the user was out of funds.
// If that's not the case, we clear it here.
if FixedRatePaymentStreams::<T>::get(provider_id, user_account).is_some() {
// Release the deposit of this payment stream to the User
let deposit = FixedRatePaymentStreams::<T>::get(provider_id, user_account)
.ok_or(Error::<T>::PaymentStreamNotFound)?
.user_deposit;
T::NativeBalance::release(
&HoldReason::PaymentStreamDeposit.into(),
&user_account,
deposit,
Precision::Exact,
)?;

// Remove the payment stream from the FixedRatePaymentStreams mapping
FixedRatePaymentStreams::<T>::remove(provider_id, user_account);
// Remove the payment stream from the FixedRatePaymentStreams mapping
FixedRatePaymentStreams::<T>::remove(provider_id, user_account);

// Decrease the user's payment streams count
let mut user_payment_streams_count = RegisteredUsers::<T>::get(user_account);
user_payment_streams_count = user_payment_streams_count
.checked_sub(1)
.ok_or(ArithmeticError::Underflow)?;
RegisteredUsers::<T>::insert(user_account, user_payment_streams_count);
// Decrease the user's payment streams count
let mut user_payment_streams_count = RegisteredUsers::<T>::get(user_account);
user_payment_streams_count = user_payment_streams_count
.checked_sub(1)
.ok_or(ArithmeticError::Underflow)?;
RegisteredUsers::<T>::insert(user_account, user_payment_streams_count);
}

Ok(())
}
Expand Down Expand Up @@ -494,26 +498,30 @@ where
});
}

// Release the deposit of this payment stream to the User
let deposit = DynamicRatePaymentStreams::<T>::get(provider_id, user_account)
.ok_or(Error::<T>::PaymentStreamNotFound)?
.user_deposit;
T::NativeBalance::release(
&HoldReason::PaymentStreamDeposit.into(),
&user_account,
deposit,
Precision::Exact,
)?;
// The payment stream may have been deleted when charged if the user was out of funds.
// If that's not the case, we clear it here.
if DynamicRatePaymentStreams::<T>::get(provider_id, user_account).is_some() {
// Release the deposit of this payment stream to the User
let deposit = DynamicRatePaymentStreams::<T>::get(provider_id, user_account)
.ok_or(Error::<T>::PaymentStreamNotFound)?
.user_deposit;
T::NativeBalance::release(
&HoldReason::PaymentStreamDeposit.into(),
&user_account,
deposit,
Precision::Exact,
)?;

// Remove the payment stream from the DynamicRatePaymentStreams mapping
DynamicRatePaymentStreams::<T>::remove(provider_id, user_account);
// Remove the payment stream from the DynamicRatePaymentStreams mapping
DynamicRatePaymentStreams::<T>::remove(provider_id, user_account);

// Decrease the user's payment streams count
let mut user_payment_streams_count = RegisteredUsers::<T>::get(user_account);
user_payment_streams_count = user_payment_streams_count
.checked_sub(1)
.ok_or(ArithmeticError::Underflow)?;
RegisteredUsers::<T>::insert(user_account, user_payment_streams_count);
// Decrease the user's payment streams count
let mut user_payment_streams_count = RegisteredUsers::<T>::get(user_account);
user_payment_streams_count = user_payment_streams_count
.checked_sub(1)
.ok_or(ArithmeticError::Underflow)?;
RegisteredUsers::<T>::insert(user_account, user_payment_streams_count);
}

Ok(())
}
Expand Down

0 comments on commit 26fc867

Please sign in to comment.