Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions pkgs/id/src/currency_poc/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -247,6 +313,17 @@ impl InMemoryLedger {
staged: &mut HashMap<AccountId, AccountBalance>,
staged_transfers: &HashMap<TransferId, Transfer>,
) -> 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)
Expand All @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down
Loading
Loading