<!-- XE-0124 · store: account chain stored as one JSON array (Θ(N²) writes, measured 2k=1.1 s → 4k=4.4 s) and the lease table scanned in full 3× per 10 s · testnet -->
*Markdown view of <https://bounty.xe.network/XE-0124>. The page is the record; this is the same page for reading.*

XE-0124 · Bounty

# store: account chain stored as one JSON array (Θ(N²) writes, measured 2k\=1.1 s → 4k\=4.4 s) and the lease table scanned in full 3× per 10 s

Triaging
claimed Medium
[xe#200](https://github.com/xeprotocol/xe/issues/200)
filed 2026-10-29 16:51 UTC
by hayokakoweda75

Verdict
:   published when the fix ships

Tier
:   published when the fix ships

Claimed
:   Medium

Area
:   Node / API

Duplicate of
:   —

Tracked
:   —

Updated
:   2026-11-08 20:17 UTC

Told
:   2026-11-08 20:18 UTC

### Patches

- [xe#213](https://github.com/xeprotocol/xe/pull/213) open by arturbarszcz78
  fix(store): index account chains and secondary lease state/provider tables (fixes #200)

### History

- [43804](https://bounty.xe.network/log/43804) 2026-11-08 20:19 UTC reply.mirror\_failed
- [43367](https://bounty.xe.network/log/43367) 2026-11-08 20:18 UTC proposal.adopted
- [43366](https://bounty.xe.network/log/43366) 2026-11-08 20:18 UTC reply.sent
- [40435](https://bounty.xe.network/log/40435) 2026-11-08 20:17 UTC proposal.adopted
- [40434](https://bounty.xe.network/log/40434) 2026-11-08 20:17 UTC ticket.facts area
- [40433](https://bounty.xe.network/log/40433) 2026-11-08 20:17 UTC proposal.adopted
- [40432](https://bounty.xe.network/log/40432) 2026-11-08 20:17 UTC ticket.facts
- [40431](https://bounty.xe.network/log/40431) 2026-11-08 20:17 UTC proposal.adopted
- [40430](https://bounty.xe.network/log/40430) 2026-11-08 20:17 UTC ticket.facts
- [40429](https://bounty.xe.network/log/40429) 2026-11-08 20:17 UTC proposal.adopted
- [40428](https://bounty.xe.network/log/40428) 2026-11-08 20:17 UTC ticket.facts
- [40427](https://bounty.xe.network/log/40427) 2026-11-08 20:17 UTC proposal.adopted

[Full history](https://bounty.xe.network/log?ref=XE-0124) · [JSON](https://bounty.xe.network/v1/tickets/XE-0124) · [markdown](https://bounty.xe.network/XE-0124.md)

### Report

**Suggested severity: Medium (2,500 XE)**
**Area:** `store/badger_store.go` (account chain value, lease prefix scans), `node/node.go` (`settleLoop`)
**Repo pin:** 9787859 (testnet-0005)

Two hot paths in the store do whole-table / whole-value work. Both grow monotonically because nothing in the normal path deletes records, and neither needs a privileged actor to grow.

## A — a per-account chain is one JSON array, rewritten on every block

```
// store/badger_store.go:701-713
hashes := make([]string, len(c.Chain.Blocks))
for i, b := range c.Chain.Blocks { hashes[i] = b.Hash }
chainData, err := json.Marshal(hashes)      // whole history in one value
...
if err := txn.Set(chainKey, chainData); err != nil { ... }
```

Reading is symmetric: one value, then N random `Get`s \+ N `Unmarshal`s (`store/badger_store.go:144-156`). `prepareBlockWrite` (`core/ledger.go:2250-2261`) calls `GetAccountChain` for **every** new block, so each block costs Θ(N) reads and rewrites 67·N bytes. Over a chain of N blocks the node writes Σ67·i \= Θ(N²) bytes. Nothing chunks, prunes or caps the chain length.

Measured (sequential commits on one account, shipped store):

```
N=2 000: 1.112 s total
N=4 000: 4.378 s total      (3.9x for 2x N -> quadratic)
```

Extrapolated to N\=100 000: ≈335 GB written per node for ≈30 MB of actual block data (≈11 000× write amplification — SSD wear, IOPS, value-log GC), and every `GetBalance` / `GetBlockHeight` / `IsFinalized` / `GetAssetBalanceAtBlock` for that account becomes O(N) (they are on the quorum path: `promotable`, `confirmConflict`).

## B — the lease table is scanned in full three times every 10 s

```
// store/badger_store.go:1619 — GetLeasesByState (and GetLeasesByProvider at :1592)
opts := badger.DefaultIteratorOptions
it := txn.NewIterator(opts)
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
	var l core.Lease
	item.Value(func(val []byte) error { return json.Unmarshal(val, &l) })   // every lease
	if l.State == state { out = append(out, &l) }
}
```

```
// node/node.go:1754-1770 — ticker 10 s
case <-ticker.C:
	n.trySettleExpiredLeases()      // GetLeasesByProvider
	n.tryForceSettleStaleLeases()   // GetLeasesByState(Accepted)
	n.tryArchiveExpiredLeases()     // GetLeasesByState(Accepted)
```

Leases are never deleted: `archiveExpiredLease` only writes state `Expired` (`node/node.go:1886-1896`), and rollback is the only removal path. A lease costs micro-XUSD to create, so the table is attacker-growable.

Measured (`-mode=leases`):

```
5 000 leases:  47 ms, 3.46 MB allocated per scan
20 000 leases: 279 ms, 13.45 MB per scan     (linear, ~14 µs and ~673 B per lease)
```

At 1 000 000 leases: ≈14 s and ≈670 MB allocation per scan, ×3 every 10 s — the node spends all its time scanning and GCing and stops processing gossip/finality, permanently, with no recovery short of deleting the DB. The same call backs `/leases?state=` (found in `api/handler.go:735`), so one unauthenticated request also blocks for that duration.

## Suggested fix

- Store the per-account chain as an index: one key per block position (`account | index -> hash`) plus a frontier key, so a commit writes O(1) and reads page-sized ranges.
- Keep `state -> hashes` and `provider -> hashes` as incremental indexes updated inside the same `db.Update`, so the 10 s loop reads only the relevant bucket.
- Add pagination at the store layer (`start/limit`) and use it in the loop and in `/leases`.
- Prune or archive settled/expired leases to a separate prefix with a bounded retention.

### Thread

**Joseph Denne**team2026-11-08 20:17 UTCdesk

Thanks for the report and the measurements. We have confirmed both storage behaviours you describe and they are being handled; we will update this ticket when there is a fix to point at.
