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
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 Gets + N Unmarshals (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 -> hashesandprovider -> hashesas incremental indexes updated inside the samedb.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
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.