Bank Structure
The Bank (~/workspace/source/runtime/src/bank.rs:1) is the primary interface for all state management:Key Responsibilities
- Account Management: Load, store, and cache account data
- Transaction Processing: Coordinate execution through SVM
- Sysvar Updates: Maintain system variables (clock, rent, etc.)
- Rent Collection: Apply rent to accounts
- Epoch Transitions: Handle epoch boundary logic
Bank Lifecycle
Banks progress through several states:1. Active Bank
- Created as child of parent bank
- Open for transaction processing
- Tracks slot, parent slot, and tick height
- Maintains working state in memory
2. Frozen Bank
- No more transactions can be applied
- Rent applied to all accounts
- Sysvars updated to new state
- Hash computed over all accounts
3. Rooted Bank
- Received sufficient votes for finality
- Cannot be removed from chain
- State is finalized
- Parent banks can be cleaned up
Transaction Execution
Transaction execution flows through several layers:Processing Pipeline
- Load Accounts - AccountsDb loads required accounts
- Validate Fees - Check fee payer has sufficient balance
- Execute Transaction - Pass to SVM for execution
- Verify Results - Check rent, account ownership rules
- Commit State - Write modified accounts back
Bank Processing Interface
The main entry point (~/workspace/source/runtime/src/bank.rs:7):- Load accounts using AccountsDb reference
- Pass accounts to SVM TransactionBatchProcessor
- SVM creates InvokeContext with loaded accounts
- Programs execute through InvokeContext
- Results stored back to AccountsDb
Account Management
The runtime provides several account management features:Account Loading
- Load from AccountsDb with caching
- Rent-exempt status validation
- Owner and executable checks
- Size and data validation
Account Storage
Account storage uses the Accounts type (~/workspace/source/accounts-db/src/accounts.rs):Account Cache
Recently accessed accounts cached in memory:- Reduces disk I/O
- Faster transaction execution
- Write-through cache strategy
- Cache flushed periodically to disk
Bank Forks
The BankForks structure (~/workspace/source/runtime/src/bank_forks.rs) manages the fork tree:Fork Management
- Maintains tree of all active banks
- Tracks working bank (current leader slot)
- Tracks root bank (finalized state)
- Prunes old forks after rooting
Fork Selection
- Replay stage selects best fork
- Based on Tower BFT consensus
- Considers stake weight and lockouts
- May switch forks based on votes
State Transitions
Slot Transitions
Each slot gets a new bank:Epoch Transitions
Epoch boundaries trigger special processing:- Stake Activation/Deactivation: Update active stake
- Inflation Rewards: Distribute staking rewards
- Leader Schedule: Compute next epoch’s leaders
- Rent Collection: Collect rent from all accounts
- Snapshot Creation: Full account snapshot
Sysvar Updates
System variables updated each slot:- Clock: Current slot, epoch, timestamp
- Slot Hashes: Recent slot hashes
- Rent: Current rent rates
- Epoch Schedule: Epoch boundaries
- Stake History: Historical stake levels
Transaction Batch Processing
The TransactionBatch (~/workspace/source/runtime/src/transaction_batch.rs) provides:- Account lock management
- Batch-level validation
- Fee collection
- Error handling
Lock Validation
Commitment Levels
The runtime tracks multiple commitment levels (~/workspace/source/runtime/src/commitment.rs):- Processed: Transaction executed, not yet finalized
- Confirmed: Received 2/3+ stake votes
- Finalized: Rooted, cannot be rolled back
Performance Optimizations
Parallel Execution
- Non-conflicting transactions execute in parallel
- Scheduler identifies independent transactions
- Thread pool for concurrent execution
Account Caching
- Hot accounts kept in memory
- Reduces disk I/O overhead
- Write-through cache consistency
Snapshot & Restore
- Periodic full state snapshots
- Incremental snapshots for recent changes
- Fast validator startup from snapshots
Integration Points
With SVM
- Bank provides TransactionProcessingCallback
- SVM calls back for account state
- Bank validates and commits results
With AccountsDb
- Bank loads/stores through Accounts
- AccountsDb handles persistence
- Shared account cache
With Consensus
- Replay stage drives bank replay
- Tower BFT selects forks
- Banks frozen and rooted based on votes
Key Files
- Bank: ~/workspace/source/runtime/src/bank.rs
- BankForks: ~/workspace/source/runtime/src/bank_forks.rs
- TransactionBatch: ~/workspace/source/runtime/src/transaction_batch.rs
- Commitment: ~/workspace/source/runtime/src/commitment.rs
- Stakes: ~/workspace/source/runtime/src/stakes.rs