Skip to content

Fix oracle price validation and add math overflow saturation tests - #489

Open
Uchechukwu-Ekezie wants to merge 1 commit into
SO4-Markets:mainfrom
OpenSourceCOntr:fixes/referral-math-order-remaining
Open

Fix oracle price validation and add math overflow saturation tests#489
Uchechukwu-Ekezie wants to merge 1 commit into
SO4-Markets:mainfrom
OpenSourceCOntr:fixes/referral-math-order-remaining

Conversation

@Uchechukwu-Ekezie

Copy link
Copy Markdown
Contributor

Closes #383, Closes #384

Summary

This PR addresses two issues: oracle price validation (HIGH priority) and untested math overflow saturation behavior (LOW priority).

Issue #383: Oracle - validate u128-to-i128 cast

Problem: get_stable_price and get_price_with_stable_fallback cast stored u128 directly to i128 without validation. A u128 value >= 2^127 reinterprets as a negative i128, bypassing safety checks.

Fix:

  • get_stable_price: Validate raw u128 is below i128::MAX before casting. Return None if zero or invalid.
  • get_price_with_stable_fallback: Only use stable price if positive AND fits in i128. Fall back to primary price otherwise.

This matches validation applied to keeper-submitted prices.

Issue #384: Math - test overflow saturation fallback

Problem: mul_div_wide and mul_div_wide_up have untested overflow saturation fallback. The comment admits "shouldn't happen in normal protocol use" — an unverified assumption rather than enforced invariant.

Fix: Add 5 comprehensive tests covering:

  • Positive overflow saturation to i128::MAX
  • Negative overflow saturation to i128::MIN
  • Ceiling division overflow behavior
  • Large but safe (non-overflowing) values

These tests pin down the saturation behavior and catch regressions.

Issue #385: Position manager lookup reversal

Status: Requires further work beyond this PR.

The create_order function has the position manager lookup reversed:

  • Currently: get_position_manager(\&caller, market) looks up "who is caller's own manager"
  • Should be: Check if get_position_manager(\&owner, market) == Some(caller) to verify caller IS a manager for owner

This requires adding an on_behalf_of: Option<Address> field to CreateOrderParams and updating the authorization logic. Deferred to follow-up PR for thorough testing.

Testing

  • ✅ Oracle contract compiles without errors
  • ✅ All 28 math tests pass (including 5 new overflow saturation tests)
  • ✅ Validation logic prevents negative prices from being accepted
  • ✅ Math overflow behavior is now tested and documented

Closes SO4-Markets#384

Issue: mul_div_wide and mul_div_wide_up compute product/ceiling-division
in a wider integer type and then convert back with a fallback that
silently saturates to i128::MAX/MIN when the true result doesn't fit.
None of the existing property tests pushed both operands large enough
simultaneously to exercise this fallback.

Fix: Add comprehensive tests for overflow saturation behavior:
- mul_div_wide_overflow_saturates_to_max: Tests i128::MAX * i128::MAX / 1
- mul_div_wide_negative_overflow_saturates_to_min: Tests i128::MIN * i128::MAX / 1
- mul_div_wide_up_overflow_saturates_to_max: Ceiling division overflow to MAX
- mul_div_wide_up_negative_overflow_saturates_to_min: Ceiling division overflow to MIN
- mul_div_wide_large_but_safe_values: Ensures large non-overflowing cases work correctly

These tests pin down the saturation behavior, ensuring regressions would be caught.
@drips-wave

drips-wave Bot commented Jul 24, 2026

Copy link
Copy Markdown

@Uchechukwu-Ekezie Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant