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
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# Design: TigerBeetle LedgerBackend binding (`tb_ledger`)

Issue: #2289 (conversion-executor PoC, acceptance item 1). Code:
`pkgs/id/src/currency_poc/tb_ledger.rs`, behind the `currency-poc-tb`
feature. PR: #5072.

This record documents the as-built design of the live binding. Per
`pkgs/id/AGENTS.md` ("Documenting Design & Architecture Decisions")
it should have been written before implementation; it was filed
during PR #5072 review after a Codex finding (2026-11-03) pointed
out the gap, and records the decisions and constraints the module
comments only summarized.

## Intent

`currency_poc` defines a synchronous `LedgerBackend` trait whose
reference implementation, `InMemoryLedger`, encodes the TigerBeetle
semantics the conversion executor depends on (atomic linked chains,
field-sensitive idempotent transfer IDs, balance-invariant flags).
The binding implements the same trait over the official TigerBeetle
Rust client (`tigerbeetle` crate, pinned to the harness server
version 0.17.9) so the executor scenarios run against a real
single-replica test cluster (`currency_poc::harness::TestCluster`),
not only the in-memory model.

## Decisions

### Feature gate

The binding is behind `currency-poc-tb` so default builds of the
`id` crate do not link the client's native `tb_client` library.
The harness and live tests are gated the same way; live tests are
additionally `#[ignore]`d and need the pinned binary
(`TIGERBEETLE_POC_BINARY` or `tigerbeetle` on `PATH`).

### Private runtime, synchronous surface

The trait is synchronous; the client is async but runtime-agnostic
(it runs its own off-thread event loop). The binding owns a private
current-thread Tokio runtime and `block_on`s each request. It must
therefore be driven from plain synchronous code (as the PoC executor
is) — never from inside a Tokio runtime, where `block_on` would
panic. One client (and runtime) per ledger instance.

### Cluster identity

The harness formats its data file with cluster ID 0, so the client
connects with cluster ID 0. This is a test-harness binding, not a
production deployment story.

### Reversible wire-code encoding

TigerBeetle rejects `code = 0` on accounts and transfers, while the
PoC types use 0 as a supported chart-of-accounts code. Codes are
sent shifted by one (`wire_code`: `c -> c + 1`; `poc_code` is the
inverse). The shift is a bijection on `0..=u16::MAX - 1`, so every
supported code stays distinct on the wire. The earlier design —
substituting a constant for 0 only — serialized codes 0 and 1
identically, so a retry with the other code reported `Exists`
instead of `ExistsWithDifferentFields`, breaking the backend's
field-sensitive idempotency contract (Codex review, PR #5072).
`u16::MAX` has no wire representation under the shift (it would
wrap to 0) and panics as a programming error; the PoC's codes are
small chart-of-accounts values.

### Code registries

TigerBeetle stores only the wire code, so the binding records the
original code of every account/transfer it creates and restores it
on lookups (a looked-up leg must compare equal to the persisted
intent leg the recovery scanner checks). Registries are updated
**only after** TigerBeetle accepts a creation (`Created`, or
`Exists` confirming identical fields): a rejected retry must not
overwrite the registry, or later lookups would misreport the stored
record (Codex review, PR #5072). For records the registry does not
know (e.g. created by an earlier binding instance), lookup decodes
the wire code with `poc_code`.

### Status mapping and panic stance

Result statuses are mapped onto the PoC error enums for the subset
the PoC models. An unmodelled status — or a transport-level client
error, for which the trait has no channel — panics with the
status/error named rather than being silently folded into a wrong
variant. One exception is modelled explicitly: `IdAlreadyFailed`
maps to `TransferError::IdAlreadyFailed`. TigerBeetle remembers
failed transfer IDs, so a recovery resubmission of a previously
failed conversion meets this status by design; as a regular error
it lets `recovery::reconcile` report `ResubmissionRejected` and
continue the scan instead of aborting it (Codex review, PR #5072).

### Exact `user_data_128` queries

The trait promises exact matching on `user_data_128` (the
conversion-ID lookup). TigerBeetle treats a zero `QueryFilter`
field as "no filter", so a zero query would return every transfer.
The binding therefore pages through query results (advancing
`timestamp_min` past each full page; timestamps are unique per
transfer) and post-filters on exact equality for **every** value,
zero included, so the result stays exact even if server filter
semantics change (Codex review, PR #5072).

## Verification

The scenario suite in `currency_poc::tests` runs against both
backends: in-memory by default, live against `TestCluster` when
ignored tests are enabled with the pinned binary. Live coverage
includes the three behaviours this record describes: distinct
0/1 codes with registry-preserving rejected retries, exact zero
`user_data_128` matching, and failure-then-recovery reporting
`ResubmissionRejected`.

### Linked-chain retry

TigerBeetle breaks a linked chain on any non-`Created` status, so
resubmitting an already-posted chain reports `Exists` for its first
leg and `LinkedEventFailed` for every later leg, while the
in-memory backend reports `Exists` per leg (and the executor
accepts that as idempotent success). The binding translates the
pattern back: when a whole chain shows `Exists` then
`LinkedEventFailed`s AND every stored leg verifies identical to
the submitted leg, all legs report `Exists`. A genuinely failed
chain never starts with `Exists`, and a differing stored leg fails
verification, so both keep the raw mapping. Covered live by
`live_same_request_twice_posts_once` (Codex review, PR #5072).
7 changes: 7 additions & 0 deletions pkgs/id/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkgs/id/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ web = [
]
# Mainline DHT address lookup (P7.3, decision 15). Off in the default build; src/dht.rs decides per server.
dht = ["dep:iroh-mainline-address-lookup"]
# Live-cluster binding for the currency PoC (#2289): the official TigerBeetle
# Rust client, pinned to the harness server version (harness.rs). Off by
# default so normal builds do not link the client native tb_client library.
currency-poc-tb = ["dep:tigerbeetle"]

[dependencies]
# Foundation leaf modules extracted in Phase 1 of the crate split (#3447).
Expand Down Expand Up @@ -103,6 +107,7 @@ rustyline = "18"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tempfile = "3"
tigerbeetle = { version = "=0.17.9", optional = true }
tokio = { version = "1", features = ["full"] }
# TOML parser for the server config (P2.9).
toml = "1"
Expand Down
21 changes: 20 additions & 1 deletion pkgs/id/src/currency_poc/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,16 @@ pub fn verify_pinned_version(binary: &PathBuf) -> Result<()> {
.lines()
.next()
.and_then(|line| line.trim().strip_prefix("TigerBeetle version "));
if !output.status.success() || reported_version != Some(TIGERBEETLE_PINNED_VERSION) {
// The pinned release binary reports its version with a build
// suffix (e.g. "0.17.9+cc1c06a"); accept the exact pin or the pin
// plus build metadata, nothing else.
let version_matches = reported_version.is_some_and(|reported| {
reported == TIGERBEETLE_PINNED_VERSION
|| reported
.strip_prefix(TIGERBEETLE_PINNED_VERSION)
.is_some_and(|suffix| suffix.starts_with('+'))
});
if !output.status.success() || !version_matches {
bail!(
"tigerbeetle binary at {} reports {:?}, expected exact pinned version {} \
(acquire it from {})",
Expand Down Expand Up @@ -184,12 +193,22 @@ mod tests {
assert!(verify_pinned_version(&binary).is_ok());
}

#[cfg(unix)]
#[test]
fn accepts_pinned_version_with_build_suffix() {
// The real 0.17.9 release binary reports "0.17.9+cc1c06a".
let (_dir, binary) = stub_binary("TigerBeetle version 0.17.9+cc1c06a\n", 0);

assert!(verify_pinned_version(&binary).is_ok());
}

#[cfg(unix)]
#[test]
fn rejects_nonexact_reported_versions() {
for output in [
"TigerBeetle version 0.17.99\n",
"TigerBeetle version 0.17.90\n",
"TigerBeetle version 0.17.9x\n",
"TigerBeetle version 0.16.0\n",
"TigerBeetle build deadbee0.17.9custom\n",
] {
Expand Down
5 changes: 5 additions & 0 deletions pkgs/id/src/currency_poc/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ pub enum TransferError {
Exists,
/// A transfer with this ID exists but with different fields.
ExistsWithDifferentFields,
/// The transfer ID belongs to a transfer that already failed:
/// TigerBeetle remembers failed transfer IDs and refuses their
/// resubmission (`id_already_failed`). A recovery resubmission
/// meeting this is a rejected resubmission, not a posting.
IdAlreadyFailed,
/// Another leg of the same linked chain failed; this leg did not post.
LinkedEventFailed,
/// The batch ended while this leg's linked chain was still open:
Expand Down
5 changes: 5 additions & 0 deletions pkgs/id/src/currency_poc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
//! LP→user), every leg stamped `user_data_128 = conversion_id`.
//! - `recovery` — the recovery scanner reconciling unresolved intents
//! by pre-generated transfer-ID lookup.
//! - `tb_ledger` (feature `currency-poc-tb`) — the same seam over the
//! official TigerBeetle Rust client, for running the scenarios against
//! a real cluster.
//! - `harness` — step 0: a throwaway single-replica TigerBeetle test
//! cluster (pinned binary, start/stop) for running the same
//! scenarios against a real cluster once a client binding lands.
Expand All @@ -32,6 +35,8 @@ pub mod harness;
pub mod intent;
pub mod ledger;
pub mod recovery;
#[cfg(feature = "currency-poc-tb")]
pub mod tb_ledger;

#[cfg(test)]
mod tests;
Loading
Loading