feat: oracle fail-closed on stale data + stable error mappings - #1425
Merged
greatest0fallt1me merged 1 commit intoAug 29, 2026
Conversation
Closes #1392, Closes #1411 ## #1392 — Oracle resolution fail-closed on stale data ### Problem resolve_with_median's fetch_quote helper silently absorbed OracleStale errors by marking the stale quote as included=false. This allowed resolution to proceed on fewer sources than intended when staleness was the cause of exclusion — a fail-open behaviour that could produce incorrect outcomes from insufficient fresh data. Additionally, ReflectorOracle::get_price_data used the current ledger timestamp as publish_time when the oracle returned no data (mock fallback path), which bypassed staleness validation entirely. ### Fix - Added fetch_quote_fail_closed() to OracleResolutionManager: this variant checks publish_time against the configured staleness threshold before returning a quote. If the data is stale, it returns Err(Error::OracleStale) which aborts the entire resolution rather than silently excluding the source. - Updated resolve_with_median to use fetch_quote_fail_closed() for all three oracle sources (Pyth, Reflector, Band). Non-stale unavailability (oracle offline, feed not found) still produces included=false so the min_sources check remains effective. - Fixed ReflectorOracle::get_price_data: in non-test builds, when the on-chain oracle returns None, the function now returns Err(Error::OracleUnavailable) instead of a mock price with publish_time=now. Test builds retain the mock path but the publish_time is set clearly. - Emits emit_oracle_validation_failed event on stale detection so operators can diagnose which oracle was stale without reading state. ## #1411 — Stable error mappings for client integrations ### Problem The Error enum had no stable off-chain mapping for client SDKs. Clients were forced to rely on the raw discriminant value (e.g. 202 for OracleStale), which can shift if a new variant is inserted between existing ones. The error_code_tests.rs required client_code() and recoverability() methods that did not yet exist. ### Fix - Added Error::client_code() -> u32: stable, category-partitioned numeric codes that must never change once assigned. Ranges: Oracle 1000-1099, Market 1100-1199, Validation 1200-1299, Financial 1300-1399, Dispute 1400-1499, Auth 1500-1599, CircuitBreaker 1600-1699, System 1700-1799, UserOp 1800-1899, Metadata 1900-1999. - Added Error::recoverability() -> Recoverability: labels each variant as Retryable | RequiresAdmin | Terminal so client SDKs can implement retry policies without maintaining their own mapping table. - Fixed OperationWouldExceedBudget discriminant: moved from 418 to 444 to restore AdminNotSet = 418 as frozen by err_stability.rs. - Fixed StorageTtlPressure: replaced Val field (incompatible with #[contracttype]) with String key_repr field. - Fixed oracles.rs test structs: added missing auto_pause_duration_secs field to GlobalOracleValidationConfig initialisers in test code. ## Tests - tests/oracle_fail_closed_and_error_codes.rs: 22 new focused tests covering range validation, uniqueness, stability pinning, recoverability labels, and oracle-stale error surface behaviour. - All 7 err_stability tests pass. - All 15 proptest_fee tests pass. - Library builds cleanly (cargo build -p predictify-hybrid).
|
@Chris-P-web 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! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1392, Closes #1411
#1392 — Oracle resolution fail-closed on stale data
Problem
resolve_with_median's fetch_quote helper silently absorbed OracleStale errors by marking the stale quote as included=false. This allowed resolution to proceed on fewer sources than intended when staleness was the cause of exclusion — a fail-open behaviour that could produce incorrect outcomes from insufficient fresh data.
Additionally, ReflectorOracle::get_price_data used the current ledger timestamp as publish_time when the oracle returned no data (mock fallback path), which bypassed staleness validation entirely.
Fix
#1411 — Stable error mappings for client integrations
Problem
The Error enum had no stable off-chain mapping for client SDKs. Clients were forced to rely on the raw discriminant value (e.g. 202 for OracleStale), which can shift if a new variant is inserted between existing ones. The error_code_tests.rs required client_code() and recoverability() methods that did not yet exist.
Fix
Tests
Pull Request Description
📋 Basic Information
Type of Change
Please select the type of change this PR introduces:
Related Issues
Closes #(issue number)
Fixes #(issue number)
Related to #(issue number)
Priority Level
📝 Detailed Description
What does this PR do?
Why is this change needed?
How was this tested?
Alternative Solutions Considered
🏗️ Smart Contract Specific
Contract Changes
Please check all that apply:
Oracle Integration
Market Resolution Logic
Security Considerations
🧪 Testing
Test Coverage
Test Results
Manual Testing Steps
📚 Documentation
Documentation Updates
Breaking Changes
Breaking Changes:
Migration Guide:
🔍 Code Quality
Code Review Checklist
Performance Impact
Security Review
🚀 Deployment & Integration
Deployment Notes
Integration Points
📊 Impact Assessment
User Impact
Business Impact
✅ Final Checklist
Pre-Submission
Review Readiness
📸 Screenshots (if applicable)
🔗 Additional Resources
💬 Notes for Reviewers
Please pay special attention to:
Questions for reviewers:
Thank you for your contribution to Predictify! 🚀
Closes #1392, Closes #1411