Skip to content

Commit be49aed

Browse files
sameh-faroukclaude
andcommitted
chore: update compose stacks and shared networking
- Upgrade PostgreSQL 16 -> 17 - Upgrade CockroachDB v22.2.2 -> v22.2.15 - Upgrade substrate-gateway 2.5.0 -> 2.7.0 - Rename indexer CockroachDB service from 'db' to 'cockroachdb' to avoid DNS collision when both stacks share a Docker network - Add shared external network 'tfgrid_bknd' to both compose files so the processor can reach the indexer gateway by service name - Update INDEXER_ENDPOINT_URL default to http://gateway:8000/graphql (Docker service name) instead of localhost:8888 (host port mapping) - Bind database ports to localhost only (PostgreSQL 5432, CockroachDB 26257, CockroachDB admin 8080) to prevent unauthorized access - Remove obsolete 'version' field from both compose files - Update production docs with shared network setup and env var changes Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1e96cca commit be49aed

7 files changed

Lines changed: 110 additions & 30 deletions

File tree

.env

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
DB_NAME=tfgrid-graphql
22
DB_USER=postgres
33
DB_PASS=postgres
4-
#DB_HOST=172.17.0.1
54
DB_PORT=5432
65
TYPEORM_LOGGING=error
7-
INDEXER_ENDPOINT_URL=http://localhost:8888/graphql
8-
WS_URL=ws://localhost:9944
6+
# For Docker deployment (shared tfgrid_bknd network): use gateway service name
7+
# For local development outside Docker: use http://localhost:8888/graphql
8+
INDEXER_ENDPOINT_URL=http://gateway:8000/graphql
9+
WS_URL=wss://tfchain.dev.grid.tf/ws

docker-compose.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
version: "3.4"
1+
networks:
2+
default:
3+
name: tfgrid_bknd
4+
external: true
25

36
services:
47
db:
5-
image: postgres:16
8+
image: postgres:17
69
restart: always
710
ports:
8-
- "${DB_PORT}:5432"
11+
- "127.0.0.1:${DB_PORT}:5432"
912
volumes:
1013
- /var/lib/postgresql/data
1114
environment:

docs/development.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ yarn build
1313

1414
See https://github.com/threefoldtech/tfchain
1515

16+
### Create the shared Docker network
17+
18+
Both compose stacks use a shared external network. Create it once:
19+
20+
```bash
21+
docker network create tfgrid_bknd
22+
```
23+
1624
### Run Indexer
1725

1826
Check `indexer/.env` and adjust the websocket endpoint to your local TFChain address.
@@ -34,7 +42,15 @@ You should see TFChain blocks being processed:
3442

3543
### Run Processor (local, outside Docker)
3644

37-
Check `.env` and adjust the websocket endpoint and indexer URL to your local setup.
45+
Check `.env` and adjust the settings. When running the processor locally (not in Docker) while the indexer runs in Docker, change `INDEXER_ENDPOINT_URL` to use the published port:
46+
47+
```bash
48+
# .env — for local development (processor outside Docker):
49+
INDEXER_ENDPOINT_URL=http://localhost:8888/graphql
50+
51+
# .env — for Docker deployment (processor inside Docker, shared network):
52+
# INDEXER_ENDPOINT_URL=http://gateway:8000/graphql
53+
```
3854

3955
Start the local PostgreSQL container and run the processor:
4056

docs/production.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,40 @@ The production stack has two independent layers:
1515

1616
## Run the Setup
1717

18+
Both compose stacks share a Docker network (`tfgrid_bknd`) so the processor can reach the indexer gateway by service name. Create it before starting either stack:
19+
20+
```bash
21+
docker network create tfgrid_bknd
22+
```
23+
1824
### 1. Indexer
1925

26+
#### Using a snapshot (recommended)
27+
28+
Download a pre-built CockroachDB snapshot to avoid syncing from scratch (which takes days):
29+
30+
```bash
31+
# Check https://bknd.snapshot.grid.tf/ for latest snapshots per network
32+
wget -O snapshot.tar.gz <snapshot_url>
33+
34+
# Extract — do NOT use --strip-components
35+
# (SST files are at root level; --strip-components silently skips them all)
36+
mkdir -p /path/to/cockroach-data
37+
tar -xzf snapshot.tar.gz -C /path/to/cockroach-data/
38+
39+
# Verify: should see thousands of .sst files
40+
find /path/to/cockroach-data/ -name "*.sst" | wc -l
41+
```
42+
43+
Bind-mount the data directory in `indexer/docker-compose.yml`:
44+
```yaml
45+
cockroachdb:
46+
volumes:
47+
- /path/to/cockroach-data:/cockroach/cockroach-data
48+
```
49+
50+
#### Configuration
51+
2052
Configure `indexer/.env`:
2153

2254
| Variable | Description | Default |
@@ -41,8 +73,8 @@ Configure `.env` in the project root:
4173
| `DB_USER` | PostgreSQL user | `postgres` |
4274
| `DB_PASS` | PostgreSQL password | `postgres` |
4375
| `DB_PORT` | PostgreSQL port | `5432` |
44-
| `INDEXER_ENDPOINT_URL` | Indexer GraphQL gateway URL | `http://localhost:8888/graphql` |
45-
| `WS_URL` | TFChain node WebSocket URL (used for RPC calls) | `ws://localhost:9944` |
76+
| `INDEXER_ENDPOINT_URL` | Indexer GraphQL gateway URL. Use `http://gateway:8000/graphql` for Docker (shared network), `http://localhost:8888/graphql` for local dev | `http://gateway:8000/graphql` |
77+
| `WS_URL` | TFChain node WebSocket URL (used for RPC calls) | `wss://tfchain.dev.grid.tf/ws` |
4678
| `TYPEORM_LOGGING` | TypeORM log level | `error` |
4779

4880
```bash

indexer/docker-compose.yml

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
1-
version: "3.8"
1+
networks:
2+
default:
3+
name: tfgrid_bknd
4+
external: true
25

36
services:
4-
db:
5-
image: cockroachdb/cockroach:v22.2.2
7+
cockroachdb:
8+
image: cockroachdb/cockroach:v22.2.15
69
restart: always
710
ports:
8-
- "26257:26257"
9-
- "8080:8080"
11+
- "127.0.0.1:26257:26257"
12+
- "127.0.0.1:8080:8080"
1013
command: start-single-node --insecure
1114
volumes:
1215
- /cockroach/cockroach-data
1316

14-
db-init:
17+
cockroachdb-init:
1518
depends_on:
16-
- db
17-
image: cockroachdb/cockroach:v22.2.2
18-
19+
- cockroachdb
20+
image: cockroachdb/cockroach:v22.2.15
1921
volumes:
2022
- ./setup_db.sh:/setup_db.sh
2123
entrypoint: "/bin/bash"
2224
command: /setup_db.sh
2325
restart: on-failure
24-
26+
2527
ingest:
2628
depends_on:
27-
db-init:
29+
cockroachdb-init:
2830
condition: service_completed_successfully
2931
restart: on-failure
3032
image: subsquid/substrate-ingest:1
@@ -37,7 +39,7 @@ services:
3739
"-c",
3840
"20",
3941
"--out",
40-
"postgres://root@db:26257/defaultdb",
42+
"postgres://root@cockroachdb:26257/defaultdb",
4143
"--types-bundle",
4244
"/configs/typesBundle.json",
4345
"--start-block",
@@ -46,24 +48,28 @@ services:
4648

4749
gateway:
4850
depends_on:
49-
db-init:
51+
cockroachdb-init:
5052
condition: service_completed_successfully
51-
image: subsquid/substrate-gateway:2.5.0
53+
image: subsquid/substrate-gateway:2.7.0
5254
environment:
5355
DATABASE_MAX_CONNECTIONS: 5
5456
RUST_LOG: "actix_web=info,actix_server=info"
55-
command: [ "--database-url", "postgres://root@db:26257/defaultdb" ]
57+
command: [ "--database-url", "postgres://root@cockroachdb:26257/defaultdb" ]
58+
# Published for local dev (localhost:8888) and browser access.
59+
# In production, the processor reaches the gateway via service name
60+
# (http://gateway:8000/graphql) on the shared network. Comment out
61+
# these ports if you don't need host access.
5662
ports:
5763
- "8888:8000"
5864

5965
explorer:
6066
depends_on:
61-
db-init:
67+
cockroachdb-init:
6268
condition: service_completed_successfully
6369
image: subsquid/substrate-explorer:firesquid
6470
environment:
6571
DB_TYPE: cockroach
66-
DB_HOST: db
72+
DB_HOST: cockroachdb
6773
DB_PORT: "26257"
6874
DB_NAME: "defaultdb"
6975
DB_USER: "root"

indexer/readme.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Setting Up an Indexer
22

3+
## Prerequisites
4+
5+
Both the indexer and processor compose stacks share a Docker network. Create it before starting:
6+
7+
```bash
8+
docker network create tfgrid_bknd
9+
```
10+
311
## Configuration
412

513
The `.env` file contains the indexer options:
@@ -18,23 +26,37 @@ START_HEIGHT=0
1826

1927
Start the indexer stack:
2028

21-
```
29+
```bash
2230
docker compose up -d
2331
```
2432

2533
Stop:
2634

27-
```
35+
```bash
2836
docker compose down
2937
```
3038

3139
### Stack Components
3240

3341
| Container | Image | Role |
3442
|-----------|-------|------|
35-
| db | `cockroachdb/cockroach` | Database for storing raw indexed block data |
43+
| cockroachdb | `cockroachdb/cockroach` | Database for storing raw indexed block data |
3644
| ingest | `subsquid/substrate-ingest` | Connects to the TFChain node and ingests blocks into the database |
3745
| gateway | `subsquid/substrate-gateway` | GraphQL gateway over ingested data — the processor queries this |
3846
| explorer | `subsquid/substrate-explorer` | Web UI to browse raw ingested data and check sync status |
3947

48+
### CockroachDB Memory Tuning
49+
50+
By default, CockroachDB uses up to 25% of system RAM for cache and SQL memory. On VMs with limited RAM (4 GB or less), this can cause OOM. Add `--cache` and `--max-sql-memory` flags to the compose command to control usage:
51+
52+
```yaml
53+
# Fixed sizes for small VMs:
54+
command: start-single-node --insecure --cache=256MiB --max-sql-memory=256MiB
55+
56+
# Or fractions of total RAM for production (8+ GB):
57+
command: start-single-node --insecure --cache=.25 --max-sql-memory=.25
58+
```
59+
60+
If you see `memory budget exceeded` errors from the gateway or explorer, increase `--max-sql-memory`.
61+
4062
**Note on CockroachDB:** The `--insecure` flag is used for non-production/testing only. For production, use a secure cluster. See [CockroachDB docs](https://www.cockroachlabs.com/docs/stable/deploy-cockroachdb-on-premises-insecure).

indexer/setup_db.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
sleep 2
33
echo "Set sql.conn.max_read_buffer_message_size to 32 MiB"
44

5-
HOSTPARAMS="--host db --insecure"
5+
HOSTPARAMS="--host cockroachdb --insecure"
66
SQL="/cockroach/cockroach.sh sql $HOSTPARAMS"
77

88
# https://github.com/threefoldtech/tfchain_graphql/issues/130#issuecomment-1689987550

0 commit comments

Comments
 (0)