From 09526854d0784ba085e76edd047f73bf60ceaaa4 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Wed, 11 Nov 2026 06:29:24 -0600 Subject: [PATCH] fix(id/currency_poc): map every reachable TigerBeetle status to a typed error (#6108) --- pkgs/id/src/currency_poc/ledger.rs | 108 +++++++ pkgs/id/src/currency_poc/tb_ledger.rs | 429 +++++++++++++++++++++++++- pkgs/id/src/currency_poc/tests.rs | 36 +++ 3 files changed, 560 insertions(+), 13 deletions(-) diff --git a/pkgs/id/src/currency_poc/ledger.rs b/pkgs/id/src/currency_poc/ledger.rs index 0373a8dae3..08c1289bd7 100644 --- a/pkgs/id/src/currency_poc/ledger.rs +++ b/pkgs/id/src/currency_poc/ledger.rs @@ -85,6 +85,34 @@ pub enum AccountError { ExistsWithDifferentFields, /// `ledger` must be non-zero. LedgerMustNotBeZero, + /// Another leg of the same linked chain failed; this account did + /// not get created (mirrors TigerBeetle's `linked_event_failed`). + LinkedEventFailed, + /// The batch ended while this account's linked chain was still + /// open (mirrors TigerBeetle's `linked_event_chain_open`). + LinkedEventChainOpen, + /// The account ID is zero, which TigerBeetle forbids + /// (`id_must_not_be_zero`). The in-memory backend rejects the + /// same shape with this error (#6108). + IdMustNotBeZero, + /// The account ID is the integer maximum, which TigerBeetle + /// reserves (`id_must_not_be_int_max`). The in-memory backend + /// rejects the same shape with this error (#6108). + IdMustNotBeIntMax, + /// The account code is zero on the wire + /// (`code_must_not_be_zero`). The binding shifts PoC codes by + /// one (see `wire_code` in tb_ledger.rs), so this is not + /// reachable from a well-formed binding request; it is typed + /// anyway so the mapping stays exhaustive. + CodeMustNotBeZero, + /// The account flags combine mutually exclusive options + /// (`flags_are_mutually_exclusive`). + FlagsAreMutuallyExclusive, + /// A created account must start with zero balances: one of + /// TigerBeetle's `debits_pending_must_be_zero`, + /// `debits_posted_must_be_zero`, `credits_pending_must_be_zero`, + /// or `credits_posted_must_be_zero` fired. + InitialBalanceMustBeZero, /// The PoC code cannot be represented by the TigerBeetle wire encoding. CodeOutOfRange, /// The backend request failed before account statuses were returned. @@ -124,6 +152,44 @@ pub enum TransferError { LedgerMismatch, DebitsMustNotExceedCredits, CreditsMustNotExceedDebits, + /// The transfer ID is zero, which TigerBeetle forbids + /// (`id_must_not_be_zero`). The in-memory backend rejects the + /// same shape with this error (#6108). + IdMustNotBeZero, + /// The transfer ID is the integer maximum, which TigerBeetle + /// reserves (`id_must_not_be_int_max`). The in-memory backend + /// rejects the same shape with this error (#6108). + IdMustNotBeIntMax, + /// The debit account ID is zero (`debit_account_id_must_not_be_zero`). + /// The in-memory backend rejects the same shape with this error + /// (#6108) instead of reporting the account as not found. + DebitAccountIdMustNotBeZero, + /// The debit account ID is the integer maximum + /// (`debit_account_id_must_not_be_int_max`); in-memory parity per #6108. + DebitAccountIdMustNotBeIntMax, + /// The credit account ID is zero (`credit_account_id_must_not_be_zero`); + /// in-memory parity per #6108. + CreditAccountIdMustNotBeZero, + /// The credit account ID is the integer maximum + /// (`credit_account_id_must_not_be_int_max`); in-memory parity per #6108. + CreditAccountIdMustNotBeIntMax, + /// The transfer code is zero on the wire (`code_must_not_be_zero`). + /// The binding shifts PoC codes by one (see `wire_code` in + /// tb_ledger.rs), so this is not reachable from a well-formed + /// binding request; it is typed anyway so the mapping stays + /// exhaustive. + CodeMustNotBeZero, + /// The transfer flags combine mutually exclusive options + /// (`flags_are_mutually_exclusive`). + FlagsAreMutuallyExclusive, + /// The debit account is closed (`debit_account_already_closed`): + /// a real cluster state a scenario can legitimately meet. + DebitAccountAlreadyClosed, + /// The credit account is closed (`credit_account_already_closed`). + CreditAccountAlreadyClosed, + /// The pending transfer's timeout arithmetic overflowed + /// (`overflows_timeout`). + OverflowsTimeout, /// The PoC code cannot be represented by the TigerBeetle wire encoding. CodeOutOfRange, /// The backend request failed before transfer statuses were returned. @@ -247,6 +313,17 @@ impl InMemoryLedger { staged: &mut HashMap, staged_transfers: &HashMap, ) -> Result<(), TransferError> { + // Reserved transfer IDs are refused before any ID lookup: + // TigerBeetle validates them ahead of its existing/failed + // transfer lookup (state_machine.zig on 0.17.9), so a retry + // of a reserved-ID transfer returns the ID error again + // rather than `IdAlreadyFailed` (Codex P2 on #8670). + if transfer.id == 0 { + return Err(TransferError::IdMustNotBeZero); + } + if transfer.id == u128::MAX { + return Err(TransferError::IdMustNotBeIntMax); + } // A transfer ID previously recorded as failed is refused // with `IdAlreadyFailed`, matching the variant's contract // above and the live binding's status mapping (tb_ledger.rs) @@ -273,12 +350,31 @@ impl InMemoryLedger { Err(TransferError::ExistsWithDifferentFields) }; } + // The remaining ID shapes TigerBeetle rejects outright are + // rejected here with the same typed errors, so a scenario + // means the same thing on both backends (#6108). The + // account-ID checks sit before the lookups below so a + // zero/int-max account ID is not misreported as "not found"; + // the transfer's own reserved-ID checks sit at the top of + // this function, ahead of the failed-ID lookup. if transfer.amount == 0 { return Err(TransferError::AmountMustNotBeZero); } if transfer.debit_account_id == transfer.credit_account_id { return Err(TransferError::AccountsMustBeDifferent); } + if transfer.debit_account_id == 0 { + return Err(TransferError::DebitAccountIdMustNotBeZero); + } + if transfer.debit_account_id == u128::MAX { + return Err(TransferError::DebitAccountIdMustNotBeIntMax); + } + if transfer.credit_account_id == 0 { + return Err(TransferError::CreditAccountIdMustNotBeZero); + } + if transfer.credit_account_id == u128::MAX { + return Err(TransferError::CreditAccountIdMustNotBeIntMax); + } let debit = self .accounts .get(&transfer.debit_account_id) @@ -336,6 +432,18 @@ impl LedgerBackend for InMemoryLedger { accounts .iter() .map(|account| { + // ID shapes TigerBeetle rejects outright are rejected + // here with the same typed errors, so a scenario means + // the same thing on both backends (#6108). Before this + // reconciliation the in-memory backend accepted + // accounts with ID 0 / u128::MAX that a real cluster + // refuses. + if account.id == 0 { + return Err(AccountError::IdMustNotBeZero); + } + if account.id == u128::MAX { + return Err(AccountError::IdMustNotBeIntMax); + } if account.ledger == 0 { return Err(AccountError::LedgerMustNotBeZero); } diff --git a/pkgs/id/src/currency_poc/tb_ledger.rs b/pkgs/id/src/currency_poc/tb_ledger.rs index 49968216b7..1f9343aff1 100644 --- a/pkgs/id/src/currency_poc/tb_ledger.rs +++ b/pkgs/id/src/currency_poc/tb_ledger.rs @@ -22,11 +22,28 @@ //! and restores it on lookups, so a looked-up leg still compares //! equal to the persisted intent leg the recovery scanner checks //! (`super::recovery`). -//! - Status mapping covers the subset of TigerBeetle result codes the -//! PoC types model. Unmodelled statuses become explicit typed errors; -//! transport failures from creation requests become per-item backend -//! errors, and lookup/query failures propagate through the fallible -//! trait methods instead of being mistaken for missing records. +//! - Status mapping covers every variant of the pinned crate +//! explicitly (#6108): `map_account_status` and +//! `map_transfer_status` name each status of the pinned +//! TigerBeetle crate's enums; the enums are `#[non_exhaustive]`, +//! so a wildcard arm remains for future variants only, mapping +//! them to `UnmodelledStatus` — and the variant tables in the +//! tests are the inventory to extend when the pin moves. Every +//! status reachable +//! from inputs the PoC types accept maps to a specific typed error; +//! the families the binding can never emit (imported events, +//! pending transfers, reserved fields/flags, explicit timestamps) +//! are enumerated arm-by-arm and map to `UnmodelledStatus` with a +//! comment saying why they are unreachable. Transport failures from +//! creation requests become per-item backend errors, and +//! lookup/query failures propagate through the fallible trait +//! methods instead of being mistaken for missing records. +//! - The in-memory backend (`super::ledger::InMemoryLedger`) rejects +//! the same zero/int-max ID shapes with the same typed errors, so +//! scenario tests mean the same thing on both backends (#6108). +//! Statuses with no in-memory analogue (closed accounts, initial +//! balances, flag exclusivity) exist only on this binding: the PoC +//! types cannot express the state that produces them. //! //! The binding is behind the `currency-poc-tb` feature so default //! builds of the `id` crate do not link the client's native @@ -197,6 +214,25 @@ fn from_tb_transfer(transfer: &tb::Transfer, code: u16) -> Transfer { } } +fn unmodelled_account(status: tb::CreateAccountStatus) -> AccountError { + tracing::error!(?status, "TigerBeetle returned an unmodelled account status"); + AccountError::UnmodelledStatus +} + +fn unmodelled_transfer(status: tb::CreateTransferStatus) -> TransferError { + tracing::error!(?status, "TigerBeetle returned an unmodelled transfer status"); + TransferError::UnmodelledStatus +} + +/// Map a TigerBeetle account-creation status to the PoC's typed +/// errors. Every variant of the pinned crate is matched explicitly +/// (#6108); the enum is `#[non_exhaustive]`, so a wildcard arm is +/// still required — it exists only for variants a future crate bump +/// adds, maps them to `UnmodelledStatus`, and is inventoried by the +/// variant table in the tests: when the pin moves, extend the +/// explicit arms and the table together. Statuses the binding can +/// never emit are enumerated explicitly and keep the +/// `UnmodelledStatus` outcome. fn map_account_status(status: tb::CreateAccountStatus) -> Result<(), AccountError> { use tb::CreateAccountStatus as Status; match status { @@ -209,13 +245,42 @@ fn map_account_status(status: tb::CreateAccountStatus) -> Result<(), AccountErro | Status::ExistsWithDifferentLedger | Status::ExistsWithDifferentCode => Err(AccountError::ExistsWithDifferentFields), Status::LedgerMustNotBeZero => Err(AccountError::LedgerMustNotBeZero), - other => { - tracing::error!(?other, "TigerBeetle returned an unmodelled account status"); - Err(AccountError::UnmodelledStatus) + Status::LinkedEventFailed => Err(AccountError::LinkedEventFailed), + Status::LinkedEventChainOpen => Err(AccountError::LinkedEventChainOpen), + Status::IdMustNotBeZero => Err(AccountError::IdMustNotBeZero), + Status::IdMustNotBeIntMax => Err(AccountError::IdMustNotBeIntMax), + Status::CodeMustNotBeZero => Err(AccountError::CodeMustNotBeZero), + Status::FlagsAreMutuallyExclusive => Err(AccountError::FlagsAreMutuallyExclusive), + // A created account must start from zero balances. + Status::DebitsPendingMustBeZero + | Status::DebitsPostedMustBeZero + | Status::CreditsPendingMustBeZero + | Status::CreditsPostedMustBeZero => Err(AccountError::InitialBalanceMustBeZero), + // Imported events: the binding never sets the `imported` + // flag or an event timestamp, so a cluster can only return + // these for requests this binding did not make. + Status::ImportedEventExpected + | Status::ImportedEventNotExpected + | Status::ImportedEventTimestampOutOfRange + | Status::ImportedEventTimestampMustNotAdvance + | Status::ImportedEventTimestampMustNotRegress => { + Err(unmodelled_account(status)) } + // The binding never sets an account timestamp. + Status::TimestampMustBeZero => Err(unmodelled_account(status)), + // The binding never sets reserved fields or reserved flags. + Status::ReservedField | Status::ReservedFlag => Err(unmodelled_account(status)), + // `#[non_exhaustive]`: variants a future crate bump adds. + // They keep the UnmodelledStatus outcome (and log) until + // they are enumerated above — see the variant table test. + _ => Err(unmodelled_account(status)), } } +/// Map a TigerBeetle transfer-creation status to the PoC's typed +/// errors. Every variant of the pinned crate is matched explicitly +/// (#6108), like [`map_account_status`]; the wildcard arm is the +/// `#[non_exhaustive]` contract for future variants only. fn map_transfer_status(status: tb::CreateTransferStatus) -> Result<(), TransferError> { use tb::CreateTransferStatus as Status; match status { @@ -238,8 +303,28 @@ fn map_transfer_status(status: tb::CreateTransferStatus) -> Result<(), TransferE // TigerBeetle; resubmitting it (recovery) is a regular // rejection, not a panic-worthy unmodelled status. Status::IdAlreadyFailed => Err(TransferError::IdAlreadyFailed), + Status::IdMustNotBeZero => Err(TransferError::IdMustNotBeZero), + Status::IdMustNotBeIntMax => Err(TransferError::IdMustNotBeIntMax), + Status::DebitAccountIdMustNotBeZero => { + Err(TransferError::DebitAccountIdMustNotBeZero) + } + Status::DebitAccountIdMustNotBeIntMax => { + Err(TransferError::DebitAccountIdMustNotBeIntMax) + } + Status::CreditAccountIdMustNotBeZero => { + Err(TransferError::CreditAccountIdMustNotBeZero) + } + Status::CreditAccountIdMustNotBeIntMax => { + Err(TransferError::CreditAccountIdMustNotBeIntMax) + } + Status::FlagsAreMutuallyExclusive => Err(TransferError::FlagsAreMutuallyExclusive), + Status::CodeMustNotBeZero => Err(TransferError::CodeMustNotBeZero), Status::DebitAccountNotFound => Err(TransferError::DebitAccountNotFound), Status::CreditAccountNotFound => Err(TransferError::CreditAccountNotFound), + Status::DebitAccountAlreadyClosed => Err(TransferError::DebitAccountAlreadyClosed), + Status::CreditAccountAlreadyClosed => { + Err(TransferError::CreditAccountAlreadyClosed) + } Status::AccountsMustBeDifferent => Err(TransferError::AccountsMustBeDifferent), Status::AccountsMustHaveTheSameLedger | Status::TransferMustHaveTheSameLedgerAsAccounts @@ -255,10 +340,46 @@ fn map_transfer_status(status: tb::CreateTransferStatus) -> Result<(), TransferE Status::OverflowsCreditsPending | Status::OverflowsCreditsPosted | Status::OverflowsCredits => Err(TransferError::CreditsMustNotExceedDebits), - other => { - tracing::error!(?other, "TigerBeetle returned an unmodelled transfer status"); - Err(TransferError::UnmodelledStatus) - } + Status::OverflowsTimeout => Err(TransferError::OverflowsTimeout), + // Pending transfers: the PoC `Transfer` type has no + // `pending_id` and the binding never sets the pending, + // post-pending, or void-pending flags, so it cannot emit a + // transfer these statuses describe. + Status::PendingIdMustBeZero + | Status::PendingIdMustNotBeZero + | Status::PendingIdMustNotBeIntMax + | Status::PendingIdMustBeDifferent + | Status::TimeoutReservedForPendingTransfer + | Status::ClosingTransferMustBePending + | Status::PendingTransferNotFound + | Status::PendingTransferNotPending + | Status::PendingTransferHasDifferentDebitAccountId + | Status::PendingTransferHasDifferentCreditAccountId + | Status::PendingTransferHasDifferentLedger + | Status::PendingTransferHasDifferentCode + | Status::ExceedsPendingTransferAmount + | Status::PendingTransferHasDifferentAmount + | Status::PendingTransferAlreadyPosted + | Status::PendingTransferAlreadyVoided + | Status::PendingTransferExpired => Err(unmodelled_transfer(status)), + // Imported events: the binding never sets the `imported` + // flag or an event timestamp. + Status::ImportedEventExpected + | Status::ImportedEventNotExpected + | Status::ImportedEventTimestampOutOfRange + | Status::ImportedEventTimestampMustNotAdvance + | Status::ImportedEventTimestampMustNotRegress + | Status::ImportedEventTimestampMustPostdateDebitAccount + | Status::ImportedEventTimestampMustPostdateCreditAccount + | Status::ImportedEventTimeoutMustBeZero => Err(unmodelled_transfer(status)), + // The binding never sets a transfer timestamp. + Status::TimestampMustBeZero => Err(unmodelled_transfer(status)), + // The binding never sets reserved flags. + Status::ReservedFlag => Err(unmodelled_transfer(status)), + // `#[non_exhaustive]`: variants a future crate bump adds. + // They keep the UnmodelledStatus outcome (and log) until + // they are enumerated above — see the variant table test. + _ => Err(unmodelled_transfer(status)), } } @@ -478,7 +599,11 @@ impl LedgerBackend for TigerBeetleLedger { #[cfg(test)] mod tests { - use super::{poc_code, wire_code}; + use super::{ + map_account_status, map_transfer_status, poc_code, wire_code, + }; + use crate::currency_poc::ledger::{AccountError, TransferError}; + use tigerbeetle as tb; #[test] fn wire_code_rejects_the_unrepresentable_maximum() { @@ -490,4 +615,282 @@ mod tests { fn zero_wire_code_is_reported_as_an_error() { assert!(poc_code(0).is_err()); } + + /// Oracle (#6108 acceptance): every `CreateAccountStatus` + /// variant of the pinned tigerbeetle crate maps to its specific + /// typed error, except the enumerated unreachable families + /// (imported events, explicit timestamps, reserved fields/flags) + /// which map to `UnmodelledStatus` — and no variant panics. The + /// table lists every variant by name; it is the inventory a + /// crate bump must extend (the enums are `#[non_exhaustive]`, + /// so a new variant cannot break compilation — it lands in the + /// mapper's commented wildcard until it is recorded here). + #[test] + fn account_status_mapping_covers_every_variant() { + use tb::CreateAccountStatus as S; + let expected: &[(S, Result<(), AccountError>)] = &[ + (S::Created, Ok(())), + (S::Exists, Err(AccountError::Exists)), + ( + S::ExistsWithDifferentFlags, + Err(AccountError::ExistsWithDifferentFields), + ), + ( + S::ExistsWithDifferentUserData128, + Err(AccountError::ExistsWithDifferentFields), + ), + ( + S::ExistsWithDifferentUserData64, + Err(AccountError::ExistsWithDifferentFields), + ), + ( + S::ExistsWithDifferentUserData32, + Err(AccountError::ExistsWithDifferentFields), + ), + ( + S::ExistsWithDifferentLedger, + Err(AccountError::ExistsWithDifferentFields), + ), + ( + S::ExistsWithDifferentCode, + Err(AccountError::ExistsWithDifferentFields), + ), + (S::LedgerMustNotBeZero, Err(AccountError::LedgerMustNotBeZero)), + (S::LinkedEventFailed, Err(AccountError::LinkedEventFailed)), + ( + S::LinkedEventChainOpen, + Err(AccountError::LinkedEventChainOpen), + ), + (S::IdMustNotBeZero, Err(AccountError::IdMustNotBeZero)), + (S::IdMustNotBeIntMax, Err(AccountError::IdMustNotBeIntMax)), + (S::CodeMustNotBeZero, Err(AccountError::CodeMustNotBeZero)), + ( + S::FlagsAreMutuallyExclusive, + Err(AccountError::FlagsAreMutuallyExclusive), + ), + ( + S::DebitsPendingMustBeZero, + Err(AccountError::InitialBalanceMustBeZero), + ), + ( + S::DebitsPostedMustBeZero, + Err(AccountError::InitialBalanceMustBeZero), + ), + ( + S::CreditsPendingMustBeZero, + Err(AccountError::InitialBalanceMustBeZero), + ), + ( + S::CreditsPostedMustBeZero, + Err(AccountError::InitialBalanceMustBeZero), + ), + // Unreachable families: enumerated in the mapper with + // their reasons; the outcome stays UnmodelledStatus. + (S::ImportedEventExpected, Err(AccountError::UnmodelledStatus)), + ( + S::ImportedEventNotExpected, + Err(AccountError::UnmodelledStatus), + ), + ( + S::ImportedEventTimestampOutOfRange, + Err(AccountError::UnmodelledStatus), + ), + ( + S::ImportedEventTimestampMustNotAdvance, + Err(AccountError::UnmodelledStatus), + ), + ( + S::ImportedEventTimestampMustNotRegress, + Err(AccountError::UnmodelledStatus), + ), + (S::TimestampMustBeZero, Err(AccountError::UnmodelledStatus)), + (S::ReservedField, Err(AccountError::UnmodelledStatus)), + (S::ReservedFlag, Err(AccountError::UnmodelledStatus)), + ]; + for (status, want) in expected { + assert_eq!(&map_account_status(*status), want, "status {status:?}"); + } + } + + /// Oracle (#6108 acceptance): the transfer-side twin of + /// `account_status_mapping_covers_every_variant` — every + /// `CreateTransferStatus` variant of the pinned crate is listed + /// with its expected outcome, the pending-transfer and + /// imported-event families being the enumerated + /// `UnmodelledStatus` set, and no variant panics. + #[test] + fn transfer_status_mapping_covers_every_variant() { + use tb::CreateTransferStatus as S; + let typed: &[(S, Result<(), TransferError>)] = &[ + (S::Created, Ok(())), + (S::Exists, Err(TransferError::Exists)), + ( + S::ExistsWithDifferentFlags, + Err(TransferError::ExistsWithDifferentFields), + ), + ( + S::ExistsWithDifferentPendingId, + Err(TransferError::ExistsWithDifferentFields), + ), + ( + S::ExistsWithDifferentTimeout, + Err(TransferError::ExistsWithDifferentFields), + ), + ( + S::ExistsWithDifferentDebitAccountId, + Err(TransferError::ExistsWithDifferentFields), + ), + ( + S::ExistsWithDifferentCreditAccountId, + Err(TransferError::ExistsWithDifferentFields), + ), + ( + S::ExistsWithDifferentAmount, + Err(TransferError::ExistsWithDifferentFields), + ), + ( + S::ExistsWithDifferentUserData128, + Err(TransferError::ExistsWithDifferentFields), + ), + ( + S::ExistsWithDifferentUserData64, + Err(TransferError::ExistsWithDifferentFields), + ), + ( + S::ExistsWithDifferentUserData32, + Err(TransferError::ExistsWithDifferentFields), + ), + ( + S::ExistsWithDifferentLedger, + Err(TransferError::ExistsWithDifferentFields), + ), + ( + S::ExistsWithDifferentCode, + Err(TransferError::ExistsWithDifferentFields), + ), + (S::LinkedEventFailed, Err(TransferError::LinkedEventFailed)), + ( + S::LinkedEventChainOpen, + Err(TransferError::LinkedEventChainOpen), + ), + (S::IdAlreadyFailed, Err(TransferError::IdAlreadyFailed)), + (S::IdMustNotBeZero, Err(TransferError::IdMustNotBeZero)), + (S::IdMustNotBeIntMax, Err(TransferError::IdMustNotBeIntMax)), + ( + S::DebitAccountIdMustNotBeZero, + Err(TransferError::DebitAccountIdMustNotBeZero), + ), + ( + S::DebitAccountIdMustNotBeIntMax, + Err(TransferError::DebitAccountIdMustNotBeIntMax), + ), + ( + S::CreditAccountIdMustNotBeZero, + Err(TransferError::CreditAccountIdMustNotBeZero), + ), + ( + S::CreditAccountIdMustNotBeIntMax, + Err(TransferError::CreditAccountIdMustNotBeIntMax), + ), + ( + S::FlagsAreMutuallyExclusive, + Err(TransferError::FlagsAreMutuallyExclusive), + ), + (S::CodeMustNotBeZero, Err(TransferError::CodeMustNotBeZero)), + ( + S::DebitAccountNotFound, + Err(TransferError::DebitAccountNotFound), + ), + ( + S::CreditAccountNotFound, + Err(TransferError::CreditAccountNotFound), + ), + ( + S::DebitAccountAlreadyClosed, + Err(TransferError::DebitAccountAlreadyClosed), + ), + ( + S::CreditAccountAlreadyClosed, + Err(TransferError::CreditAccountAlreadyClosed), + ), + ( + S::AccountsMustBeDifferent, + Err(TransferError::AccountsMustBeDifferent), + ), + ( + S::AccountsMustHaveTheSameLedger, + Err(TransferError::LedgerMismatch), + ), + ( + S::TransferMustHaveTheSameLedgerAsAccounts, + Err(TransferError::LedgerMismatch), + ), + (S::LedgerMustNotBeZero, Err(TransferError::LedgerMismatch)), + (S::ExceedsCredits, Err(TransferError::DebitsMustNotExceedCredits)), + (S::ExceedsDebits, Err(TransferError::CreditsMustNotExceedDebits)), + ( + S::OverflowsDebitsPending, + Err(TransferError::DebitsMustNotExceedCredits), + ), + ( + S::OverflowsDebitsPosted, + Err(TransferError::DebitsMustNotExceedCredits), + ), + (S::OverflowsDebits, Err(TransferError::DebitsMustNotExceedCredits)), + ( + S::OverflowsCreditsPending, + Err(TransferError::CreditsMustNotExceedDebits), + ), + ( + S::OverflowsCreditsPosted, + Err(TransferError::CreditsMustNotExceedDebits), + ), + ( + S::OverflowsCredits, + Err(TransferError::CreditsMustNotExceedDebits), + ), + (S::OverflowsTimeout, Err(TransferError::OverflowsTimeout)), + ]; + for (status, want) in typed { + assert_eq!(&map_transfer_status(*status), want, "status {status:?}"); + } + // Unreachable families: enumerated in the mapper with their + // reasons; the outcome stays UnmodelledStatus. + let unmodelled: &[S] = &[ + S::PendingIdMustBeZero, + S::PendingIdMustNotBeZero, + S::PendingIdMustNotBeIntMax, + S::PendingIdMustBeDifferent, + S::TimeoutReservedForPendingTransfer, + S::ClosingTransferMustBePending, + S::PendingTransferNotFound, + S::PendingTransferNotPending, + S::PendingTransferHasDifferentDebitAccountId, + S::PendingTransferHasDifferentCreditAccountId, + S::PendingTransferHasDifferentLedger, + S::PendingTransferHasDifferentCode, + S::ExceedsPendingTransferAmount, + S::PendingTransferHasDifferentAmount, + S::PendingTransferAlreadyPosted, + S::PendingTransferAlreadyVoided, + S::PendingTransferExpired, + S::ImportedEventExpected, + S::ImportedEventNotExpected, + S::ImportedEventTimestampOutOfRange, + S::ImportedEventTimestampMustNotAdvance, + S::ImportedEventTimestampMustNotRegress, + S::ImportedEventTimestampMustPostdateDebitAccount, + S::ImportedEventTimestampMustPostdateCreditAccount, + S::ImportedEventTimeoutMustBeZero, + S::TimestampMustBeZero, + S::ReservedFlag, + ]; + for status in unmodelled { + assert_eq!( + map_transfer_status(*status), + Err(TransferError::UnmodelledStatus), + "status {status:?}" + ); + } + } } diff --git a/pkgs/id/src/currency_poc/tests.rs b/pkgs/id/src/currency_poc/tests.rs index d1daa9f6c9..6af5b33e67 100644 --- a/pkgs/id/src/currency_poc/tests.rs +++ b/pkgs/id/src/currency_poc/tests.rs @@ -436,6 +436,42 @@ fn resubmitted_failed_transfer_returns_id_already_failed_after_cause_fixed() { assert!(ledger.lookup_transfer(700).is_none()); } +/// Parity (#6108): the in-memory backend refuses the reserved ID +/// shapes with the same typed errors the TigerBeetle binding maps, +/// so a scenario means the same thing on both backends. A retry of +/// a reserved-ID transfer returns the ID error again — not +/// `IdAlreadyFailed` — because TigerBeetle validates reserved IDs +/// ahead of its failed-transfer lookup (Codex P2 on #8670). +#[test] +fn in_memory_rejects_the_reserved_id_shapes_like_tigerbeetle() { + let mut ledger = funded_ledger(); + let results = ledger.create_accounts(&[account(0, SRC, false)]); + assert_eq!(results, vec![Err(AccountError::IdMustNotBeZero)]); + let results = ledger.create_accounts(&[account(u128::MAX, SRC, false)]); + assert_eq!(results, vec![Err(AccountError::IdMustNotBeIntMax)]); + + for (id, error) in [ + (0, TransferError::IdMustNotBeZero), + (u128::MAX, TransferError::IdMustNotBeIntMax), + ] { + let results = ledger.create_transfers(&[funding_transfer(id, USER_SRC, LP_SRC, 10, SRC)]); + assert_eq!(results, vec![Err(error)]); + let results = ledger.create_transfers(&[funding_transfer(id, USER_SRC, LP_SRC, 10, SRC)]); + assert_eq!(results, vec![Err(error)], "retry of reserved ID {id}"); + } + + // Reserved debit/credit account IDs are refused with the typed + // errors, not misreported as "not found". + let results = ledger.create_transfers(&[funding_transfer(777_001, 0, LP_SRC, 10, SRC)]); + assert_eq!(results, vec![Err(TransferError::DebitAccountIdMustNotBeZero)]); + let results = + ledger.create_transfers(&[funding_transfer(777_002, USER_SRC, u128::MAX, 10, SRC)]); + assert_eq!( + results, + vec![Err(TransferError::CreditAccountIdMustNotBeIntMax)] + ); +} + #[test] fn single_unterminated_linked_transfer_rejected() { // #4121: a lone transfer with `linked: true` promises a following