catcrafts.net/server/interfaces
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Jorijn van der Graaf 06ec1c525c
orders: the fold is kept in memory, and a reference names one order
The ledger was folded from disk on every call: every order page, every
/financials request, every reconciler sweep, and every two seconds for
the mailer. Each event found its order by walking the orders so far, so
a fold was quadratic. A ledger of 40 000 orders took 11.9 s to fold,
anyone can add orders, and /financials is public and uncached.

Orders are now found through an index, which makes a fold one pass
(0.33 s for the same ledger), and the fold is held in memory. Every
call stats the file, and a different size or modification time means
fold it again, so an append by the CLI from another process and an edit
by hand are seen on the next call without a restart. What this process
appends itself is applied to the held fold by the same function that
folds a line from disk, provided the file grew by exactly what was
written; if it grew by more, another writer's line is in there and the
file is folded again. With 40 000 orders /financials takes about 1 ms
and an order page 0.3 ms. /financials sums from the held fold through
LedgerSales and no longer copies every order to do it.

The file stays the ledger and the held fold a reading of it. It costs 3
to 4 KB of memory an order, and it cannot see a change that leaves both
the size and the modification time as they were.

One thing the writer does differently. A file that ends mid-line, after
a crash during a write, now gets its line break before the next event.
The next event used to be written onto the end of the broken line and
was skipped with it, so a crash cost two events, not one.

The reference a payer quotes is the first six hex digits of the token:
24 bits, where two orders in a few thousand share one often enough to
plan for, and a transfer quoting it would pay for both. Checkout now
mints its token through NewUniqueOrderToken, which reads every
reference the ledger has ever held, lapsed and cancelled orders
included, and holds the reference while the checkout is in flight.

ShouldFoldTheOrderLedger compares the held fold with a fresh one after
appends of every kind, an outside append, a same-size edit, a replaced
file and a torn tail. It hands colliding draws to the mint, since the
real generator collides once in sixteen million. And it races one
outside line against a stream of this process's own appends, which
fails within a few rounds when the size check is removed.

Found in the security review of 2026-11-26.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
2026-11-26 11:05:35 +01:00
..
Catcrafts.Server.cppm orders: the fold is kept in memory, and a reference names one order 2026-11-26 11:05:35 +01:00