Skip to content

Commit 879f49e

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

File tree

6 files changed

+633
-1
lines changed

6 files changed

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