Skip to content

fail-closed credit price on stale oracle - #26

Merged
SaulBuilds merged 2 commits into
mainfrom
fix/credit-price-fail-closed-stale-oracle
Nov 30, 2026
Merged

SaulBuilds merged 2 commits into
mainfrom
fix/credit-price-fail-closed-stale-oracle

Conversation

@SaulBuilds

Copy link
Copy Markdown
Contributor

Summary

X21 (0.6.0 x TOK1B crosswalk) and the oracle v2 ADR (citrate-federation#745) section 5, consumer C9 (the C5 view): TreasuryManager.get_credit_price_usd never returns a price from a stale or unset oracle.

BulkComputeGateway.currentCreditPriceUsd() returns oracle.computePriceUsdCents() * 10_000 with no staleness check. So before reading it, the manager:

  • reads the gateway's oracle(), then that oracle's lastComputeUpdateBlock() (the view reads only the compute track);
  • refuses a 0 block (no committee round ever set the compute price; the constructor value is not a posted price);
  • reads that block's timestamp and refuses when it is more than oracle_staleness_seconds old on the local clock. The new keyword defaults to DEFAULT_ORACLE_STALENESS_SECONDS = 900 (owner value, 2026-11-30). It is a time bound, not v1's block-counted isPriceStale(), and it is per track, not the shared lastUpdateBlock;
  • raises the new PriceUnavailableError (a CitrateError, exported) on any refusal, including an unreadable view or block, and reads no price.

A 0 or negative oracle_staleness_seconds raises ConfigurationError at construction. At the TOK1B reroll, v2's credit price view reverts when stale and this bridge maps that revert to the same error.

Tests

  • Red first (49136af): tests/test_treasury_stale_price.py, 6 of 7 failing on main.
  • Green (2d07eb0): treasury modules 37 passed locally; ruff clean; mypy clean on the changed files. The full suite runs in CI (its economics simulation and key-roundtrip tests take a long time locally).
  • test_get_credit_price_usd_decode now answers the freshness reads (a block mined now) and asserts that the price read is the last call. No assertion was dropped.

Review record

Money path (pricing): one independent review requested in the swarm review queue.

🤖 Generated with Claude Code

https://claude.ai/code/session_019cgBjEUXiPasw1yPyGXkXC

SaulBuilds and others added 2 commits November 29, 2026 23:01
… (red)

X21 / oracle v2 ADR (fed#745) consumer C9. BulkComputeGateway's
currentCreditPriceUsd() view has no staleness check, and
get_credit_price_usd returns it as is today: the stale, unset and
unreadable cases return a price and the bound option does not exist
(6 of 7 fail).

Signed-off-by: Larry Klosowski <larryklosowski@proton.me>

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019cgBjEUXiPasw1yPyGXkXC
… oracle

X21 / oracle v2 ADR (fed#745) section 5, consumer C9 (C5 view).
BulkComputeGateway.currentCreditPriceUsd() returns the v1 compute price
with no staleness check, so get_credit_price_usd now checks it first:
the gateway's oracle(), that oracle's lastComputeUpdateBlock() and the
block's timestamp. It raises the new PriceUnavailableError, and reads no
price, when the compute price was never set by a committee round (block
0), is older than oracle_staleness_seconds on the local clock (new
keyword, default DEFAULT_ORACLE_STALENESS_SECONDS = 900, owner
2026-11-30; a time bound, not v1's block-counted isPriceStale(), and the
compute track only, not the shared lastUpdateBlock), or cannot be read.
A 0 or negative bound is refused at construction.

The existing decode test now answers the freshness reads (a block mined
now) and asserts the price read comes last. pytest on the treasury
modules 37 passed (local); ruff and mypy clean on the changed files.

Signed-off-by: Larry Klosowski <larryklosowski@proton.me>

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019cgBjEUXiPasw1yPyGXkXC
@SaulBuilds
SaulBuilds requested a review from a team as a code owner November 30, 2026 07:17
@SaulBuilds

Copy link
Copy Markdown
Contributor Author

Independent review (Claude reviewer-K) @2d07eb07: APPROVE

The data source is real (Rule 7), checked against citrate-chain main contracts/src/BulkComputeGateway.sol.

  • oracle is IComputePricingOracle public oracle (line 42), so the oracle() getter exists.
  • currentCreditPriceUsd() (line 307) returns oracle.computePriceUsdCents() * 10_000 with no staleness check. The compute track alone is the right one to check.
  • The oracle address is read from the gateway on every call, so a governance setOracle (line 332) is followed.
  • lastComputeUpdateBlock is a public field of ComputePricingOracle (added in 75fcee50), and the constructor leaves it at 0, so 0 really means "never posted".
  • I found no other oracle-priced read in the SDK that bypasses the check:
    • purchaseCredits is guarded on chain (require(!oracle.isPriceStale()), line 158);
    • estimateCallsRemaining does not read the oracle.

Fails closed on every path, and the price is never read on a refusal.

  • Any exception reading oracle() or lastComputeUpdateBlock().
  • A block of 0.
  • A missing header or timestamp: None["timestamp"] and a KeyError are both caught.
  • A timestamp more than the bound ahead of the local clock.
  • An age over the bound.
  • A non-int, bool, 0 or negative oracle_staleness_seconds raises ConfigurationError.

The default of 900 s is one named constant. PriceUnavailableError subclasses CitrateError and is exported.

Tests.

  • FakeChain routes by to and selector, so reading the freshness from the wrong contract would fail. It counts price reads, and every refusal asserts 0 of them.
  • In test_get_credit_price_usd_decode, the existing price assertion is kept, and a new one pins that the price read comes last.
  • Mac run (repo .venv against this checkout): pytest tests/test_treasury.py tests/test_treasury_stale_price.py gives 37 passed.
  • CI: CodeQL, uv-lock-check, contract-drift, keyshare tripwire and secret-scan are green. python / test, locked-env and ci are still running. Merge only when they are green.

nb (not blocking). The same nb as on sdk-marketplace#23:

  1. The freshness reads and the price read use latest separately. Pinning all of them to one block number would make the check and the price describe the same state.
  2. A timestamp up to bound ahead of the local clock is accepted.

Not using the approve button and not merging.

@BerryManifold BerryManifold left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated Codex static review of 2d07eb0797c94ac845e1717c809f6ec13f2252bb is complete. Result: blockers; details retained privately. Details and coverage limits are recorded in the project's private review log. No tests were executed by this reviewer. Both account submissions represent the same Codex review, not independent reviews.

@Matr0xshka Matr0xshka left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated Codex static review of 2d07eb0797c94ac845e1717c809f6ec13f2252bb is complete. Result: blockers; details retained privately. Details and coverage limits are recorded in the project's private review log. No tests were executed by this reviewer. Both account submissions represent the same Codex review, not independent reviews.

@SaulBuilds
SaulBuilds merged commit 9e7c20b into main Nov 30, 2026
10 checks passed
@SaulBuilds
SaulBuilds deleted the fix/credit-price-fail-closed-stale-oracle branch November 30, 2026 07:33
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.

3 participants