{"id":"XE-12555","kind":"bounty","visibility":"public","source":"web","source_repo":"","source_ref":"","source_url":"","reporter_id":2086,"title":"[Minor] sdk: latestEpoch walks block history, not KV state — picks the wrong epoch when publication order differs from number order, renewLease rejected","area":"sdk-docs-web","claimed_tier":"minor","assessed_tier":null,"verdict":"duplicate","duplicate_of":null,"tracked":false,"internal_ref":"","internal_url":"","status":"closed","closed_reason":"duplicate","github_state":"","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-11-14T06:35:24.484552+00:00","updated_at":"2026-11-20T19:45:55.580763+00:00","source_created_at":null,"told_at":"2026-11-20T19:45:55.597455+00:00","status_changed_at":"2026-11-20T19:45:55.585443+00:00","url":"https://bounty.xe.network/XE-12555","body":"## Summary\r\n\r\nThe SDK defines \"latest epoch\" as the most recently PUBLISHED epoch, while the node defines it as the HIGHEST-NUMBERED epoch. `latestEpoch` walks back through statechain blocks and returns the first `epoch.*` op it finds; the node's `latestEpoch` max-picks `ev.Epoch` over the KV state. Once the oracle publishes a lower-numbered epoch after a higher-numbered one — legal, since epoch numbers are never validated — the two diverge: the SDK locks renewal params from the old epoch, and the ledger rejects the renewal when that epoch falls outside the top-4 tolerance window. The lookup succeeds; the answer is wrong.\r\n\r\n## Location\r\n\r\n- `sdk/src/client.ts:342-356` (`latestEpoch`, `maxWalk = 64` — walks back from the tip, returns the first `epoch.*` set op scanning ops last-to-first)\r\n- `sdk/src/xe.ts:554-562` (`#epochAt` — starts from `latestEpoch()`, steps back only when `startNs > at`, so it inherits `latestEpoch`'s selection)\r\n- Go (correct side): `xe/node/node.go:401-415` (`latestEpoch` — max-pick `ev.Epoch` over `GetKVByPrefix(\"epoch.\")`), `:439-461` (`epochAtFn` — filter `StartNS <= ns`, sort `ev.Epoch` desc, top `EpochLockTolerance+1` = 4)\r\n- Ledger check: `xe/core/ledger.go:2091-2112` (`validateLockedEmissionAt` — accepts only if locked params match one of the 4 candidates), `:223` (`EpochLockTolerance = 3`)\r\n- Trigger enabler: `xe/statechain/kvstore.go:333-428` (`validateEpochValue` never checks `ev.Epoch` against the key suffix or monotonicity — XE-6400); `xe/statechain/chain.go:181-183` (write-once is per KEY only — a NEW key with a lower number passes)\r\n\r\n## Description\r\n\r\n`latestEpoch`'s own doc comment states the design choice: walk back from the tip \"rather than list the KV space (which pages lexicographically — epoch.1000 sorts before epoch.999)\". That choice bakes in the assumption that publication order equals number order. The validator never enforces that assumption (XE-6400), and write-once only forbids re-setting an EXISTING key — a new key carrying a lower number is admitted.\r\n\r\nTwo live triggers (both confirmed by executed PoC against the real built SDK + mock fetch):\r\n1. **Out-of-order publication across blocks:** blocks 96–99 publish epoch.5–8; block 100 (tip) publishes a NEW key `epoch.3` (backfill / operator correction). SDK `latestEpoch` returns epoch 3 (R=1000); Go max-pick is epoch 8 (R=3000).\r\n2. **Order within one block:** block 100 carries ops `[set epoch.6, set epoch.5]`. The SDK scans ops last-to-first and returns epoch 5; Go max-pick is 6.\r\n\r\n`#epochAt` inherits the wrong selection: it starts from `latestEpoch()` and only steps back when `startNs > at`. In trigger 1 the wrong epoch has already started, so no step-back happens and `renewLease` submits a block locking epoch.3's params. The ledger's tolerance window is the top 4 by number ([8,7,6,5]); epoch.3 is outside it, so `validateLockedEmissionAt` rejects the renewal with \"locked emission mismatch\". `renewLease` — the SDK's only renewal path — is dead on a valid chain until the oracle publishes the next epoch (at which point the walk finds the new tip op and self-heals).\r\n\r\nA second symptom of the same root cause, stated honestly and NOT claimed as a separate finding: if the wrongly-picked epoch is still INSIDE the top-4 window, the renewal is ACCEPTED with stale params. That in-window consequence overlaps XE-0103 (the ledger accepting locked terms from any of the last 4 epochs, whose suggested fix names the renew path), and realised impact today is zero — XE-0103 measured 1,000 sampled epochs carrying identical params. The finding claimed here is the liveness leg (rejection outside the window), not the stale-params leg.\r\n\r\n## Impact\r\n\r\nSDK renewal liveness: `renewLease` fails on a valid chain whenever publication order diverges from number order and the picked epoch falls outside the top-4 window. No funds are lost (the block is rejected at submit). The trigger needs an out-of-order publication — a backfill or operator correction, both legal under the current validator — which is why this is Minor, not Medium: rarer than XE-12404's gap/distance triggers in normal operation, and self-healing on the next publication.\r\n\r\n## Reproduction\r\n\r\nExecuted PoC (`dd2_s1_poc.mjs`, Node + built `sdk/dist` @ 116f4b7, mock fetch — 5/5 scenarios confirmed):\r\n- Control: tip empty, block 99 sets epoch.5 (R=3000) → SDK latestEpoch epoch 5, Go max-pick 5. Agree.\r\n- Out-of-order: blocks 96–99 epoch.5–8; block 100 sets NEW epoch.3 (R=1000) → SDK latestEpoch **epoch 3, R=1000**; Go max-pick 8.\r\n- In-tolerance: block 99 epoch.5 (R=3000); block 100 sets NEW epoch.4 (R=5000) → SDK epoch 4; Go max-pick 5.\r\n- Same block: ops `[set epoch.6, set epoch.5]` → SDK epoch 5 (last op); Go max-pick 6.\r\n- Full `renewLease` on the out-of-order chain (mock node: lease, cert, threshold-1 timekeeper with real Ed25519 attestation, balance, chain): submitted block locks `locked_r=1000` (epoch.3); Go's window is [8,7,6,5] — epoch.3 is outside it, the ledger rejects the block.\r\n\r\n## Suggested fix\r\n\r\nRead `latestEpoch` from KV (`GET /statechain/kv/epoch.` prefix, max-pick `ev.Epoch`) like Go's `epochAtFn` — do not walk block history. The lexicographic-paging concern in the doc comment is solvable client-side (parse the numeric suffix, max-pick numerically). The minimal XE-12404 fix (step back on 404 + raise `maxWalk`) does NOT fix this finding — the walk itself is the wrong definition of \"latest\".\r\n\r\n## Not a duplicate\r\n\r\n- **XE-12404** (r2183, Medium, SOLID) is the nearest neighbor and a DIFFERENT failure mode: its lookup FAILS (404 `key not found` on a missing `epoch.N-1`, 503 `no epoch in the last 64 blocks`, 4-step limit exhausted) — the SDK gets NO answer. This finding's lookup SUCCEEDS and returns the WRONG epoch, with no error. Different trigger (out-of-order publication vs gap/distance), different failure mode (wrong answer vs no answer), and XE-12404's own suggested fix (step back on 404 + raise maxWalk) does not fix this finding at all.\r\n- **XE-6400** is the PREREQUISITE (epoch numbers never validated), not a duplicate — the same framing XE-12404 itself used and that was accepted as SOLID. Fixing XE-6400 would close the trigger, but the SDK/Go definitional divergence is a separate defect in the SDK.\r\n- **XE-8377 / XE-2376 / XE-7563** are the Go-node RenewLease stamping family (the NODE builder stamps locked params from the tip/latest epoch instead of the attested-time epoch). Different component (Go `node/lease_renew.go` vs SDK `client.ts`), different trigger (epoch boundary at attestation time vs out-of-order publication). XE-8377's own body states the TS SDK \"already resolve[s] the epoch at the attested timestamp\" — this finding shows the SDK's resolution is itself wrong in a different way.\r\n- **XE-1297** (closed) is duplicate `EpochValue.Epoch` numbers making GO's max-pick nondeterministic on ties (Go map iteration order). Different trigger (duplicate numbers vs out-of-order publication), different code (Go max-pick vs SDK walk), different failure (nondeterministic tie vs deterministic wrong pick). S1 triggers with all-distinct epoch numbers.\r\n- **XE-0103** (triaging, Medium) claims the LEDGER's tolerance window (locked terms from any of the last 4 epochs are accepted; provider-selected stale terms). It bounds this finding's in-window symptom only — the liveness leg claimed here (rejection OUTSIDE the window, caused by the SDK's selection) is not in XE-0103, whose mechanism is the validator's acceptance, not the SDK's selection.\r\n- **XE-3017** (Go-side epoch selection ignoring the time window), **XE-11800** (future epoch poisoning Go `CurrentR`), **XE-8700/XE-9089** (EndNS never enforced), **XE-1862** (zero-fill mapper), **XE-2793** (R unbounded), **XE-10695** (phase-1 terms lingering), **XE-1125** (multi-op pre-block snapshot), **XE-7732** (unbounded epoch scan cost) — all different mechanisms, components, or layers.\r\n- Discriminative searches on the public board return nothing on this defect: `q=\"out-of-order epoch\"` → 0, `q=\"publication order\"` → 0, `q=\"most recently published\"` → 0, `q=\"walks back\"` → XE-12404 only. (~84% of tickets are sealed, so this is clean as far as the public board shows.)","patch_refs":[],"thread":[{"id":15344,"source":"desk","source_ref":"reply:13235","author":"Joseph Denne","team":true,"body":"Thanks. This is the same finding as an earlier report that is not public, which was filed first; the programme pays the\nfirst verifiable submission, so this one closes as a duplicate. If you believe it is a different\nbug, file a new report that links https://bounty.xe.network/XE-12555 and says what differs; it is read like any other.","created_at":"2026-11-20T19:45:55.580763+00:00"}]}