{"id":"XE-13379","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"533","source_url":"https://github.com/xeprotocol/xe/issues/533","reporter_id":2252,"title":"Rolling back a lease_cancel that won over an accept restores the lease to `created`, losing the accept and renewal history","area":"leasing-ledger","claimed_tier":"minor","assessed_tier":null,"verdict":"duplicate","duplicate_of":null,"tracked":false,"internal_ref":"","internal_url":"","status":"closed","closed_reason":"duplicate","github_state":"closed","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-11-14T20:42:13.581393+00:00","updated_at":"2026-11-20T00:51:55.732207+00:00","source_created_at":"2026-11-13T03:33:22+00:00","told_at":"2026-11-20T00:50:59+00:00","status_changed_at":"2026-11-20T00:50:05.739486+00:00","url":"https://bounty.xe.network/XE-13379","body":"**Suggested tier:** Low\n\n**XE address (reward destination):** 9b663fdac108be34a3b715ef78a9f9bd878af6ef8142f388c6514918e6852dd7\n\n## Summary\n\n`core/rollback.go`, `buildBlockUndo` for `BlockLeaseCancel` always restores the lease to `LeaseCreated` (`priorLease(b.Source, LeaseCreated)`), regardless of the lease's pre-cancel state. But cancel has two possible pre-states: `created`, or `accepted` (cancel wins over a racing accept via `resolveCancelWinsOverAccept`). Undoing a cancel that won over an accept therefore zeroes `StartTime` / `LockedR` / `LockedPayoutCap` / `LockedTWAP` / `Stake`, while keeping `Renewals` and restoring the full escrow.\n\nIf the provider then re-accepts, the fresh `PutLease` carries no renewals, so a later settle pays only the base emission and deletes the whole pending escrow. The renewal escrow is destroyed with no corresponding emission.\n\nRelated to #391 (same symptom family — renewal escrow destroyed) but a different root cause (`rollback.go` undo-restore, not the cancel refund in `ledger.go`) and a different trigger (fork rollback via quorum conflict resolution, not the pre-finality race). #391's fix (refund `pending.Amount` on cancel) does NOT fix this path.\n\n## Impact\n\nBounded fund destruction, same order as #391: 517 micro-XUSD (~$0.0005) per renewal. No theft, no inflation. Requires: fork wins a quorum vote → cancel rolled back → provider re-accepts → provider settles. Reachable in production: `core/quorum.go:321` `confirmConflict` → `swapBlockLocked` (`Ledger.SwapBlock`) undoes the loser block through exactly this `buildBlockUndo` path.\n\n## Steps to reproduce\n\nUnit test against the real ledger code (`github.com/xeprotocol/xe`, testnet-0005):\n\n```\ngit clone https://github.com/xeprotocol/xe\ncd xe\ncp round2_ledger_test.go core/\ngo test ./core -run TestRound2_UndoCancelLosesAcceptedState -v -count=1\n```\n\nNote: `round2_ledger_test.go` is NOT in the public repository — the PoC test file is attached to this issue; without it, the XE team cannot run this reproduction.\n\nThe test funds consumer + provider with 1,000,000 micro-XUSD each, creates a 60 s lease (cost 517 micro-XUSD), provider accepts, consumer renews for another 60 s (escrow 1034), consumer cancels, then `SwapBlock` replaces the cancel with a plain send (fork wins), provider re-accepts, provider settles.\n\n**Expected:** after rollback the lease returns to `accepted` with accept metadata intact; after re-accept renewals = 1; after settle provider XE emission = 1034.\n**Actual (reproduced 2026-11-13, verified independently by re-running the PoC):**\n```\nafter cancel: consumer XUSD=999483 (F1: renewal 517 destroyed)\nafter undo-cancel: state=created settled=false start=0 lockedR=0 renewals=1\nafter undo-cancel: escrow restored=1034 (base+renewal=1034), total XUSD in system=2000000\nafter re-accept: state=accepted renewals=0 (was 1 before the rollback)\nafter settle: provider XE emission=517 (full base+renewal emission would be 1034); total XUSD in system=1998966\n--- FAIL: TestRound2_UndoCancelLosesAcceptedState\n```\n\n## Root cause\n\n`core/rollback.go`, `buildBlockUndo`, `BlockLeaseCancel` case: `restoreLease = l.priorLease(b.Source, LeaseCreated)` — fixed target state. `priorLease` with target `LeaseCreated` additionally zeroes `Stake`, `StartTime`, `LockedR`, `LockedPayoutCap`, `LockedTWAP`. By contrast, undo of settle/force-settle always restores `accepted` (correct — those only run from `accepted`), undo of accept restores `created` (correct), and undo of renew pops exactly one renewal (correct, verified by control test).\n\n## Suggested fix\n\nRestore the lease to its actual pre-cancel state instead of synthesising one from a fixed target: record the pre-cancel lease snapshot at cancel time (or in the `BlockUndo`, e.g. a `RestoreLease` built from the lease as it stood before the cancel, including `State`, `StartTime`, `Locked*`, `Stake`, `Renewals`). Defense-in-depth: make `validateAndAddLeaseAccept` preserve any existing `Renewals` on re-accept, and/or make `validateAndAddLeaseSettle` reject when `escrow.Amount != lease.Cost + sum(renewal costs)`.\n\n## Controls verified (same PoC file, all PASS)\n\n- Undo of `lease_renew`: escrow back to exactly 517, settle pays base only.\n- Undo of `lease_settle`: lease back to `accepted` + renewals, escrow 1034, re-settle pays full 1034 XE.\n- Force-settle after renew refunds the full pending escrow (consumer 998,966 → 1,000,000).\n- Terminal-state guards: renew/cancel/second settle/force-settle after settle, and renew after cancel, all rejected.\n\n[round2_ledger.txt](https://github.com/user-attachments/files/34781944/round2_ledger.txt)","patch_refs":[{"id":572,"repo":"xeprotocol/xe","kind":"pr","ref":"535","title":"fix(core/rollback): preserve accept state and renewal history on lease_cancel rollback (#533)","url":"https://github.com/xeprotocol/xe/pull/535","note":"","state":"open","author":"arturbarszcz78","added_by":"desk","created_at":"2026-11-14T20:42:20.251597+00:00"}],"thread":[{"id":13539,"source":"github","source_ref":"7026823683","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-13379 and says what differs; it is read like any other.","created_at":"2026-11-20T00:50:59+00:00"}]}