Skip to content

fix(scenarios/phoenix): oracle staleness - #29

Merged
failfmi merged 5 commits into
fix/scenarios/phoenix/oracle-stalenessfrom
fix/scenarios/phoenix-oracle-staleness
Oct 7, 2026
Merged

failfmi merged 5 commits into
fix/scenarios/phoenix/oracle-stalenessfrom
fix/scenarios/phoenix-oracle-staleness

Conversation

@failfmi

@failfmi failfmi commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

RetriggerConfidence Score: 5/5

The PR appears safe to merge, with no outstanding findings.

Summary

Phoenix overrides raise oracle stale thresholds in the local PerpAssetMap, keeping fetched readings usable as the clock advances.

  • Prices and reading slots stay unchanged.
  • Tests cover both SOL and BTC, addressing the earlier concern about later markets.
  • No new issues were found. Changes since the previous review do not touch this PR’s files.

Reviews (4) · Last reviewed commit: "ci: remove claude code" · Reviewed by Greptile

@failfmi failfmi self-assigned this Oct 7, 2026

@github-actions github-actions Bot 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.

Summary

Forked Phoenix markets go stale because nothing updates their oracle readings. This PR fixes that by raising every market's spot and perp stale_threshold in the local PerpAssetMap to u32::MAX whenever any Phoenix override runs. Map overrides put the raised thresholds in the same write. Other overrides fetch the map if it isn't local, raise the thresholds, and log a warning on failure without failing the override. The unit tests and the mainnet clock-advance test cover the main paths. The PR also adds a claude-review.yml workflow, which the title doesn't mention.

Overall the approach is sound and the tests are good. Main concerns:

  • Errors are handled differently on the two paths. On the PerpAssetMap path, a raise_stale_thresholds error is propagated with ?, so any decode or offset-search problem now fails a map override that used to succeed. On every other path the same error is only logged.
  • The offset search is weaker than the existing helper. It finds metadata by taking the first byte match after a cursor and assumes iteration order matches storage order. patch_market_metadata uses unique_subslice_offset, which requires exactly one match.
  • Side effects on failure. keep_oracle_readings_usable runs before the Trader override is validated, so the map is changed (and possibly fetched from upstream) even when that override then fails. It also re-parses the whole map on every Phoenix override, including accounts that end up on the generic Ok(None) path. This is probably acceptable, but it should be documented.
  • Unrelated workflow. .github/workflows/claude-review.yml has nothing to do with oracle staleness and should be a separate PR.

let kept = async {
let map = phoenix_dependency(svm, &PHOENIX_PERP_ASSET_MAP, remote_ctx).await?;
let Some(data) = raise_stale_thresholds(&PHOENIX_PERP_ASSET_MAP, &map.data)? else {
return Ok(());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The map is written here with svm.set_account, but the caller in svm.rs never adds it to settled_this_slot. As a result, a later map override in the same slot with fetchBeforeUse: true refetches the map from upstream. It still ends up with raised thresholds, because the map path raises them again, but it pays for one more remote fetch.

const PREPARATION_SLOT: u64 = 0;
/// The threshold is a `u64` that Phoenix may multiply by a `u8` hard-stale multiplier; unlike
/// `u64::MAX`, this keeps that product from overflowing.
const KEPT_STALE_THRESHOLD_SLOTS: u64 = u32::MAX as u64;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The comment says Phoenix multiplies the threshold by a u8. If Phoenix also adds the threshold to a slot (e.g. last_update_slot + threshold * mult), u32::MAX * 255 still fits in a u64, so this is safe. Please note in the comment which Phoenix check this is based on, so the bound can be re-checked when the program is upgraded.

Comment thread .github/workflows/claude-review.yml Resolved
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown

Overview

The PR raises every market's spot and perp oracle stale_threshold in the local PerpAssetMap to u32::MAX, so Phoenix markets stay usable on a fork as the local clock advances. The raise happens in two places:

  • Map overrides: it goes into the same write as the override.
  • Any other Phoenix override: a best-effort pass runs first and fetches the map if it isn't local.

The PR also adds unit tests (including a two-market fixture), a mainnet clock-advance test, README guidance, and an unrelated Claude review workflow.

Assessment

The core logic is sound:

  • The search cursor only moves forward, so each market's metadata is found once.
  • Thresholds that are already higher are kept.
  • Failures only log a warning and never fail the override.
  • u32::MAX × u8 multiplier fits in a u64.

The tests are thorough. The byte-diff check that nothing outside the PriceComponents changes is a good addition.

Issues and suggestions

  • Scope: .github/workflows/claude-review.yml has nothing to do with oracle staleness. It would be better as a separate PR.
  • Silent behavior change: after any Phoenix override, staleness can no longer be triggered for the rest of the session. A user who wants to test Phoenix's own stale-oracle rejection can't do it once any Phoenix scenario has run. This should at least be stated in the README, or be possible to turn off.
  • Side effect of a refused override: keep_oracle_readings_usable runs before the override's own validation. An override that is later rejected (for example, raising collateral) still fetches the map and writes the raised thresholds.
  • Warning noise: with no remote and no local map, every Phoenix override logs the same warning. Consider logging it once.

crates/core/src/scenarios/protocols/phoenix-eternal/v1/state_builder.rs:209

The offset is found by searching the raw bytes for each decoded metadata. This works, but it depends on metadata.as_bytes() being byte-for-byte identical to what is stored, and on the market pubkey keeping each match unique. If the decoder can return each entry's offset, or the entry layout is fixed (48-byte header + 1584-byte stride + 16-byte symbol, as the tests already assume), computing the offset directly would be O(n) and clearer than a sliding-window search over the whole map. It would also fail more obviously if the layout changes.


crates/core/src/scenarios/protocols/phoenix-eternal/v1/state_builder.rs:217

This depends on PriceComponent being at offset 0 of PerpAssetMetadata, and that is only stated in a comment. Add a compile-time check, e.g. const _: () = assert!(core::mem::offset_of!(PerpAssetMetadata, <field>) == 0);, or use offset_of! when computing the write range. Otherwise an upstream layout change would silently overwrite the wrong bytes.


crates/core/src/scenarios/protocols/phoenix-eternal/v1/state_builder.rs:356

This runs for every Phoenix-owned account that isn't a map, including None (unknown discriminant) and kinds that end up returning Ok(None) and take the generic path. When the map isn't local, each of these overrides now makes a network fetch. If the override is later refused (e.g. ensure_collateral_is_lowered fails), the map is still fetched and modified. Consider:

  • running this only after the override's own preparation succeeds, or
  • documenting that a refused override still changes the map's thresholds.

crates/core/src/tests/phoenix/mod.rs:620

The test builds a second full mainnet-backed locker just to get a clean starting state, which doubles the RPC load in an already network-bound test. You could reuse the first locker: reset the clock to graph.clock before playing the scenario, then move it forward again.


.github/workflows/claude-review.yml:76

This workflow is unrelated to the oracle-staleness fix; consider moving it to its own PR. Also, the PR title and body are pasted directly into the prompt. Fork PRs are skipped, but any same-repo contributor can steer the review output. The impact is limited because the tools are read-only, but it is worth knowing.

@github-actions github-actions Bot 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.

What this PR does: Phoenix rejects a market once its oracle readings are older than the market's stale threshold. Nothing updates those readings on a local fork, so this PR raises every market's spot and perp stale_threshold in the local PerpAssetMap to u32::MAX. The map override applies the raise in the same write. Every other Phoenix-owned override applies it first, through keep_oracle_readings_usable. The PR also updates the README and adds unit tests (two markets, failure fallbacks) and a mainnet test that moves the clock forward by 1M slots.

Assessment: The approach is reasonable and well tested. If the raise fails, the requested override still applies, and the multiplier math cannot overflow (u8 * u32::MAX fits in a u64). Things to consider:

  • Side effect applies to the whole session, even when it shouldn't. keep_oracle_readings_usable writes the map before the override's own validation runs. If the override then errors and is skipped, or the account falls through to the generic IDL path (_ => Ok(None)), the thresholds still stay raised. After any Phoenix scenario is played, users can no longer test stale-oracle behaviour. It may be worth documenting this as a deliberate trade-off, or putting it behind an opt-out.
  • Inconsistent with the map path. If forge_phoenix_override fails on a PerpAssetMap override (e.g. a bad value), the thresholds are not raised. Every other Phoenix override kind raises them unconditionally.
  • Cost. Each Trader or other Phoenix override decodes and scans the whole map again. It's cheap when nothing changes, but it repeats work across multiple overrides in the same slot.
  • Unrelated change. .github/workflows/claude-review.yml has nothing to do with the oracle-staleness fix and would be better as its own PR. Also, the workflow puts the PR title and body directly into the prompt. That is acceptable only because fork PRs are skipped, so it's worth keeping in mind.

for entry in map.iter() {
let metadata = entry.map_err(decode_error)?.metadata;
let metadata_bytes = metadata.as_bytes();
let offset = data[cursor..]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This finds each entry's offset by searching the raw bytes for the metadata (windows().position()). It works because each market's metadata includes its unique market pubkey, but it's indirect, and it costs O(len × metadata_len) in the worst case. If the decoder can return each entry's byte offset, or if entries sit at fixed header + slot * entry_size positions (the tests already assume 48 + i * 1584), computing the offset directly would be simpler and could never point at the wrong location.

.map(Some),
_ => Ok(None),
kind => {
keep_oracle_readings_usable(svm, remote_ctx).await;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This runs before prepare_trader_override validates anything, and it also runs for Phoenix accounts that return Ok(None) (the generic IDL path). As a result, the map is changed permanently even when this override is later skipped with an error. If that's intended ("keep the session usable regardless"), it would help to say so explicitly in the README. Users who want to test stale-oracle rejection lose that ability as soon as any Phoenix scenario runs.

@@ -280,6 +334,16 @@ pub async fn prepare_phoenix_override(
Some(PhoenixAccount::PerpAssetMap) => {
let mark_slot = svm.inner.get_sysvar::<Clock>().slot;
let data = forge_phoenix_override(account_pubkey, account, values, mark_slot)?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If forge_phoenix_override fails here, the ? returns before raise_stale_thresholds runs, so a bad PerpAssetMap override leaves the thresholds unchanged. All other Phoenix override kinds raise them unconditionally through keep_oracle_readings_usable. Consider making the two paths consistent: either always raise, or only raise when the override succeeds.

@failfmi
failfmi merged commit a54c85d into fix/scenarios/phoenix/oracle-staleness Oct 7, 2026
2 checks passed
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