fix(core): foreign amounts and rates take at most 10 places (D-75)
The K-U1 review: foreign 0.99999999999999999999999999 (26 places) x rate
0.005 into USD booked 0.01, though the exact product
0.00499999999999999999999999995 is under half a cent. Decimal::checked_mul
rounds a product finer than 28 places to 28 before fx_booked rounds it to
the cent: two roundings, one cent off. A ledger in one money books the
exact product rounded once (BS-ledger-1, BS-ledger-14).
Owner decision D-75 (2026-11-08): a new line's foreign amount and its rate
take at most MAX_LINE_SCALE (10) decimal places each, as a base amount does
(B-28), so their product has at most 20 places, under Decimal's 28.
Ledger::add_line_fx refuses past it with ValueOutOfRange ("foreign amount
..." / "FX rate ..."; the variant for an amount that is not a journal
line's), trailing zeros not counted, before computing, and never on replay
(is_replaying; replay_add_line_fx books a journal's older line verbatim).
Tests: 11-place foreign amount and rate refused; ten and ten accepted with
hand-checked books; the review's input refused, not mis-booked; trailing
zeros are not places; replay keeps a 26-place foreign line in core, and a
store journal holding one opens with its figures.
fx_booked's spec block states the precondition (scales within 10 each, and
the mantissas' product within Decimal's 96 bits) and that the result is
exact under it, and documents Decimal::checked_mul's behaviours outside it
(rounding past scale 28 and past 96 bits; 1e-28 x 1e-28 underflowing to
zero); invariant 1 now says None when T's checked_mul cannot hold the
product. The bridge covers every D-75 scale (0..=10 each, products to 20
places) against an independent exact reference, shows 26 places is where the
reference and Decimal part (and the engine refuses), and pins an open edge
this bridge found: ten places each still allow a 20-place product past
~7.9e8 that outgrows 96 bits (1050000010.0000000001 x 9.9999999999 books
10500000099.90, exact 10500000099.89). B-28 and B-3 claims record the
enforcement and checks.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bf2fe200c2
commit
c5a4894985
7 files changed
+394
-33
No files matched your search
@@ -1792,6 +1792,44 @@ mod refusals {
|
||||
}
|
||||
}
|
||||
|
||||
/// D-75: the ten-place bound on a foreign amount is the command's, never
|
||||
/// replay's (B-28). A journal an earlier version wrote, holding a line with
|
||||
/// a 26-place foreign amount, opens with its figures exactly as journaled.
|
||||
#[test]
|
||||
fn a_journaled_foreign_line_finer_than_ten_places_still_opens() {
|
||||
use serde_json::json;
|
||||
let dir = test_dir("fx-fine-foreign");
|
||||
let mut store = Store::create(&dir, Currency::USD).unwrap();
|
||||
let cash = store
|
||||
.add_account("1000", "Cash", AccountType::Asset, None, false)
|
||||
.unwrap();
|
||||
drop(store);
|
||||
let mut w = JournalWriter::open(&dir.join(ledger_store::JOURNAL_FILE)).unwrap();
|
||||
w.append(
|
||||
"entry.created",
|
||||
json!({"id": 1, "date": "2026-03-01", "description": "old fx"}),
|
||||
)
|
||||
.unwrap();
|
||||
w.append(
|
||||
"entry.line_added",
|
||||
json!({"id": 1, "account": cash, "side": "debit", "amount": "0.01", "currency": "USD",
|
||||
"foreign_currency": "EUR", "foreign_amount": "0.99999999999999999999999999",
|
||||
"fx_rate": "0.005"}),
|
||||
)
|
||||
.unwrap();
|
||||
drop(w);
|
||||
let _ = std::fs::remove_file(dir.join(ledger_store::db_file()));
|
||||
let store = Store::open(&dir).expect("the older foreign line still opens");
|
||||
store.verify().unwrap();
|
||||
let lines = store.ledger().get_entry(1).unwrap().lines();
|
||||
assert_eq!(lines.len(), 1);
|
||||
assert_eq!(lines[0].amount.amount, "0.01".parse::<Decimal>().unwrap());
|
||||
assert_eq!(
|
||||
lines[0].foreign_amount,
|
||||
Some("0.99999999999999999999999999".parse::<Decimal>().unwrap())
|
||||
);
|
||||
}
|
||||
|
||||
/// Contract (2026-10-13): rules added to the engine never lock anyone out
|
||||
/// of books written before them. A journal holding what the new rules
|
||||
/// refuse (a child account of another type, partially overlapping periods,
|
||||
|
||||
Reference in new issue
Block a user