{"id":"XE-0583","kind":"bounty","visibility":"public","source":"web","source_repo":"","source_ref":"","source_url":"","reporter_id":210,"title":"[High] PR #147 (XE-0022 fix) is fix-incomplete: unwinding a consumer lease_open deletes the shared lease record, orphans the provider's lease_accept and makes the provider's fork unresolvable","area":"consensus","claimed_tier":null,"assessed_tier":null,"verdict":"duplicate","duplicate_of":"XE-0002","tracked":true,"internal_ref":"#1064","internal_url":"https://github.com/xeprotocol/core/issues/1064","status":"closed","closed_reason":"duplicate","github_state":"","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-11-05T04:15:00.333262+00:00","updated_at":"2026-11-14T22:52:05.854491+00:00","source_created_at":null,"told_at":"2026-11-14T22:52:06.681122+00:00","status_changed_at":"2026-11-08T20:32:04.413388+00:00","url":"https://bounty.xe.network/XE-0583","body":"# PR #147 (the fix for XE-0022) is fix-incomplete: unwinding a consumer-side `lease_open` permanently deletes the shared lease record, orphans the provider's `lease_accept`, and makes the provider's own fork unresolvable\r\n\r\n## Summary\r\n\r\nPR #147 removes the \"cannot unwind descendant\" refusal in `swapBlockLocked` / `planRollbackFrom`, allowing lease-family blocks to be unwound during conflict resolution. That is the intended remedy for **XE-0022** (`core/ledger: swap cannot unwind lease and multisig descendants, permanently stalling the account on a contested send`).\r\n\r\nThe new path has a cross-account side effect that PR #147 does not handle: unwinding a consumer's `lease_open` block **deletes the shared lease record** from the store. The provider's `lease_accept` lives on the *provider's* chain, so it is not a descendant on the consumer's chain and is not unwound — it survives as an **orphan**, permanently referencing a lease that no longer exists.\r\n\r\nThree consequences, all reproduced deterministically:\r\n\r\n1. **The provider can never settle the lease it accepted.** `validateAndAddLeaseSettle` looks the lease up in the record store and fails: `lease not found: <hash>`. The re-accept path is also closed (`CommitBlock: lease state conflict: lease <hash> not found`), so the orphan is **permanent and unrepairable**.\r\n2. **Any later fork on the provider's chain at or below the accept is unresolvable.** `SwapBlock` fails deterministically with `undo: lease <hash> not found while reversing lease_accept`. Every candidate winner at that fork point requires unwinding the same orphaned accept, so no candidate can ever be promoted. Worse, the error contains the substring `not found`, so `core.IsRetryableError` returns `true` and the failed-promotion quarantine at `core/quorum.go:328` (`errors.Is(err, ErrWinnerFullValidation) && !IsRetryableError(err)`) never triggers — the stale-conflict sweep retries the same impossible promotion indefinitely instead of substituting a different candidate.\r\n3. **Live-vs-restart reputation divergence.** The live node reports `leases_accepted_as_provider: 1` and `leases_accepted_as_consumer: 1`; a node rebuilt from the same store reports **zero for both**, because `rebuildReputation` derives `EventLeaseAccepted` solely from lease *records*.\r\n\r\nThe failure is **order-dependent**: if the provider resolves their own fork first, the accept unwind restores the lease to `LeaseCreated` and no orphan remains (T8). The bad order — consumer forks first, provider forks later — is the natural race.\r\n\r\nThis is a **fix-incomplete residual on XE-0022**, framed the same way as **XE-0570** (a gap left by an accepted fix PR), not a duplicate of XE-0022 itself.\r\n\r\n## Affected component and versions\r\n\r\n- `xeprotocol/xe`, `master` @ `02e9641` **with the PR #147 diff applied** (PR head `ba4a167`).\r\n- PR #147 diff: 2 source hunks — `core/ledger.go` (`swapBlockLocked` descendant allow-list) and `core/rollback.go` (`planRollbackFrom` descendant allow-list), plus the new test `core/swap_lease_descendant_test.go`.\r\n- Code paths: `core/rollback.go` (`buildBlockUndo`, `priorLease`), `core/ledger.go` (`swapBlockLocked`, `validateAndAddLeaseAccept`, `validateAndAddLeaseSettle`, `rebuildReputation`), `store/badger_store.go` (`applyUndoTxn`).\r\n\r\n## Relation to existing reports (dedup)\r\n\r\n- **XE-0022** (`accepted` / `likely-valid` / `high`, area `consensus`, reporter 13, GitHub issue #43) is the **home ticket** for PR #147. Its body states that `buildUndo` \"already knows how to reverse lease descendants\". PR #147 is that fix. This report is the residual the fix does not address.\r\n- **XE-0135** (reporter 43, GitHub issue #226) and its fix PR **#227 / #232** address *stale store reads inside a single cascade*: they make `priorLease`, `escrowPendingOf`, `priorLeasePopRenewal` and `xusdBalanceOnChain` consult the in-flight `cascadeBuild` overlay instead of the store. That is **not** this defect. Here the lease record was deleted by an **earlier, completed** cascade, so the second cascade's overlay has no entry for that lease; `cb.lookupLease` falls through to the store and returns `nil`, and the same fatal error fires. The #227 diff changes the *source of the lookup*, not the handling of a missing lease, so the residual survives it. The failure modes are also different: #227 is about stale values corrupting balance math, this is about a *permanently absent* record making a fork unresolvable.\r\n- **XE-0137** (reporter 44) is a pending-send destination race in the same function family (supply inflation) — unrelated mechanism.\r\n- **XE-0570** (reporter 210, mine) is the precedent for filing a *fix-incomplete residual* rather than re-reporting the parent ticket.\r\n- Desk scan: all **199** public tickets were searched for `147`, `lease_accept`, `lease accept`, `orphan`, `not found while reversing`, `swapblock`, `reputation`, `descendant`, `unwind`, `priorLease`, `missing lease`, `lease record`, `fork`, `conflict`, `lease_settle`, `settle`, `force_settle`, `multisig`, `rebuild`, `restart`. **No ticket** mentions the orphaned accept, the `not found while reversing` error, `deleteLeaseHash`, `priorLease`, cross-account lease destruction, or the live/restart reputation divergence.\r\n\r\n## Root cause\r\n\r\n### 1. Unwinding `lease_open` deletes a **global** lease record\r\n\r\n`core/rollback.go:222-225`:\r\n\r\n```go\r\ncase BlockLease:\r\n    deletePendingID = b.Hash\r\n    deleteLeaseHash = b.Hash\r\n```\r\n\r\n`core/rollback.go:315` puts it in the undo as `DeleteLeaseHash`, and `store/badger_store.go:1101-1104` physically removes it:\r\n\r\n```go\r\nif u.DeleteLeaseHash != \"\" {\r\n    if err := txn.Delete(prefixedKey(prefixLease, u.DeleteLeaseHash)); err != nil {\r\n```\r\n\r\nThe lease record is **shared state** between two accounts (consumer and provider), but the unwind is driven by the **consumer's** chain only. The provider's `lease_accept` is not on the consumer's chain, so it is not in the unwind set and is left behind. Note that `applyUndoTxn` never deletes the block record itself (`prefixBlock`), so the `lease_open` **block** survives while the lease **record** does not — the two halves of the lease state diverge. The harness confirms this split directly:\r\n\r\n```\r\npost-swap lease record: DELETED | lease_open block record present=true (needed to re-validate lease_accept: core/ledger.go:1615)\r\n```\r\n\r\nThat split is what keeps the orphan alive rather than making it self-evident: `validateAndAddLeaseAccept` resolves the lease **block** (`core/ledger.go:1615`), which still exists, while everything that depends on the lease **record** fails.\r\n\r\n### 2. The orphaned `lease_accept` cannot be unwound, and cannot be settled\r\n\r\n`core/rollback.go:226-228` (unwinding an accept) and `core/rollback.go:255` (the fatal check):\r\n\r\n```go\r\ncase BlockLeaseAccept:\r\n    restoreLease = l.priorLease(b.Source, LeaseCreated)\r\n...\r\nif (b.Type == BlockLeaseAccept || ...) && restoreLease == nil {\r\n    return nil, nil, fmt.Errorf(\"undo: lease %s not found while reversing %s\", shortHash(b.Source), b.Type)\r\n}\r\n```\r\n\r\n`priorLease` (`core/rollback.go:89`) reads the lease from the store; the record is gone, so it returns `nil` and the unwind aborts. `core/ledger.go:2995` wraps that as `ErrWinnerFullValidation`.\r\n\r\nSettling is blocked too — `core/ledger.go:2125-2134` resolves the lease through the same record store (`lease not found: <hash>`), and `core/ledger.go:2186-2192` additionally requires the escrow pending send, which the unwind already deleted. Re-accepting is blocked by the store CAS (`lease state conflict: lease <hash> not found`). The provider's chain therefore keeps a block whose referenced lease no longer exists, with no way to settle, re-accept, or unwind it.\r\n\r\n### 3. Reputation is derived from lease records only\r\n\r\n`core/ledger.go:607-683` (`rebuildReputation`) enumerates `GetAllLeases()` and emits `EventLeaseAccepted` per lease record (`core/ledger.go:627`). With the record deleted, a restarted node loses the accept event entirely — for **both** the provider (`leases_accepted_as_provider`) and the consumer (`leases_accepted_as_consumer`), while the live node still counts both.\r\n\r\n## Reproduction\r\n\r\nDeterministic PoC harness: `cmd/pocdiff` (scenarios `T1`–`T10`), run against `master` + the PR #147 diff. No network, no wall-clock dependence (fake clock via `core.Now`), fixed base time `time.Unix(1700000000,0)` and shared keys so MemStore and BadgerStore runs produce identical hashes.\r\n\r\n```\r\ngo run ./cmd/pocdiff/\r\n```\r\n\r\nActors: `keys[0]` = consumer, `keys[1]` = provider, `keys[2]` = timekeeper / third party.\r\n\r\n| Scenario | What it does | Result |\r\n|---|---|---|\r\n| T1 | consumer opens lease, provider accepts, consumer forks at the `lease_open` and the swap unwinds it | lease record **DELETED**, provider accept **still present** |\r\n| T2 | same, with a renewal in between | lease record **DELETED**, escrow pending dropped |\r\n| T3 | provider forks at its own accept (before the consumer forks) | lease restored to `created`, **no orphan** |\r\n| T4 | provider unwinds its own settle | parity, no orphan |\r\n| T5 | **consumer forks first, then the provider forks** | provider fork **UNRESOLVABLE** |\r\n| T6 | provider tries to settle the orphaned lease | `lease not found` |\r\n| T7 | provider forks at a block *after* the accept | swap succeeds, but the accept stays orphaned forever |\r\n| T8 | **reverse order** (provider forks first, consumer second) | no orphan, no later failure |\r\n| T9 | classifies the fatal unwind error | `errors.Is(ErrWinnerFullValidation)=true`, `IsRetryableError=true` |\r\n| T10 | provider tries to re-accept the orphaned lease | `CommitBlock: lease state conflict: lease <hash> not found` — orphan is unrepairable |\r\n\r\n### T5 — the provider's fork becomes unresolvable\r\n\r\n```\r\nsteps: lease: ok | accept: ok | lease state=accepted stake=0 renewals=0 |\r\n       conflict-send: ok | swap-lease-away: ok |\r\n       post-swap lease record: DELETED |\r\n       provider chain len=2 accept_present=true prov_xusd=10000000 |\r\n       provider-conflict-send: ok |\r\n       provider-swap: ERR SwapBlock: winner failed full validation: build undo for 39c9bda27f3a...:\r\n                       undo: lease 3251a1454bfd... not found while reversing lease_accept\r\n```\r\n\r\nFull error:\r\n\r\n```\r\nSwapBlock: winner failed full validation: build undo for 39c9bda27f3a...: undo: lease 3251a1454bfd... not found while reversing lease_accept\r\n```\r\n\r\n### T9 — the failure is deterministic but misclassified as retryable\r\n\r\n```\r\nclassification: errors.Is(ErrWinnerFullValidation)=true IsRetryableError=true\r\n```\r\n\r\n`core/retryable.go:13` lists the bare substring `\"not found\"` as a retryable pattern, so `core/quorum.go:328`\r\n\r\n```go\r\nif errors.Is(err, ErrWinnerFullValidation) && !IsRetryableError(err) {\r\n    qm.failedPromotions[...] = Now()\r\n```\r\n\r\nnever records the failure. The stale-conflict sweep (`core/quorum.go:528-573`) therefore re-promotes the same impossible candidate on every cycle, instead of quarantining it and substituting a promotable one. Because **every** candidate at that fork point needs the same accept unwind, the conflict is never resolved and the provider's account stays conflicted (new blocks are rejected with `account has unresolved conflict — block rejected pending resolution`, `core/ledger.go:817`).\r\n\r\n### T1/T2 — live vs restart divergence\r\n\r\nLive node:\r\n\r\n```\r\nrep 6c99ab87024b {\"version\":2,\"leases_accepted_as_provider\":1,...}\r\nrep 98dcb595e34a {\"version\":2,\"leases_accepted_as_consumer\":1,...}\r\n```\r\n\r\nNode rebuilt from the same store (same balances, same delegated weight, same account keys):\r\n\r\n```\r\n(no reputation block at all for either account)\r\n```\r\n\r\n### Layer results (store independence)\r\n\r\nThe harness runs every scenario three ways:\r\n\r\n- **Layer 1 — MemStore vs BadgerStore**, same sequence, comparing per-step accept/reject and the final state: **0 divergences across all 18 scenarios**. The defect is therefore *not* a test-store artifact; production nodes run `BadgerStore` (`MemStore` is used only by `cmd/fuzzsim`, `cmd/poc*` and tests).\r\n- **Layer 2 — store-level edge conditions**: 6 pre-existing divergences (mem silent-ok vs badger error). These are latent/unreachable in the current tree and are **not** part of this report.\r\n- **Layer 3 — live in-memory state vs state rebuilt from the store**: 7 divergences (T1, T2, T5, T6, T7, T9, T10) — all of them the reputation divergence described above. Balances, delegated weight and account keys are identical in every scenario.\r\n\r\n## Impact\r\n\r\n- **Cross-account state destruction.** One account's conflict resolution unilaterally deletes another account's lease state. The provider's chain is left containing a block that references a lease which exists nowhere in the store — the chain is no longer self-consistent with the lease store, and the block cannot be settled, re-accepted, or unwound.\r\n- **Permanent account stall (the XE-0022 impact class, reintroduced for `lease_accept`).** Any later conflict on the provider's chain at or below the orphaned accept can never be resolved. The failure is deterministic, and the retryable misclassification prevents the substitute-candidate mechanism from ever engaging.\r\n- **Node-state divergence.** A node that keeps running reports a lease acceptance that a node restarted over the same store does not. Any consumer of the reputation API sees different answers before and after a restart. (Reputation does **not** feed finalization eligibility — `core/repeligibility.go` uses the `sys.representatives` allow-list — so this is an inconsistency, not a fork.)\r\n- **Dead revenue for the provider.** The lease escrow is returned to the consumer by the unwind, so the provider is left with an accepted-but-unsettleable lease and can never collect the lease revenue.\r\n- **The provider's chain is no longer replayable from genesis on a node that lacks the unwound `lease_open` block.** `validateAndAddLeaseAccept` requires the lease block record (`core/ledger.go:1615`, failing with `source lease block not found` at `core/ledger.go:1619-1620`). On the node that performed the unwind the block record survives, but on a node that only ever receives the provider's chain, the `lease_open` block is no longer part of any chain — the provider's `lease_accept` cannot be re-validated. This last point is derived from the code path rather than separately reproduced in the harness, and is listed here as a risk to investigate rather than as a demonstrated outcome.\r\n\r\n## Honest bounds (what is *not* claimed)\r\n\r\n- **No supply inflation.** The XUSD census is conserved across every stage (`total_xusd=20000000` at funded / after-accept / after-renew / after-swap): the escrow is refunded to the consumer, it is not destroyed. The settle path additionally refuses without the escrow pending record (`no escrow pending to burn for lease`), so the provider cannot convert the orphan into emission.\r\n- **No double-accept.** The store CAS rejects a second accept for the same lease (`lease state conflict: lease <hash> not found`).\r\n- **No stake-burn claim.** `LeaseStakeDivisor` is `const uint64(0)` in `master` (`core/ledger.go:116`), so the provider stake is always 0 and no stake can be burned.\r\n- **Not a consensus fork.** Balances, delegated weight, account keys and keysets are identical live vs rebuilt. The fork-level impact is the *unresolvable conflict* on the provider's account, not divergent ledger state.\r\n- **Precondition.** The trigger requires a lease that has been accepted, a contested block on the consumer's chain at or below the `lease_open`, and a later contested block on the provider's chain at or below the `lease_accept`. No special privileges are needed — any account can fork its own chain — but it is a compound race rather than a single transaction.\r\n- **The fresh-node replayability consequence above is code-path analysis, not a reproduced result.** Everything else in this report is reproduced by the harness.\r\n\r\n## Suggested fix direction\r\n\r\n1. **Do not delete a lease record while another account's chain still references it.** In `buildBlockUndo` / `applyUndoTxn`, a `lease_open` unwind should not physically remove the lease record when a `lease_accept` for that lease exists on another account's chain; instead it should demote the lease back to `LeaseCreated` and re-anchor the escrow, leaving the provider's accept valid.\r\n2. **Make the unwind of a `lease_accept` tolerant of a missing lease record.** `priorLease` should reconstruct the prior lease state from the chain (`lease_open` block + the accept's own undo data) when the record is absent, rather than aborting with a fatal error. The abort is what turns a recoverable state inconsistency into a permanently unresolvable conflict.\r\n3. **Make the orphan detectable.** If a lease record is missing while a `lease_accept` referencing it remains on-chain, the provider's chain should be flagged/quarantined rather than silently accepted.\r\n4. **Fix the error classification.** `core.IsRetryableError` matching the bare substring `\"not found\"` makes deterministic \"lease not found\" failures look transient; that is what prevents `core/quorum.go:328` from quarantining the impossible promotion and substituting a different candidate.\r\n5. **Derive reputation accept events from the chain, not only from lease records**, so the live and rebuilt views agree (or make the rebuild reproduce the live events exactly).\r\n\r\n## Artifacts\r\n\r\n- Harness: `cmd/pocdiff` (master + PR #147 diff), scenarios `S1`–`S8` and `T1`–`T10`; `cmd/pocdiff/lease_unwind.go` holds the lease-descendant scenarios.\r\n- Run log: `pocdiff_pr147_output.txt` (T5 error string, T9 classification, T1/T2 live-vs-rebuilt reputation dumps).\r\n- PR diff used: `pr147.diff` (2 hunks: `core/ledger.go`, `core/rollback.go`).\r\n- Base commit: `master` @ `02e9641`; PR head: `ba4a167`.","patch_refs":[],"thread":[{"id":3612,"source":"desk","source_ref":"reply:2354","author":"XE desk","team":true,"body":"Thanks. This is the same finding as https://bounty.xe.network/XE-0002, 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-0583 and says what differs; it is read like any other.","created_at":"2026-11-14T22:52:05.854491+00:00"}]}