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
|
|
@ -45,6 +45,11 @@ export namespace Catcrafts::Server {
|
|||
// Nothing is ever rewritten in place, so the file is also the audit
|
||||
// trail, and a crash mid-append costs at most the line being written.
|
||||
//
|
||||
// The fold is held in memory and redone when the file's size or
|
||||
// modification time changes, so the calls below cost a stat and a lookup
|
||||
// and still see what the CLI appended or an operator edited a moment ago.
|
||||
// The file is the ledger; nothing here is trusted over it.
|
||||
//
|
||||
// The volume argument: this sells single-digit units per week. When that
|
||||
// is wrong by two orders of magnitude, the log imports into SQLite in one
|
||||
// sitting — the reverse migration would not be so kind.
|
||||
|
|
@ -138,6 +143,12 @@ export namespace Catcrafts::Server {
|
|||
};
|
||||
SalesSummary SummarizeSales(std::span<const OrderRecord> orders);
|
||||
|
||||
// The same summary over the ledger as it stands, without handing out a
|
||||
// copy of every order to get it. /financials is public and uncached and
|
||||
// asks on every request; ListOrders() would copy the whole ledger each
|
||||
// time for four integers.
|
||||
SalesSummary LedgerSales();
|
||||
|
||||
// ── the bank side of /financials ──────────────────────────────────
|
||||
//
|
||||
// Bank-side donations and expenses come from an aggregates file written
|
||||
|
|
@ -199,6 +210,39 @@ export namespace Catcrafts::Server {
|
|||
std::string NewOrderToken();
|
||||
std::string ReferenceFromToken(std::string_view token);
|
||||
|
||||
// A token for a new order whose REFERENCE no other order has, or nullopt
|
||||
// when none could be found in a bounded number of draws.
|
||||
//
|
||||
// The token is 128 bits and never repeats. The reference is its first six
|
||||
// hex digits, 24 bits, because a person types it into a transfer: two
|
||||
// orders in a ledger of a few thousand share one often enough to plan
|
||||
// for, and a transfer quoting it would then be a payment for both. So the
|
||||
// reference is checked against every order the ledger has ever held,
|
||||
// paid, lapsed or cancelled alike (an old order's payment is still in the
|
||||
// bank's history, quoting it), and a colliding token is thrown away for a
|
||||
// fresh draw. The reference is HELD from this call on, so two checkouts
|
||||
// minting at the same moment cannot both be handed it; CreateOrder
|
||||
// releases the hold once the order is in the ledger, where the next call
|
||||
// finds it.
|
||||
//
|
||||
// Checkout mints through this. NewOrderToken stays for what needs only
|
||||
// the token's shape.
|
||||
std::optional<std::string> NewUniqueOrderToken();
|
||||
|
||||
// The same, drawing candidate tokens from `draw` instead of the CSPRNG,
|
||||
// and giving up after kUniqueTokenDraws of them. It exists for the
|
||||
// self-test: a collision is one draw in sixteen million from the real
|
||||
// generator, so the only way to see a colliding draw thrown away, or a
|
||||
// run of them end in a refusal, is to hand the draws in.
|
||||
inline constexpr int kUniqueTokenDraws = 32;
|
||||
std::optional<std::string> NewUniqueOrderToken(const std::function<std::string()>& draw);
|
||||
|
||||
// Whether an order with this reference exists, or one is being created.
|
||||
// Compared the way a transfer is matched, on upper-case alphanumerics, so
|
||||
// "CC-2B6457" and "cc 2b6457" are one reference. Exported for the
|
||||
// self-test.
|
||||
bool ReferenceInUse(std::string_view reference);
|
||||
|
||||
// The same reference again, as an ISO 11649 structured creditor reference:
|
||||
// "RF" + two ISO 7064 mod-97-10 check digits + the CC-style body. Derived
|
||||
// from the same token for the same reason, so the two can never disagree.
|
||||
|
|
|
|||
Loading…
Reference in a new issue