From f9affd0e1f60fe3ebbada1873690e73ff6c0b232 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Sun, 8 Nov 2026 15:11:36 -0700 Subject: [PATCH] refactor(core): foreign currency conversion, fx_booked, in the kernel The one FX conversion that books money (add_line_fx: foreign x rate, rounded once to the base currency's minor unit, half away from zero, B-3 / BS-ledger-1 and its CHOICE BS-ledger-14) was inline in ledger.rs. It now lives in kernel/fx.rs as fx_booked (exact checked product, one round_step; None on a non-positive rate or overflow) with fx_booked_minor as its Decimal/currency spelling, which also keeps the pre-kernel scale and books the exact product for a currency with no minor unit. add_line_fx keeps its own error variants and refusals, so behaviour is identical for every input; every test and golden passes unchanged. Grep found no other FX conversion site: set/get_fx_rate only record a rate, the store delegates to the core, and replay books a recorded amount verbatim. Co-Authored-By: Muse Wonder 1.0 --- crates/ledger-core/src/kernel/fx.rs | 108 ++++++++++++++++++++++++++-- crates/ledger-core/src/ledger.rs | 13 ++-- 2 files changed, 110 insertions(+), 11 deletions(-) diff --git a/crates/ledger-core/src/kernel/fx.rs b/crates/ledger-core/src/kernel/fx.rs index 11680873..6681c51f 100644 --- a/crates/ledger-core/src/kernel/fx.rs +++ b/crates/ledger-core/src/kernel/fx.rs @@ -1,5 +1,103 @@ -//! U1: foreign-currency conversion, the booking of a foreign amount at the transaction-date rate (kernel sweep). -//! -//! Empty in U0: the unit adds its arithmetic here, in this file alone, so -//! no two sweep units ever edit the same file. The module root re-exports -//! it, keeping every path `crate::kernel::foo`. +//! U1: foreign-currency conversion, the booking of a foreign amount at +//! the transaction-date rate (kernel sweep, `plan/kernel-sweep-audit.md` +//! section 3). The one place foreign x rate becomes booked money: the +//! product is computed exactly and rounded once, by [`round_step`], to +//! the base currency's smallest unit. No call site multiplies by a rate +//! itself. + +use super::round::{Rounding, round_step}; +use crate::Currency; +use crate::kernel::Amount; +use rust_decimal::Decimal; + +/// The booked base amount for a foreign amount at a rate: `foreign x +/// rate` computed exactly, rounded once to a whole multiple of `step` +/// (the base currency's minor unit) in `mode`. +/// +/// The rate's precision is carried by the value itself: a `Decimal` +/// rate keeps its own scale (1.0834567 is exact), and the product is the +/// exact `Decimal` product before the single rounding, exactly as +/// `Ledger::add_line_fx` computed it before this kernel existed. At +/// `i64` (the Kani model) amounts and the rate are plain integers in +/// the smallest unit, and the same law is proved of the product. +/// +/// ```text +/// GOAL: book a foreign amount in the ledger's one money: the exact +/// product foreign x rate, rounded once to the base currency's +/// smallest unit, half away from zero. +/// SOURCE: bookspec BS-ledger-1 (one money; a foreign amount at the +/// transaction-date rate, ASC 830-20-30-1; the smallest unit +/// from ISO 4217), and its CHOICE BS-ledger-14 +/// "book-in-the-smallest-unit" (the owner, 2026-10-13: book in +/// the smallest unit, half away from zero); wanderbooks +/// docs/spec/claims/ledger.md B-3 states the same pick. The +/// rounding itself is kernel::round_step (BS-ledger-1, +/// BS-entry-3); owner decision D-69 (the laws live once, in +/// bookspec's Lean). +/// INVARIANTS: +/// 1. Some(r) is a whole multiple of step: r = n x step for an +/// integer n. None when step <= 0, when rate <= 0, or when the +/// exact product or the rounded result cannot be held. +/// 2. In HalfAwayFromZero (the mode add_line_fx books in) r is +/// within half a step of the exact product foreign x rate, an +/// exact half going to the multiple farther from zero. +/// 3. Positive foreign and rate give a non-negative r, and a +/// positive r whenever the exact product reaches half a step +/// (a smaller product rounds to zero, which the caller refuses +/// as it does today). The sign never crosses zero. +/// 4. On Decimal the value equals the pre-kernel computation +/// (checked_mul, then round_minor half away from zero) exactly; +/// fx_booked_minor also keeps its scale. +/// SCENARIOS: +/// 1. IF 100.00 at 1.0834567 to the cent: SHOULD book 108.35. +/// 2. IF 10.00 at 151.237 to the yen (step 1): SHOULD book 1512. +/// 3. IF the rate is zero or negative: SHOULD be None, never a guess. +/// 4. IF foreign x rate overflows: SHOULD be None, never a panic. +/// ``` +pub fn fx_booked(foreign: T, rate: T, step: T, mode: Rounding) -> Option { + if rate <= T::ZERO { + return None; + } + let exact = foreign.checked_mul(rate)?; + round_step(exact, step, mode) +} + +/// [`fx_booked`] at a currency's minor unit, on `Decimal`: the spelling +/// `Ledger::add_line_fx` books through. A currency ISO 4217 gives no +/// minor unit (gold, SDRs) books the exact product, as `round_minor` +/// has always done (B-3). The result's scale is the pre-kernel +/// computation's: `round_places` keeps an amount already at or under +/// the minor places unchanged, and otherwise carries exactly the minor +/// places (a booked 1.50 prints "1.50", never "1.5000"). +pub fn fx_booked_minor( + foreign: Decimal, + rate: Decimal, + currency: Currency, + mode: Rounding, +) -> Option { + let Some(places) = currency.minor_units() else { + if rate <= Decimal::ZERO { + return None; + } + return foreign.checked_mul(rate); + }; + let step = Decimal::try_from_i128_with_scale(1, places).ok()?; + let rounded = fx_booked(foreign, rate, step, mode)?; + if rounded.is_zero() { + // The old round_places scale for a zero (the caller refuses it). + return Decimal::try_from_i128_with_scale(0, places).ok(); + } + if rounded.scale() != places { + // fx_booked returns an exact multiple unchanged, trailing zeros + // and all; the old computation carried exactly the minor places + // whenever the product was finer than them. The multiple is + // exact at `places`, so this rescale rounds nothing. + let exact = foreign.checked_mul(rate)?; + if exact.scale() > places { + let mut rounded = rounded; + rounded.rescale(places); + return Some(rounded); + } + } + Some(rounded) +} diff --git a/crates/ledger-core/src/ledger.rs b/crates/ledger-core/src/ledger.rs index 6358a1c1..8fc03d64 100644 --- a/crates/ledger-core/src/ledger.rs +++ b/crates/ledger-core/src/ledger.rs @@ -1864,15 +1864,16 @@ impl Ledger { found: foreign_currency.code().to_string(), }); } - let exact = foreign_amount - .checked_mul(fx_rate) - .ok_or(LedgerError::AmountOutOfRange)?; // Booked in the base currency's smallest unit (cents for USD), half // away from zero: a ledger in one money holds no fractions of its // smallest unit (rules.md B-3; DECISION, owner 2026-10-13, after - // the GnuCash cross-check found the difference). - let base_amount = crate::kernel::round_minor( - exact, + // the GnuCash cross-check found the difference). The kernel's + // fx_booked_minor is that computation (foreign x rate exactly, + // rounded once); the rate and zero-result refusals above and + // below keep their error variants. + let base_amount = crate::kernel::fx_booked_minor( + foreign_amount, + fx_rate, self.base_currency, crate::kernel::Rounding::HalfAwayFromZero, )