feat: weighted median oracle aggregation#806
Open
Davichi-1 wants to merge 1 commit into
Open
Conversation
Add a configurable per-source weighted median primitive in oracles.rs to aggregate oracle readings where each source carries an explicit, configurable weight (issue Predictify-org#704). - New `WeightedOraclePrice { price, weight }` type; weight 0 disables a source via configuration without removing it from the list. - New `OracleUtils::weighted_median(env, readings)` returning the lower weighted median. Overflow-safe checked i128 math, no unwrap() on the production path, outlier-robust, and input-order independent. - 11 focused unit tests covering dominance, equal-weight parity with a plain median, zero-weight skipping, exact-half tie-break, large-value overflow safety, and empty/all-zero rejection. - Document the new API in API_DOCUMENTATION.md. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
@Davichi-1 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.
Overview
This PR adds a configurable per-oracle weighted median aggregation primitive to
contracts/predictify-hybrid/src/oracles.rsso oracle readings can be combined using explicit, per-source weights. Each source contributes a caller-configured weight, and the result is the weighted median — which is robust to outliers (a single mis-reporting source cannot drag the result far, regardless of its weight). A weight of0lets an operator disable a source via configuration without removing it from the list.Related Issue
Closes #704
Changes
⚙️ Weighted Median Aggregation
[ADD]
contracts/predictify-hybrid/src/oracles.rs—WeightedOraclePrice { price, weight }Defined a
#[contracttype]struct pairing an oracle price with its configurable per-source weight.A weight of
0excludes the reading from aggregation (disable-via-config) without removing it from the list.Added a
new(price, weight)constructor.[ADD]
contracts/predictify-hybrid/src/oracles.rs—OracleUtils::weighted_median(env, readings)Aggregates
Vec<WeightedOraclePrice>and returns the (lower) weighted median price.Sorts
(price, weight)pairs ascending and returns the price at which twice the cumulative weight first reaches the total weight (division-free crossing test).Overflow-safe: all weight accumulation uses checked
i128arithmetic; nounwrap()on the production path.Outlier-robust and input-order independent; returns
Error::InvalidInputwhen no reading carries a positive weight (empty list or all-zero weights).Full
///rustdoc with formula, error table, tie-break convention, overflow-safety notes, and a doctest.🧪 Tests
contracts/predictify-hybrid/src/oracles.rs—#[cfg(test)] mod weighted_median_testsi128::MAX) overflow safety, and empty / all-zero rejection.📚 Documentation
API_DOCUMENTATION.mdWeightedOraclePricestruct andOracleUtils::weighted_medianentry under Oracle Management.Verification Results
The
predictify-hybridcrate has pre-existing, unrelated native build errors onmaster(e.g. an undeclaredperformance_benchmarksmodule inlib.rs), so the full crate cannot be compiled/tested natively today. The new code was verified to add zero new errors/warnings (build diagnostics identical before and after: 259 errors / 162 warnings → 259 / 162), and the new struct + function + test module were compiled and run verbatim against the realsoroban-sdk25.3.1 in an isolated crate — all 11 tests pass.weight = 0disables a source via configurationunwrap()on production pathsError::InvalidInputon empty / all-zero-weight inputAPI_DOCUMENTATION.md)