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>
This commit is contained in:
parent
21d34b13ed
commit
06ec1c525c
4 changed files with 810 additions and 112 deletions
|
|
@ -496,12 +496,14 @@ HTTPResponse RenderPage(std::string_view target) {
|
|||
// The financials page: lifetime sales folded live from the order ledger,
|
||||
// donations and expenses from the bank-aggregates file. Server-rendered
|
||||
// here because both inputs are runtime state; the shared dispatch's case
|
||||
// is the backend-down fallback, like orders. Refolding the ledger per
|
||||
// request is what every order lookup already does, and the page's whole
|
||||
// promise is that a refresh shows the current totals — so no caching.
|
||||
// is the backend-down fallback, like orders. The page's whole promise is
|
||||
// that a refresh shows the current totals, so the response is not cached:
|
||||
// the totals are summed on every request, from the fold of the ledger the
|
||||
// order store holds, which follows the file (LedgerSales). That is a pass
|
||||
// over orders already in memory, not a read of the ledger from disk,
|
||||
// which is what this page cost each visitor before 2026-11-26.
|
||||
if (route.kind == RouteKind::Financials) {
|
||||
const std::vector<OrderRecord> orders = ListOrders();
|
||||
const SalesSummary sales = SummarizeSales(orders);
|
||||
const SalesSummary sales = LedgerSales();
|
||||
const Financials fin = CurrentFinancials();
|
||||
// Shop donations ride along from the same fold: they are live like
|
||||
// sales, and the renderer joins them with the bank-side donations.
|
||||
|
|
@ -1027,8 +1029,25 @@ HTTPResponse HandleCheckout(const HTTPRequest& req, const Route& route) {
|
|||
unitMinor, parsed.value.quantity, *shippingMinor, parsed.value.country);
|
||||
}
|
||||
|
||||
// A token whose reference no other order has. The reference is what a
|
||||
// payer quotes and a transfer is matched on, and it is only 24 bits of
|
||||
// the token, so it is checked against the whole ledger rather than left
|
||||
// to chance (NewUniqueOrderToken has the reasoning). Minted here, after
|
||||
// validation and both rate limits, so a flood of refused submissions
|
||||
// never reads the ledger at all.
|
||||
const std::optional<std::string> token = NewUniqueOrderToken();
|
||||
if (!token) {
|
||||
std::println(std::cerr,
|
||||
"checkout: no order reference could be minted that no other "
|
||||
"order has. The ledger holds too many orders for a six-digit "
|
||||
"reference; it needs lengthening before checkout can resume");
|
||||
return reject({{ "", "Couldn't create the order — something is wrong on this "
|
||||
"end. Nothing was charged. Please try again later." }},
|
||||
parsed.value, "500");
|
||||
}
|
||||
|
||||
OrderRecord order;
|
||||
order.token = NewOrderToken();
|
||||
order.token = *token;
|
||||
order.reference = ReferenceFromToken(order.token);
|
||||
order.product = product->slug;
|
||||
order.color = parsed.value.color;
|
||||
|
|
|
|||
Loading…
Reference in a new issue