Skip to content

fix: make late-fee arithmetic overflow-safe (Closes #1126) - #1213

Draft
s6pa1rta3n-lab wants to merge 1 commit into
Creditra:mainfrom
s6pa1rta3n-lab:fix-issue-1126
Draft

s6pa1rta3n-lab wants to merge 1 commit into
Creditra:mainfrom
s6pa1rta3n-lab:fix-issue-1126

Conversation

@s6pa1rta3n-lab

Copy link
Copy Markdown

Summary

Closes #1126

Makes late-fee arithmetic overflow-safe across all fee modes, installment boundaries, and configuration states. Ensures deterministic execution at maximum amount and installment limits, defines floor rounding for delinquency surcharge calculations, and guarantees neutral protocol behavior when configuration is cleared.

Acceptance Criteria Checklist

  • Overflow returns a stable error before mutation: Handled via Uint128::checked_mul and crate::accrual::accrued_interest, returning ContractError::Overflow before any state transition or storage write can occur. Verified in contracts/creditra-credit/tests/late_fee_overflow.rs::overflow_protection.
  • Zero and maximum boundaries are tested: Exhaustively covered zero missed installments across all configs, single installment at Uint128::MAX, max u64::MAX installments at unit amount, maximum safe product and single-step overflow boundary, and surcharge bps bounds (0, 10_000, 10_001, u32::MAX). Verified in contracts/creditra-credit/tests/late_fee_overflow.rs::boundary_conditions and unit tests in contracts/creditra-credit/src/penalties.rs.
  • Switching fee modes cannot carry stale state: Verified rapid alternating state transitions (None -> Flat -> AprBased -> None -> Flat -> AprBased -> None) and verified raw storage key b"lfc" isolation across mode switches. Verified in contracts/creditra-credit/tests/late_fee_overflow.rs::state_transition_and_clearing.
  • Golden tests cover all public fee variants: Table-driven golden vectors cover Flat variants, AprBased variants with floor rounding, cleared (None) variant, and extreme boundary vectors. Verified in contracts/creditra-credit/tests/late_fee_overflow.rs::golden_tests.

Implementation Details & Code/Test Mapping

Acceptance Criterion Implementation Symbol Test Verification
Overflow Protection compute_late_fee, compute_apr_late_fee late_fee_overflow.rs::overflow_protection
Boundary Verification validate_late_fee_config, compute_late_fee late_fee_overflow.rs::boundary_conditions
Mode Isolation execute_set_late_fee_config, compute_late_fee_optional late_fee_overflow.rs::state_transition_and_clearing
Public Variants Golden Suite LateFeeConfig late_fee_overflow.rs::golden_tests
Authorization Boundaries execute_set_late_fee_config late_fee_overflow.rs::authorization_boundaries

Security & Failure-Mode Considerations

  • Deterministic Failure: Arithmetic uses checked operations (checked_mul, checked_div). Any calculation that would exceed Uint128::MAX immediately yields ContractError::Overflow, halting execution before any state or balance modification takes effect.
  • Rounding Policy: APR delinquency surcharge calculations apply integer floor division (truncation toward zero), matching protocol interest accrual policy and favoring borrowers. Flat fees use discrete token units with whole installment multipliers without fractional truncation.
  • State Neutrality: When configuration is cleared (None), compute_late_fee_optional evaluates to zero, ensuring no penalty is charged and preventing stale parameters from affecting subsequent contract interactions.
  • Authorization Enforcement: Only the contract admin can set or clear late-fee configuration; unauthorized attempts are rejected with ContractError::Unauthorized.

Verification

  • cargo fmt -- --check: PASSED
  • cargo clippy --all-targets -- -D warnings: PASSED
  • cargo test --no-fail-fast: PASSED (all unit, integration, and property tests)
  • cargo build --release: PASSED
  • cargo build --release --lib --target wasm32-unknown-unknown: PASSED (373 KB WASM, within 614,400-byte budget)
  • scripts/validate_schemas.sh: PASSED (schemas up to date)

Payout Routing

  • EVM (Base/Arbitrum/Polygon/ETH): 0xF46C9F6d70C50BF81ef3588AB523a90a594a2F89
  • Stellar: GCL6OXAMLD75BMTINA6EMRUDWK5THQUSHMYNLSNBCJAPZJHNYJTUNIBC

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.

[GrantFox][Medium] Make late-fee arithmetic overflow-safe

1 participant