Skip to main content

Bank8:Ledger - Detailed Design Document

1. Scope & Context​

Bank8:Ledger owns: the ledger domain schema in the bank8 vertical database (44 domain tables in the bank8 schema + the service leaf schema ledger for the outbox and audit journal), the account / product / posting / balance / schedule REST API (91 endpoints), the product module validation library, the scheduled-posting engine, the internal proxy-back handlers for Cron and Func, and the bank8:ledger:* event family (30 event types).

It does NOT own: scheduling (Sentin8:Cron), tenant-authored executable hooks (Sentin8:Func), workflow orchestration (Sentin8:Flow), analytics telemetry (it only emits to Sentin8:Stat), or customer identity (Sentin8:Auth is the identity provider; customers is a banking record, optionally linked via sentin8_auth_user_id).

2. Runtime Architecture​

Boot is shell-owned (sentin8::shell): pool, policy engine load (fail-closed), outbox relay (step 8b), audit-journal pusher (step 8c), TLS. build_state then adds the ledger's own wiring:

Request lifecycle - synchronous posting (the canonical family; every other handler is a subset - auth verify, tenant resolve, policy gate, context, execute, emit):

3. Data Model​

44 domain tables in the service's own schema ledger (schema-per-service), all env_id-scoped + FORCE-RLS; the same schema carries the producer tables (ledger.outbox, ledger.audit_journal).

Complete table inventory with purpose and visibility:

TablePurposeRLS tier
accountsCustomer + internal accounts (account_type, product_id, nullable customer_id for internal)holder-scoped
products / product_versions / product_versions_extProduct library: type, parameters JSONB, modules listenv
postings / posting_instructionsPosting batches + instructions (status, idempotency_key, batch_id)env
balancesPer (account, denomination, address, phase) amounts - UPSERT targetenv
transactionsPer-account movement records (posting_id, direction)env
statementsGenerated statement metadata (payload in Storage)env
schedules / jobs / dlqScheduled-posting engine + job log + local dead-letterenv
customersBanking customer records (sentin8_auth_user_id nullable)env
flag_definitions / flags / account_flagsFlag library + instances + account bindingsholder (flags)
restriction_definitions / restriction_setsRestriction library + instancesholder (sets)
parameters / parameter_values / parameter_value_hierarchy_nodes / global_parametersParameter library + resolution hierarchy (global → product → account)env
calendars / calendar_events / calendar_periods / bookkeeping_datesCalendar model for value/booking datesenv
plans / plan_assocs / plan_migrationsProduct plan assignment + migration batchesenv
processing_groupsAccount grouping for batch processing control (pause/resume)env
schedule_tags / schedule_tag_assocs / account_schedule_tags / account_schedule_assocsSchedule tagging for routing/selectionenv
journal_eventsReplayable domain journal (checksum chain)env
adjustmentsPost-posting corrections (reason, amount)env
supervisor_contractsCross-account supervision contracts (Func module refs)env
payment_devices / account_attributesAccount extras: device registry + KV attributesenv
ledger_internal_accounts (via accounts)Seeded counterparty accounts (cash/cheque/liquidity/clearing/suspense per denomination)env
account_migrations / account_update_batchesBulk migration/update staging + bulk-retryenv
audit_eventsDomain audit trail (actor, action, target)env
ledger.outbox / ledger.audit_journalProducer tables (relay/pusher drained)FORCE
deferred_fees / deferred_fee_releasesFee income received before delivery: the fee, its recognition method and term, and one immutable row per recognised periodFORCE
tax_withholdingsTax deducted from interest paid, held as a liability until remitted - one row per account, jurisdiction and tax periodFORCE
equity_holdingsWho holds which class, how many units, and from when (opened_on / closed_on) - the span is what entitlement is measured over. One live row per (account, class)FORCE
equity_distributionsOne declared distribution per (class, period): explicit period_start/period_end, rate per unit, declared gross, tax withheld, net paidFORCE
equity_distribution_linesOne immutable row per holder per distribution: the share-days credited, the exact split, before and after taxFORCE

Posting lifecycle:

Instruction (async) lifecycle: pending → processing → completed | failed | cancelled - terminal states immutable (guarded updates).

Fee recognition and tax (deferred income, withholding):

Two postings that look like fees are deliberately not booked as income at the moment cash moves, and both are driven by the same COB dispatch as every other schedule.

Recognition rules:

  • The fee amount, term and method are product data (Recognition::{Immediate, StraightLine, EffectiveInterest}). Immediate reproduces the previous behaviour exactly, so no existing product changes when it is not configured.
  • release_for_period makes the final period absorb the rounding residual. Straight-line division alone can leave a cent on the liability forever; the residual makes the releases sum exactly to the fee, so the liability always clears. remaining_before uses a residual-free variant internally so that asking "how much is left" cannot recurse.
  • The idempotency spine is the database, not the worker: UNIQUE (deferred_fee_id, period_no). ON CONFLICT DO NOTHING returns rows_affected() == 0 on a replay, and periods_released is advanced by a guarded UPDATE ... WHERE periods_released = $2 - 1.
  • The release posting is TRANSFER_OUT from the unearned liability to fee income: no cash is created, because the cash arrived when the fee was charged.

Withholding rules:

  • Tax is due when the customer is paid, so the base is the sum of settled INTEREST_APPLICATION postings credited to the account in the current tax period - read from the ledger rather than recomputed, since the ledger is the record of what was actually paid. (Reading DEPOSIT here would tax the customer's own deposits.)
  • The row is written before the posting exists, so the key cannot contain posting_id: a NULL-bearing unique key never collides, and a replay would tax twice. The key is (account_id, jurisdiction, remittance_period), with remittance_period NOT NULL because it is part of that key.
  • Both counterparty accounts are resolved through CounterpartyKind, which pairs every internal account with its SEED_ACCOUNTS key. A variant with no seeded account would resolve to None and the feature would silently do nothing, so the pairing is asserted by test.

Equity distribution (optional product module)

Equity is already a balance-sheet account type, so the ledger can represent capital. The gap this closes is the link from a declared distribution to the holders entitled to it, and the arithmetic of that split.

Entitlement is share-days within the period, not a snapshot. The weight a holder carries into the split is units x days held, where the span is clipped into the declared period: from the later of the holding's opened_on and period_start, to the earlier of closed_on and period_end. A holder present for 1 day of a 90-day period is entitled to 1/90; a holder who bought the day before the record date earns that day only. Paying an end-of-period snapshot pays the recent buyer as if they had held from the start - the common error, and the one the reference implementation is careful to avoid.

Documented limitation. Until a per-purchase equity transaction ledger exists, a mid-period top-up is weighted from the holding's opened_on, so the holder is under-entitled for the units added later. It is conservative - it never over-pays - and it is stated rather than hidden.

Why largest remainder, not per-holder rounding. A declared amount is a fixed sum. Three equal holders of 100.00 are each owed 33.333, which rounds to 33.33 three times - 99.99 distributed, a cent stranded on the liability every period, and the declaration never fully discharged. Rounding up instead pays 100.02: more than was declared. crate::equity::allocate floors every share, then distributes the leftover currency units to the largest fractional parts, so the parts sum to the whole exactly. The tie-break is the account id, so a rerun over the same register allocates identically.

Why the reference implementation's arithmetic is not copied. Apache Fineract computes amountPerShareDay = amount.doubleValue() / numberOfShareDays and multiplies per account. That is a double holding money, which bank8 forbids (invariant 10), and it does not guarantee the per-account amounts sum to the declared amount - the stranded-cent problem reappears through the floating division. We adopt its entitlement basis (share-days) and reject its arithmetic.

Why a true floor and not round_dp. round_dp is half-up and can round a share UP; clamping that with .min(exact) returns the unrounded value and leaves a holder with more decimal places than the currency has. The allocation uses RoundingStrategy::ToZero, the only operation that guarantees floored <= exact at exactly the scale. The leftover is quantised before it is handed out and the loop is bounded, because this runs inside COB: a caller passing more precision than the scale must not hang the run.

Why the declaration is claimed by an INSERT. The scheduled-work loop visits every customer account in scope, so a whole-environment run reaches this branch hundreds of times. A dividend is declared once per class per period, not once per account, so correctness cannot depend on which account the loop reached first. The declaration is claimed under UNIQUE (env_id, class, period): the first caller inserts and proceeds, every later one collides and returns. That makes the branch safe under a Cron retry, a concurrent run and a manual replay with no coordination in the worker.

Why a distribution is not an expense. Payout debits Retained Earnings: profit leaving equity. Booking it to expense would understate equity and overstate cost in the same period the profit is distributed.

Out of scope. Share issuance, transfer and redemption. Those are ordinary account movements in the general ledger; the register reads holdings, it does not create them.

4. API Surface​

All 91 utoipa-declared endpoints, grouped by family with their authorization gates (Shield step-5 targets; every handler also WHERE env_id + RLS):

FamilyEndpointsGate
AccountsGET/POST /v1/accounts, GET /v1/accounts/{id}, …/balances, …/transactions, …/statements, …/schedules, …/attributes, …/devices, …/schedule-tagsbank8:ledger:accounts:read / :write
ProductsGET/POST /v1/products, GET /v1/products/{id}, POST …/{id}/activate, …/{id}/versionsbank8:ledger:products:read / :write / :publish
PostingsPOST /v1/postings, POST /v1/postings/async, GET /v1/postings/{id}, GET …/instructions, GET /v1/post-posting-failures, POST …/{id}/republishbank8:ledger:postings:read / :write / :dry_run
Balances v2GET /v2/balances/live, /v2/balances/end, /v2/balances/seriesbank8:ledger:balances:read
SchedulesGET/POST /v1/schedules, GET /v1/schedules/{id}, /v1/schedule-tags, …/{id}/assocs, /v1/account-schedule-tagsbank8:ledger:schedules:read / :write
Jobs/DLQGET /v1/jobs, …/{id}, POST …/{id}/retry, …:batchRepublish, GET /v1/dlq, POST /v1/dlq/{id}/republishbank8:ledger:jobs:read / :admin, dlq:read / :republish
CalendarsGET/POST /v1/calendars, …/{id}/events, …/{id}/periods, /v1/calendar/bookkeeping-date/{date}bank8:ledger:schedules:*
Parameters/v1/parameters, /v1/parameter-values, …:viewEffective, /v1/parameter-value-hierarchy-nodes, /v1/derived-parameter-values, /v1/global-parameters + /{id}bank8:ledger:parameters:*
Flags/v1/flag-definitions, /v1/flagsbank8:ledger:flags:*
Restrictions/v1/restriction-definitions, /v1/restriction-setsbank8:ledger:restrictions:*
Plans/v1/plans, …/{id}, …/{id}/migrations, /v1/account-plan-assocsbank8:ledger:plans:read / :write
Contracts/v1/contract-modules, /v1/contract-events, /v1/contracts/simulate, /v1/supervisor-contracts + /{id}bank8:ledger:contracts:simulate
CustomersGET/POST /v1/customers, GET …/{id}, POST …/{id}/link-auth, POST …/{id}/updatebank8:ledger:customers:write
AdjustmentsGET/POST /v1/adjustments, GET …/{id}, POST /v1/adjustments/bulk-retrybank8:ledger:audit:read family
Internal accountsGET/POST /v1/internal-accounts, GET …/{id}staff-realm surface
JournalGET /v1/journal-events, …/{id}, POST …/{id}/replaybank8:ledger:journal:read / :replay
ProcessingGET/POST /v1/processing-groups, POST …/{id}/pause, …/{id}/resumebank8:ledger:processing:*
Bulk ops/v1/account-migrations, /v1/account-update-batches, POST …/{id}/bulk-retrybank8:ledger:accounts:write
Ops/auditGET /v1/audit, /v1/audit/action-logs, GET /v1/streamsbank8:ledger:audit:read
HealthGET /v1/healthnone

Internal (proxy-back, M2M bearer + env cross-check):

EndpointCallerContract
POST /v1/internal/schedules/runSentin8:Crondue-schedule batch generation
POST /v1/internal/func-hostSentin8:Functenant hook execution (module ids from schedule contracts)

Admin/SDUI: /v1/admin/ui/{page} + /v1/admin/{resource} POST aliases

  • 19 pages (overview, accounts, postings, products, schedules, dlq, reconcile, balances, parameters, restrictions, flags, customers, statements, settings, policy-sets, policy-decisions, relations, attributes) - bank8:ledger:admin scoped.

Complete event catalog (family ledger, DBX producer path):

Event typeTriggerPayload
bank8:ledger:posting:createdsync posting batch / replay / schedule run{posting_id, batch_id, replay?, schedule_type?}
bank8:ledger:posting:async:requestasync posting enqueue (REST produce){posting_id}
bank8:ledger:posting:republishedDLQ republish{posting_id, original_error}
bank8:ledger:account:created / :updatedaccount create/update{account_id, product_id, customer_id?} / {account_id, fields_changed[]}
bank8:ledger:product:created / :updated / :activatedproduct lifecycle{product_id, product_type} / {env_id}
bank8:ledger:productversion:createdversion create{product_id, version_id}
bank8:ledger:customer:created / :updated / :authlinkedcustomer lifecycle + Auth link{customer_id, display_name} / {sentin8_auth_user_id}
bank8:ledger:adjustment:createdadjustment create{adjustment_id, account_id, reason}
bank8:ledger:schedule:createdschedule create{schedule_id, account_id?}
bank8:ledger:job:retried / :batchrepublishedjob ops{job_id} / {job_ids[]}
bank8:ledger:flagdefinition:created / flag:createdflag library{flag_definition_id} / {flag_id, account_id}
bank8:ledger:restrictiondefinition:created / restrictionset:createdrestriction library{restriction_definition_id} / {restriction_set_id, account_id}
bank8:ledger:parameter:created / parametervalue:created / hierarchynode:createdparameter library{parameter_id} / {parameter_id, value_id} / {node_id}
bank8:ledger:calendar:created / calendarevent:created / bookkeepingdate:updatedcalendar model{calendar_id} / {event_id, event_type} / {date}
bank8:ledger:plan:created / planmigration:createdplan lifecycle{plan_id} / {plan_id, migration_id}
bank8:ledger:processinggroup:updatedgroup pause/resume{group_id, fields_changed[]}
bank8:ledger:internalaccount:created / paymentdevice:created / supervisorcontract:createdinternal/extras/contracts{account_id} / {device_id} / {contract_id}
bank8:ledger:scheduletag:createdtag create{tag_id}

5. Cross-Cutting Concerns​

  • Auth: platform 8-step pipeline; step 5 is the only gate (embedded Shield, live-resolved roles, enforced scope-mirror set).
  • Tenant resolution: token env_id claims; X-Sentin8-Env-Id honored for BFF-forwarded requests; unresolved context is rejected.
  • RLS context: set_tenant_context[_ext] on the same connection/transaction as the data query; explicit WHERE env_id = $N clauses are defense-in-depth.
  • Idempotency: posting (env_id, idempotency_key) partial unique index + ON CONFLICT; outbox row id is the produce idempotency key.
  • Audit: domain writes to audit_events (posting path: fail-closed); canonical journal rows push to Sys via step 8c.
  • Rate limiting: platform middleware stack (proxy-tier).
  • Graceful drain: health shares the traffic counter gauge; Captain drains on upgrade-instance.
  • TLS: in-process PQ-TLS via sentin8-tls; X-Sentin8-* context headers are the only tenant source.

6. Failure Modes & Recovery​

  • Crash mid-transaction: atomic rollback; no partial posting.
  • JWKS/credential rotation: standard platform verify path; the workload identity is cert-file-based (minted-or-loaded from Acme), survives restarts.
  • Pool saturation: capped pool (≤3); requests queue, never cascade.

7. Concurrency & Performance Model​

  • Tasks: shell relays/pushers + event_consumer (sys-family SSE); no other background loops.
  • Posting batches process instructions sequentially in one transaction; per-product module context is cached per request (HashMap) to avoid re-reading product parameters per instruction.
  • Balance updates use UPSERT (ON CONFLICT … DO UPDATE) so concurrent postings on one account serialize on the row, not the table.
  • Hot paths: account/balance reads (indexed on (account_id, denomination, address, phase)), schedule due-scan (indexed on due-at + status).

8. Invariants, Insecure-Defaults Rejected, Unsafe Rationale​

  • A posting batch is double-entry balanced per denomination or is rejected before any write.
  • Terminal states (posting completed, instruction terminal, account closed) never regress - every transition is guarded.
  • idempotency_key duplicates return the original result, never a second posting.
  • Money columns are DECIMAL; no float arithmetic on money anywhere.
  • No unsafe code in the service.
  • Rejected defaults: silent let _ = on in-transaction writes (mandatory ? propagation); header-based tenant override without token binding.

9. Testing Strategy​

  • Unit: the module library and posting math are pure-function tested; contracts round-trips pin the wire format.
  • Integration: policy wiring suites + handler suites against a scratch database.
  • Deliberately not tested: the platform's transport layers (TLS/bus/relay), Flutter app UIs.
  • Distribution arithmetic is property-tested: a sweep asserts every declared amount distributes exactly across many unit weightings, because a stranded cent per period is invisible in a single-case test. Schema invariants (FORCE RLS and the two idempotency keys) are asserted against the live database rather than assumed, since a lost constraint fails silently.

10. Open Questions​

  • Statement generation - implemented (design §10): the scheduled step computes the period rows and writes the JSON document to Sentin8:Storage via the ledger-storage workload certificate (sentin8:storage:env:write), and regeneration is a per-account-period Flow workflow (domain: ledger, logic_id: statement-regen, correlation_id = account_id:period) whose steps proxy back into POST /v1/internal/statements/regen - per-account retry/DLQ, and Flow's pending-dedupe partial index makes regenerate-idempotency structural.
  • Parameter-hierarchy caching - implemented (design §10): the viewEffective read API serves from a bounded TTL cache keyed (env_id, account_id, parameter_id, effective_at) (30 s, prune-on-insert eviction); the posting money path stays transactional by design.
  • /v1/jobs:batchRepublish colon-form - closed: the colon-form is canonical (the vendor wire-compat custom-method form), and the semantic follow-up landed: the override now lands in a distinct overridden status (CHECK constraint + handler), preserving the audit intent.

Architecture & Design: bank8:ledger and its underlying concepts were designed by Hamid Jahanzad.