|
| 1 | +--- |
| 2 | +sidebar_position: 5 |
| 3 | +title: Transactions |
| 4 | +description: Understanding transaction types in the Mina protocol implementation |
| 5 | +slug: /developers/transactions |
| 6 | +--- |
| 7 | + |
| 8 | +# Transactions |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +Transactions in Mina represent state changes to the ledger. The protocol |
| 13 | +supports both user-initiated transactions and protocol-generated transactions, |
| 14 | +each serving distinct purposes in the blockchain's operation. |
| 15 | + |
| 16 | +This document provides an entry point for developers to understand how |
| 17 | +transactions are structured and processed in the Rust codebase. |
| 18 | + |
| 19 | +## Transaction Types |
| 20 | + |
| 21 | +The top-level `Transaction` enum represents all possible transactions in the |
| 22 | +Mina protocol: |
| 23 | + |
| 24 | +<!-- CODE_REFERENCE: ledger/src/scan_state/transaction_logic/mod.rs#L1093-L1100 --> |
| 25 | + |
| 26 | +```rust reference title="ledger/src/scan_state/transaction_logic/mod.rs" |
| 27 | +https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/transaction_logic/mod.rs#L1093-L1100 |
| 28 | +``` |
| 29 | + |
| 30 | +### User-Initiated Transactions |
| 31 | + |
| 32 | +**[User Commands](#user-initiated-transactions)** are transactions submitted and |
| 33 | +signed by users: |
| 34 | + |
| 35 | +- **[Payment transactions](#user-initiated-transactions)** - Transfer MINA |
| 36 | + tokens between accounts |
| 37 | +- **[Stake delegation](#user-initiated-transactions)** - Delegate stake to block |
| 38 | + producers |
| 39 | +- **[zkApp commands](#user-initiated-transactions)** - Execute complex |
| 40 | + multi-account zero-knowledge operations |
| 41 | + |
| 42 | +### Protocol Transactions |
| 43 | + |
| 44 | +**Protocol transactions** (also called **system transactions**) are generated |
| 45 | +automatically by the system: |
| 46 | + |
| 47 | +- **[Fee transfers](#protocol-transactions)** - Distribute collected transaction |
| 48 | + fees to block producers |
| 49 | +- **[Coinbase rewards](./transactions/coinbase)** - Issue block production |
| 50 | + rewards |
| 51 | + |
| 52 | +## Transaction Application Model |
| 53 | + |
| 54 | +Transactions in Mina are applied in two phases: |
| 55 | + |
| 56 | +### First Pass (apply_transaction_first_pass) |
| 57 | + |
| 58 | +The first pass validates preconditions and begins transaction application: |
| 59 | + |
| 60 | +- Validates account existence and permissions |
| 61 | +- Checks nonces and balances |
| 62 | +- Applies fee payments |
| 63 | +- Performs initial state changes |
| 64 | +- Returns `TransactionPartiallyApplied` containing transaction state |
| 65 | + |
| 66 | +**Function:** |
| 67 | +[`ledger/src/scan_state/transaction_logic/transaction_partially_applied.rs`](https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/transaction_logic/transaction_partially_applied.rs#L190) |
| 68 | + |
| 69 | +### Second Pass (apply_transaction_second_pass) |
| 70 | + |
| 71 | +The second pass completes the transaction after SNARK work: |
| 72 | + |
| 73 | +- Finalizes account updates |
| 74 | +- Applies protocol-specific logic |
| 75 | +- Updates receipt chain hashes |
| 76 | +- Returns final `TransactionApplied` status |
| 77 | + |
| 78 | +**Function:** |
| 79 | +[`ledger/src/scan_state/transaction_logic/transaction_applied.rs`](https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/transaction_logic/transaction_applied.rs) |
| 80 | + |
| 81 | +## Common Transaction Components |
| 82 | + |
| 83 | +### Fees |
| 84 | + |
| 85 | +All user-initiated transactions require fees. The `Fee` type is generated by a |
| 86 | +macro along with other currency types in |
| 87 | +[`ledger/src/scan_state/currency.rs`](https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/currency.rs#L575-L578). |
| 88 | + |
| 89 | +Fees serve two purposes: they compensate block producers for including |
| 90 | +transactions and prevent network spam by making it economically infeasible to |
| 91 | +flood the network with transactions. Fees are paid by the transaction sender (or |
| 92 | +fee payer in zkApp commands) and distributed to block producers through fee |
| 93 | +transfers. |
| 94 | + |
| 95 | +### Nonces |
| 96 | + |
| 97 | +User accounts maintain nonces to prevent replay attacks. The `Nonce` type is |
| 98 | +generated by a macro along with other currency types in |
| 99 | +[`ledger/src/scan_state/currency.rs`](https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/currency.rs#L575-L578). |
| 100 | + |
| 101 | +Each user-initiated transaction must specify the correct nonce, which increments |
| 102 | +after successful application. |
| 103 | + |
| 104 | +### Memos |
| 105 | + |
| 106 | +Transactions can include optional 32-byte memos: |
| 107 | + |
| 108 | +<!-- CODE_REFERENCE: ledger/src/scan_state/transaction_logic/mod.rs#L548-L548 --> |
| 109 | + |
| 110 | +```rust reference title="ledger/src/scan_state/transaction_logic/mod.rs" |
| 111 | +https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/transaction_logic/mod.rs#L548-L548 |
| 112 | +``` |
| 113 | + |
| 114 | +Memos provide auxiliary information and are included in transaction commitments. |
| 115 | + |
| 116 | +### Slots |
| 117 | + |
| 118 | +A slot is a fixed time interval in the consensus protocol during which a block |
| 119 | +can be produced. Slots are used to measure time in the blockchain and determine |
| 120 | +block production opportunities. |
| 121 | + |
| 122 | +Transactions include validity windows using slot numbers. The `Slot` type is |
| 123 | +generated by a macro along with other currency types in |
| 124 | +[`ledger/src/scan_state/currency.rs`](https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/currency.rs#L575-L578). |
| 125 | + |
| 126 | +The `valid_until` field specifies when a transaction expires. |
| 127 | + |
| 128 | +## Account Creation |
| 129 | + |
| 130 | +Creating new accounts requires an account creation fee (1 MINA by default): |
| 131 | + |
| 132 | +```rust |
| 133 | +ConstraintConstants { |
| 134 | + account_creation_fee: 1_000_000_000, // 1 MINA in nanomina |
| 135 | + // ... |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +The fee is deducted from the amount being transferred to the new account: |
| 140 | + |
| 141 | +- **Payments**: Sender pays `amount + fee`, receiver gets |
| 142 | + `amount - account_creation_fee` (if creating account) |
| 143 | +- **Fee transfers**: Receiver gets `fee_amount - account_creation_fee` (if |
| 144 | + creating account) |
| 145 | +- **Coinbase**: Receiver gets `coinbase_amount - account_creation_fee` (if |
| 146 | + creating account) |
| 147 | + |
| 148 | +## Transaction Status |
| 149 | + |
| 150 | +After application, transactions have a status: |
| 151 | + |
| 152 | +<!-- CODE_REFERENCE: ledger/src/scan_state/transaction_logic/mod.rs#L240-L243 --> |
| 153 | + |
| 154 | +```rust reference title="ledger/src/scan_state/transaction_logic/mod.rs" |
| 155 | +https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/transaction_logic/mod.rs#L240-L243 |
| 156 | +``` |
| 157 | + |
| 158 | +Failed transactions may still consume fees in some cases (zkApp commands with |
| 159 | +fee payer failures). |
| 160 | + |
| 161 | +## Further Reading |
| 162 | + |
| 163 | +- [Coinbase Rewards](./transactions/coinbase) - Block production rewards |
| 164 | + |
| 165 | +Documentation for additional transaction types is coming soon: |
| 166 | + |
| 167 | +- Payment Transactions - Transferring tokens between accounts |
| 168 | +- Stake Delegation - Delegating stake to block producers |
| 169 | +- Fee Transfers - Protocol fee distribution |
| 170 | +- zkApp Commands - Zero-knowledge applications |
| 171 | + |
| 172 | +## Related Files |
| 173 | + |
| 174 | +- [`ledger/src/scan_state/transaction_logic/mod.rs`](https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/transaction_logic/mod.rs) - |
| 175 | + Transaction type definitions |
| 176 | +- [`ledger/src/scan_state/transaction_logic/transaction_partially_applied.rs`](https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/transaction_logic/transaction_partially_applied.rs) - |
| 177 | + First pass application |
| 178 | +- [`ledger/src/scan_state/transaction_logic/transaction_applied.rs`](https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/transaction_logic/transaction_applied.rs) - |
| 179 | + Second pass application |
| 180 | +- [`ledger/src/scan_state/transaction_logic/signed_command.rs`](https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/transaction_logic/signed_command.rs) - |
| 181 | + Signed command types |
| 182 | +- [`ledger/src/scan_state/transaction_logic/zkapp_command/`](https://github.com/o1-labs/mina-rust/tree/develop/ledger/src/scan_state/transaction_logic/zkapp_command) - |
| 183 | + zkApp command implementation |
| 184 | +- [`ledger/src/scan_state/currency.rs`](https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/currency.rs) - |
| 185 | + Currency types (Fee, Nonce, Slot, Amount, Balance) |
0 commit comments