bank: a payment is spent once

A payer could place several orders and send one transfer naming all of
them. Each order looked for its own reference, found it, and saw the
full amount, so every order settled on one payment: three EUR 5 orders
on a single EUR 5 credit, reproduced. The rail logged "settle this one
by hand" and settled each of them. The README called it a known
limitation; it was a way to pay once for several orders.

The rail now records which payment settled which order, by the bank's
id, in <credits>.claims, before it reports the order paid, and a claimed
payment is left out of every other order's sum. The record cannot live
in the credits file, which the pull replaces every hour. It is read back
at start. If it cannot be written the rail settles nothing and says why;
if it is there and cannot be read the rail does not start. A claims
file that ends mid-line gets its line break before the next claim, or
that claim would be lost at the next start and the payment free again.

It does not rest on what a description looks like, because the payer
writes it. The "also quotes another reference" flag stays as a pointer
in the log and decides nothing: a name followed by a date has the same
shape. A payer who really sends one transfer for two orders gets the
first settled and the other by hand, with one line in the journal.

One payment is also counted once however many lines of the credits file
carry its id. Two lines are not meant to happen and were reproduced all
the same (one callback delivered on two connections at once passed the
id check twice), and summed they settle an order on half its price. The
callback's look-then-append is now under a lock as well.

ShouldMatchBankTransfers drives the matcher and the rail itself across
a restart, a torn claims file, an unreadable one and a duplicated line;
ShouldSettleBankTransfers shows over HTTP that one of three orders
settles.

Found in the security review of 2026-11-26; the torn claims file and
the duplicate line by the adversarial review of the first fix.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
This commit is contained in:
Jorijn van der Graaf 2026-11-26 11:05:35 +01:00
commit 3c61116531
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
4 changed files with 683 additions and 23 deletions

View file

@ -275,5 +275,89 @@ int main(int argc, char** argv) {
"the order's own credit still settles it afterwards");
}
// ── one payment pays for one order ────────────────────────────────
//
// Three orders, then ONE transfer for the price of one whose text names
// all three. Each order looks for its own reference and finds the full
// amount against it, so before the rail recorded what a payment had been
// spent on, all three settled: three thank-you pages, three entries in
// the public donations total, one payment. Found in review on 2026-11-26.
//
// Over HTTP and not only in ShouldMatchBankTransfers, because the rule is
// not in the matcher alone: it needs the rail to write the claim when it
// settles and to judge the next order against it, across separate polls
// on separate threads. That wiring is what this proves.
{
const std::array<Order, 3> orders = {
PlaceDonation(srv, "5.00"), PlaceDonation(srv, "5.00"),
PlaceDonation(srv, "5.00") };
for (const Order& o : orders) {
if (o.token.empty()) return Finish();
}
Credit(credits, "e2e-4",
std::format("{} {} {}", orders[0].reference, orders[1].reference,
orders[2].reference),
500);
auto settled = [&](const Order& o) {
return srv.Body("/order/" + o.token).find(kPaidCopy) != std::string::npos;
};
auto settledCount = [&] {
int n = 0;
for (const Order& o : orders) n += settled(o) ? 1 : 0;
return n;
};
int paid = 0;
for (int i = 0; i < 120 && paid == 0; ++i) {
paid = settledCount();
if (paid == 0) std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
Check(paid >= 1, "a payment naming three orders settles one of them");
// Long enough for several sweeps to have judged the other two against
// the same credit, which is still sitting in the file naming them.
std::this_thread::sleep_for(std::chrono::seconds(4));
paid = settledCount();
Check(paid == 1, "and only one: the same money does not settle the others",
std::format("{} of 3 settled", paid));
// The two it did not pay for read as plainly unpaid. Not "part
// payment received": that would be counting the spent money toward
// them after all, only short of the total.
const Order* passedOver = nullptr;
for (const Order& o : orders) {
if (settled(o)) continue;
if (!passedOver) passedOver = &o;
srv.BodyHas("/order/" + o.token, "awaiting payment",
"an order the payment did not settle still reads as unpaid");
srv.BodyLacks("/order/" + o.token, "part payment received",
"and is not shown the spent money as a part payment");
}
// What was spent is on disk, next to the credits, so a restart does
// not forget it, and the log says why two orders are still waiting.
const std::string claims = ReadFile(fs::path(credits.string() + ".claims"));
Check(CountOccurrences(claims, "\"credit\":\"e2e-4\"") == 1,
"the settlement recorded one claim for that payment",
std::format("{} claim line(s) for it",
CountOccurrences(claims, "\"credit\":\"e2e-4\"")));
Check(ReadFile(srv.Work() / "server.log").find("one payment pays for one order")
!= std::string::npos,
"the log says why the other orders are not settled");
// An order that was passed over is not stuck: a payment of its own
// settles it, and the third still waits.
if (passedOver) {
Credit(credits, "e2e-4b", passedOver->reference, 500);
const std::string body =
srv.WaitForBody("/order/" + passedOver->token, kPaidCopy, 120);
Check(body.find(kPaidCopy) != std::string::npos,
"an order passed over settles on a payment of its own");
Check(settledCount() == 2, "and the third order is still unpaid",
std::format("{} of 3 settled", settledCount()));
}
}
return Finish();
}