Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(loans): add mutation testing decorations #971

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/loans/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ orml-oracle = { git = "https://github.com/open-web3-stack/open-runtime-module-li
mocktopus = "0.8.0"
visibility = { version = "0.0.1" }
currency = { path = "../currency", features = ["testing-utils"] }
mutagen = { git = "https://github.com/llogiq/mutagen" }
pallet-scheduler = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31" }

[features]
Expand Down
9 changes: 9 additions & 0 deletions crates/loans/src/farming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ impl<T: Config> Pallet<T> {
T::PalletId::get().into_sub_account_truncating(REWARD_SUB_ACCOUNT)
}

#[cfg_attr(test, mutate)]
fn reward_scale() -> u128 {
10_u128.pow(12)
}

#[cfg_attr(test, mutate)]
fn calculate_reward_delta_index(
delta_block: T::BlockNumber,
reward_speed: BalanceOf<T>,
Expand All @@ -47,6 +49,7 @@ impl<T: Config> Pallet<T> {
Ok(delta_index)
}

#[cfg_attr(test, mutate)]
fn calculate_reward_delta(
share: BalanceOf<T>,
reward_delta_index: u128,
Expand All @@ -60,6 +63,7 @@ impl<T: Config> Pallet<T> {
Ok(reward_delta)
}

#[cfg_attr(test, mutate)]
pub(crate) fn update_reward_supply_index(asset_id: CurrencyId<T>) -> DispatchResult {
let current_block_number = <frame_system::Pallet<T>>::block_number();
RewardSupplyState::<T>::try_mutate(asset_id, |supply_state| -> DispatchResult {
Expand All @@ -82,6 +86,7 @@ impl<T: Config> Pallet<T> {
})
}

#[cfg_attr(test, mutate)]
pub(crate) fn update_reward_borrow_index(asset_id: CurrencyId<T>) -> DispatchResult {
let current_block_number = <frame_system::Pallet<T>>::block_number();
RewardBorrowState::<T>::try_mutate(asset_id, |borrow_state| -> DispatchResult {
Expand All @@ -106,6 +111,7 @@ impl<T: Config> Pallet<T> {
})
}

#[cfg_attr(test, mutate)]
pub(crate) fn distribute_supplier_reward(asset_id: CurrencyId<T>, supplier: &T::AccountId) -> DispatchResult {
RewardSupplierIndex::<T>::try_mutate(asset_id, supplier, |supplier_index| -> DispatchResult {
let supply_state = RewardSupplyState::<T>::get(asset_id);
Expand Down Expand Up @@ -137,6 +143,7 @@ impl<T: Config> Pallet<T> {
})
}

#[cfg_attr(test, mutate)]
pub(crate) fn distribute_borrower_reward(asset_id: CurrencyId<T>, borrower: &T::AccountId) -> DispatchResult {
RewardBorrowerIndex::<T>::try_mutate(asset_id, borrower, |borrower_index| -> DispatchResult {
let borrow_state = RewardBorrowState::<T>::get(asset_id);
Expand Down Expand Up @@ -166,6 +173,7 @@ impl<T: Config> Pallet<T> {
})
}

#[cfg_attr(test, mutate)]
pub(crate) fn collect_market_reward(asset_id: CurrencyId<T>, user: &T::AccountId) -> DispatchResult {
Self::update_reward_supply_index(asset_id)?;
Self::distribute_supplier_reward(asset_id, user)?;
Expand All @@ -176,6 +184,7 @@ impl<T: Config> Pallet<T> {
Ok(())
}

#[cfg_attr(test, mutate)]
pub(crate) fn pay_reward(user: &T::AccountId) -> DispatchResult {
let pool_account = Self::reward_account_id();
let reward_asset = T::RewardAssetId::get();
Expand Down
8 changes: 8 additions & 0 deletions crates/loans/src/interest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::*;
impl<T: Config> Pallet<T> {
/// Accrue interest and update corresponding storage
#[cfg_attr(any(test, feature = "integration-tests"), visibility::make(pub))]
#[cfg_attr(test, mutate)]
pub(crate) fn accrue_interest(asset_id: CurrencyId<T>) -> DispatchResult {
let now = T::UnixTime::now().as_secs();
let last_accrued_interest_time = Self::last_accrued_interest_time(asset_id);
Expand Down Expand Up @@ -64,6 +65,7 @@ impl<T: Config> Pallet<T> {
Ok(())
}

#[cfg_attr(test, mutate)]
pub fn get_market_status(
asset_id: CurrencyId<T>,
) -> Result<(Rate, Rate, Rate, Ratio, BalanceOf<T>, BalanceOf<T>, FixedU128), DispatchError> {
Expand Down Expand Up @@ -124,6 +126,7 @@ impl<T: Config> Pallet<T> {
/// Update the exchange rate according to the totalCash, totalBorrows and totalSupply.
/// This function does not accrue interest before calculating the exchange rate.
/// exchangeRate = (totalCash + totalBorrows - totalReserves) / totalSupply
#[cfg_attr(test, mutate)]
pub fn exchange_rate_stored(asset_id: CurrencyId<T>) -> Result<Rate, DispatchError> {
let total_supply = Self::total_supply(asset_id)?;
let total_cash = Self::get_total_cash(asset_id);
Expand All @@ -136,6 +139,7 @@ impl<T: Config> Pallet<T> {
/// Calculate the borrowing utilization ratio of the specified market
///
/// utilizationRatio = totalBorrows / (totalCash + totalBorrows − totalReserves)
#[cfg_attr(test, mutate)]
pub(crate) fn calc_utilization_ratio(
cash: &Amount<T>,
borrows: &Amount<T>,
Expand All @@ -155,6 +159,7 @@ impl<T: Config> Pallet<T> {
/// This ensures the exchange rate cannot be attacked by a deposit so big that
/// subsequent deposits to receive zero lendTokens (because of rounding down). See this
/// PR for more details: https://github.com/parallel-finance/parallel/pull/1552/files
#[cfg_attr(test, mutate)]
pub(crate) fn ensure_valid_exchange_rate(exchange_rate: Rate) -> DispatchResult {
ensure!(
exchange_rate >= Self::min_exchange_rate() && exchange_rate < Self::max_exchange_rate(),
Expand All @@ -164,13 +169,15 @@ impl<T: Config> Pallet<T> {
Ok(())
}

#[cfg_attr(test, mutate)]
pub(crate) fn update_last_accrued_interest_time(asset_id: CurrencyId<T>, time: Timestamp) -> DispatchResult {
LastAccruedInterestTime::<T>::try_mutate(asset_id, |last_time| -> DispatchResult {
*last_time = time;
Ok(())
})
}

#[cfg_attr(test, mutate)]
pub(crate) fn accrue_index(borrow_rate: Rate, index: Rate, delta_time: Timestamp) -> Result<Rate, DispatchError> {
// Compound interest:
// new_index = old_index * (1 + annual_borrow_rate / SECONDS_PER_YEAR) ^ delta_time
Expand All @@ -184,6 +191,7 @@ impl<T: Config> Pallet<T> {
Ok(index.checked_mul(&compounded_rate).ok_or(ArithmeticError::Overflow)?)
}

#[cfg_attr(test, mutate)]
fn calculate_exchange_rate(
total_supply: &Amount<T>,
total_cash: &Amount<T>,
Expand Down
Loading