|
1 | | -# Example Module |
| 1 | +# Oracle Module |
2 | 2 |
|
3 | | -This is a module base generated with [`spawn`](https://github.com/rollchains/spawn). |
| 3 | +## Abstract |
| 4 | + |
| 5 | +This module is a **fork of `ojo-network/x/oracle`**, refactored to utilize the **Cosmos SDK Collections** framework for state management. This ensures type-safety, improved performance through native binary codecs, and a more maintainable schema definition. |
| 6 | + |
| 7 | +## Contents |
| 8 | + |
| 9 | +1. **[Concepts](https://www.google.com/search?q=%23concepts)** |
| 10 | +2. **[State & Collections](https://www.google.com/search?q=%23state--collections)** |
| 11 | +3. **[End Block](https://www.google.com/search?q=%23end-block)** |
| 12 | +4. **[Messages](https://www.google.com/search?q=%23messages)** |
| 13 | +5. **[Events](https://www.google.com/search?q=%23events)** |
| 14 | +6. **[Parameters](https://www.google.com/search?q=%23params)** |
| 15 | + |
| 16 | +## Concepts |
| 17 | + |
| 18 | +### Voting Procedure |
| 19 | + |
| 20 | +The Oracle module obtains consensus via a **Commit-Reveal scheme** over a `VotePeriod`. |
| 21 | + |
| 22 | +* **Prevote and Vote**: |
| 23 | +* `MsgAggregateExchangeRatePrevote`: A SHA256 hash of the rates. |
| 24 | +* `MsgAggregateExchangeRateVote`: The salt and actual rates to reveal the previous period's commitment. |
| 25 | + |
| 26 | + |
| 27 | +* **Vote Tally**: At the end of `VotePeriod`, the module verifies hashes and calculates the **Median** exchange rate. Rates receiving less than `VoteThreshold` power are deleted. |
| 28 | +* **Ballot Rewards**: Winners (those within the `RewardBand`) are rewarded from the reward pool. In this fork, the `ValidatorRewardSet` is cached to optimize reward distribution across the `SlashWindow`. |
| 29 | + |
| 30 | +### Slashing |
| 31 | + |
| 32 | +Validators must maintain a `MinValidPerWindow` (e.g., 5%) success rate. Failure to vote on **all** assets in the `AcceptList` or voting outside the `RewardBand` results in a "miss." If the threshold is not met by the end of a `SlashWindow`, the validator is slashed and jailed. |
| 33 | + |
| 34 | +## State & Collections |
| 35 | + |
| 36 | +This module utilizes `cosmossdk.io/collections` for all on-chain storage. This removes manual byte-prefixing and Protobuf wrapping (e.g., `gogotypes.UInt64Value`) in favor of type-safe Maps and Items. |
| 37 | + |
| 38 | +### Exchange Rates |
| 39 | + |
| 40 | +Stored as a `math.LegacyDec`. |
| 41 | + |
| 42 | +* `ExchangeRates`: `Map<string, math.LegacyDec>` |
| 43 | +* `HistoricPrices`: `Map<Pair<string, uint64>, math.LegacyDec>` (Denom + BlockHeight) |
| 44 | + |
| 45 | +### Validator Management |
| 46 | + |
| 47 | +* **FeederDelegation**: Maps a validator operator to a proxy "feeder" account. |
| 48 | +* `FeederDelegations`: `Map<sdk.ValAddress, sdk.AccAddress>` |
| 49 | + |
| 50 | + |
| 51 | +* **MissCounter**: Tracks missed vote periods. |
| 52 | +* `MissCounters`: `Map<sdk.ValAddress, uint64>` |
| 53 | + |
| 54 | + |
| 55 | +* **ValidatorRewardSet**: A singleton storing the active validators eligible for rewards. |
| 56 | +* `ValidatorRewardSet`: `Item<types.ValidatorRewardSet>` |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +### Voting State |
| 61 | + |
| 62 | +* **AggregateExchangeRatePrevote**: `Map<sdk.ValAddress, types.AggregateExchangeRatePrevote>` |
| 63 | +* **AggregateExchangeRateVote**: `Map<sdk.ValAddress, types.AggregateExchangeRateVote>` |
| 64 | + |
| 65 | +### Price Averages (dmchain specific) |
| 66 | + |
| 67 | +The `dmchain` fork includes native support for computed averages: |
| 68 | + |
| 69 | +* `Averages`: `Map<Pair<string, string>, math.LegacyDec>` (Denom + AvgType, e.g., "SMA", "EMA") |
| 70 | + |
| 71 | +## End Block |
| 72 | + |
| 73 | +At the end of every `VotePeriod`: |
| 74 | + |
| 75 | +1. **Purge**: Expired exchange rates are cleared. |
| 76 | +2. **Organize**: Votes are grouped into ballots by denomination. |
| 77 | +3. **Tally**: |
| 78 | +* Calculate the **Median** and **Standard Deviation**. |
| 79 | +* Define the winners within the `RewardBand`. |
| 80 | + |
| 81 | + |
| 82 | +4. **Record**: Update `ExchangeRates` and compute `Averages` (SMA/EMA/WMA). |
| 83 | +5. **Slash/Reward**: Increment `MissCounters`, distribute rewards to `ValidatorRewardSet`, and jail underperforming validators at the end of the `SlashWindow`. |
| 84 | +6. **Cleanup**: Clear previous period votes and prevotes. |
| 85 | + |
| 86 | +## Messages |
| 87 | + |
| 88 | +The module supports the standard Ojo Oracle message set, including: |
| 89 | + |
| 90 | +* `MsgAggregateExchangeRatePrevote` |
| 91 | +* `MsgAggregateExchangeRateVote` |
| 92 | +* `MsgDelegateFeedConsent` |
| 93 | + |
| 94 | +## Params |
| 95 | + |
| 96 | +Parameters are managed via the standard `Params` struct, typically updated via governance. Keys include `VotePeriod`, `VoteThreshold`, `RewardBand`, `SlashWindow`, and the `AcceptList` (denominations to provide prices for). |
0 commit comments