diff --git a/crates/ledger-core/src/ledger.rs b/crates/ledger-core/src/ledger.rs index f7dcd0bc..95f5cf4c 100644 --- a/crates/ledger-core/src/ledger.rs +++ b/crates/ledger-core/src/ledger.rs @@ -8533,7 +8533,7 @@ impl Ledger { } let entry = self.try_create_entry(date, &format!("Payroll tax remittance {date}"))?; for (account, side, amount) in - payroll_remit_plan(&payables, cash_account, self.base_currency) + payroll_remit_plan(&payables, cash_account, self.base_currency)? { self.add_line(entry, account, side, amount)?; } diff --git a/crates/ledger-core/src/payroll.rs b/crates/ledger-core/src/payroll.rs index 004eff86..4b581a77 100644 --- a/crates/ledger-core/src/payroll.rs +++ b/crates/ledger-core/src/payroll.rs @@ -213,24 +213,28 @@ pub fn payroll_entry_plan( /// The remittance entry's lines: Dr each payable with a non-zero balance, /// Cr cash for the total. Balances are normal-side (positive = owed), so /// the plan balances by construction. +/// +/// Refused (TooLargeToCompute) when the payables' total is past exact +/// arithmetic, never a panic: the total is the kernel's checked fold, so +/// this function does not depend on its callers having refused first. pub fn payroll_remit_plan( payables: &[(AccountId, Decimal)], cash_account: AccountId, base: Currency, -) -> Vec<(AccountId, Side, Money)> { +) -> Result, LedgerError> { + let total = crate::kernel::total_of(payables.iter(), |(_, b)| *b) + .ok_or_else(|| LedgerError::TooLargeToCompute("the payroll taxes payable".into()))?; let mut plan = Vec::new(); - let mut total = Decimal::ZERO; for (account, balance) in payables { if balance.is_zero() { continue; } plan.push((*account, Side::Debit, Money::new(*balance, base))); - total += *balance; } if !total.is_zero() { plan.push((cash_account, Side::Credit, Money::new(total, base))); } - plan + Ok(plan) } #[cfg(test)] diff --git a/crates/ledger-core/tests/spec_assets_payroll.rs b/crates/ledger-core/tests/spec_assets_payroll.rs index d8e304ec..9e5a4df4 100644 --- a/crates/ledger-core/tests/spec_assets_payroll.rs +++ b/crates/ledger-core/tests/spec_assets_payroll.rs @@ -1029,7 +1029,8 @@ fn remit_plan_hand_worked() { &[(3, d("600.00")), (4, d("0")), (6, d("145.00"))], PLAN_CASH, Currency::USD, - ); + ) + .unwrap(); let got: Vec<(AccountId, Side, Decimal)> = plan.iter().map(|(a, s, m)| (*a, *s, m.amount)).collect(); assert_eq!( @@ -1042,6 +1043,29 @@ fn remit_plan_hand_worked() { ); } +/// Oracle: BS-entry-4 (every derived figure computed with checked +/// arithmetic): payables whose total is past exact arithmetic are refused +/// as TooLargeToCompute, never a panic, even when a caller did not +/// pre-check. +#[test] +fn remit_plan_past_exact_arithmetic_is_refused() { + let err = payroll_remit_plan( + &[(3, Decimal::MAX), (6, Decimal::ONE)], + PLAN_CASH, + Currency::USD, + ) + .unwrap_err(); + assert!(matches!(err, LedgerError::TooLargeToCompute(_)), "{err:?}"); + // Past the range downward, too. + let err = payroll_remit_plan( + &[(3, Decimal::MIN), (6, -Decimal::ONE)], + PLAN_CASH, + Currency::USD, + ) + .unwrap_err(); + assert!(matches!(err, LedgerError::TooLargeToCompute(_)), "{err:?}"); +} + /// Oracle: run_payroll INVARIANTS 1–2 and doc (auto-create 6100/6110/ /// 2110–2150), hand-worked Alice entry; balances after: cash 10000 − 3567.50 /// = 6432.50; payables as in the plan. @@ -1478,7 +1502,7 @@ proptest! { .enumerate() .map(|(i, c)| (i as AccountId + 1, Decimal::new(*c, 2))) .collect(); - let plan = payroll_remit_plan(&payables, PLAN_CASH, Currency::USD); + let plan = payroll_remit_plan(&payables, PLAN_CASH, Currency::USD).unwrap(); let total: Decimal = payables.iter().map(|(_, b)| *b).sum(); let (dr, cr) = plan_totals(&plan); prop_assert_eq!(dr, cr); diff --git a/crates/ledger-store/src/store.rs b/crates/ledger-store/src/store.rs index 428e7ebf..b8455551 100644 --- a/crates/ledger-store/src/store.rs +++ b/crates/ledger-store/src/store.rs @@ -4437,7 +4437,7 @@ impl Store { } let entry_id = self.ledger.next_entry_id(); let base = self.base_currency; - let plan = payroll_remit_plan(&payables, cash_account, base); + let plan = payroll_remit_plan(&payables, cash_account, base)?; let description = format!("Payroll tax remittance {date}"); let mut journaled = Vec::with_capacity(plan.len() + 2); journaled.push(self.append_journal(