|
| 1 | +# Universe Supply Commitment State Machine |
| 2 | + |
| 3 | +This package implements a state machine responsible for managing the on-chain |
| 4 | +commitment to the supply of a specific Taproot Asset (identified by its |
| 5 | +`asset.Specifier`, namely the group key). |
| 6 | + |
| 7 | +## Rationale and Purpose |
| 8 | + |
| 9 | +Taproot Assets allow for issuance and transfer off-chain, but maintaining a |
| 10 | +verifiable, global view of the *total supply* requires an on-chain commitment |
| 11 | +mechanism. This state machine addresses that need. |
| 12 | + |
| 13 | +Its primary purpose is to: |
| 14 | + |
| 15 | +1. **Track Supply Changes:** Receive events representing new asset mints |
| 16 | + (`NewMintEvent`), asset burns (`NewBurnEvent`), and ignored outputs |
| 17 | + (`NewIgnoreEvent`). |
| 18 | + |
| 19 | +2. **Batch Updates:** Collect these supply-altering events over time. |
| 20 | + |
| 21 | +3. **Commit Periodically:** On a regular trigger (`CommitTickEvent`), create a |
| 22 | + new cryptographic commitment (an MS-SMT root) representing the current state |
| 23 | + of the asset's supply. This involves separate sub-trees for mints, burns, and |
| 24 | + ignores, which are then committed into a single root supply tree. |
| 25 | + |
| 26 | +4. **Anchor On-Chain:** Construct, fund, sign, and broadcast a Bitcoin |
| 27 | + transaction that anchors this new supply root commitment into the |
| 28 | + blockchain. This transaction spends the previous commitment output (if one |
| 29 | + exists) and any relevant pre-commitment outputs (from minting transactions). |
| 30 | + |
| 31 | +5. **Finalize State:** Wait for the commitment transaction to confirm on-chain |
| 32 | + and then finalize the state update, making the new commitment the canonical |
| 33 | + one for the asset. |
| 34 | + |
| 35 | +This ensures that there is a publicly verifiable, tamper-proof record reflecting |
| 36 | +the known supply changes for an asset within the Universe. |
| 37 | + |
| 38 | +## Scope |
| 39 | + |
| 40 | +The state machine handles the lifecycle of a single supply commitment update |
| 41 | +cycle for a *specific asset specifier*. It manages: |
| 42 | + |
| 43 | +* Receiving and staging supply update events. |
| 44 | +* Calculating the new supply root based on staged updates and previous state. |
| 45 | +* Interacting with the `Wallet` interface to: |
| 46 | + * Derive keys for commitment outputs. |
| 47 | + * Fund the commitment transaction. |
| 48 | + * Sign the commitment transaction. |
| 49 | +* Interacting with the `ChainBridge` to broadcast the transaction and monitor |
| 50 | + for confirmation. |
| 51 | + |
| 52 | +* Persisting its state and pending updates via the `StateMachineStore` to |
| 53 | + ensure resilience across restarts. |
| 54 | + |
| 55 | +It does *not* handle: |
| 56 | + |
| 57 | +* Discovering supply update events (this is done by other parts of the system |
| 58 | + feeding events into the state machine). |
| 59 | + |
| 60 | +* Serving supply proofs to external requesters (this is handled by the broader |
| 61 | + Universe server components). |
| 62 | + |
| 63 | +* Managing commitments for multiple different assets simultaneously (each |
| 64 | + asset specifier typically gets its own state machine instance). |
| 65 | + |
| 66 | + |
| 67 | +## State Machine Overview |
| 68 | + |
| 69 | +The state machine transitions through several states to process supply updates |
| 70 | +and create a new on-chain commitment. It starts in an idle `DefaultState`. When |
| 71 | +new supply updates arrive, it moves to `UpdatesPendingState`. A periodic |
| 72 | +`CommitTickEvent` triggers the commitment process, moving through states for |
| 73 | +tree creation (`CommitTreeCreateState`), transaction creation |
| 74 | +(`CommitTxCreateState`), signing (`CommitTxSignState`), broadcasting |
| 75 | +(`CommitBroadcastState`), and finally finalization (`CommitFinalizeState`) upon |
| 76 | +confirmation, before returning to the `DefaultState`. |
| 77 | + |
| 78 | +### States and Transitions |
| 79 | + |
| 80 | +```mermaid |
| 81 | +stateDiagram-v2 |
| 82 | + [*] --> DefaultState: Initialize / Finalize |
| 83 | +
|
| 84 | + DefaultState --> UpdatesPendingState: SupplyUpdateEvent |
| 85 | + DefaultState --> DefaultState: CommitTickEvent |
| 86 | +
|
| 87 | + UpdatesPendingState --> UpdatesPendingState: SupplyUpdateEvent |
| 88 | + UpdatesPendingState --> CommitTreeCreateState: CommitTickEvent (emits CreateTreeEvent) |
| 89 | +
|
| 90 | + CommitTreeCreateState --> CommitTxCreateState: CreateTreeEvent (emits CreateTxEvent) |
| 91 | + CommitTreeCreateState --> CommitTreeCreateState: CommitTickEvent |
| 92 | +
|
| 93 | + CommitTxCreateState --> CommitTxSignState: CreateTxEvent (emits SignTxEvent) |
| 94 | +
|
| 95 | + CommitTxSignState --> CommitBroadcastState: SignTxEvent (emits BroadcastEvent) |
| 96 | +
|
| 97 | + CommitBroadcastState --> CommitBroadcastState: BroadcastEvent (Broadcasts Tx, Registers Conf) |
| 98 | + CommitBroadcastState --> CommitFinalizeState: ConfEvent (emits FinalizeEvent) |
| 99 | +
|
| 100 | + CommitFinalizeState --> DefaultState: FinalizeEvent (Applies Transition) |
| 101 | +``` |
| 102 | + |
| 103 | +* **DefaultState:** The idle state. Awaiting new supply updates. |
| 104 | + |
| 105 | +* **UpdatesPendingState:** One or more supply updates have been received and |
| 106 | + are staged, waiting for the next commit trigger. |
| 107 | + |
| 108 | +* **CommitTreeCreateState:** Triggered by `CommitTickEvent`. Fetches existing |
| 109 | + sub-trees, applies pending updates, calculates the new sub-tree roots, and |
| 110 | + calculates the new root supply tree. |
| 111 | + |
| 112 | +* **CommitTxCreateState:** Triggered by `CreateTreeEvent`. Fetches the |
| 113 | + previous commitment (if any) and unspent pre-commitments. Constructs the new |
| 114 | + commitment transaction (PSBT) spending these inputs and creating the new |
| 115 | + commitment output. Funds the PSBT using the wallet. |
| 116 | + |
| 117 | +* **CommitTxSignState:** Triggered by `CreateTxEvent`. Signs the funded |
| 118 | + commitment PSBT using the wallet. Persists the signed transaction details and |
| 119 | + state before broadcasting. |
| 120 | + |
| 121 | +* **CommitBroadcastState:** Triggered by `SignTxEvent`. Broadcasts the signed |
| 122 | + transaction and registers for its confirmation. Waits for the `ConfEvent`. |
| 123 | + |
| 124 | +* **CommitFinalizeState:** Triggered by `ConfEvent`. The commitment |
| 125 | + transaction is confirmed. Finalizes the state transition by updating the |
| 126 | + canonical supply trees and commitment details in persistent storage. |
| 127 | + Transitions back to `DefaultState`. |
| 128 | + |
| 129 | +## Environment and Persistence |
| 130 | + |
| 131 | +The state machine operates within an `Environment` that provides necessary |
| 132 | +dependencies like the `Wallet`, `ChainBridge`, `CommitmentTracker`, |
| 133 | +`SupplyTreeView`, and `StateMachineStore`. The `StateMachineStore` is crucial |
| 134 | +for persisting the current state and any pending updates or intermediate |
| 135 | +transaction data, allowing the machine to resume correctly after restarts. |
0 commit comments