{"id":"XE-0275","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"246","source_url":"https://github.com/xeprotocol/xe/issues/246","reporter_id":121,"title":"core/ledger: genesis final-height tear can leave an isolated sole-representative node unable to self-finalize after kill -9","area":null,"claimed_tier":"medium","assessed_tier":null,"verdict":null,"duplicate_of":null,"tracked":false,"internal_ref":"","internal_url":null,"status":"triaging","closed_reason":null,"github_state":"open","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-11-03T01:26:09.162907+00:00","updated_at":"2026-11-05T04:17:57.354505+00:00","source_created_at":"2026-11-03T01:25:45+00:00","told_at":null,"status_changed_at":"2026-11-05T04:17:39.552494+00:00","url":"https://bounty.xe.network/XE-0275","body":"# core/ledger: genesis final-height tear can leave an isolated sole-representative node unable to self-finalize after kill -9\n\nSuggested tier: **Medium**. Availability only; no fund loss, mint, double spend,\nor network-wide finality effect.\n\n## Summary\n\n`core/ledger.go:325-342` commits the genesis block in one Badger transaction and\nthen sets `SetFinalHeight(genesis.Account, 1)` in a second transaction at `:339`.\nA `kill -9` between them persists the genesis block, account chain, frontier,\nbalance, delegation, and account key, but leaves the final-height key absent.\nOn restart the non-empty path (`core/ledger.go:348-375`) rebuilds caches but\nnever sets or reconstructs the genesis final height. Because `depsFinalized`\nrequires the genesis to be final and `VoteManager.sweepFrontiersOnce` skips\n`BlockGenesis`, an isolated node that holds all delegated weight cannot\nself-finalize blocks on the genesis account or blocks whose dependency includes\nthe torn genesis position. Unrelated account chains are not shown affected.\n\nThe affected case is an **isolated sole-representative** node. A torn\n**non-representative** node connected to a healthy representative recovered via\nvotesync in the two-node test below.\n\n## Target\n\n- Pinned commit: `978785929bc17a2f4ab30c383e5fc9cf4d634512`\n- Unmodified binary SHA-256:\n  `c3bbf2612ab282fd31ea9b78eec109e9554a260b4ca6e6aa6e01d9fb04ff1f34`\n- Instrumented pause build SHA-256:\n  `b7cfe3552be4501b4ec98e13914a9a741f76dfe0f4cfd601891fefab86f1e77f`\n  (env-controlled pauses only; every restart and the two-node run use the\n  unmodified binary)\n- `crashlab` SHA-256:\n  `999f65207a8a464567a5cfb3bcaf925151ad06ba9a3007ebf88d687febfde48c`,\n  built from `lab/scenarios/t-105` with the target `replace`; target tree clean\n  at `9787859`.\n- Network: `t102-lab` (custom local genesis)\n- Ledger genesis:\n  `85694777dcff96bd9bb4dbc0aa826561c64e24ebaa73e6ef4970f62f88ca03f1`\n- Statechain genesis:\n  `bdf1594c9a5fb323bb059f87eb11b02430195200b95eb710a558a263893708fe`\n- Activation state: no activations exercised\n- Store/topology: Badger; local nodes; `--disable-mdns --no-discovery`;\n  default PoW; loopback API/metrics. No public testnet.\n- Scope: pinned target only; the live build `xe/4f19518` was not tested.\n\n## Preconditions\n\nA `kill -9` (or equivalent power loss) between the genesis `CommitBlock` and the\nseparate `SetFinalHeight` transaction during the first start on an empty data\ndirectory. No attacker, malformed block, forged signature, or special account is\nneeded. The tested affected node is isolated and is the sole representative with\nall delegated weight; no other representative or healthy quorum is reachable.\n\nThe instrumented pause patch widens the real two-transaction window to 30 s so\nthe kill is deterministic. The crash was not observed in a real first bootstrap\nwithout the pause patch. The manual store-level case (`crashlab -mode init-gap`)\nproves the resulting state through the unmodified store API, and the pause case\nproves the kill window; all restarts and controls are the unmodified target.\n\n## Torn state (isolated case)\n\nAfter the kill, raw Badger state is one account with\n`blocks=[genesis]`, `frontier=genesis`, `final_height=0`, pending empty.\nOn restart the unmodified node stays up (`/health` 200), accepts the send\n(block_count 2, pending 1,000 XE), but keeps:\n\n- treasury (the genesis account) `final_height: 0`;\n- `finality_advances: 0`, `last_finality_ns: 0`;\n- `spendable: {}`;\n- `/ready` 503 (`quorum_reachable:false`, no representative vote; note the\n  readiness checks also include a no-peer check in this isolated setup, so the\n  finality-specific evidence is `quorum_reachable:false` together with\n  `final_height:0` and empty `spendable`).\n\nA clean control on the same genesis and representative key, given the same send,\nreaches `final_height: 2`, `finality_advances: 1`, and\n`spendable {XE:41999999999000}`. Supply is conserved on both nodes\n(balances + in-flight = created, discrepancy 0). The send is\n`b19c69111efefc6558d5456352aa5bda2fe9ce8e89c9594305e4fe7b681b1bda`\nat height 2.\n\nThe tear survives local restarts; no local startup path repairs it. The isolated\nsole representative cannot self-heal locally.\n\n## Two-node peer recovery\n\nH was the healthy sole representative (representative key, all delegated\nweight). T was the torn chain with a non-representative node key, same genesis\nand same send, explicitly dialed T to H. T started at `final_height 0`,\n`finality_advances 0`, `spendable {}`, `/ready` 503; by the 20 s sample it had\n`final_height 2`, `finality_advances 1`, `spendable {XE:41999999999000}`, and\n`/ready` 200. H was at `final_height 2`, `finality_advances 1`.\n`T/node.log` records:\n\n```\nvotesync: ingested 1 vote(s) for 32020cbc47cf...|85694777dcff... from peers\n```\n\nThus a torn non-representative node can recover from a 67% final-vote quorum.\nThe remaining affected case is the isolated/sole-representative node, which has\nno other representative to supply that quorum. This report does not claim a\nnetwork-wide finality stall.\n\n## Evidence labels\n\n- **[U]** unmodified target: restarts, clean control, two-node H/T run.\n- **[I]** instrumented pause build: only the 30 s crash-window widening.\n- **[M]** manual store-level gap: unmodified store API writes the genesis commit\n  and omits `SetFinalHeight`; the unmodified target restarts on it.\n- **[C]** code reading from the pinned tree.\n\n## Impact\n\nIn the isolated sole-representative case, the affected node remains alive and\nAPI-reachable but cannot finalize blocks on the genesis account or blocks whose\ndependency includes the torn genesis position; the tested treasury (genesis\naccount) has an empty spendable balance and `/ready` is 503.\n\nNo fund loss, no mint, no double spend, and no network-wide finality effect was\nobserved. Supply is conserved and the crash state is conservative: spendable is\nempty, not above balance. The affected node's impact is availability only.\nRecovery options: connect a healthy representative quorum (the r-037\nnon-representative case), wipe and resync/rejoin, or repair the missing final\nheight. The isolated sole representative does not self-heal locally.\n\n## Root cause\n\n- `core/ledger.go:325-342`: genesis `commitBlockWrite` and the separate\n  `SetFinalHeight` at `:339` have no enclosing transaction or recovery marker.\n- `core/ledger.go:348-375`: non-empty restart rebuilds caches but never calls\n  `SetFinalHeight` for the persisted genesis.\n- `core/finalization.go:44-57`: `depsFinalized` requires the dependency to be\n  final; `sweepFrontiersOnce` skips `BlockGenesis`.\n- `core/spendable.go:15-49`: spendability starts from the account final height.\n- Peer recovery: non-representative nodes take the pull-only path\n  (`core/finalization.go:413-490`), wired at `node/node.go:586-589` to\n  `net/votesync.go:40-70`; the provider returns final votes via\n  `core/finalization.go:519-540`; the pulled vote is tallied at\n  `core/quorum.go:151-181`; `confirmConflict` sets the\n  final height at `core/quorum.go:353-362`. The two-node run observed this\n  path. [C]/[U]\n\n## Reproduction\n\nUse a fresh run ID; the scripts refuse to overwrite existing artifact\ndirectories. `r-035b`/`r-037` are the existing artifact labels.\n\n```sh\n# Isolated kill -9 at the genesis commit -> final-height boundary, then an\n# unmodified-target restart and clean control.\nlab/scenarios/t-105/genesis-crash.sh r-035c-repro 25501 25502 25601 25602\n\n# Two-node peer recovery: H healthy sole representative, T torn\n# non-representative; T dials H. Uses the r-035b torn chain as T.\nlab/scenarios/t-105/peer-recovery.sh r-037-peer-recovery 25701 25702 25801 25802\n\n# Deterministic store-level state without the pause patch, then the unmodified\n# target restart. The exact commands are in the finding draft and\n# lab/scenarios/t-105/scenario.md.\nlab/scenarios/t-105/crashlab -mode init-gap -data <dir>/data \\\n  -genesis-dir <setup>/genesis -keys <setup>/keys.json\n\n# Commit-boundary control: before/after CommitBlock for pending add/delete.\nlab/scenarios/t-105/commit-crash.sh r-036-commit-boundary-kill\n```\n\n## Duplicate distinction\n\n- **#226** (open): cascade/lease stale reads, `SwapBlock` credential/cache\n  issues, renewed-lease reputation, `CommitCascade` lease CAS and pending-dst\n  duplicate append. This report is the genesis bootstrap/final-height split; no\n  cascade, lease, or pending-dst index is involved.\n- **#200** (open): account-chain Θ(N²) writes and full lease scans. This report\n  is a two-transaction durability gap, not write amplification.\n- **#60** (open): SIGTERM during sync does not cancel; shutdown holds the Badger\n  lock. This report is a `kill -9` between bootstrap transactions and does not\n  depend on SIGTERM handling or lock drain.\n- **#196** (open): statechain sync byte cap. This report is ledger-store\n  bootstrap, not statechain sync.\n- **#96** (open) and **#106** (open PR): descendant finalization implicitly\n  finalizing ancestors / gating descendant finalization on ancestor resolution.\n  They are related to the peer-recovery and ancestor-finalization surface but do\n  not describe the genesis commit/final-height split. #106 is an open PR, not a\n  merged fix.\n\n## Suggested fix\n\nMake the genesis block commit and its final-height watermark one transaction\n(for example, carry the final height in `BlockCommit` and write it inside the\nsame Badger `db.Update`). Independently, repair on non-empty startup: if the\npersisted genesis chain exists and the genesis final height is less than 1, set\nit to 1 before serving. Add a regression test that opens a store with the\ngenesis chain committed and no final-height key, constructs the ledger, and\nasserts the genesis is final and a child block can finalize; add a two-node test\nfor the non-representative votesync recovery path.\n\n## Payout\n\nXE address: `b50f71f5ad1743456550ad7bd7720e18ce3a271f1c73eb90c56a79915404521b`\n","patch_refs":[{"id":244,"repo":"xeprotocol/xe","kind":"pr","ref":"249","title":"fix(core,store,net): atomic genesis final-height commit/repair and sync request/dependency convergence (fixes #244, #245, #246)","url":"https://github.com/xeprotocol/xe/pull/249","note":"","state":"open","author":"Khanhtnq","added_by":"desk","created_at":"2026-11-03T14:28:53.094909+00:00"}],"thread":[]}