|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +Graph Node is a Rust-based decentralized blockchain indexing protocol that enables efficient querying of blockchain data through GraphQL. It's the core component of The Graph protocol, written as a Cargo workspace with multiple crates organized by functionality. |
| 8 | + |
| 9 | +## Essential Development Commands |
| 10 | + |
| 11 | +### Build and Run |
| 12 | +```bash |
| 13 | +# Build and run Graph Node |
| 14 | +cargo run -p graph-node --release -- \ |
| 15 | + --postgres-url $POSTGRES_URL \ |
| 16 | + --ethereum-rpc NETWORK_NAME:[CAPABILITIES]:URL \ |
| 17 | + --ipfs 127.0.0.1:5001 |
| 18 | + |
| 19 | +# Development with auto-reload (requires cargo-watch) |
| 20 | +cargo watch \ |
| 21 | + -x "fmt --all" \ |
| 22 | + -x check \ |
| 23 | + -x "test -- --test-threads=1" \ |
| 24 | + -x "doc --no-deps" |
| 25 | +``` |
| 26 | + |
| 27 | +### Testing |
| 28 | +```bash |
| 29 | +# Run all tests (requires test config) |
| 30 | +GRAPH_NODE_TEST_CONFIG=`pwd`/store/test-store/config.simple.toml cargo test --workspace --exclude graph-tests |
| 31 | + |
| 32 | +# Run a single test |
| 33 | +cargo test <test_name> |
| 34 | + |
| 35 | +# Test setup using Docker Compose |
| 36 | +./store/test-store/devel/up.sh |
| 37 | + |
| 38 | +# Reset test databases |
| 39 | +./store/test-store/devel/db-reset |
| 40 | +``` |
| 41 | + |
| 42 | +### Code Quality |
| 43 | +```bash |
| 44 | +# Format all code |
| 45 | +cargo fmt --all |
| 46 | + |
| 47 | +# Check code without building |
| 48 | +cargo check |
| 49 | + |
| 50 | +# Build documentation |
| 51 | +cargo doc --no-deps |
| 52 | +``` |
| 53 | + |
| 54 | +### Database Setup |
| 55 | +```bash |
| 56 | +# Create test database |
| 57 | +psql -U <SUPERUSER> <<EOF |
| 58 | +create user graph with password '<password>'; |
| 59 | +create database "graph-node" with owner=graph template=template0 encoding='UTF8' locale='C'; |
| 60 | +create extension pg_trgm; |
| 61 | +create extension btree_gist; |
| 62 | +create extension postgres_fdw; |
| 63 | +grant usage on foreign data wrapper postgres_fdw to graph; |
| 64 | +EOF |
| 65 | + |
| 66 | +export POSTGRES_URL=postgresql://graph:<password>@localhost:5432/graph-node |
| 67 | +``` |
| 68 | + |
| 69 | +## High-Level Architecture |
| 70 | + |
| 71 | +### Core Components |
| 72 | +- **`graph/`**: Core abstractions, traits, and shared types |
| 73 | +- **`node/`**: Main executable and CLI (graphman) |
| 74 | +- **`chain/`**: Blockchain-specific adapters (ethereum, near, substreams) |
| 75 | +- **`runtime/`**: WebAssembly runtime for subgraph execution |
| 76 | +- **`store/`**: PostgreSQL-based storage layer |
| 77 | +- **`graphql/`**: GraphQL query execution engine |
| 78 | +- **`server/`**: HTTP/WebSocket APIs |
| 79 | + |
| 80 | +### Data Flow |
| 81 | +``` |
| 82 | +Blockchain → Chain Adapter → Block Stream → Trigger Processing → Runtime → Store → GraphQL API |
| 83 | +``` |
| 84 | + |
| 85 | +1. **Chain Adapters** connect to blockchain nodes and convert data to standardized formats |
| 86 | +2. **Block Streams** provide event-driven streaming of blockchain blocks |
| 87 | +3. **Trigger Processing** matches blockchain events to subgraph handlers |
| 88 | +4. **Runtime** executes subgraph code in WebAssembly sandbox |
| 89 | +5. **Store** persists entities with block-level granularity |
| 90 | +6. **GraphQL** processes queries and returns results |
| 91 | + |
| 92 | +### Key Abstractions |
| 93 | +- **`Blockchain`** trait: Core blockchain interface |
| 94 | +- **`Store`** trait: Storage abstraction with read/write variants |
| 95 | +- **`RuntimeHost`**: WASM execution environment |
| 96 | +- **`TriggerData`**: Standardized blockchain events |
| 97 | +- **`EventConsumer`/`EventProducer`**: Component communication |
| 98 | + |
| 99 | +### Architecture Patterns |
| 100 | +- **Event-driven**: Components communicate through async streams and channels |
| 101 | +- **Trait-based**: Extensive use of traits for abstraction and modularity |
| 102 | +- **Async/await**: Tokio-based async runtime throughout |
| 103 | +- **Multi-shard**: Database sharding for scalability |
| 104 | +- **Sandboxed execution**: WASM runtime with gas metering |
| 105 | + |
| 106 | +## Development Guidelines |
| 107 | + |
| 108 | +### Commit Convention |
| 109 | +Use format: `{crate-name}: {description}` |
| 110 | +- Single crate: `store: Support 'Or' filters` |
| 111 | +- Multiple crates: `core, graphql: Add event source to store` |
| 112 | +- All crates: `all: {description}` |
| 113 | + |
| 114 | +### Git Workflow |
| 115 | +- Rebase on master (don't merge master into feature branch) |
| 116 | +- Keep commits logical and atomic |
| 117 | +- Use `git rebase -i` to clean up history before merging |
| 118 | + |
| 119 | +### Testing Requirements |
| 120 | +- Unit tests inline with source code |
| 121 | +- Integration tests require Docker Compose setup |
| 122 | +- Tests should run with `--test-threads=1` for consistency |
| 123 | +- Use `GRAPH_NODE_TEST_CONFIG` environment variable for test configuration |
| 124 | + |
| 125 | +### Environment Variables |
| 126 | +- `POSTGRES_URL`: Database connection string |
| 127 | +- `GRAPH_LOG=debug`: Enable debug logging |
| 128 | +- `THEGRAPH_STORE_POSTGRES_DIESEL_URL`: For Diesel/Postgres store tests |
| 129 | +- `GRAPH_NODE_TEST_CONFIG`: Points to test configuration file |
| 130 | + |
| 131 | +## Crate Structure |
| 132 | + |
| 133 | +### Core Crates |
| 134 | +- **`graph`**: Shared types, traits, and utilities |
| 135 | +- **`node`**: Main binary and component wiring |
| 136 | +- **`core`**: Business logic and subgraph management |
| 137 | + |
| 138 | +### Blockchain Integration |
| 139 | +- **`chain/ethereum`**: Ethereum chain support |
| 140 | +- **`chain/near`**: NEAR protocol support |
| 141 | +- **`chain/substreams`**: Substreams data source support |
| 142 | + |
| 143 | +### Infrastructure |
| 144 | +- **`store/postgres`**: PostgreSQL storage implementation |
| 145 | +- **`runtime/wasm`**: WebAssembly runtime and host functions |
| 146 | +- **`graphql`**: Query processing and execution |
| 147 | +- **`server/`**: HTTP/WebSocket servers |
| 148 | + |
| 149 | +### Key Dependencies |
| 150 | +- **`diesel`**: PostgreSQL ORM |
| 151 | +- **`tokio`**: Async runtime |
| 152 | +- **`tonic`**: gRPC framework |
| 153 | +- **`wasmtime`**: WebAssembly runtime |
| 154 | +- **`web3`**: Ethereum interaction |
| 155 | + |
| 156 | +## Common Development Tasks |
| 157 | + |
| 158 | +### Adding New Blockchain Support |
| 159 | +1. Create new crate under `chain/` |
| 160 | +2. Implement `Blockchain` trait |
| 161 | +3. Add chain-specific `TriggerData` types |
| 162 | +4. Implement block streaming and event parsing |
| 163 | +5. Add integration tests |
| 164 | + |
| 165 | +### Modifying Storage Layer |
| 166 | +- Storage interfaces defined in `graph/src/components/store/` |
| 167 | +- PostgreSQL implementation in `store/postgres/` |
| 168 | +- Use migrations for schema changes |
| 169 | +- Consider multi-shard implications |
| 170 | + |
| 171 | +### GraphQL Changes |
| 172 | +- Schema parsing in `graphql/src/schema/` |
| 173 | +- Query execution in `graphql/src/execution/` |
| 174 | +- Resolver implementations handle entity fetching |
| 175 | +- Consider query complexity and performance |
| 176 | + |
| 177 | +### Runtime Modifications |
| 178 | +- Host functions in `runtime/wasm/src/host.rs` |
| 179 | +- AssemblyScript bindings in `runtime/wasm/src/asc_abi/` |
| 180 | +- Gas metering in `runtime/wasm/src/gas/` |
| 181 | +- Security considerations for sandboxing |
0 commit comments