|
| 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](./transactions/user-commands)** are transactions submitted and |
| 33 | +signed by users: |
| 34 | + |
| 35 | +- **[Payment transactions](./transactions/payments)** - Transfer MINA tokens |
| 36 | + between accounts |
| 37 | +- **[Stake delegation](./transactions/delegations)** - Delegate stake to block |
| 38 | + producers |
| 39 | +- **[zkApp commands](./transactions/zkapps)** - Execute complex multi-account |
| 40 | + zero-knowledge operations |
| 41 | + |
| 42 | +### Protocol Transactions |
| 43 | + |
| 44 | +**Protocol transactions** are generated automatically by the system: |
| 45 | + |
| 46 | +- **[Fee transfers](./transactions/fee-transfers)** - Distribute collected |
| 47 | + transaction fees to block producers |
| 48 | +- **[Coinbase rewards](./transactions/coinbase)** - Issue block production |
| 49 | + rewards |
| 50 | + |
| 51 | +## Transaction Application Model |
| 52 | + |
| 53 | +Transactions in Mina are applied in two phases: |
| 54 | + |
| 55 | +### First Pass (apply_transaction_first_pass) |
| 56 | + |
| 57 | +The first pass validates preconditions and begins transaction application: |
| 58 | + |
| 59 | +- Validates account existence and permissions |
| 60 | +- Checks nonces and balances |
| 61 | +- Applies fee payments |
| 62 | +- Performs initial state changes |
| 63 | +- Returns `TransactionPartiallyApplied` containing transaction state |
| 64 | + |
| 65 | +**Function:** |
| 66 | +`ledger/src/scan_state/transaction_logic/transaction_partially_applied.rs` |
| 67 | + |
| 68 | +### Second Pass (apply_transaction_second_pass) |
| 69 | + |
| 70 | +The second pass completes the transaction after SNARK work: |
| 71 | + |
| 72 | +- Finalizes account updates |
| 73 | +- Applies protocol-specific logic |
| 74 | +- Updates receipt chain hashes |
| 75 | +- Returns final `TransactionApplied` status |
| 76 | + |
| 77 | +**Function:** `ledger/src/scan_state/transaction_logic/transaction_applied.rs` |
| 78 | + |
| 79 | +## Common Transaction Components |
| 80 | + |
| 81 | +### Fees |
| 82 | + |
| 83 | +All user-initiated transactions require fees: |
| 84 | + |
| 85 | +```rust |
| 86 | +pub struct Fee(Amount); |
| 87 | +``` |
| 88 | + |
| 89 | +Fees are paid by the transaction sender (or fee payer in zkApp commands) and |
| 90 | +distributed to block producers through fee transfers. |
| 91 | + |
| 92 | +### Nonces |
| 93 | + |
| 94 | +User accounts maintain nonces to prevent replay attacks: |
| 95 | + |
| 96 | +```rust |
| 97 | +pub struct Nonce(u32); |
| 98 | +``` |
| 99 | + |
| 100 | +Each user-initiated transaction must specify the correct nonce, which increments |
| 101 | +after successful application. |
| 102 | + |
| 103 | +### Memos |
| 104 | + |
| 105 | +Transactions can include optional 32-byte memos: |
| 106 | + |
| 107 | +```rust |
| 108 | +pub struct Memo { |
| 109 | + data: [u8; MEMO_BYTES], |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +Memos provide auxiliary information and are included in transaction commitments. |
| 114 | + |
| 115 | +### Slots |
| 116 | + |
| 117 | +Transactions include validity windows using slot numbers: |
| 118 | + |
| 119 | +```rust |
| 120 | +pub struct Slot(u32); |
| 121 | +``` |
| 122 | + |
| 123 | +The `valid_until` field specifies when a transaction expires. |
| 124 | + |
| 125 | +## Account Creation |
| 126 | + |
| 127 | +Creating new accounts requires an account creation fee (1 MINA by default): |
| 128 | + |
| 129 | +```rust |
| 130 | +ConstraintConstants { |
| 131 | + account_creation_fee: 1_000_000_000, // 1 MINA in nanomina |
| 132 | + // ... |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +The fee is deducted from the amount being transferred or received: |
| 137 | + |
| 138 | +- **Payments**: Sender pays `amount + fee + account_creation_fee` |
| 139 | +- **Fee transfers**: Receiver gets `fee_amount - account_creation_fee` |
| 140 | +- **Coinbase**: Receiver gets `coinbase_amount - account_creation_fee` |
| 141 | + |
| 142 | +## Transaction Status |
| 143 | + |
| 144 | +After application, transactions have a status: |
| 145 | + |
| 146 | +<!-- CODE_REFERENCE: ledger/src/scan_state/transaction_logic/mod.rs#L240-L243 --> |
| 147 | + |
| 148 | +```rust reference title="ledger/src/scan_state/transaction_logic/mod.rs" |
| 149 | +https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/transaction_logic/mod.rs#L240-L243 |
| 150 | +``` |
| 151 | + |
| 152 | +Failed transactions may still consume fees in some cases (zkApp commands with |
| 153 | +fee payer failures). |
| 154 | + |
| 155 | +## Testing |
| 156 | + |
| 157 | +Comprehensive tests for each transaction type verify correct ledger updates: |
| 158 | + |
| 159 | +- `tests/test_transaction_logic_first_pass.rs` - Payment transactions |
| 160 | +- `tests/test_transaction_logic_first_pass_delegation.rs` - Stake delegations |
| 161 | +- `tests/test_transaction_logic_first_pass_fee_transfer.rs` - Fee transfers |
| 162 | +- `tests/test_transaction_logic_first_pass_coinbase.rs` - Coinbase rewards |
| 163 | +- `tests/test_transaction_logic_first_pass_zkapp.rs` - zkApp commands |
| 164 | + |
| 165 | +## Further Reading |
| 166 | + |
| 167 | +- [Payment Transactions](./transactions/payments) - Transferring tokens between |
| 168 | + accounts |
| 169 | +- [Stake Delegation](./transactions/delegations) - Delegating stake to block |
| 170 | + producers |
| 171 | +- [Fee Transfers](./transactions/fee-transfers) - Protocol fee distribution |
| 172 | +- [Coinbase Rewards](./transactions/coinbase) - Block production rewards |
| 173 | +- [zkApp Commands](./transactions/zkapps) - Zero-knowledge applications |
| 174 | + |
| 175 | +## Related Files |
| 176 | + |
| 177 | +- `ledger/src/scan_state/transaction_logic/mod.rs` - Transaction type |
| 178 | + definitions |
| 179 | +- `ledger/src/scan_state/transaction_logic/transaction_partially_applied.rs` - |
| 180 | + First pass application |
| 181 | +- `ledger/src/scan_state/transaction_logic/transaction_applied.rs` - Second pass |
| 182 | + application |
| 183 | +- `ledger/src/scan_state/transaction_logic/signed_command.rs` - Signed command |
| 184 | + types |
| 185 | +- `ledger/src/scan_state/transaction_logic/zkapp_command/` - zkApp command |
| 186 | + implementation |
0 commit comments