Skip to content

test(oracle): cover price aggregation math (median, twap, deviation) - #2086

Open
parthod0x wants to merge 1 commit into
akash-network:mainfrom
parthod0x:test/oracle-aggregation-math
Open

parthod0x wants to merge 1 commit into
akash-network:mainfrom
parthod0x:test/oracle-aggregation-math

Conversation

@parthod0x

Copy link
Copy Markdown

What

Adds unit tests for the oracle price-aggregation helpers, which had no coverage.

x/oracle/keeper computes the aggregated AKT/USD price via calculateMedian, calculateTWAP and calculateDeviationBps. That price gates two consensus-critical paths — the BME mint circuit breaker (mintStatusUpdate/calculateCR) and the escrow AKT settlement fallback (settleFromAktFallback) — yet none of the three helpers were tested.

I reviewed the three functions and they are correct, so this is a pinning/regression test, not a bug fix.

Coverage

  • calculateMedian: empty → zero; odd → middle; even → average of the two middle values (unsorted input exercises the sort).
  • calculateDeviationBps: zero-min guard; (max-min)/min in basis points; no-deviation → 0.
  • calculateTWAP: empty → error; single point → that price; descending points time-weighted ((100·10s + 220·20s)/30s = 180); identical timestamps fall back to the simple average.

Internal test (package keeper) so it can exercise the unexported helpers. go test ./x/oracle/keeper/ passes; go vet ./x/oracle/keeper/ is clean.

The oracle price-aggregation helpers (calculateMedian, calculateTWAP,
calculateDeviationBps) compute the aggregated AKT/USD price that gates the BME
mint circuit breaker and the escrow AKT settlement fallback, but had no test
coverage. Add internal (package keeper) unit tests pinning their behaviour:

- calculateMedian: empty -> zero; odd -> middle; even -> average of the two
  middle values (unsorted input exercises the sort).
- calculateDeviationBps: zero-min guard; (max-min)/min in basis points; no
  deviation -> 0.
- calculateTWAP: empty -> error; single point -> that price; descending points
  time-weighted; identical timestamps fall back to the simple average.

Test-only; the helpers are unchanged.

Signed-off-by: parthod0x <parthms.id@gmail.com>
@parthod0x
parthod0x requested a review from a team as a code owner October 25, 2026 08:51
@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 5193e54 and f3a1664.

Files selected for processing: 1
  • x/oracle/keeper/aggregation_math_test.go

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

Walkthrough

Added tests for median, deviation in basis points, and TWAP calculations. The cases include empty inputs, odd and even medians, zero or equal values, time-weighted prices, and identical timestamps.

Changes

Aggregation calculation tests

Layer / File(s) Summary
Calculation test cases
x/oracle/keeper/aggregation_math_test.go
Added test data setup and tests for median, deviation in basis points, and TWAP calculations. The tests cover empty inputs, unsorted values, time weighting, and identical timestamps.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Other

Suggested reviewers: boz

Merge Risk: Minimal · up to f3a16

No actionable merge-blocking risk was identified.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description check Passed The description clearly explains that the pull request adds unit tests for the oracle aggregation helpers and lists the covered behaviors.
Title check Passed The title clearly and concisely identifies the test-only change and the covered price aggregation calculations.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

🔧 golangci-lint (2.14.0)

golangci-lint skipped: reviews.tools.golangci-lint.config_file must be a repository-relative path.

A rabbit checks the median line
Then weighs the prices, neat and fine
The timestamps hop into a row
The weighted totals start to glow
Tests pass; the rabbit nibbles clover

Comment @coderabbitai help to get the list of available commands.

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.

1 participant