Phase 2 section 7 (the other half, alongside sdk/go/ration and sdk/rust/ration-client already pushed in c5297e4/8215fb8): a hand-rolled internal/httpapi on services/gateway translating JSON <-> the gRPC source of truth (cards, deposits, bridge funding, activity), documented as a convenience facade over gRPC, not a parallel implementation. Also folds in the remaining phase-2 doc updates (ARCHITECTURE/RUNBOOK/THREAT-MODEL/ AIRWALLEX-SETUP/SUBMISSION, ADR 0003/0004) and docker-compose/Makefile/ .env.example wiring from the resumed phase-2 workflow's final agents. Verified: go build/vet/test clean across all 10 services + both new SDKs, `make build && make test` clean end to end (C WAL, Rust ledger-core + the new Rust SDK, every Go module including sdk/go/ration). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
149 lines
8.1 KiB
Markdown
149 lines
8.1 KiB
Markdown
# ADR 0003: gRPC as the source of truth, REST as a convenience facade
|
|
|
|
## Status
|
|
|
|
Accepted and fully implemented. Every service-to-service call in the
|
|
codebase is gRPC over the proto contracts in `proto/`, and the external
|
|
REST/JSON facade and SDKs described in `docs/SCALE-EXPANSION.md` section
|
|
7 are now built — see Consequences for exactly what exists and where.
|
|
|
|
## Context
|
|
|
|
Every contract in this system is already defined once, in `proto/`:
|
|
`common.proto` (shared types: `Money`, `LedgerEntryType`, `AgentAction`,
|
|
`DepositStatus`), `ledger.proto` (`LedgerCore` service, consumed by
|
|
`services/gateway/internal/ledgerclient` and `services/agent/internal/
|
|
ledgerclient`), `gateway.proto` (`Gateway` service: `IssueCard`,
|
|
`AdjustCardLimit`, `RationBridgeFunding`, `StreamActivity`, consumed by
|
|
the TUI and CLI), and `audit.proto`. `make proto` (`scripts/gen-proto.sh`)
|
|
generates Go stubs into `pkg/pb/` (checked in, per `ARCHITECTURE.md`, "so
|
|
judges don't need protoc to build") and Rust stubs via `tonic-build` in
|
|
`ledger-core`'s `build.rs`.
|
|
|
|
Two different audiences need to talk to this system:
|
|
|
|
1. **Services talking to each other** — gateway → ledger-core, agent →
|
|
gateway → ledger-core, every hop needing low latency, strict typing,
|
|
bidirectional streaming (`StreamActivity`), and (per section 8) mTLS
|
|
client certs issued per service.
|
|
2. **Humans and simple tooling** — a judge or integrator without a gRPC
|
|
client installed who wants to `curl` the gateway, and third-party
|
|
backend consumers who would reasonably expect a typed SDK rather than
|
|
hand-rolling gRPC stubs themselves.
|
|
|
|
## Decision
|
|
|
|
gRPC over the `proto/` contracts remains the single source of truth for
|
|
every service boundary; nothing in this system is ever defined twice.
|
|
Where a non-gRPC audience needs access (the `curl`-able case, external
|
|
integrators), that access is built as a thin facade *in front of* the
|
|
existing gRPC service — translating JSON <-> the generated gRPC
|
|
client — not as a second, independently-maintained implementation of the
|
|
business logic.
|
|
|
|
Concretely, per `docs/SCALE-EXPANSION.md` section 7:
|
|
|
|
- `services/gateway` gets a REST/JSON facade (either `grpc-gateway`
|
|
reverse-proxy annotations on `gateway.proto`, or a hand-rolled
|
|
`internal/httpapi` package translating JSON <-> the `Gateway` gRPC
|
|
client) that sits in front of the same `grpcserver.Server` /
|
|
`bridge.Service` / `card.Service` code paths gateway already runs for
|
|
its gRPC listener — never a parallel implementation of the rationing
|
|
math or card-issuance logic.
|
|
- `sdk/go/ration` and `sdk/rust/ration-client` wrap the generated gRPC
|
|
stubs with ergonomic constructors — they are typed clients for the
|
|
gRPC surface, not an alternate protocol.
|
|
- Documentation for the facade states explicitly, in its own words, "REST
|
|
is a convenience facade over the gRPC source of truth," so no consumer
|
|
mistakes the JSON surface for an independently-versioned API.
|
|
|
|
Why gRPC first rather than REST first:
|
|
|
|
- **Strict typing end to end.** `Money{minor_units, currency}` as an
|
|
`int64` + ISO 4217 string (`ARCHITECTURE.md`) round-trips through
|
|
protobuf without the float-vs-string ambiguity a hand-written JSON
|
|
schema would have to re-litigate at every service boundary.
|
|
- **Streaming is a first-class proto feature.** `StreamActivity` (in
|
|
`gateway.proto`) is a server-streaming RPC consumed directly by the
|
|
TUI's activity feed (`services/tui/internal/client/grpc.go`) and
|
|
gateway's internal fan-out (`internal/activity/stream.go`) — REST has
|
|
no equivalent primitive; it would need SSE or WebSockets layered on
|
|
top, which is exactly the kind of second protocol this ADR avoids
|
|
making a second source of truth.
|
|
- **mTLS and interceptor-based cross-cutting concerns compose cleanly on
|
|
gRPC.** Section 8's service-to-service mTLS (`pkg/mtls`) and section
|
|
2's trace/metric propagation (`pkg/telemetry`'s gRPC client/server
|
|
interceptors, `pkg/metrics`'s gRPC interceptors) are both written once,
|
|
against gRPC's interceptor chain, and apply uniformly to every
|
|
service-to-service call without being re-implemented for a second
|
|
transport.
|
|
- **Generated stubs are already the integration point.** `pkg/pb/` is
|
|
already checked into the repo specifically so nothing downstream (Go
|
|
services, the planned Go/Rust SDKs, a REST facade's translation layer)
|
|
needs its own hand-maintained copy of the wire format.
|
|
|
|
## Alternatives considered
|
|
|
|
- **REST/JSON as the primary (and only) protocol.** Rejected: loses
|
|
native streaming for `StreamActivity`, loses compile-time-checked
|
|
request/response types across language boundaries (Go gateway, Rust
|
|
ledger-core, Go agent/tui/cli), and would require hand-maintaining an
|
|
OpenAPI schema in parallel with `proto/` — exactly the "two sources of
|
|
truth" failure mode this ADR exists to avoid.
|
|
- **GraphQL facade instead of REST.** Rejected as unnecessary complexity
|
|
for this system's actual external consumers (judges running `curl`,
|
|
backend integrators who want a typed SDK) — a REST/JSON facade with a
|
|
stated single responsibility (convenience over the real gRPC API)
|
|
serves that need with far less machinery, and `grpc-gateway`-style
|
|
reverse-proxy annotations give REST "for free" from the proto
|
|
definitions that already exist, with no new schema to maintain.
|
|
- **Two independently maintained APIs (gRPC internal, hand-written REST
|
|
external with its own handlers reimplementing the business logic).**
|
|
Rejected explicitly: that duplicates `bridge.Service`'s rationing math
|
|
and `card.Service`'s issuance flow behind two code paths that WILL
|
|
drift, which is precisely the production-reliability failure mode
|
|
"fintech bar, not hackathon shortcuts" (`SCALE-EXPANSION.md`'s standing
|
|
rule) rules out.
|
|
|
|
## Consequences
|
|
|
|
- `services/gateway/internal/httpapi` is the hand-rolled facade this ADR
|
|
called for: `cmd/gateway/main.go` dials this process's own gRPC
|
|
listener (a loopback `GatewayClient`, `GATEWAY_GRPC_DIAL_ADDR`) and
|
|
passes that client into `httpapi.NewHandler` — every REST handler in
|
|
that package calls exactly that client, never
|
|
`internal/cardissuanceclient`, `internal/ledgerclient`, or any other
|
|
business-logic package directly. That boundary (REST handlers may only
|
|
import the generated `Gateway` client) is enforced by code review
|
|
today, same as the day this ADR was first written; the lint/CI check
|
|
described below is still a follow-up, not yet built.
|
|
- `sdk/go/ration` and `sdk/rust/ration-client` are built: thin typed
|
|
clients wrapping the generated `gateway.proto` stubs (Go via
|
|
`pkg/pb/gateway`, Rust via `tonic-build` against `proto/gateway.proto`
|
|
directly in the SDK's own `build.rs`) with ergonomic method
|
|
signatures. Neither reimplements any RPC's behavior — both are pure
|
|
wire-format + transport, exactly as this ADR specified.
|
|
- The REST facade is documented, in its own package doc comment
|
|
(`services/gateway/internal/httpapi`'s doc.go-equivalent top-of-file
|
|
comment) and in this ADR, as "a convenience facade over the gRPC
|
|
source of truth" — curl-able at `GATEWAY_HTTP_ADDR` (default
|
|
`localhost:8082`), e.g. `curl http://localhost:8082/v1/tenants/
|
|
tenant_1/cards`. `StreamActivity`'s gRPC server-streaming RPC has no
|
|
REST equivalent primitive (as predicted below): the facade
|
|
approximates it with newline-delimited JSON on `GET /v1/activity`,
|
|
explicitly documented as an approximation, not a second streaming
|
|
protocol with its own semantics.
|
|
- **Known, precisely-scoped gap**: a `GetCard` call for a nonexistent
|
|
card ID currently surfaces as gRPC code `Unknown` / HTTP 500 through
|
|
this facade, not `NotFound` / 404, because
|
|
`services/card-issuance/internal/cardsvc.Service.GetCard` returns a
|
|
plain wrapped Go error rather than a `status.Error(codes.NotFound,
|
|
...)` — every `GatewayClient` caller (TUI, CLI, this facade, both
|
|
SDKs) is equally affected, and the fix is scoped to
|
|
`services/card-issuance/internal/grpcserver` wrapping that one error,
|
|
not to anything in this ADR's scope.
|
|
- Every new RPC (e.g. a future `webhook-ingestion` or `card-issuance`
|
|
split per section 1 / ADR 0004) must be added to `proto/` first; a REST
|
|
endpoint or SDK method with no corresponding `.proto` RPC is a review
|
|
rejection, not a style nit.
|