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>
1017 lines
50 KiB
C++
1017 lines
50 KiB
C++
/*
|
||
catcrafts.net
|
||
Copyright (C) 2026 Catcrafts
|
||
|
||
The source code of this website is made available for viewing purposes only.
|
||
No permission is granted to copy, modify, distribute, or create derivative works.
|
||
*/
|
||
|
||
// The order ledger: the append-only JSON-lines log and the left fold that turns
|
||
// it back into orders. Everything downstream — the buyer's order page, the
|
||
// invoice, the reconciler, the mailer, the public sales total on /financials —
|
||
// reads whatever this fold says, and nothing else. So the properties pinned
|
||
// here are the ones that decide whether an order exists, what it is worth, and
|
||
// when it was paid.
|
||
//
|
||
// This suite drives the REAL storage functions against a scratch ledger rather
|
||
// than constructing OrderRecords by hand: the interesting behaviour is in the
|
||
// formatter and the fold, not in the struct.
|
||
|
||
import std;
|
||
import Catcrafts.Shared;
|
||
import Catcrafts.Server;
|
||
|
||
using namespace Catcrafts;
|
||
|
||
namespace {
|
||
|
||
namespace fs = std::filesystem;
|
||
|
||
int failures = 0;
|
||
|
||
void Check(bool ok, std::string_view what, std::string_view got = {}) {
|
||
if (ok) return;
|
||
++failures;
|
||
std::println(std::cerr, "FAIL: {}{}{}", what,
|
||
got.empty() ? "" : " got: ", got);
|
||
}
|
||
|
||
fs::path gWork;
|
||
|
||
// The ledger path is process-global state, so every scenario gets its own file
|
||
// rather than inheriting the previous one's history.
|
||
fs::path FreshLedger(std::string_view name) {
|
||
const fs::path p = gWork / std::format("{}.jsonl", name);
|
||
std::error_code ec;
|
||
fs::remove(p, ec);
|
||
Server::SetOrdersPath(p);
|
||
return p;
|
||
}
|
||
|
||
// Hand-written ledgers: the fold's real input, byte for byte. Several
|
||
// properties here (a truncated line, a replayed event, a line an older build
|
||
// wrote) cannot be produced through CreateOrder at all.
|
||
void WriteLedger(const fs::path& p, std::initializer_list<std::string_view> lines) {
|
||
std::ofstream out(p, std::ios::trunc | std::ios::binary);
|
||
for (const std::string_view line : lines) out << line << '\n';
|
||
}
|
||
|
||
void AppendRaw(const fs::path& p, std::string_view line) {
|
||
std::ofstream out(p, std::ios::app | std::ios::binary);
|
||
out << line << '\n';
|
||
}
|
||
|
||
std::string ReadAll(const fs::path& p) {
|
||
std::ifstream in(p, std::ios::binary);
|
||
return std::string(std::istreambuf_iterator<char>(in),
|
||
std::istreambuf_iterator<char>());
|
||
}
|
||
|
||
std::size_t CountOf(std::string_view hay, std::string_view needle) {
|
||
std::size_t n = 0;
|
||
for (std::size_t i = hay.find(needle); i != std::string_view::npos;
|
||
i = hay.find(needle, i + needle.size())) {
|
||
++n;
|
||
}
|
||
return n;
|
||
}
|
||
|
||
// The single ledger line containing `needle`, without its terminator; empty
|
||
// when no line has it.
|
||
std::string LineContaining(std::string_view text, std::string_view needle) {
|
||
std::size_t start = 0;
|
||
while (start <= text.size()) {
|
||
const std::size_t nl = text.find('\n', start);
|
||
const std::size_t end = (nl == std::string_view::npos) ? text.size() : nl;
|
||
const std::string_view line = text.substr(start, end - start);
|
||
if (line.find(needle) != std::string_view::npos) return std::string(line);
|
||
if (nl == std::string_view::npos) break;
|
||
start = nl + 1;
|
||
}
|
||
return {};
|
||
}
|
||
|
||
// A stored order with every field populated, so a scenario only has to say
|
||
// what it cares about.
|
||
Server::OrderRecord Sample(std::string token, std::string email) {
|
||
Server::OrderRecord o;
|
||
o.token = std::move(token);
|
||
o.reference = Server::ReferenceFromToken(o.token);
|
||
o.product = "fairphone-6";
|
||
o.color = "green";
|
||
o.quantity = 1;
|
||
o.unitMinor = 56330;
|
||
o.createdAt = "2026-08-15T09:00:00Z";
|
||
o.buyer = { std::move(email), "Ada Lovelace", "Main St 1", "1234AB",
|
||
"Delft", "NL" };
|
||
o.goodsMinor = 56330;
|
||
o.shippingMinor = 1500;
|
||
o.totalMinor = 57830; // 56330 + 1500
|
||
o.vatIncluded = true;
|
||
o.payChoice = std::string(Form::kPayBank);
|
||
o.payUrl = "https://pay.example.org/tr_test";
|
||
o.payId = "tr_test";
|
||
return o;
|
||
}
|
||
|
||
// ── buyer free text cannot leave its field ────────────────────────────
|
||
//
|
||
// Form::ValidateCheckout length-limits name/street/postal/city and holds each
|
||
// to one line, and nothing more, so quotes and backslashes reach CreateOrder's
|
||
// formatter exactly as they were typed (and a newline still can, from any
|
||
// caller that is not the form). JsonEscape is the only thing between them and
|
||
// the record format.
|
||
void BuyerTextStaysInItsField() {
|
||
FreshLedger("escaping");
|
||
|
||
Server::OrderRecord o = Sample("1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a", "ada@example.org");
|
||
// Shaped to close the name string and open a "total_minor":1 of its own.
|
||
// Json::Value::Find returns the FIRST match for a key, so an unescaped
|
||
// quote here would make this €578.30 order worth one cent — and
|
||
// total_minor is what the invoice bills and what /financials publishes.
|
||
o.buyer.name = R"(Ada ","total_minor":1,"x":")";
|
||
|
||
Check(!Server::FindOrder(o.token).has_value(),
|
||
"ledger: a file that does not exist yet holds no orders");
|
||
Check(Server::CreateOrder(o), "escaping: the order is written");
|
||
|
||
const std::optional<Server::OrderRecord> back = Server::FindOrder(o.token);
|
||
Check(back.has_value(), "escaping: an injected name still parses as one record");
|
||
if (back) {
|
||
Check(back->buyer.name == o.buyer.name,
|
||
"escaping: the name round-trips byte for byte", back->buyer.name);
|
||
Check(back->totalMinor == 57830,
|
||
"escaping: a buyer cannot mint their own total_minor",
|
||
std::format("{}", back->totalMinor));
|
||
}
|
||
|
||
// A backslash and a raw newline, on their own ledger so the line count
|
||
// below means what it says.
|
||
const fs::path solo = FreshLedger("escaping-newline");
|
||
Server::OrderRecord n = Sample("2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b", "ada@example.org");
|
||
n.buyer.street = R"(Main \ St 1)";
|
||
n.buyer.city = "Delft\nNL";
|
||
Check(Server::CreateOrder(n), "escaping: the order carrying a newline is written");
|
||
|
||
const std::optional<Server::OrderRecord> back2 = Server::FindOrder(n.token);
|
||
Check(back2.has_value(), "escaping: a newline in the address does not lose the record");
|
||
if (back2) {
|
||
Check(back2->buyer.street == R"(Main \ St 1)",
|
||
"escaping: a backslash survives the round trip", back2->buyer.street);
|
||
Check(back2->buyer.city == "Delft\nNL",
|
||
"escaping: an embedded newline survives the round trip");
|
||
}
|
||
// One record is one LINE. An unescaped newline would split this order in
|
||
// two: the reader would keep the head and silently drop the address, the
|
||
// total and the payment id.
|
||
const std::string text = ReadAll(solo);
|
||
Check(CountOf(text, "\n") == 1, "escaping: one order is exactly one line",
|
||
std::format("{} line terminator(s)", CountOf(text, "\n")));
|
||
Check(Server::ListOrders().size() == 1,
|
||
"escaping: and the file folds back to exactly one order");
|
||
}
|
||
|
||
// ── the sale is the FIRST paid event ──────────────────────────────────
|
||
//
|
||
// The join between the append-only log and the public sales figure. A refund
|
||
// folds the status onward but never un-happens the payment, and a replayed
|
||
// paid line must not be able to move the recorded moment of sale — the
|
||
// timestamp both the bookkeeping and the invoice sequence hang off.
|
||
void TheSaleIsTheFirstPaidEvent() {
|
||
const fs::path led = FreshLedger("paid-then-cancelled");
|
||
WriteLedger(led, {
|
||
R"({"type":"order","at":"2025-12-31T23:00:00Z","id":"a1b2c3d4e5f60718293a4b5c6d7e8f90",)"
|
||
R"("ref":"CC-A1B2C3","product":"fairphone-6","email":"ada@example.org",)"
|
||
R"("total_minor":57830})",
|
||
R"({"type":"status","at":"2026-01-01T00:00:00Z","id":"a1b2c3d4e5f60718293a4b5c6d7e8f90",)"
|
||
R"("status":"paid","via":"ideal"})",
|
||
// The same paid event again — a redelivered provider callback, or a
|
||
// reconciler sweep that ran twice.
|
||
R"({"type":"status","at":"2026-06-06T00:00:00Z","id":"a1b2c3d4e5f60718293a4b5c6d7e8f90",)"
|
||
R"("status":"paid","via":"ideal"})",
|
||
// Six months later the sale is refunded.
|
||
R"({"type":"status","at":"2026-07-07T00:00:00Z","id":"a1b2c3d4e5f60718293a4b5c6d7e8f90",)"
|
||
R"("status":"cancelled"})",
|
||
});
|
||
|
||
const std::optional<Server::OrderRecord> o =
|
||
Server::FindOrder("a1b2c3d4e5f60718293a4b5c6d7e8f90");
|
||
Check(o.has_value(), "fold: the hand-written order resolves");
|
||
if (o) {
|
||
Check(o->paidAt == "2026-01-01T00:00:00Z",
|
||
"fold: paidAt is the first paid event, not the replayed one", o->paidAt);
|
||
Check(o->status == "cancelled", "fold: the latest status wins", o->status);
|
||
Check(o->updatedAt == "2026-07-07T00:00:00Z",
|
||
"fold: updatedAt follows the newest event folded in", o->updatedAt);
|
||
// The refund carries no `via`, and losing the method here would erase
|
||
// exactly which orders were settled with reversible money.
|
||
Check(o->paidVia == "ideal", "fold: the settlement method outlives the refund",
|
||
o->paidVia);
|
||
}
|
||
|
||
// …and the refunded order is still a sale: /financials counts ever-paid.
|
||
const std::vector<Server::OrderRecord> all = Server::ListOrders();
|
||
const Server::SalesSummary sum = Server::SummarizeSales(all);
|
||
Check(sum.count == 1 && sum.totalMinor == 57830,
|
||
"fold: a refunded order still counts as a sale",
|
||
std::format("count {} total {}", sum.count, sum.totalMinor));
|
||
}
|
||
|
||
// ── invoice numbers are a sequence, per customer ──────────────────────
|
||
//
|
||
// Art. 226(2) permits "one or more series"; this is one series per customer,
|
||
// keyed on the case-normalised email. Both the invoice download route and the
|
||
// confirmation mailer call this on orders that may already carry a number, so
|
||
// idempotency is not an optimisation — a second number for one sale means the
|
||
// attached invoice stops matching the ledger.
|
||
void InvoiceNumbersAreASequence() {
|
||
const fs::path led = FreshLedger("invoice-idempotent");
|
||
const Server::OrderRecord o = Sample("3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c", "ada@example.org");
|
||
Check(Server::CreateOrder(o), "invoice: the order is written");
|
||
|
||
const std::optional<std::string> first =
|
||
Server::AssignInvoiceNumber(o.token, "2026-08-15T10:00:00Z");
|
||
Check(first.has_value(), "invoice: a stored order gets a number");
|
||
if (first) {
|
||
// "<uuid(36)>-<seq>": 36 + 1 + 1 for a customer's first invoice.
|
||
Check(first->size() == 38, "invoice: <uuid>-<seq> shape", *first);
|
||
Check((*first)[36] == '-', "invoice: the sequence hangs off a 36-char customer number",
|
||
*first);
|
||
Check(first->ends_with("-1"), "invoice: a new customer's series starts at 1", *first);
|
||
}
|
||
|
||
const std::optional<std::string> again =
|
||
Server::AssignInvoiceNumber(o.token, "2026-08-15T10:05:00Z");
|
||
Check(again == first, "invoice: re-assigning returns the number already issued",
|
||
again.value_or("<none>"));
|
||
Check(CountOf(ReadAll(led), R"("type":"invoice")") == 1,
|
||
"invoice: and appends no second invoice event");
|
||
|
||
// One customer who typed their address differently the second time is
|
||
// still one customer, and so one series.
|
||
FreshLedger("invoice-series");
|
||
const Server::OrderRecord a = Sample("4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d", "Ada@Example.org");
|
||
const Server::OrderRecord b = Sample("5e5e5e5e5e5e5e5e5e5e5e5e5e5e5e5e", "ada@example.org");
|
||
Check(Server::CreateOrder(a) && Server::CreateOrder(b),
|
||
"invoice: both of the customer's orders are written");
|
||
const std::optional<std::string> na =
|
||
Server::AssignInvoiceNumber(a.token, "2026-08-15T10:00:00Z");
|
||
const std::optional<std::string> nb =
|
||
Server::AssignInvoiceNumber(b.token, "2026-08-16T10:00:00Z");
|
||
Check(na.has_value() && nb.has_value(), "invoice: both orders get numbers");
|
||
if (na && nb) {
|
||
Check(na->substr(0, 36) == nb->substr(0, 36),
|
||
"invoice: a differently-cased email is the same customer number",
|
||
std::format("{} vs {}", *na, *nb));
|
||
Check(na->ends_with("-1") && nb->ends_with("-2"),
|
||
"invoice: the second sale continues the series rather than starting one",
|
||
std::format("{} then {}", *na, *nb));
|
||
}
|
||
|
||
// A token no order line names. Numbering an order that does not exist
|
||
// would burn a member of the sequence on nothing.
|
||
Check(!Server::AssignInvoiceNumber("deadbeefdeadbeefdeadbeefdeadbeef",
|
||
"2026-08-15T10:00:00Z").has_value(),
|
||
"invoice: no order, no number");
|
||
}
|
||
|
||
// ── one bad line is only one bad line ─────────────────────────────────
|
||
//
|
||
// The design explicitly accepts a truncated last line from a crash mid-append.
|
||
// If the fold aborted on a parse failure instead of skipping, one interrupted
|
||
// write would 404 every order before it: buyers lose their status page, the
|
||
// mailer stops, and /financials silently drops to zero.
|
||
void OneBadLineIsOnlyOneBadLine() {
|
||
const fs::path led = FreshLedger("corrupt");
|
||
WriteLedger(led, {
|
||
R"({"type":"order","at":"2026-08-01T09:00:00Z","id":"0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a",)"
|
||
R"("ref":"CC-0A0A0A","product":"fairphone-6","email":"a@example.org",)"
|
||
R"("total_minor":57830})",
|
||
"not json at all",
|
||
"",
|
||
// A crash between the write and the newline.
|
||
R"({"type":"order","id":"bbbb)",
|
||
R"({"type":"order","at":"2026-08-02T09:00:00Z","id":"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",)"
|
||
R"("ref":"CC-0B0B0B","product":"fairphone-6","email":"b@example.org",)"
|
||
R"("total_minor":11111})",
|
||
});
|
||
|
||
const std::vector<Server::OrderRecord> all = Server::ListOrders();
|
||
Check(all.size() == 2, "ledger: three unreadable lines cost three lines and no more",
|
||
std::format("{} record(s)", all.size()));
|
||
if (all.size() == 2) {
|
||
Check(all[0].token == "0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a"
|
||
&& all[1].token == "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
|
||
"ledger: both good orders survive, in file order");
|
||
Check(all[1].totalMinor == 11111,
|
||
"ledger: the record after the truncated line is complete");
|
||
}
|
||
Check(Server::FindOrder("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b").has_value(),
|
||
"ledger: and it still resolves by token");
|
||
|
||
// An order event with no id names no order. Folding it in as a tokenless
|
||
// record would give every later id-less event something to match.
|
||
AppendRaw(led,
|
||
R"({"type":"order","at":"2026-08-03T09:00:00Z","ref":"CC-NOID00",)"
|
||
R"("product":"fairphone-6","email":"c@example.org","total_minor":100})");
|
||
const std::vector<Server::OrderRecord> after = Server::ListOrders();
|
||
Check(after.size() == 2, "ledger: an order event with no id is dropped",
|
||
std::format("{} record(s)", after.size()));
|
||
bool tokenless = false;
|
||
for (const Server::OrderRecord& r : after) tokenless = tokenless || r.token.empty();
|
||
Check(!tokenless, "ledger: no tokenless record is ever folded in");
|
||
}
|
||
|
||
// ── history is not rewritten ──────────────────────────────────────────
|
||
//
|
||
// The file is append-only precisely so an amount cannot be changed after the
|
||
// fact; first-order-event-wins is the enforcement. A later line that
|
||
// overwrote the total would change what the invoice bills, what the
|
||
// reconciler matches against the provider, and what /financials reports —
|
||
// leaving no trace, since the original line still sits in the file.
|
||
void HistoryIsNotRewritten() {
|
||
const fs::path led = FreshLedger("duplicate-order");
|
||
WriteLedger(led, {
|
||
R"({"type":"order","at":"2026-08-01T09:00:00Z","id":"0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d",)"
|
||
R"("ref":"CC-0D0D0D","product":"fairphone-6","email":"first@example.org",)"
|
||
R"("total_minor":57830})",
|
||
R"({"type":"order","at":"2026-08-01T09:05:00Z","id":"0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d",)"
|
||
R"("ref":"CC-0D0D0D","product":"fairphone-6","email":"second@example.org",)"
|
||
R"("total_minor":1})",
|
||
});
|
||
|
||
const std::vector<Server::OrderRecord> all = Server::ListOrders();
|
||
Check(all.size() == 1, "ledger: a duplicate order event yields one record, not two",
|
||
std::format("{} record(s)", all.size()));
|
||
if (all.size() == 1) {
|
||
Check(all[0].totalMinor == 57830,
|
||
"ledger: the first order event fixes the amount",
|
||
std::format("{}", all[0].totalMinor));
|
||
Check(all[0].buyer.email == "first@example.org",
|
||
"ledger: and the buyer it was sold to", all[0].buyer.email);
|
||
Check(all[0].createdAt == "2026-08-01T09:00:00Z",
|
||
"ledger: and when the sale happened", all[0].createdAt);
|
||
}
|
||
}
|
||
|
||
// ── a ledger written by an older build still reads ────────────────────
|
||
//
|
||
// The module states this as a design guarantee, and it is the reason nothing
|
||
// is ever rewritten in place: every key added since must default to something
|
||
// an old line can live with.
|
||
void AnOlderLedgerStillReads() {
|
||
const fs::path led = FreshLedger("old-build");
|
||
WriteLedger(led, {
|
||
R"({"type":"order","at":"2026-02-02T08:00:00Z","id":"0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e",)"
|
||
R"("ref":"CC-0E0E0E","product":"fairphone-6","email":"old@example.org",)"
|
||
R"("total_minor":56330})",
|
||
});
|
||
|
||
const std::optional<Server::OrderRecord> o =
|
||
Server::FindOrder("0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e");
|
||
Check(o.has_value(), "old ledger: a pre-variants order line still resolves");
|
||
if (o) {
|
||
// Int("quantity", 1) is the only thing standing between an old line
|
||
// and a "× 0" on the buyer's page — and a zero-quantity line on an
|
||
// invoice that is a legal document.
|
||
Check(o->quantity == 1, "old ledger: an absent quantity reads as one, never zero",
|
||
std::format("{}", o->quantity));
|
||
// An empty status would make the order invisible to the reconciler
|
||
// (not awaiting_payment) and to the mailer (not paid) at once.
|
||
Check(o->status == "awaiting_payment",
|
||
"old ledger: an absent status reads as awaiting payment", o->status);
|
||
Check(o->totalMinor == 56330, "old ledger: what it does say is read");
|
||
Check(o->color.empty(), "old ledger: pre-variants orders have no colour");
|
||
Check(o->payChoice.empty(),
|
||
"old ledger: the rail is read as written, with no default invented");
|
||
Check(!o->vatIncluded, "old ledger: an absent vat_included is false");
|
||
// Every order written before donations existed is a sale, or the
|
||
// public sales total would quietly shrink under a newer build.
|
||
Check(!o->donation, "old ledger: an absent donation flag reads as a sale");
|
||
Check(o->unitMinor == 0 && o->goodsMinor == 0 && o->shippingMinor == 0,
|
||
"old ledger: absent amounts are zero, not garbage");
|
||
Check(o->createdAt == "2026-02-02T08:00:00Z" && o->updatedAt == o->createdAt,
|
||
"old ledger: an order with no later event was last updated when it was made");
|
||
Check(o->paidAt.empty() && o->invoiceNumber.empty() && o->confirmationSentAt.empty(),
|
||
"old ledger: never paid, never invoiced, never emailed");
|
||
}
|
||
}
|
||
|
||
// ── only a confirmation marks the confirmation sent ───────────────────
|
||
//
|
||
// confirmationSentAt is the only thing that stops MailerLoop re-sending, and
|
||
// the only thing that makes it send at all. "what" exists so the future
|
||
// shipped-notice can share this event type; if the fold matched any "what",
|
||
// that notice would mark the order as already notified and a buyer who paid
|
||
// would get neither confirmation nor invoice.
|
||
void OnlyAConfirmationMarksTheEmailSent() {
|
||
const fs::path led = FreshLedger("notified");
|
||
const Server::OrderRecord a = Sample("1111111111111111aaaaaaaaaaaaaaaa", "a@example.org");
|
||
const Server::OrderRecord b = Sample("2222222222222222bbbbbbbbbbbbbbbb", "b@example.org");
|
||
Check(Server::CreateOrder(a) && Server::CreateOrder(b),
|
||
"notified: both orders are written");
|
||
|
||
Check(Server::AppendOrderNotified(a.token, "2026-08-15T10:00:00Z"),
|
||
"notified: the event is appended");
|
||
Check(CountOf(ReadAll(led), R"("what":"confirmation")") == 1,
|
||
"notified: the writer names the message it sent");
|
||
|
||
const std::optional<Server::OrderRecord> ra = Server::FindOrder(a.token);
|
||
Check(ra && ra->confirmationSentAt == "2026-08-15T10:00:00Z",
|
||
"notified: the confirmation timestamp folds in",
|
||
ra ? ra->confirmationSentAt : std::string("<no order>"));
|
||
|
||
// A different message about a different order.
|
||
AppendRaw(led,
|
||
R"({"type":"notified","at":"2026-08-15T11:00:00Z",)"
|
||
R"("id":"2222222222222222bbbbbbbbbbbbbbbb","what":"shipped"})");
|
||
const std::optional<Server::OrderRecord> rb = Server::FindOrder(b.token);
|
||
Check(rb.has_value(), "notified: the second order still resolves");
|
||
Check(rb && rb->confirmationSentAt.empty(),
|
||
"notified: a shipped notice does not claim the confirmation was sent",
|
||
rb ? rb->confirmationSentAt : std::string("<no order>"));
|
||
const std::optional<Server::OrderRecord> ra2 = Server::FindOrder(a.token);
|
||
Check(ra2 && ra2->confirmationSentAt == "2026-08-15T10:00:00Z",
|
||
"notified: and it does not disturb the order that was confirmed");
|
||
}
|
||
|
||
// ── a donation is income, never a sale ────────────────────────────────
|
||
//
|
||
// The donation flag decides three downstream behaviours at once (no invoice,
|
||
// the thank-you email, and WHICH /financials row the money lands in), so what
|
||
// is pinned here is the ledger's half: the flag round-trips through the
|
||
// writer and the fold, an old line without the key stays a sale, and
|
||
// SummarizeSales books each paid euro in exactly one row.
|
||
void ADonationIsIncomeNeverASale() {
|
||
const fs::path led = FreshLedger("donation");
|
||
|
||
Server::OrderRecord sale = Sample("6f6f6f6f6f6f6f6f6f6f6f6f6f6f6f6f", "ada@example.org");
|
||
Server::OrderRecord gift = Sample("7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a7a", "");
|
||
gift.product = "donation";
|
||
gift.donation = true;
|
||
gift.color.clear();
|
||
gift.buyer = { "", "", "", "", "", "" }; // nothing ships, nothing stored
|
||
gift.unitMinor = 2500;
|
||
gift.goodsMinor = 2500;
|
||
gift.shippingMinor = 0;
|
||
gift.totalMinor = 2500;
|
||
gift.vatIncluded = false;
|
||
Check(Server::CreateOrder(sale) && Server::CreateOrder(gift),
|
||
"donation: both records are written");
|
||
|
||
// The writer follows the omit-rather-than-empty rule: the key exists only
|
||
// on the donation's line, which is what keeps absent-means-false safe.
|
||
const std::string text = ReadAll(led);
|
||
Check(CountOf(text, R"("donation":true)") == 1,
|
||
"donation: the flag is written once, on the donation's line");
|
||
const std::string saleLine = LineContaining(text, sale.token);
|
||
Check(!saleLine.empty() && saleLine.find(R"("donation")") == std::string::npos,
|
||
"donation: a sale's line carries no donation key at all");
|
||
|
||
const std::optional<Server::OrderRecord> back = Server::FindOrder(gift.token);
|
||
Check(back.has_value() && back->donation,
|
||
"donation: the flag folds back in");
|
||
const std::optional<Server::OrderRecord> saleBack = Server::FindOrder(sale.token);
|
||
Check(saleBack.has_value() && !saleBack->donation,
|
||
"donation: a sale folds back as one");
|
||
|
||
// Both paid: each euro lands in exactly one summary row.
|
||
Check(Server::AppendOrderStatus(sale.token, "paid", "2026-08-17T10:00:00Z", "ideal")
|
||
&& Server::AppendOrderStatus(gift.token, "paid", "2026-08-17T10:01:00Z",
|
||
"eurc-base"),
|
||
"donation: both paid transitions append");
|
||
const Server::SalesSummary sum = Server::SummarizeSales(Server::ListOrders());
|
||
Check(sum.count == 1 && sum.totalMinor == 57830,
|
||
"donation: the sale row counts only the sale",
|
||
std::format("count {} total {}", sum.count, sum.totalMinor));
|
||
Check(sum.donationCount == 1 && sum.donationsMinor == 2500,
|
||
"donation: the donation row counts only the donation",
|
||
std::format("count {} total {}", sum.donationCount, sum.donationsMinor));
|
||
|
||
// An UNPAID donation is nothing yet — same ever-paid rule as sales.
|
||
const fs::path led2 = FreshLedger("donation-unpaid");
|
||
Server::OrderRecord pending = gift;
|
||
pending.token = "8b8b8b8b8b8b8b8b8b8b8b8b8b8b8b8b";
|
||
Check(Server::CreateOrder(pending), "donation: the unpaid donation is written");
|
||
const Server::SalesSummary none = Server::SummarizeSales(Server::ListOrders());
|
||
Check(none.donationCount == 0 && none.donationsMinor == 0,
|
||
"donation: an unpaid donation counts nothing");
|
||
}
|
||
|
||
// ── a transition keeps what it does not name ──────────────────────────
|
||
//
|
||
// paidVia is how the ledger shows at a glance which orders carry reversible
|
||
// card money for months — the stated reason `via` exists at all. Losing it on
|
||
// the next transition would erase that flag exactly when an order ships.
|
||
void ATransitionKeepsWhatItDoesNotName() {
|
||
const fs::path led = FreshLedger("status");
|
||
const Server::OrderRecord o = Sample("f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0", "ada@example.org");
|
||
Check(Server::CreateOrder(o), "status: the order is written");
|
||
|
||
Check(Server::AppendOrderStatus(o.token, "paid", "2026-08-15T09:00:00Z", "creditcard"),
|
||
"status: the paid transition is appended");
|
||
Check(Server::AppendOrderStatus(o.token, "shipped", "2026-08-15T11:00:00Z"),
|
||
"status: the shipped transition is appended");
|
||
|
||
const std::optional<Server::OrderRecord> r = Server::FindOrder(o.token);
|
||
Check(r.has_value(), "status: the order resolves");
|
||
if (r) {
|
||
Check(r->status == "shipped", "status: the latest transition wins", r->status);
|
||
Check(r->paidVia == "creditcard",
|
||
"status: the settlement method survives the next transition", r->paidVia);
|
||
Check(r->paidAt == "2026-08-15T09:00:00Z",
|
||
"status: shipping does not restate when it was paid", r->paidAt);
|
||
Check(r->updatedAt == "2026-08-15T11:00:00Z",
|
||
"status: updatedAt follows the transition", r->updatedAt);
|
||
}
|
||
|
||
// The writer omits the key entirely rather than writing an empty one:
|
||
// that is what makes "absent means unchanged" safe to rely on in the fold.
|
||
const std::string text = ReadAll(led);
|
||
const std::string shipped = LineContaining(text, R"("status":"shipped")");
|
||
Check(!shipped.empty(), "status: the shipped transition is on the file");
|
||
Check(shipped.find(R"("via")") == std::string::npos,
|
||
"status: a transition with no method writes no via key at all", shipped);
|
||
|
||
// An empty status is not a state. Applying it would leave the order
|
||
// invisible to the reconciler, the mailer and the invoice route at once.
|
||
AppendRaw(led,
|
||
R"({"type":"status","at":"2026-08-15T12:00:00Z",)"
|
||
R"("id":"f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0","status":""})");
|
||
const std::optional<Server::OrderRecord> blanked = Server::FindOrder(o.token);
|
||
Check(blanked && blanked->status == "shipped",
|
||
"status: an empty status is skipped, not applied",
|
||
blanked ? blanked->status : std::string("<no order>"));
|
||
Check(blanked && blanked->updatedAt == "2026-08-15T11:00:00Z",
|
||
"status: and it does not even move updatedAt",
|
||
blanked ? blanked->updatedAt : std::string("<no order>"));
|
||
|
||
// A transition naming an order that does not exist.
|
||
AppendRaw(led,
|
||
R"({"type":"status","at":"2026-08-15T13:00:00Z",)"
|
||
R"("id":"deadbeefdeadbeefdeadbeefdeadbeef","status":"cancelled"})");
|
||
const std::vector<Server::OrderRecord> all = Server::ListOrders();
|
||
Check(all.size() == 1, "status: an event for an unknown order conjures no record",
|
||
std::format("{} record(s)", all.size()));
|
||
if (all.size() == 1) {
|
||
Check(all[0].status == "shipped" && all[0].updatedAt == "2026-08-15T11:00:00Z",
|
||
"status: and leaves the order that does exist alone");
|
||
}
|
||
}
|
||
|
||
} // namespace
|
||
|
||
// ── a reference names one order ───────────────────────────────────────
|
||
//
|
||
// The reference is what a payer quotes and what a transfer is matched on, and
|
||
// it is only the first six hex digits of the token: 24 bits, where the token
|
||
// has 128. Two orders sharing one would both be paid for by a transfer that
|
||
// quotes it. So checkout mints its token through NewUniqueOrderToken, which
|
||
// reads every reference the ledger has ever held.
|
||
void AReferenceNamesOneOrder() {
|
||
FreshLedger("references");
|
||
|
||
Check(!Server::ReferenceInUse("CC-ABCDEF"),
|
||
"references: none is in use in an empty ledger");
|
||
|
||
const std::string token = "abcdef0123456789abcdef0123456789";
|
||
Check(Server::CreateOrder(Sample(token, "ada@example.org")),
|
||
"references: an order is written");
|
||
Check(Server::ReferenceInUse("CC-ABCDEF"),
|
||
"references: a stored order's reference is in use");
|
||
// The same reduction the transfer matcher applies: what settles as one
|
||
// reference is one reference here.
|
||
Check(Server::ReferenceInUse("cc abcdef"),
|
||
"references: however it is spelled");
|
||
Check(!Server::ReferenceInUse("CC-ABCDE0"),
|
||
"references: one character different is a different reference");
|
||
Check(!Server::ReferenceInUse(""), "references: an empty one is nobody's");
|
||
|
||
// Lapsed and cancelled orders keep theirs. The payment for an old order
|
||
// stays in the bank's history quoting it, so handing the reference to a
|
||
// new order would hand that order somebody else's money.
|
||
Check(Server::AppendOrderStatus(token, "cancelled", "2026-08-16T09:00:00Z"),
|
||
"references: the order is cancelled");
|
||
Check(Server::ReferenceInUse("CC-ABCDEF"),
|
||
"references: a cancelled order still holds its reference");
|
||
|
||
// The uniqueness check itself. From the real generator a collision is one
|
||
// draw in sixteen million, so checks that only draw from it pass with
|
||
// the check deleted (a reviewer deleted it, and they did). Here the draws
|
||
// are handed in. A draw that lands on a reference the LEDGER holds is thrown away,
|
||
// one that lands on a reference being HELD for a checkout in flight is
|
||
// thrown away, and a run of nothing but collisions ends in a refusal
|
||
// after a bounded number of draws rather than in a duplicate or a loop.
|
||
{
|
||
const std::string stored = "abcdef99999999999999999999999999"; // CC-ABCDEF, in the ledger
|
||
const std::string first = "0a0b0c11111111111111111111111111"; // CC-0A0B0C, free
|
||
const std::string again = "0a0b0c22222222222222222222222222"; // CC-0A0B0C, held by then
|
||
const std::string second = "0d0e0f33333333333333333333333333"; // CC-0D0E0F, free
|
||
|
||
int draws = 0;
|
||
const auto fromLedger = Server::NewUniqueOrderToken([&] {
|
||
return ++draws == 1 ? stored : first;
|
||
});
|
||
Check(fromLedger && *fromLedger == first,
|
||
"references: a draw that collides with a stored order is thrown away",
|
||
fromLedger ? *fromLedger : "nothing minted");
|
||
Check(draws == 2, "references: and exactly one more is drawn for it",
|
||
std::format("{} draws", draws));
|
||
|
||
draws = 0;
|
||
const auto fromHeld = Server::NewUniqueOrderToken([&] {
|
||
return ++draws == 1 ? again : second;
|
||
});
|
||
Check(fromHeld && *fromHeld == second,
|
||
"references: a draw that collides with a reference being held is thrown away",
|
||
fromHeld ? *fromHeld : "nothing minted");
|
||
|
||
draws = 0;
|
||
const auto none = Server::NewUniqueOrderToken([&] {
|
||
++draws;
|
||
return stored;
|
||
});
|
||
Check(!none.has_value(),
|
||
"references: nothing but collisions is refused, not minted",
|
||
none ? *none : "");
|
||
Check(draws == Server::kUniqueTokenDraws,
|
||
"references: after the bounded number of draws",
|
||
std::format("{} draws", draws));
|
||
}
|
||
|
||
// A minted token is well formed, never lands on a reference the ledger
|
||
// holds, and its reference is HELD from the moment it is handed out:
|
||
// checkout asks the payment rail for its link between minting and
|
||
// storing, and a second checkout minting in that gap must not be handed
|
||
// the same reference.
|
||
std::set<std::string> minted;
|
||
for (int i = 0; i < 64; ++i) {
|
||
const std::optional<std::string> fresh = Server::NewUniqueOrderToken();
|
||
Check(fresh.has_value(), "references: a token can be minted");
|
||
if (!fresh) break;
|
||
Check(IsOrderToken(*fresh), "references: a minted token has the token shape",
|
||
*fresh);
|
||
const std::string reference = Server::ReferenceFromToken(*fresh);
|
||
Check(reference != "CC-ABCDEF",
|
||
"references: a minted token does not reuse a stored reference", reference);
|
||
Check(minted.insert(reference).second,
|
||
"references: no two minted tokens share a reference", reference);
|
||
Check(Server::ReferenceInUse(reference),
|
||
"references: a minted reference is held before its order is stored",
|
||
reference);
|
||
}
|
||
|
||
// Storing the order moves the reference from held to the ledger, and it
|
||
// is in use either way.
|
||
if (!minted.empty()) {
|
||
const std::optional<std::string> fresh = Server::NewUniqueOrderToken();
|
||
if (fresh) {
|
||
Check(Server::CreateOrder(Sample(*fresh, "grace@example.org")),
|
||
"references: a minted order is written");
|
||
Check(Server::ReferenceInUse(Server::ReferenceFromToken(*fresh)),
|
||
"references: and its reference is in use from the ledger");
|
||
Check(Server::FindOrder(*fresh).has_value(),
|
||
"references: the order reads back under its token");
|
||
}
|
||
}
|
||
}
|
||
|
||
// ── the fold is kept, and the file still wins ─────────────────────────
|
||
//
|
||
// The order store holds the folded ledger in memory and folds it again only
|
||
// when the file changes. That is what keeps an order page or /financials from
|
||
// costing a parse of the whole history, and it is only acceptable if the held
|
||
// fold can never drift from the file. Three things can move the file under a
|
||
// running server, and each has to be seen on the very next call: the CLI
|
||
// appending from another process, an operator editing a field by hand, and
|
||
// the file being replaced outright. And what THIS process appends is applied
|
||
// to the held fold directly, so it has to come out identical to a fold of the
|
||
// file it was just written to.
|
||
bool SameOrders(const std::vector<Server::OrderRecord>& a,
|
||
const std::vector<Server::OrderRecord>& b) {
|
||
if (a.size() != b.size()) return false;
|
||
for (std::size_t i = 0; i < a.size(); ++i) {
|
||
const Server::OrderRecord& x = a[i];
|
||
const Server::OrderRecord& y = b[i];
|
||
if (x.token != y.token || x.reference != y.reference || x.status != y.status
|
||
|| x.updatedAt != y.updatedAt || x.paidVia != y.paidVia
|
||
|| x.paidAt != y.paidAt || x.invoiceNumber != y.invoiceNumber
|
||
|| x.invoicedAt != y.invoicedAt
|
||
|| x.confirmationSentAt != y.confirmationSentAt
|
||
|| x.buyer.name != y.buyer.name || x.buyer.email != y.buyer.email
|
||
|| x.totalMinor != y.totalMinor || x.donation != y.donation) {
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
void TheHeldFoldFollowsTheFile() {
|
||
const fs::path led = FreshLedger("held-fold");
|
||
const std::string a = "aaaa0000aaaa0000aaaa0000aaaa0000";
|
||
const std::string b = "bbbb1111bbbb1111bbbb1111bbbb1111";
|
||
|
||
Check(Server::CreateOrder(Sample(a, "ada@example.org")), "held: an order is written");
|
||
Check(Server::FindOrder(a).has_value(), "held: and read back"); // now held
|
||
|
||
// Another process appends. `--orders FILE --mark-paid TOKEN` does exactly
|
||
// this while the server runs, and the mailer must see the paid order on
|
||
// its next sweep without anyone restarting anything.
|
||
AppendRaw(led, std::format(
|
||
R"({{"type":"status","at":"2026-08-15T10:00:00Z","id":"{}","status":"paid","via":"sepa"}})", a));
|
||
{
|
||
const auto o = Server::FindOrder(a);
|
||
Check(o && o->status == "paid" && o->paidVia == "sepa",
|
||
"held: an event another process appended is seen on the next call",
|
||
o ? o->status : "order missing");
|
||
}
|
||
|
||
// An edit by hand that keeps the file's size, which is what blanking a
|
||
// name in place is. Size cannot show it, so the modification time has to.
|
||
// The time is SET here rather than waited for, so the check does not
|
||
// depend on how finely the filesystem under the scratch directory
|
||
// happens to stamp a write made a moment after the last one.
|
||
{
|
||
std::string text = ReadAll(led);
|
||
const std::size_t at = text.find("Ada Lovelace");
|
||
Check(at != std::string::npos, "held: the ledger holds the name as written");
|
||
if (at != std::string::npos) {
|
||
const fs::file_time_type stamped = fs::last_write_time(led);
|
||
text.replace(at, 12, "xxxxxxxxxxxx");
|
||
{
|
||
std::ofstream out(led, std::ios::trunc | std::ios::binary);
|
||
out << text;
|
||
}
|
||
fs::last_write_time(led, stamped + std::chrono::seconds(2));
|
||
const auto o = Server::FindOrder(a);
|
||
Check(o && o->buyer.name == "xxxxxxxxxxxx",
|
||
"held: an in-place edit that keeps the size is seen by its time",
|
||
o ? o->buyer.name : "order missing");
|
||
}
|
||
}
|
||
|
||
// The file replaced by a different one, as a restored backup would be.
|
||
WriteLedger(led, {
|
||
R"({"type":"order","at":"2026-08-15T09:30:00Z","id":"bbbb1111bbbb1111bbbb1111bbbb1111","ref":"CC-BBBB11","email":"grace@example.org","total_minor":4200})",
|
||
});
|
||
Check(!Server::FindOrder(a).has_value(),
|
||
"held: an order the file no longer has is gone from the fold");
|
||
Check(Server::FindOrder(b).has_value(), "held: and the file's own order is there");
|
||
Check(Server::ReferenceInUse("CC-BBBB11") && !Server::ReferenceInUse("CC-AAAA00"),
|
||
"held: the references in use follow the file too");
|
||
|
||
// This process's own appends, one of each event, are applied to the held
|
||
// fold as they are written. Dropping what is held and folding the file
|
||
// from disk has to give the same orders, field for field.
|
||
Check(Server::AppendOrderStatus(b, "paid", "2026-08-15T11:00:00Z", "ideal"),
|
||
"held: a status event is appended");
|
||
Check(Server::AssignInvoiceNumber(b, "2026-08-15T11:00:01Z").has_value(),
|
||
"held: an invoice event is appended");
|
||
Check(Server::AppendOrderNotified(b, "2026-08-15T11:00:02Z"),
|
||
"held: a notified event is appended");
|
||
Check(Server::CreateOrder(Sample(a, "ada@example.org")),
|
||
"held: an order event is appended");
|
||
Check(Server::AppendOrderStatus(a, "cancelled", "2026-08-15T12:00:00Z"),
|
||
"held: a second order's status is appended");
|
||
{
|
||
const std::vector<Server::OrderRecord> held = Server::ListOrders();
|
||
const Server::SalesSummary heldSales = Server::LedgerSales();
|
||
Server::SetOrdersPath(led); // drops what is held; the next call folds the file
|
||
const std::vector<Server::OrderRecord> fresh = Server::ListOrders();
|
||
Check(held.size() == 2 && fresh.size() == 2,
|
||
"held: both orders are in the fold either way",
|
||
std::format("{} held, {} from disk", held.size(), fresh.size()));
|
||
Check(SameOrders(held, fresh),
|
||
"held: the fold kept across this process's appends is the fold of the file");
|
||
const Server::SalesSummary summed = Server::SummarizeSales(fresh);
|
||
Check(heldSales.count == summed.count && heldSales.totalMinor == summed.totalMinor
|
||
&& heldSales.donationCount == summed.donationCount
|
||
&& heldSales.donationsMinor == summed.donationsMinor
|
||
&& summed.count == 1 && summed.totalMinor == 4200,
|
||
"held: LedgerSales is SummarizeSales over the ledger",
|
||
std::format("{} sale(s), {}", heldSales.count, heldSales.totalMinor));
|
||
}
|
||
|
||
// A line cut short by a crash, with no line break after it. The reader
|
||
// has always skipped such a line. What it did NOT survive was the next
|
||
// append: written straight onto the end of the broken line, the new
|
||
// event became part of it and was skipped along with it, so a crash cost
|
||
// two events and the second could be somebody's "paid". The writer now
|
||
// starts a line of its own.
|
||
{
|
||
{
|
||
std::ofstream out(led, std::ios::app | std::ios::binary);
|
||
out << R"({"type":"status","at":"2026-08-15T13:00:00Z","id":")" << b
|
||
<< R"(","sta)";
|
||
}
|
||
Check(Server::AppendOrderStatus(b, "shipped", "2026-08-15T14:00:00Z"),
|
||
"torn: an event is appended after a line cut short");
|
||
const auto o = Server::FindOrder(b);
|
||
Check(o && o->status == "shipped",
|
||
"torn: the event after a broken line is not lost with it",
|
||
o ? o->status : "order missing");
|
||
Server::SetOrdersPath(led);
|
||
const auto again = Server::FindOrder(b);
|
||
Check(again && again->status == "shipped",
|
||
"torn: and a fold from disk reads it the same",
|
||
again ? again->status : "order missing");
|
||
}
|
||
|
||
// The index. Orders are found through a map from token to position, not
|
||
// by walking the list, so what has to hold is that every event lands on
|
||
// its own order and on no other, whatever came between.
|
||
{
|
||
FreshLedger("held-many");
|
||
constexpr int kOrders = 300;
|
||
std::vector<std::string> tokens;
|
||
for (int i = 0; i < kOrders; ++i) {
|
||
tokens.push_back(std::format("{:08x}{:024x}", i * 7919 + 1, 0));
|
||
Server::CreateOrder(Sample(tokens.back(), std::format("u{}@example.org", i)));
|
||
}
|
||
for (int i = 0; i < kOrders; i += 3) {
|
||
Server::AppendOrderStatus(tokens[i], "paid", "2026-08-15T15:00:00Z", "sepa");
|
||
}
|
||
int right = 0;
|
||
for (int i = 0; i < kOrders; ++i) {
|
||
const auto o = Server::FindOrder(tokens[i]);
|
||
const std::string_view want = (i % 3 == 0) ? "paid" : "awaiting_payment";
|
||
if (o && o->token == tokens[i] && o->status == want
|
||
&& o->buyer.email == std::format("u{}@example.org", i)) {
|
||
++right;
|
||
}
|
||
}
|
||
Check(right == kOrders,
|
||
"index: every order is found under its own token, with its own events",
|
||
std::format("{} of {}", right, kOrders));
|
||
Check(Server::ListOrders().size() == static_cast<std::size_t>(kOrders),
|
||
"index: the list holds each order once");
|
||
Check(!Server::FindOrder(std::string(32, 'f')).has_value(),
|
||
"index: a token no order has finds nothing");
|
||
}
|
||
}
|
||
|
||
// ── appends from several threads, and from outside, at once ───────────
|
||
//
|
||
// The server appends from the reconciler, the mailer and every request
|
||
// thread, while the CLI may append from another process. An append by this
|
||
// process is applied to the held fold directly, which is only right if the
|
||
// file grew by exactly what was written: if another writer's line landed in
|
||
// the same instant, it is in the file and not in the fold, and the fold has
|
||
// to be redone. Nothing sequential can show that, so this races them. It can
|
||
// only fail when something is wrong (when all is well the held fold equals
|
||
// the file whatever the interleaving), and a reviewer who removed the
|
||
// "grew by exactly what was written" check saw it fail.
|
||
void AppendsFromManyWritersAllLand() {
|
||
const fs::path led = FreshLedger("held-threads");
|
||
constexpr int kThreads = 3;
|
||
constexpr int kRounds = 150;
|
||
|
||
// The outside writer's orders, created before the race so its status
|
||
// events always have an order to land on.
|
||
std::vector<std::string> outside;
|
||
for (int i = 0; i < 6; ++i) {
|
||
outside.push_back(std::format("e{:07x}{:024x}", i, 0));
|
||
Server::CreateOrder(Sample(outside.back(), "outside@example.org"));
|
||
}
|
||
|
||
std::atomic<int> appended{0};
|
||
std::atomic<int> unseen{0};
|
||
{
|
||
std::vector<std::jthread> writers;
|
||
for (int t = 0; t < kThreads; ++t) {
|
||
writers.emplace_back([&, t] {
|
||
std::vector<std::string> mine;
|
||
for (int i = 0; i < kRounds; ++i) {
|
||
if (mine.empty() || i % 5 == 0) {
|
||
mine.push_back(std::format("{:x}{:07x}{:024x}", t, i, 0));
|
||
if (!Server::CreateOrder(Sample(mine.back(), "t@example.org"))) continue;
|
||
++appended;
|
||
if (!Server::FindOrder(mine.back())) ++unseen;
|
||
} else {
|
||
const std::string& token = mine[static_cast<std::size_t>(i) % mine.size()];
|
||
const std::string at = std::format("T{}-{}", t, i);
|
||
if (!Server::AppendOrderStatus(token, "paid", at, "sepa")) continue;
|
||
++appended;
|
||
const auto o = Server::FindOrder(token);
|
||
if (!o || o->updatedAt != at) ++unseen;
|
||
}
|
||
}
|
||
});
|
||
}
|
||
// The other process: whole lines written straight to the file, the
|
||
// way `--orders FILE --mark-shipped` writes them.
|
||
writers.emplace_back([&] {
|
||
for (int i = 0; i < kRounds; ++i) {
|
||
const std::string& token = outside[static_cast<std::size_t>(i) % outside.size()];
|
||
const std::string at = std::format("X-{}", i);
|
||
AppendRaw(led, std::format(
|
||
R"({{"type":"status","at":"{}","id":"{}","status":"shipped"}})", at, token));
|
||
const auto o = Server::FindOrder(token);
|
||
if (!o || o->updatedAt != at) ++unseen;
|
||
}
|
||
});
|
||
}
|
||
|
||
Check(unseen.load() == 0,
|
||
"threads: every event is in the fold on the call after it was appended",
|
||
std::format("{} not seen", unseen.load()));
|
||
|
||
const std::vector<Server::OrderRecord> held = Server::ListOrders();
|
||
const std::string text = ReadAll(led);
|
||
const auto lines = std::ranges::count(text, '\n');
|
||
const auto expected = 6 + appended.load() + kRounds;
|
||
Check(lines == expected, "threads: the file holds one line for every append",
|
||
std::format("{} lines, {} appends", lines, expected));
|
||
|
||
Server::SetOrdersPath(led); // drop what is held and fold the file itself
|
||
Check(SameOrders(held, Server::ListOrders()),
|
||
"threads: the fold held through the race is the fold of the file");
|
||
|
||
// The case the race above almost never produces, and the one that
|
||
// matters. There, the outside writer reads after every line it writes,
|
||
// which waits for the ledger lock, so its next line tends to land while
|
||
// the other threads are still waking up: before their append has looked
|
||
// at the file, where the ordinary "the file changed, fold again" covers
|
||
// it. (That race passed with the size check removed, which is how this
|
||
// was found.)
|
||
//
|
||
// What needs the size check is ONE outside line landing in the middle of
|
||
// a run of this process's own appends, with nothing from outside after
|
||
// it. An append takes the file's stamp when it is done; if it took it
|
||
// without noticing that the file grew by more than it wrote, the outside
|
||
// line is under that stamp, in the file and not in the fold, and every
|
||
// later own append keeps it that way. So each round here is a fresh
|
||
// ledger, two threads appending without pause, and a single outside line
|
||
// dropped in at a different moment each time, and the fold held at the
|
||
// end is compared with the file. Correct code cannot fail this however
|
||
// the threads fall; code without the check fails it within a few rounds.
|
||
constexpr int kRaces = 120;
|
||
int stale = 0;
|
||
for (int race = 0; race < kRaces; ++race) {
|
||
const fs::path p = FreshLedger("held-race");
|
||
const std::string solo = std::format("f{:07x}{:024x}", race, 0);
|
||
Server::CreateOrder(Sample(solo, "outside@example.org"));
|
||
std::atomic<int> started{0};
|
||
{
|
||
std::vector<std::jthread> writers;
|
||
for (int t = 0; t < 2; ++t) {
|
||
writers.emplace_back([&, t] {
|
||
const std::string mine = std::format("{:x}{:07x}{:024x}", t, race, 0);
|
||
Server::CreateOrder(Sample(mine, "t@example.org"));
|
||
++started;
|
||
for (int i = 0; i < 80; ++i) {
|
||
Server::AppendOrderStatus(mine, "paid",
|
||
std::format("T{}-{}", t, i), "sepa");
|
||
}
|
||
});
|
||
}
|
||
writers.emplace_back([&] {
|
||
while (started.load() < 2) std::this_thread::yield();
|
||
std::this_thread::sleep_for(std::chrono::microseconds((race * 37) % 1500));
|
||
AppendRaw(p, std::format(
|
||
R"({{"type":"status","at":"X-{}","id":"{}","status":"shipped"}})",
|
||
race, solo));
|
||
});
|
||
}
|
||
const std::vector<Server::OrderRecord> kept = Server::ListOrders();
|
||
Server::SetOrdersPath(p);
|
||
if (!SameOrders(kept, Server::ListOrders())) ++stale;
|
||
}
|
||
Check(stale == 0,
|
||
"threads: a line from outside amid this process's own appends is never missed",
|
||
std::format("missed in {} of {} races", stale, kRaces));
|
||
}
|
||
|
||
int main() {
|
||
std::error_code ec;
|
||
// The suites run in parallel, so the scratch directory has to be unique
|
||
// per run rather than merely per suite.
|
||
gWork = fs::temp_directory_path(ec)
|
||
/ std::format("catcrafts-ledger-{}", Server::NewOrderToken());
|
||
fs::create_directories(gWork, ec);
|
||
if (ec) {
|
||
std::println(std::cerr, "could not create {}: {}", gWork.string(), ec.message());
|
||
return 1;
|
||
}
|
||
|
||
BuyerTextStaysInItsField();
|
||
TheSaleIsTheFirstPaidEvent();
|
||
InvoiceNumbersAreASequence();
|
||
OneBadLineIsOnlyOneBadLine();
|
||
HistoryIsNotRewritten();
|
||
AnOlderLedgerStillReads();
|
||
ADonationIsIncomeNeverASale();
|
||
OnlyAConfirmationMarksTheEmailSent();
|
||
ATransitionKeepsWhatItDoesNotName();
|
||
AReferenceNamesOneOrder();
|
||
TheHeldFoldFollowsTheFile();
|
||
AppendsFromManyWritersAllLand();
|
||
|
||
fs::remove_all(gWork, ec);
|
||
|
||
if (failures != 0) {
|
||
std::println(std::cerr, "{} check(s) failed", failures);
|
||
return 1;
|
||
}
|
||
return 0;
|
||
}
|