{"id":"XE-12805","kind":"bounty","visibility":"public","source":"web","source_repo":"","source_ref":"","source_url":"","reporter_id":827,"title":"Committed ledger state is lost on power loss: BadgerStore never enables SyncWrites and no commit path calls Sync()","area":"consensus","claimed_tier":"medium","assessed_tier":null,"verdict":"duplicate","duplicate_of":null,"tracked":false,"internal_ref":"","internal_url":"","status":"closed","closed_reason":"duplicate","github_state":"","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-11-14T12:20:55.129393+00:00","updated_at":"2026-11-20T19:45:57.337987+00:00","source_created_at":null,"told_at":"2026-11-20T19:45:57.365548+00:00","status_changed_at":"2026-11-20T19:45:57.345648+00:00","url":"https://bounty.xe.network/XE-12805","body":"# [Medium] Committed ledger state is lost on power loss: BadgerStore never enables SyncWrites and no commit path calls Sync()\n\n**Area:** consensus / node lifecycle — storage durability\n**Repo pin:** `xeprotocol/xe` @ `89a542e` (HEAD, verified 2026-11-14)\n**Suggested tier:** Medium (CVSS 4.8, AV:P/AC:H — local power loss / host crash only, no remote trigger)\n**Related public issue:** `xeprotocol/xe#557` (same ground, filed by another researcher; this report is an independent reproduction with our own QEMU power-loss harness and our own measurements)\n\n## Summary\n\nA block whose `CommitBlock` returned success can be absent after a power loss. `NewBadgerStore` opens Badger with bare `badger.DefaultOptions(dir)` and never enables `SyncWrites`, and the only `Sync()` call sites in non-test code sit on the final-vote path — the block-commit path never issues a durability barrier. Commits return while the data exists only in the OS page cache (and, for Badger's mmap'd value log, only in dirty mapped pages). We reproduced the impact with a true power-loss cut: a QEMU guest (no KVM, TCG) running the real `BadgerStore`, killed with SIGKILL to the QEMU process immediately after the 20th `CommitBlock` returned. **All 20 committed blocks were lost in 3/3 cut cycles** (empty chain, empty frontier, clean reopen, no corruption). A control that called `store.Sync()` after every commit survived the identical cut **20/20**.\n\n## Root cause\n\n1. `store/badger_store.go:47-49` — `NewBadgerStore` opens Badger with `badger.DefaultOptions(dir).WithLogger(nil)`. Badger v4.9.1's `DefaultOptions` sets `SyncWrites: false` (`options.go:143`), and the string `SyncWrites` appears **zero** times anywhere in the xe repo. There is no opt-in, no config flag, no comment.\n2. `store/badger_store.go:58-60` — `Sync()` exists (`return s.db.Sync()`), but it is called from exactly **two** sites in non-test code, both on the final-vote path: `core/finalization.go:98` (re-affirm) and `core/finalization.go:295` (`emitFinalLocked`). No block-commit, pending-send, delegation, lease, or state-block write path calls it.\n3. `store/badger_store.go:667-670` — `CommitBlock` is a single `db.Update` writing block + chain + frontier atomically, and returns success while the data is still only in the OS page cache.\n\n## Reproduction (our own QEMU true-power-loss cut)\n\nHarness: the real `store.BadgerStore` at HEAD, driven by a Go test binary (static, `CGO_ENABLED=0`) inside a QEMU guest — Alpine `linux-virt` 6.12.117 kernel + minimal busybox initramfs, 256 MB RAM, store directory on an ext4 virtio-blk disk (`cache=writeback`, so a guest flush is what makes data durable — exactly the property under test). Each cycle: fresh 512 MB disk, boot, commit 20 blocks onto one account chain via `CommitBlock`, print READY the moment the 20th commit returns, host SIGKILLs the QEMU process (power cut), reboot the same disk, reopen the store and count.\n\n| Cycle | Sync after each commit? | Kill latency after READY | Blocks surviving (of 20) | Chain / frontier after reboot |\n|---|---|---|---|---|\n| cut 1 | no | ~3.5 ms | **0** | empty / empty |\n| cut 2 | no | ~3.1 ms | **0** | empty / empty |\n| cut 3 | no | ~4.5 ms | **0** | empty / empty |\n| control | **yes** | ~3.8 ms | **20** | 20 blocks / frontier matches last hash |\n\nLoss is always a clean suffix truncation: the store reopens cleanly (no Badger errors, no panic, no corruption), the chain is empty and the frontier is empty — as if the commits never happened. The control proves the cut is faithful: identical kill timing, identical harness, the only difference is the `Sync()` barrier, and 20/20 survive.\n\n## Mechanism evidence (host-level, same store API)\n\n- **strace, commit window:** 20 `CommitBlock` calls issue **zero** sync-class syscalls (`fsync`/`fdatasync`/`sync_file_range`/`syncfs`/`msync`) and **zero** file-write syscalls between the commit-start and commit-done markers — the commits are pure userspace mmap stores + memtable updates. (Open-phase fsyncs and close-phase syncs exist, as expected; the commit window itself is barrier-free.) The synced control issues 40 `msync`s in the same window (2 per `Sync()` × 20).\n- **Dirty pages after commit returns:** 32 kB of store-mapped dirty pages immediately after the 20th unsynced commit returns, vs 4 kB with `Sync()` after each commit.\n- **Latency:** 20 commits take ~1.5 ms unsynced vs ~283 ms with `Sync()` — the missing barrier is also a ~200× latency difference, consistent with zero syscalls on the path.\n- **Clean shutdown is unaffected:** `Close()` + reopen sees all 20 blocks — the data is real and durable under a clean shutdown; only a power cut loses it.\n\n## Why kill -9 is NOT a reproduction (negative control, included deliberately)\n\nKilling the process with SIGKILL (no `Close()`) loses **nothing**: all 20 blocks + chain + frontier survive, because a process kill leaves the OS page cache intact. Only a power loss / OS crash discards dirty page-cache pages — which is why the QEMU cut, not a process kill, is the reproduction. Any \"reproduction\" that only kills the process does not demonstrate this impact.\n\n## Impact / worst case\n\n- A node that loses power (host crash, OOM-killer on the host, hypervisor kill, datacenter power event) silently rewinds its committed ledger state: blocks it acknowledged as committed are gone, with no error, no torn-state marker, and no repair path — the node simply restarts from an older state.\n- The loss is silent and total for the unflushed suffix: frontier + chain + blocks all revert together, so nothing downstream can detect the rewind from the store alone.\n- Blast radius is bounded by the flush gap: in the worst case (a node that rarely finalizes votes, the only path that calls `Sync()`), the unflushed suffix can span a long stretch of committed blocks. A fleet-wide power event rewinds every affected node at once.\n- No remote trigger and no funds-theft vector: this is a durability / crash-resistance defect, hence Medium, not High.\n\n## Duplicate check (re-verified at filing time, 2026-11-14 ~12:08–12:20 UTC)\n\n- Board search `q=SyncWrites` → **0 hits**; `q=aurorabelle788` → 0 (the #557 author has not filed on the board). No board ticket names this mechanism.\n- Neighbours read in full and distinguished: XE-0275 (triaging Medium — genesis final-height two-transaction tear, kill -9 between two txns; atomicity, not a missing sync barrier); XE-1839 (wallet-create orphan phrase file); XE-7693 (ssh PEM staging in temp dir); XE-9594 (banStore dirty flag); XE-8937 (gossip never carries 201-accepted blocks — network, not storage durability); XE-8726 (chat messages memory-only — ChatStore, and it explicitly assumes the ledger persists money durably, the opposite direction); XE-9241 (statechain unreadable stored block bricks startup — torn/corrupt value + replay fatal gap, not committed-state loss from a missing barrier). `q=BadgerStore` (36 hits) contains no durability/sync-barrier mechanism.\n- Board state at filing: 1,365 open; the two newest public tickets (XE-12802 desk/GitHub reconciliation, XE-12804 Track-2 pre-campaign notice) are unrelated. No fix PR: xe HEAD still `89a542e`, open PRs still #553–#556, pull-refs still 186.\n\n## Remediation suggestion\n\nEnable the barrier on the commit path — either `badger.DefaultOptions(dir).WithLogger(nil).WithSyncWrites(true)` in `NewBadgerStore`, or an explicit `s.Sync()` after `CommitBlock` (and the other mutating store calls that must survive a crash). Measured cost on this workload is ~14 ms per commit — the price of the durability the API currently promises but does not deliver. A middle ground is a periodic sync (e.g. every N commits or every second) with a documented durability window, but the current window is unbounded: nothing on the commit path ever syncs.","patch_refs":[],"thread":[{"id":15377,"source":"desk","source_ref":"reply:13268","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-12805 and says what differs; it is read like any other.","created_at":"2026-11-20T19:45:57.337987+00:00"}]}