{"id":"XE-0022","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"43","source_url":"https://github.com/xeprotocol/xe/issues/43","reporter_id":13,"title":"core/ledger: swap cannot unwind lease and multisig descendants, permanently stalling the account on a contested send (High)","area":"consensus","claimed_tier":"high","assessed_tier":"high","verdict":"likely-valid","duplicate_of":null,"tracked":true,"internal_ref":"#1090","internal_url":"https://github.com/xeprotocol/core/issues/1090","status":"patched","closed_reason":null,"github_state":"open","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-10-29T13:57:16.474993+00:00","updated_at":"2026-11-18T08:10:34.677698+00:00","source_created_at":"2026-10-25T11:11:57+00:00","told_at":"2026-11-06T19:57:08+00:00","status_changed_at":"2026-11-18T08:09:51.828929+00:00","url":"https://bounty.xe.network/XE-0022","body":"## Summary\nSwapBlock refuses to unwind any descendant that is not a send, receive, burn or mint, so a contested send that has a lease, lease renew or multisig update chained under it can never be replaced. The conflict never resolves, every new block on the account is rejected while the conflict is open, and finality locks the stall because the swap failure does not match the winner-full-validation error and therefore never enters failedPromotions.\n\n## Affected component\ncore/ledger.go, SwapBlock descendant loop at lines 2926 to 2931 (default arm returns the unwind-not-implemented error), at commit 978785929bc17a2f4ab30c383e5fc9cf4d634512. buildUndo in core/rollback.go lines 223 to 243 already knows how to reverse lease descendants, but the swap path never calls it for them. Receive deferral at ledger.go lines 1101 to 1104 and the unresolved-conflict rejection at lines 815 to 817 keep the account frozen until the conflict resolves, which it never can.\n\n## Impact\nAn account owner who double submits a send and then chains a lease family block under one of them (the protocol's own retry scenario) can lock the account permanently: the lowest-hash send typically wins the vote, the swap to the competing send dies on the descendant, confirmConflict returns before setting final height, and no new block on that account is admitted. Blast radius is one account per conflict, but the stall is permanent and needs no attacker beyond the account's own signatures.\n\nSuggested severity: High (targeted stuck state transition). Confirmed by read-only source review at the pin.\n\n## PoC\n\nSetup (from a fresh clone of xeprotocol/xe at the pin):\n\n```\ngit clone https://github.com/xeprotocol/xe && cd xe\nmkdir -p poc/harness\n# save the two files below, then from poc/:\nGOTOOLCHAIN=auto go mod tidy\nGOTOOLCHAIN=auto go test -count=1 -v ./poc\n```\n\ngo.mod for the poc module:\n\n```\nmodule poc\n\ngo 1.25.0\n\nrequire github.com/xeprotocol/xe v0.0.0\n\nreplace github.com/xeprotocol/xe => ..\n```\n\n**Shared test-only helper** (`poc/harness/harness.go`; constructs signed blocks and funds accounts through the ledger's own test-only entry points, none reachable from gossip or API):\n\n```go\n// Package harness holds test-only helpers for local XE ledger PoCs.\n// None of these helpers exist on the gossip/API surface; they only\n// construct signed blocks and fund accounts via core.SeedAssetForTesting /\n// core.MintForTesting.\npackage harness\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/xeprotocol/xe/core\"\n\t\"github.com/xeprotocol/xe/store\"\n)\n\nconst TestOnlyFundAmount uint64 = 1_000_000_000\n\n// NewTestLedger builds a MemStore-backed ledger with PoW and timestamp\n// windows disabled so the PoC can drive admission/conflict logic.\nfunc NewTestLedger() (*core.Ledger, *store.MemStore) {\n\tcore.SetNetworkID(\"\")\n\tms := store.NewMemStore()\n\tl := core.NewLedger(ms, core.LedgerConfig{\n\t\tDifficulty:      0,\n\t\tTimestampWindow: 0,\n\t})\n\treturn l, ms\n}\n\n// TestOnlyFundAccount opens a new account with a signed mint block.\n// Named TestOnly: this is the ledger's own test funding entry point, not a\n// production mint path.\nfunc TestOnlyFundAccount(l *core.Ledger, kp *core.KeyPair, asset string, amount uint64) (*core.Block, error) {\n\tif amount == 0 {\n\t\tamount = TestOnlyFundAmount\n\t}\n\tb := &core.Block{\n\t\tType:           core.BlockMint,\n\t\tAccount:        kp.Address(),\n\t\tPrevious:       \"0\",\n\t\tBalance:        amount,\n\t\tAmount:         amount,\n\t\tTimestamp:      core.Now().UnixNano(),\n\t\tAsset:          asset,\n\t\tPubKey:         kp.PubKeyHex(),\n\t\tRepresentative: kp.Address(),\n\t}\n\tif err := core.SignBlock(b, kp); err != nil {\n\t\treturn nil, fmt.Errorf(\"test-only fund: sign: %w\", err)\n\t}\n\tvar err error\n\tif asset == \"XUSD\" {\n\t\terr = l.MintForTesting(b)\n\t} else {\n\t\terr = l.SeedAssetForTesting(b)\n\t}\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"test-only fund: %w\", err)\n\t}\n\treturn b, nil\n}\n\n// TestOnlyFinalizeHeight writes a final-height watermark. Test-only: the\n// production path is QuorumManager.confirmConflict.\nfunc TestOnlyFinalizeHeight(ms *store.MemStore, account string, height uint64) error {\n\treturn ms.SetFinalHeight(account, height)\n}\n\nfunc SignSend(kp *core.KeyPair, previous string, balance, amount uint64, dest, asset string) (*core.Block, error) {\n\tb := &core.Block{\n\t\tType:        core.BlockSend,\n\t\tAccount:     kp.Address(),\n\t\tPrevious:    previous,\n\t\tBalance:     balance,\n\t\tAmount:      amount,\n\t\tTimestamp:   core.Now().UnixNano(),\n\t\tAsset:       asset,\n\t\tDestination: dest,\n\t}\n\tif err := core.SignBlock(b, kp); err != nil {\n\t\treturn nil, err\n\t}\n\treturn b, nil\n}\n\nfunc SignLease(kp *core.KeyPair, previous string, balance, amount uint64, dest, certHash string, vcpus, mem, disk, dur uint64) (*core.Block, error) {\n\tb := &core.Block{\n\t\tType:            core.BlockLease,\n\t\tAccount:         kp.Address(),\n\t\tPrevious:        previous,\n\t\tBalance:         balance,\n\t\tAmount:          amount,\n\t\tTimestamp:       core.Now().UnixNano(),\n\t\tAsset:           \"XUSD\",\n\t\tDestination:     dest,\n\t\tVCPUs:           vcpus,\n\t\tMemoryMB:        mem,\n\t\tDiskGB:          disk,\n\t\tDuration:        dur,\n\t\tCertificateHash: certHash,\n\t}\n\tif err := core.SignBlock(b, kp); err != nil {\n\t\treturn nil, err\n\t}\n\treturn b, nil\n}\n```\n\n**Test file** (`poc/f4_test.go`):\n\n```go\npackage f4\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"os\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"github.com/xeprotocol/xe-poc/harness\"\n\t\"github.com/xeprotocol/xe/core\"\n)\n\n// TestPoC_F4 shows SwapBlock refuses to unwind a lease descendant of a\n// contested send, so the conflict cannot resolve. The error is not\n// ErrWinnerFullValidation, so pickWinner will keep selecting the same winner.\nfunc TestPoC_F4(t *testing.T) {\n\tpass, detail := runF4()\n\tif pass {\n\t\tfmt.Println(\"PASS: F4 SwapBlock cannot unwind lease descendant; conflict stalls\")\n\t\tfmt.Println(detail)\n\t\treturn\n\t}\n\tfmt.Println(\"FAIL: F4 expected unwind-descendant stall\")\n\tfmt.Println(detail)\n\tos.Exit(0)\n}\n\nfunc runF4() (bool, string) {\n\towner, err := core.GenerateKeyPair()\n\tif err != nil {\n\t\treturn false, \"keygen owner: \" + err.Error()\n\t}\n\tpayee, err := core.GenerateKeyPair()\n\tif err != nil {\n\t\treturn false, \"keygen payee: \" + err.Error()\n\t}\n\tprovider, err := core.GenerateKeyPair()\n\tif err != nil {\n\t\treturn false, \"keygen provider: \" + err.Error()\n\t}\n\n\tcertHash := strings.Repeat(\"ab\", 32)\n\tl, ms := harness.NewTestLedger()\n\t// Rebuild ledger with a certificate lookup so a real lease can commit.\n\tl = core.NewLedger(ms, core.LedgerConfig{\n\t\tDifficulty:      0,\n\t\tTimestampWindow: 0,\n\t\tCertificateLookupFn: func(hash string) *core.CertificateInfo {\n\t\t\tif hash != certHash {\n\t\t\t\treturn nil\n\t\t\t}\n\t\t\treturn &core.CertificateInfo{\n\t\t\t\tProvider:             provider.Address(),\n\t\t\t\tPriceMultiplierMilli: 1000,\n\t\t\t}\n\t\t},\n\t})\n\n\topen, err := harness.TestOnlyFundAccount(l, owner, \"XUSD\", harness.TestOnlyFundAmount)\n\tif err != nil {\n\t\treturn false, \"test-only fund: \" + err.Error()\n\t}\n\tif err := harness.TestOnlyFinalizeHeight(ms, owner.Address(), 1); err != nil {\n\t\treturn false, \"finalize open: \" + err.Error()\n\t}\n\n\tsendAmt := uint64(1)\n\tsendA, err := harness.SignSend(owner, open.Hash, harness.TestOnlyFundAmount-sendAmt, sendAmt, payee.Address(), \"XUSD\")\n\tif err != nil {\n\t\treturn false, \"sign send A: \" + err.Error()\n\t}\n\tif err := l.AddBlock(sendA); err != nil {\n\t\treturn false, \"add send A: \" + err.Error()\n\t}\n\n\tcost, err := core.LeaseCost(1, 0, 0, 60, 1000)\n\tif err != nil {\n\t\treturn false, \"lease cost: \" + err.Error()\n\t}\n\txusdAfterSend := harness.TestOnlyFundAmount - sendAmt\n\tlease, err := harness.SignLease(owner, sendA.Hash, xusdAfterSend-cost, cost, provider.Address(), certHash, 1, 0, 0, 60)\n\tif err != nil {\n\t\treturn false, \"sign lease: \" + err.Error()\n\t}\n\tif err := l.AddBlock(lease); err != nil {\n\t\treturn false, \"add lease descendant: \" + err.Error()\n\t}\n\n\tsendB, err := harness.SignSend(owner, open.Hash, harness.TestOnlyFundAmount-2, 2, payee.Address(), \"XUSD\")\n\tif err != nil {\n\t\treturn false, \"sign send B: \" + err.Error()\n\t}\n\tif err := l.AddBlock(sendB); err != nil {\n\t\treturn false, \"stage send B: \" + err.Error()\n\t}\n\tconflict := l.GetConflict(owner.Address(), open.Hash)\n\tif conflict == nil {\n\t\treturn false, \"expected conflict between send A and send B\"\n\t}\n\n\tswapErr := l.SwapBlock(owner.Address(), sendA.Hash, sendB.Hash, sendB)\n\tif swapErr == nil {\n\t\treturn false, \"SwapBlock succeeded; descendant stall not triggered\"\n\t}\n\tisWinnerFail := errors.Is(swapErr, core.ErrWinnerFullValidation)\n\tmsg := swapErr.Error()\n\thasUnwind := strings.Contains(msg, \"cannot unwind descendant\") && strings.Contains(msg, \"lease\")\n\tstillOnA := l.GetBlock(sendA.Hash) != nil && l.GetChain(owner.Address())[1].Hash == sendA.Hash\n\tleaseStill := l.GetBlock(lease.Hash) != nil\n\theight := l.FinalHeight(owner.Address())\n\n\tdetail := fmt.Sprintf(\n\t\t\"sendA=%s lease=%s sendB=%s\n\"+\n\t\t\t\"SwapBlock A->B err=%v\n\"+\n\t\t\t\"errors.Is(ErrWinnerFullValidation)=%v (so failedPromotions is NOT set)\n\"+\n\t\t\t\"on-chain still A=%v lease_present=%v final_height=%d (still 1, conflict unresolved)\",\n\t\tsendA.Hash[:16], lease.Hash[:16], sendB.Hash[:16],\n\t\tswapErr, isWinnerFail, stillOnA, leaseStill, height,\n\t)\n\tok := hasUnwind && !isWinnerFail && stillOnA && leaseStill && height == 1 && conflict != nil\n\treturn ok, detail\n}\n```\n\n**Run** (run 1 of 5 consecutive runs, 5/5 PASS, exit 0):\n\n```\n=== RUN   TestPoC_F4\n2026/10/25 17:32:00 Genesis block applied: 42000000000000 XE to 378d0f5c2063...\nPASS: F4 SwapBlock cannot unwind lease descendant; conflict stalls\nsendA=64c203db6ffcd25c lease=9eb55d4a359d2fed sendB=7fef36ab983fb66a\nSwapBlock A->B err=SwapBlock: cannot unwind descendant \"lease\" (9eb55d4a359d...) of the replaced block — reversing its side-effects is not yet implemented\nerrors.Is(ErrWinnerFullValidation)=false (so failedPromotions is NOT set)\non-chain still A=true lease_present=true final_height=1 (still 1, conflict unresolved)\n--- PASS: TestPoC_F4 (0.00s)\nPASS\nok  \tgithub.com/xeprotocol/xe-poc/f4\t0.012s\n```\n\n## Fix suggestion\nRoute lease and multisig descendants through buildUndo inside SwapBlock instead of failing, or at minimum enter failedPromotions on this error class so a different candidate can resolve the conflict.","patch_refs":[{"id":165,"repo":"xeprotocol/xe","kind":"pr","ref":"147","title":"fix(core): unwind lease and multisig descendants in SwapBlock (#43)","url":"https://github.com/xeprotocol/xe/pull/147","note":"","state":"open","author":"arturbarszcz78","added_by":"desk","created_at":"2026-10-29T13:57:30.091343+00:00"}],"thread":[{"id":647,"source":"github","source_ref":"6694129445","author":"Joseph Denne","team":true,"body":"Thanks for the reports. To pay out we need an XE payout address in the report itself: please edit the issue body to add a line \"Payout address: xe_...\" and say here when it is done. The desk entry is https://bounty.xe.network/XE-0022.","created_at":"2026-11-06T19:57:08+00:00"},{"id":739,"source":"github","source_ref":"6713170941","author":"byfor8","team":false,"body":"Payout address added in the body","created_at":"2026-11-07T21:21:11+00:00"}]}