Skip to content

Commit b9da928

Browse files
committed
all: add flake.nix setup and CLAUDE.md documentation
1 parent 8929c43 commit b9da928

File tree

6 files changed

+653
-1
lines changed

6 files changed

+653
-1
lines changed

.gitignore

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,12 @@ lcov.info
3232

3333
# Docker volumes and debug logs
3434
.postgres
35-
logfile
35+
logfile
36+
37+
# Nix related files
38+
.direnv
39+
.envrc
40+
.data
41+
42+
# Local claude settings
43+
.claude/settings.local.json

CLAUDE.md

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## ⚠️ CRITICAL MANDATORY STEP ⚠️
6+
**ALWAYS run `cargo fmt --all` after editing ANY .rs file - NO EXCEPTIONS!**
7+
- This is REQUIRED after every single Rust code change
8+
- Must be run from the project root directory
9+
- Must be done BEFORE committing any changes
10+
- If you forget this, you have failed to follow instructions
11+
12+
This document assumes that your human has already set up all database configuration required for the test suite to run.
13+
14+
## Project Overview
15+
16+
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.
17+
18+
## Essential Development Commands
19+
20+
### Unit Tests
21+
22+
```bash
23+
# REQUIRED: Export database connection string before running unit tests
24+
export THEGRAPH_STORE_POSTGRES_DIESEL_URL="postgresql://graph:graph@127.0.0.1:5432/graph-test"
25+
26+
# Run unit tests (integration tests are excluded due to missing environment dependencies)
27+
cargo test --workspace --exclude graph-tests
28+
29+
# Run specific tests
30+
cargo test -p graph data_source::common::tests
31+
cargo test <specific_test_name>
32+
```
33+
34+
### Integration Tests
35+
36+
⚠️ **Only run integration tests when explicitly requested or when changes require full system testing**
37+
38+
Integration tests require external services and are more complex to run than unit tests. They test the full system end-to-end and should not be run by default during development. Use unit tests for regular development and only run integration tests when:
39+
40+
- Explicitly asked to do so
41+
- Making changes to integration/end-to-end functionality
42+
- Debugging issues that require full system testing
43+
- Preparing releases or major changes
44+
45+
**Prerequisites:**
46+
1. Docker and Docker Compose **OR** Nix (for operating environment dependencies)
47+
2. Yarn (v1)
48+
3. Foundry (for smart contract compilation)
49+
50+
**Running Integration Tests:**
51+
```bash
52+
# Run all integration tests
53+
cargo test -p graph-tests --test integration_tests -- --nocapture
54+
55+
# Run a specific integration test case (e.g., "grafted" test case)
56+
TEST_CASE=grafted cargo test -p graph-tests --test integration_tests -- --nocapture
57+
```
58+
59+
**Important Notes:**
60+
- Integration tests take significant time (several minutes)
61+
- Tests automatically reset the database between runs
62+
- Logs are written to `tests/integration-tests/graph-node.log`
63+
64+
### Code Quality
65+
```bash
66+
# 🚨 MANDATORY: Format all code IMMEDIATELY after any .rs file edit
67+
cargo fmt --all
68+
69+
# 🚨 MANDATORY: Check code for warnings and errors - MUST have zero warnings
70+
cargo check
71+
```
72+
73+
🚨 **CRITICAL REQUIREMENTS for ANY implementation**:
74+
1. `cargo fmt --all` MUST be run before any commit
75+
2. `cargo check` MUST show zero warnings before any commit
76+
3. The unit test suite MUST pass before any commit
77+
78+
Forgetting any of these means you failed to follow instructions. No exceptions!
79+
80+
## High-Level Architecture
81+
82+
### Core Components
83+
- **`graph/`**: Core abstractions, traits, and shared types
84+
- **`node/`**: Main executable and CLI (graphman)
85+
- **`chain/`**: Blockchain-specific adapters (ethereum, near, substreams)
86+
- **`runtime/`**: WebAssembly runtime for subgraph execution
87+
- **`store/`**: PostgreSQL-based storage layer
88+
- **`graphql/`**: GraphQL query execution engine
89+
- **`server/`**: HTTP/WebSocket APIs
90+
91+
### Data Flow
92+
```
93+
Blockchain → Chain Adapter → Block Stream → Trigger Processing → Runtime → Store → GraphQL API
94+
```
95+
96+
1. **Chain Adapters** connect to blockchain nodes and convert data to standardized formats
97+
2. **Block Streams** provide event-driven streaming of blockchain blocks
98+
3. **Trigger Processing** matches blockchain events to subgraph handlers
99+
4. **Runtime** executes subgraph code in WebAssembly sandbox
100+
5. **Store** persists entities with block-level granularity
101+
6. **GraphQL** processes queries and returns results
102+
103+
### Key Abstractions
104+
- **`Blockchain`** trait: Core blockchain interface
105+
- **`Store`** trait: Storage abstraction with read/write variants
106+
- **`RuntimeHost`**: WASM execution environment
107+
- **`TriggerData`**: Standardized blockchain events
108+
- **`EventConsumer`/`EventProducer`**: Component communication
109+
110+
### Architecture Patterns
111+
- **Event-driven**: Components communicate through async streams and channels
112+
- **Trait-based**: Extensive use of traits for abstraction and modularity
113+
- **Async/await**: Tokio-based async runtime throughout
114+
- **Multi-shard**: Database sharding for scalability
115+
- **Sandboxed execution**: WASM runtime with gas metering
116+
117+
## Development Guidelines
118+
119+
### Commit Convention
120+
Use format: `{crate-name}: {description}`
121+
- Single crate: `store: Support 'Or' filters`
122+
- Multiple crates: `core, graphql: Add event source to store`
123+
- All crates: `all: {description}`
124+
125+
### Git Workflow
126+
- Rebase on master (don't merge master into feature branch)
127+
- Keep commits logical and atomic
128+
- Use `git rebase -i` to clean up history before merging
129+
130+
### Implementation Success Criteria
131+
Before any commit or PR, ALL of the following MUST be satisfied:
132+
133+
1. **🚨 MANDATORY**: The unit test suite MUST pass
134+
2. **🚨 MANDATORY**: `cargo check` MUST show zero warnings
135+
3. **🚨 MANDATORY**: `cargo fmt --all` MUST be run after editing ANY .rs file
136+
137+
**Testing Notes**:
138+
- Unit tests are inlined with source code
139+
- Integration tests require environment dependencies that can be provided **EITHER** through Docker & Docker Compose **OR** through Nix
140+
141+
## Crate Structure
142+
143+
### Core Crates
144+
- **`graph`**: Shared types, traits, and utilities
145+
- **`node`**: Main binary and component wiring
146+
- **`core`**: Business logic and subgraph management
147+
148+
### Blockchain Integration
149+
- **`chain/ethereum`**: Ethereum chain support
150+
- **`chain/near`**: NEAR protocol support
151+
- **`chain/substreams`**: Substreams data source support
152+
153+
### Infrastructure
154+
- **`store/postgres`**: PostgreSQL storage implementation
155+
- **`runtime/wasm`**: WebAssembly runtime and host functions
156+
- **`graphql`**: Query processing and execution
157+
- **`server/`**: HTTP/WebSocket servers
158+
159+
### Key Dependencies
160+
- **`diesel`**: PostgreSQL ORM
161+
- **`tokio`**: Async runtime
162+
- **`tonic`**: gRPC framework
163+
- **`wasmtime`**: WebAssembly runtime
164+
- **`web3`**: Ethereum interaction
165+
166+
## Test Environment Requirements
167+
168+
### ✨ New: Process Compose Setup (Recommended)
169+
170+
The repository includes a process-compose-flake setup that provides native, declarative service management.
171+
172+
**Unit Tests:**
173+
```bash
174+
# Human: Start PostgreSQL + IPFS for unit tests in a separate terminal
175+
# PostgreSQL: localhost:5432, IPFS: localhost:5001
176+
nix run .#unit
177+
178+
# Claude must export the database connection string before running unit tests
179+
export THEGRAPH_STORE_POSTGRES_DIESEL_URL="postgresql://graph:graph@127.0.0.1:5432/graph-test"
180+
181+
# Claude can then run unit tests
182+
cargo test --workspace --exclude graph-tests
183+
```
184+
185+
**Integration Tests:**
186+
```bash
187+
# Human: Start all services for integration tests in a separate terminal
188+
# PostgreSQL: localhost:3011, IPFS: localhost:3001, Anvil: localhost:3021
189+
nix run .#integration
190+
191+
# Claude can then run integration tests
192+
cargo test -p graph-tests --test integration_tests
193+
```
194+
195+
**Services Configuration:**
196+
The services are configured to use the test suite default ports for unit- and integration tests respectively.
197+
198+
| Service | Unit Tests Port | Integration Tests Port | Database/Config |
199+
|---------|-----------------|------------------------|-----------------|
200+
| PostgreSQL | 5432 | 3011 | `graph-test` / `graph-node` |
201+
| IPFS | 5001 | 3001 | Data in `./.data/unit` or `./.data/integration` |
202+
| Anvil (Ethereum) | - | 3021 | Deterministic test chain |
203+
204+
**Service Configuration:**
205+
The setup combines built-in services-flake services with custom multiService modules:
206+
207+
**Built-in Services:**
208+
- **PostgreSQL**: Uses services-flake's postgres service with a helper function (`mkPostgresConfig`) that provides graph-specific defaults including required extensions.
209+
210+
**Custom Services** (located in `./nix`):
211+
- `ipfs.nix`: IPFS (kubo) with automatic initialization and configurable ports
212+
- `anvil.nix`: Ethereum test chain with deterministic configuration

0 commit comments

Comments
 (0)