Skip to content

Conversation

@matchv
Copy link
Collaborator

@matchv matchv commented Jul 22, 2025

The issue:

The current debt_ceiling will truncate (asset_decimal - 2) digit when incrementing, it means if the borrow asset is 6 decimals, debt_ceiling will only increment to the precision of 1 cent (10^4).

A user who micro borrows slightly than 1 cent (9,999 unir) each time, for as many times as possible, will bypass the debt ceiling each and every time, which break the protocol integrity.

//validation_logic.move
let isolation_mode_total_debt =
    pool::get_reserve_isolation_mode_total_debt(mode_collateral_reserve_data) as u256;
let total_debt =
    isolation_mode_total_debt
      + (amount
         / math_utils::pow(
             10,
             (reserve_decimals
                - reserve_config::get_debt_ceiling_decimals())
         )
        );
assert!( total_debt <= isolation_mode_debt_ceiling, … );

The fix:

Add ceil_div() in math library
Use ceil_div() to replace simple division in borrow logic, isolation mode, validation logic etc.
Add more test cases

matchv added 3 commits July 22, 2025 10:10
…t calculation precision

- Add ceil_div function to math_utils for accurate ceiling division
- Replace simple division with ceil_div in isolation mode debt calculations
- Improve precision handling in borrow_logic, isolation_mode_logic, and validation_logic
- Fix potential precision loss in debt ceiling calculations
- Add test cases for basic ceiling division scenarios
- Add edge case tests for zero numerator and division by zero
- Add large number tests to verify precision handling
- Ensure proper error handling for division by zero
- Refactor test_repay_with_isolation_mode test for better clarity
- Improve test coverage for isolation mode debt ceiling scenarios
- Add comprehensive test cases for precision handling
- Ensure tests properly validate ceil_div integration
@codecov
Copy link

codecov bot commented Jul 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.89%. Comparing base (566c23b) to head (d4732d0).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #35      +/-   ##
==========================================
- Coverage   97.08%   96.89%   -0.19%     
==========================================
  Files          16       16              
  Lines         514      515       +1     
==========================================
  Hits          499      499              
- Misses         15       16       +1     
Flag Coverage Δ
move 96.89% <ø> (-0.19%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Collaborator

@mpsc0x mpsc0x left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the implementation and the reasoning behind it. LGTM

@mpsc0x mpsc0x merged commit 0beb083 into main Jul 24, 2025
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants