Balance-invariant Transfers
For some accounts, it may be useful to enforce flags.debits_must_not_exceed_credits
or flags.credits_must_not_exceed_debits
balance invariants for only a subset of all transfers, rather than all
transfers.
This can be achieved by having a control account used to test the balance invariants at the desired points in time. The control account will have a 0 balance and the balance invariant that we want to test on the destination account. At the point where we want to test the destination account balance invariant, we can initiate a pending balancing transfer for the opposite side to the control account. If the invariant is violated on the destination account, the balancing transfer has non-zero amount, violates the control account invariant, and fails the entire chain. The following example will make this clearer.
Per-transfer
credits_must_not_exceed_debits
Let’s test a credits_must_not_exceed_debits balance
invariant on a destination account after a particular transfer.
This recipe requires three accounts:
- The source account, to debit.
- The destination account, to credit. (With
neither
flags.credits_must_not_exceed_debitsnorflags.debits_must_not_exceed_creditsset, since in this recipe we are only enforcing the invariant on a per-transfer basis.) - The control account, to test the balance invariant.
The control account should have
flags.credits_must_not_exceed_debitsset.
| Id | Debit Account | Credit Account | Amount | Pending Id | Flags |
|---|---|---|---|---|---|
| 1 | Source | Destination | 123 | - | linked |
| 2 | Destination | Control | 1 | - | linked,
pending,
balancing_debit |
| 3 | - | - | 0 | 2 | void_pending_transfer |
When the destination account’s credits after transfer 1
do not exceed its debits, the chain will succeed. When the destination
account’s credits after transfer 1 exceed its debits,
transfer 2 will fail with exceeds_debits.
Per-transfer
debits_must_not_exceed_credits
This case is symmetric:
- The source is account to credit.
- The destination is account to debit. Neither
flags.credits_must_not_exceed_debitsnorflags.debits_must_not_exceed_creditsare set. - The control account has
flags.credits_must_not_exceed_debitsset.
| Id | Debit Account | Credit Account | Amount | Pending Id | Flags |
|---|---|---|---|---|---|
| 1 | Destination | Source | 123 | - | linked |
| 2 | Control | Destination | 1 | - | linked,
pending,
balancing_credit |
| 3 | - | - | 0 | 2 | void_pending_transfer |