inflight.md 7.9 KB

Inflight holds

An inflight hold lets you reserve funds now and settle later. Authorize a trade, then confirm it in full or in parts, or void it. This is the authorization and capture pattern, applied to a multi-leg trade.

adr/0014-inflight-holds-via-holding-accounts.md holds the design and its rationale. This page is the usage guide.

A hold is one instance of a general pattern. This ledger has no pending state, so a workflow state is value parked in an account that you control. pending-states.md states that rule, and works escrow through as a second instance. Start there when the shape you need is not authorise and then capture.

Model

An inflight transaction is an ordinary trade, and the ledger rewrites every destination of it to a fresh per-destination holding account. That account forbids overdraft through the DEBIT_MUST_NOT_EXCEED_CREDIT flag, and it carries the INFLIGHT flag. A commit of the rewritten transfer parks the funds:

Confirmed trade            Inflight form
-----------------          --------------------------------
A -> B   -> 100 EUR        A -> hold(B)   -> 100 EUR
B -> A   ->  10 BTC        B -> hold(A)   ->  10 BTC
A -> fee ->   1 BTC        A -> hold(fee) ->   1 BTC
B -> fee ->   1 EUR        B -> hold(fee) ->   1 EUR

A hold is keyed by destination, so hold(fee) collects EUR from B and BTC from A. The leg table of the authorize transfer records the funder of each holding posting. So a void returns each posting to the account that paid it.

The ledger stores nothing new. The authorize transfer is the record. Its TransferId is the inflight handle, and its metadata carries the leg table. Every artifact carries a CBOR-encoded payload under one inflight metadata key, and that covers the holding accounts, the authorize, the confirm and the void. So a reader takes the lifecycle from recorded fields and infers nothing.

Lifecycle

stateDiagram-v2
    [*] --> Held: authorize
    Held --> Held: confirm (partial)
    Held --> Confirmed: confirm_all / drained
    Held --> Voided: void
    Held --> Voided: expire (past its expiry)
    Confirmed --> [*]
    Voided --> [*]

Every operation is an ordinary commit. So idempotency, conservation and crash recovery all carry over unchanged. A hold closes by itself once something drains it.

API

Every method hangs off Ledger.

use kuatia::prelude::*;

// Authorize the trade. Funds leave A and B and park in the holds.
let trade = TransferBuilder::new()
    .pay(a, b, ("100.00", &eur))
    .pay(b, a, ("10.00000000", &btc))
    .pay(a, fee, ("1.00000000", &btc))
    .pay(b, fee, ("1.00", &eur))
    .build()?;
let auth = ledger.authorize(trade).await?;

// Confirm one or more legs, built with the same .pay() interface as a transfer
// (from = funder, to = destination). Deliver 40 EUR of B's hold to B now:
let some = TransferBuilder::new()
    .pay(a, b, ("40.00", &eur))
    .build()?;
ledger.confirm(&auth.inflight, some).await?;

// Confirm everything else and close the holds.
ledger.confirm_all(&auth.inflight).await?;

// ...or return everything to the funders instead.
ledger.void(&auth.inflight).await?;

// Derived status: per-leg authorized / confirmed / voided / held, plus state.
let status = ledger.inflight_status(&auth.inflight).await?;

// The holding accounts of every open inflight.
let open = ledger.list_open_inflights().await?;

// Authorize with a deadline, as unix milliseconds.
let auth = ledger.authorize_until(trade, 1_767_225_600_000).await?;

// Void one inflight if its expiry has passed. Nothing happens if it has not.
ledger.expire(&auth.inflight, now_ms).await?;

// Sweep every open hold whose expiry has passed, and report what was voided.
let voided = ledger.expire_due(now_ms).await?;

authorize returns an Authorization { inflight, receipt, legs }. The inflight field is a TransferId, and it is the handle that every other call takes.

Guarantees

  • Over-confirmation is impossible. A hold forbids overdraft, because the DEBIT_MUST_NOT_EXCEED_CREDIT flag is set. So a confirm of more than it holds fails validation, and the sum of the confirms can never exceed the authorized amount.
  • No double-spend under concurrency. Concurrent confirms serialize on the shared holding posting. The delete inside the commit transaction is the atomic single-winner claim, so one confirm wins. The caller retries the other one against the new balance that remains.
  • State is derived. The amount a leg still holds is balance(hold, asset). The ledger sums the confirmed and voided amounts from the settlement transfers that the metadata tags. It stores nothing mutable.

Subaccounts and concurrency

A hold is a subaccount of its destination, written (destination, sub). A hash of the submitted trade derives sub. See adr/0012-subaccounts.md. All the holds of one inflight share that sub.

A different trade derives a different sub, so one destination can host many concurrent inflights at once. Each one is isolated in its own subaccount, and ledger.list_subaccounts(&destination) lists them. A second authorize of the identical trade collides with the hold that exists, and the ledger rejects it.

The ledger always segregates a balance per subaccount. ledger.balances(&base, &asset, None) lists the main account and every open hold separately, and never sums them.

Constraints and limitations

  • Distinct movements. Every movement in an authorize must move between two different accounts.
  • Void needs an open payer. A void returns funds to the original funder, so that account must still be open.
  • Single funder per (hold, asset). Two accounts sometimes fund the same asset into the same destination hold. The ledger then cannot split a partly confirmed remainder back to each funder exactly, and a void returns it in leg order.
  • Books. The ledger creates a hold in the book of the authorize transfer. A book that restricts participation by flag or by account must admit the holds, and one way is to allow the INFLIGHT flag.

Expiry

An authorization with no expiry holds its funds until a caller settles it. That parks money forever if the caller never comes back. authorize_until records a deadline on each holding account at creation, as unix milliseconds. authorize is the same call with no deadline.

  • The deadline is on the hold and not in a timer. The ledger writes it into the metadata of the holding account, which is create-only. So it cannot drift afterwards. list_open_inflights already returns exactly those accounts. A sweep therefore reads no transfer body.
  • Nothing expires on its own. expire_due(now) is a method that a caller schedules. The balance projection refuses to own a timer for the same reason (adr/0019-cached-balance-projection.md). The caller passes the clock in, which also makes the behaviour testable with no wait.
  • Expiry is a void. It runs the same void path, so there is one settlement route. Funds return to the recorded funders. A partly confirmed hold returns only its remainder, and what a confirm delivered stays delivered.
  • A sweep is idempotent. A void drains and closes the holds, so the inflight is no longer open and a second sweep finds nothing. expire on an inflight that is already settled returns no receipt, rather than an error.

The ledger reports an expired hold once per inflight and not once per leg. That holds even when several legs of one authorization share the deadline.

Where it lives

  • crates/kuatia/src/inflight.rs: the API and metadata schema.
  • crates/kuatia/tests/inflight.rs: the tests for authorize, confirm, partial confirm, void, over-confirm rejection, concurrent inflights per account, segregated balances, status and expiry.
  • AccountFlags::INFLIGHT: crates/kuatia-types/src/lib.rs.