[Minor] sdk: latestEpoch walks block history, not KV state — picks the wrong epoch when publication order differs from number order, renewLease rejected
Report
Summary
The 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.
Location
sdk/src/client.ts:342-356(latestEpoch,maxWalk = 64— walks back from the tip, returns the firstepoch.*set op scanning ops last-to-first)sdk/src/xe.ts:554-562(#epochAt— starts fromlatestEpoch(), steps back only whenstartNs > at, so it inheritslatestEpoch's selection)- Go (correct side):
xe/node/node.go:401-415(latestEpoch— max-pickev.EpochoverGetKVByPrefix("epoch.")),:439-461(epochAtFn— filterStartNS <= ns, sortev.Epochdesc, topEpochLockTolerance+1= 4) - Ledger check:
xe/core/ledger.go:2091-2112(validateLockedEmissionAt— accepts only if locked params match one of the 4 candidates),:223(EpochLockTolerance = 3) - Trigger enabler:
xe/statechain/kvstore.go:333-428(validateEpochValuenever checksev.Epochagainst 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)
Description
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.
Two live triggers (both confirmed by executed PoC against the real built SDK + mock fetch):
- 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). SDKlatestEpochreturns epoch 3 (R=1000); Go max-pick is epoch 8 (R=3000). - 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.
#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).
A 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.
Impact
SDK 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.
Reproduction
Executed PoC (dd2_s1_poc.mjs, Node + built sdk/dist @ 116f4b7, mock fetch — 5/5 scenarios confirmed):
- Control: tip empty, block 99 sets epoch.5 (R=3000) → SDK latestEpoch epoch 5, Go max-pick 5. Agree.
- 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.
- In-tolerance: block 99 epoch.5 (R=3000); block 100 sets NEW epoch.4 (R=5000) → SDK epoch 4; Go max-pick 5.
- Same block: ops
[set epoch.6, set epoch.5]→ SDK epoch 5 (last op); Go max-pick 6. - Full
renewLeaseon the out-of-order chain (mock node: lease, cert, threshold-1 timekeeper with real Ed25519 attestation, balance, chain): submitted block lockslocked_r=1000(epoch.3); Go's window is [8,7,6,5] — epoch.3 is outside it, the ledger rejects the block.
Suggested fix
Read 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".
Not a duplicate
- XE-12404 (r2183, Medium, SOLID) is the nearest neighbor and a DIFFERENT failure mode: its lookup FAILS (404
key not foundon a missingepoch.N-1, 503no 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. - 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.
- 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.govs SDKclient.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. - XE-1297 (closed) is duplicate
EpochValue.Epochnumbers 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. - 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.
- 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. - 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.)
Thread
Thanks. This is the same finding as an earlier report that is not public, which was filed first; the programme pays the first verifiable submission, so this one closes as a duplicate. If you believe it is a different bug, file a new report that links https://bounty.xe.network/XE-12555 and says what differs; it is read like any other.