Skip to content

Commit d8abac2

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

6 files changed

Lines changed: 643 additions & 1 deletion

File tree

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

flake.lock

Lines changed: 178 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)