|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Common Development Commands |
| 6 | + |
| 7 | +### Build |
| 8 | +```bash |
| 9 | +make build # Build all binaries |
| 10 | +make install # Build and install binaries to $GOPATH/bin |
| 11 | +make buildsrc # Build main source (faster than full build) |
| 12 | +``` |
| 13 | + |
| 14 | +### Testing |
| 15 | +```bash |
| 16 | +make test # Run unit tests |
| 17 | +make fulltest # Run unit tests with race detection |
| 18 | +make shorttest # Run short tests with race detection |
| 19 | +make integration # Run integration tests |
| 20 | +make testall # Run all tests (unit + integration) |
| 21 | +``` |
| 22 | + |
| 23 | +### Code Quality |
| 24 | +```bash |
| 25 | +make sanity # Run all checks (fmt, lint, fix, tidy) |
| 26 | +make fmt # Format code and check licenses |
| 27 | +make lint # Run linter (requires deps) |
| 28 | +make fix # Run algofix tool |
| 29 | +make vet # Run go vet |
| 30 | +make tidy # Clean up go.mod files |
| 31 | +``` |
| 32 | + |
| 33 | +### Code Generation |
| 34 | + |
| 35 | +Some code must be re-generated after changes. Run the following to regenerate auto-generated code if changes are made to relevant files. |
| 36 | + |
| 37 | +``` |
| 38 | +make rebuild_kmd_swagger # Rebuild swagger.json files |
| 39 | +make generate # Regenerate for stringer et al. |
| 40 | +make expectlint # Run expect linter |
| 41 | +touch data/transactions/logic/fields_string.go # Ensure rebuild of teal specs |
| 42 | +make -C data/transactions/logic # Update TEAL Specs |
| 43 | +touch daemon/algod/api/algod.oas2.json # Ensure rebuild of API spec |
| 44 | +make -C daemon/algod/api generate # Regenerate REST server |
| 45 | +make msgp # Regenerate msgp files |
| 46 | +``` |
| 47 | + |
| 48 | +To verify that this wasn't missed, we run verification steps, which can be found in `scripts/travis/codegen_verification.sh`. If code is not clean, it will fail CI checks. |
| 49 | + |
| 50 | +### Development Setup |
| 51 | +```bash |
| 52 | +./scripts/configure_dev.sh # Initial environment setup |
| 53 | +./scripts/buildtools/install_buildtools.sh # Install build tools |
| 54 | +make deps # Check/install dependencies |
| 55 | +``` |
| 56 | + |
| 57 | +### Single Test Execution |
| 58 | +```bash |
| 59 | +go test -v -run TestName ./path/to/package # Run specific test |
| 60 | +go test -v ./agreement/... # Run tests in package tree rooted at agreement |
| 61 | +go test -v ./agreement/ # Run tests for just the agreement package |
| 62 | +``` |
| 63 | + |
| 64 | +### Running E2E tests |
| 65 | +E2E tests run one or more algod processes, each with their own data directory containing logs and configuration (created in a subdirectory of TESTDIR). If an E2E test fails, useful information can often be found in the node.log files produced by algod while running the test. For example: |
| 66 | +```bash |
| 67 | +export NODEBINDIR=~/go/bin # path to algod, goal, etc. Code changes to goal or algod require rebuilding with "make" to place new binaries here before running E2E tests. |
| 68 | +export TESTDATADIR=`pwd`/test/testdata # path to go-algorand/test/testdata |
| 69 | +export TESTDIR=/tmp |
| 70 | +# network and node data will be created in /tmp/TestAssetSend/, logs in /tmp/TestAssetSend/Primary/node.log and /tmp/TestAssetSend/Node/node.log |
| 71 | +go test ./test/e2e-go/features/transactions -run TestAssetSend -v -timeout=0 |
| 72 | +``` |
| 73 | + |
| 74 | +## Architecture Overview |
| 75 | + |
| 76 | +### Main Binaries |
| 77 | +- **`algod`**: Core blockchain node daemon (consensus, networking, REST API) |
| 78 | +- **`kmd`**: Key Management Daemon (secure wallet operations, isolated process) |
| 79 | +- **`goal`**: Primary CLI tool for node interaction and account management |
| 80 | +- **`algokey`**: Standalone key generation and management utility |
| 81 | + |
| 82 | +### Core Components |
| 83 | + |
| 84 | +#### Node Layer (`node/`) |
| 85 | +Central orchestrator that integrates all subsystems. The `AlgorandFullNode` struct manages: |
| 86 | +- Ledger state and transaction pool |
| 87 | +- Network communication and message routing |
| 88 | +- Agreement service for consensus participation |
| 89 | +- Catchup service for blockchain synchronization |
| 90 | + |
| 91 | +#### Agreement Layer (`agreement/`) |
| 92 | +Implements Algorand's Byzantine Agreement protocol: |
| 93 | +- **Service**: Main consensus coordinator |
| 94 | +- **State Machine**: Manages consensus rounds, periods, and steps |
| 95 | +- **Vote/Proposal Managers**: Handle consensus message flow |
| 96 | +- **CryptoVerifier**: Asynchronous signature verification |
| 97 | + |
| 98 | +#### Ledger Layer (`ledger/`) |
| 99 | +Manages blockchain state using tracker-based architecture: |
| 100 | +- **Blockchain Storage**: Sequential block storage with certificates |
| 101 | +- **Trackers**: Independent state machines consuming blockchain events |
| 102 | + - `accountUpdates`: Account balances and application state |
| 103 | + - `acctsOnline`: Online account tracking for consensus |
| 104 | + - `catchpointTracker`: Catchpoint generation for fast sync |
| 105 | + - `txTail`: Recent transaction tracking |
| 106 | +- **Atomic Updates**: Coordinated state transitions across trackers |
| 107 | + |
| 108 | +#### Network Layer (`network/`) |
| 109 | +Supports multiple networking implementations through `GossipNode` interface: |
| 110 | +- **WebSocket Network**: Traditional relay-based topology |
| 111 | +- **P2P Network**: LibP2P-based peer-to-peer networking |
| 112 | +- **Hybrid Network**: Combines both approaches |
| 113 | + |
| 114 | +#### Data Layer (`data/`) |
| 115 | +- **Transaction Pool**: Manages pending transactions |
| 116 | +- **Transaction Handler**: Processes incoming network transactions |
| 117 | +- **Account Manager**: Handles participation key lifecycle |
| 118 | +- **Core Types**: Transactions, blocks, accounts, and protocol structures |
| 119 | + |
| 120 | +#### Cryptography (`crypto/`) |
| 121 | +- Ed25519 signatures, multisig, LogicSig (smart signatures) |
| 122 | +- VRF (Verifiable Random Functions) for consensus leader selection |
| 123 | +- State proof cryptography for light client verification |
| 124 | +- Merkle tree implementations for data integrity |
| 125 | + |
| 126 | +### Key Architectural Patterns |
| 127 | + |
| 128 | +#### Interface-Based Design |
| 129 | +System boundaries defined by Go interfaces: |
| 130 | +- `GossipNode`: Network abstraction |
| 131 | +- `BlockValidator`/`BlockFactory`: Consensus integration |
| 132 | +- `Ledger`: Storage abstraction |
| 133 | +- `KeyManager`: Cryptographic operations |
| 134 | + |
| 135 | +#### Tracker Pattern |
| 136 | +Ledger uses independent state machines that can rebuild from blockchain events, enabling: |
| 137 | +- Stateless tracker logic with optional persistent caching |
| 138 | +- Atomic coordinated updates across different state types |
| 139 | +- Efficient state rebuilding and validation |
| 140 | + |
| 141 | +#### Concurrent Architecture |
| 142 | +- Agreement service separates concurrent I/O from serialized protocol logic |
| 143 | +- Crypto verification runs in dedicated thread pools |
| 144 | +- Network and disk operations use separate goroutines |
| 145 | + |
| 146 | +#### Security Isolation |
| 147 | +- KMD runs as separate process to isolate key material |
| 148 | +- Transaction verification separated from consensus participation |
| 149 | +- Clear boundaries between trusted and untrusted operations |
| 150 | + |
| 151 | +## Development Guidelines |
| 152 | + |
| 153 | +### Testing Strategy |
| 154 | +- Unit tests focus on individual component logic |
| 155 | +- Integration tests verify cross-component interactions |
| 156 | +- Race detection enabled for concurrent code validation |
| 157 | +- Benchmark tests for performance-critical paths |
| 158 | + |
| 159 | +### Code Organization |
| 160 | +- Interface-first design for testability and modularity |
| 161 | +- Dependency injection for component assembly |
| 162 | +- Clear separation between protocol logic and implementation details |
| 163 | +- Consistent error handling patterns throughout |
| 164 | + |
| 165 | +### Performance Considerations |
| 166 | +- Tracker pattern enables efficient state caching |
| 167 | +- Asynchronous block writing with in-memory queues |
| 168 | +- Parallel transaction verification |
| 169 | +- Catchpoint mechanism for fast node synchronization |
| 170 | + |
| 171 | +### Protocol Evolution |
| 172 | +- Consensus parameters support versioning for upgrades |
| 173 | +- Backward compatibility maintained through careful interface design |
| 174 | +- Feature flags and gradual rollout mechanisms |
0 commit comments