Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .env
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
DB_NAME=tfgrid-graphql
DB_USER=postgres
DB_PASS=postgres
#DB_HOST=172.17.0.1
DB_PORT=5432
TYPEORM_LOGGING=error
INDEXER_ENDPOINT_URL=http://localhost:8888/graphql
WS_URL=ws://localhost:9944
# For Docker deployment (shared tfgrid_bknd network): use gateway service name
# For local development outside Docker: use http://localhost:8888/graphql
INDEXER_ENDPOINT_URL=http://gateway:8000/graphql
WS_URL=wss://tfchain.dev.grid.tf/ws
9 changes: 6 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
version: "3.4"
networks:
default:
name: tfgrid_bknd
external: true

services:
db:
image: postgres:16
image: postgres:17
restart: always
ports:
- "${DB_PORT}:5432"
- "127.0.0.1:${DB_PORT}:5432"
volumes:
- /var/lib/postgresql/data
environment:
Expand Down
18 changes: 17 additions & 1 deletion docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ yarn build

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

### Create the shared Docker network

Both compose stacks use a shared external network. Create it once:

```bash
docker network create tfgrid_bknd
```

### Run Indexer

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

### Run Processor (local, outside Docker)

Check `.env` and adjust the websocket endpoint and indexer URL to your local setup.
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:

```bash
# .env — for local development (processor outside Docker):
INDEXER_ENDPOINT_URL=http://localhost:8888/graphql

# .env — for Docker deployment (processor inside Docker, shared network):
# INDEXER_ENDPOINT_URL=http://gateway:8000/graphql
```

Start the local PostgreSQL container and run the processor:

Expand Down
36 changes: 34 additions & 2 deletions docs/production.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,40 @@ The production stack has two independent layers:

## Run the Setup

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:

```bash
docker network create tfgrid_bknd
```

### 1. Indexer

#### Using a snapshot (recommended)

Download a pre-built CockroachDB snapshot to avoid syncing from scratch (which takes days):

```bash
# Check https://bknd.snapshot.grid.tf/ for latest snapshots per network
wget -O snapshot.tar.gz <snapshot_url>

# Extract — do NOT use --strip-components
# (SST files are at root level; --strip-components silently skips them all)
mkdir -p /path/to/cockroach-data
tar -xzf snapshot.tar.gz -C /path/to/cockroach-data/

# Verify: should see thousands of .sst files
find /path/to/cockroach-data/ -name "*.sst" | wc -l
```

Bind-mount the data directory in `indexer/docker-compose.yml`:
```yaml
cockroachdb:
volumes:
- /path/to/cockroach-data:/cockroach/cockroach-data
```

#### Configuration

Configure `indexer/.env`:

| Variable | Description | Default |
Expand All @@ -41,8 +73,8 @@ Configure `.env` in the project root:
| `DB_USER` | PostgreSQL user | `postgres` |
| `DB_PASS` | PostgreSQL password | `postgres` |
| `DB_PORT` | PostgreSQL port | `5432` |
| `INDEXER_ENDPOINT_URL` | Indexer GraphQL gateway URL | `http://localhost:8888/graphql` |
| `WS_URL` | TFChain node WebSocket URL (used for RPC calls) | `ws://localhost:9944` |
| `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` |
| `WS_URL` | TFChain node WebSocket URL (used for RPC calls) | `wss://tfchain.dev.grid.tf/ws` |
| `TYPEORM_LOGGING` | TypeORM log level | `error` |

```bash
Expand Down
40 changes: 23 additions & 17 deletions indexer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
version: "3.8"
networks:
default:
name: tfgrid_bknd
external: true

services:
db:
image: cockroachdb/cockroach:v22.2.2
cockroachdb:
image: cockroachdb/cockroach:v22.2.15
restart: always
ports:
- "26257:26257"
- "8080:8080"
- "127.0.0.1:26257:26257"
- "127.0.0.1:8080:8080"
command: start-single-node --insecure
volumes:
- /cockroach/cockroach-data

db-init:
cockroachdb-init:
depends_on:
- db
image: cockroachdb/cockroach:v22.2.2

- cockroachdb
image: cockroachdb/cockroach:v22.2.15
volumes:
- ./setup_db.sh:/setup_db.sh
entrypoint: "/bin/bash"
command: /setup_db.sh
restart: on-failure

ingest:
depends_on:
db-init:
cockroachdb-init:
condition: service_completed_successfully
restart: on-failure
image: subsquid/substrate-ingest:1
Expand All @@ -37,7 +39,7 @@ services:
"-c",
"20",
"--out",
"postgres://root@db:26257/defaultdb",
"postgres://root@cockroachdb:26257/defaultdb",
"--types-bundle",
"/configs/typesBundle.json",
"--start-block",
Expand All @@ -46,24 +48,28 @@ services:

gateway:
depends_on:
db-init:
cockroachdb-init:
condition: service_completed_successfully
image: subsquid/substrate-gateway:2.5.0
image: subsquid/substrate-gateway:2.7.0
environment:
DATABASE_MAX_CONNECTIONS: 5
RUST_LOG: "actix_web=info,actix_server=info"
command: [ "--database-url", "postgres://root@db:26257/defaultdb" ]
command: [ "--database-url", "postgres://root@cockroachdb:26257/defaultdb" ]
# Published for local dev (localhost:8888) and browser access.
# In production, the processor reaches the gateway via service name
# (http://gateway:8000/graphql) on the shared network. Comment out
# these ports if you don't need host access.
ports:
- "8888:8000"

explorer:
depends_on:
db-init:
cockroachdb-init:
condition: service_completed_successfully
image: subsquid/substrate-explorer:firesquid
environment:
DB_TYPE: cockroach
DB_HOST: db
DB_HOST: cockroachdb
DB_PORT: "26257"
DB_NAME: "defaultdb"
DB_USER: "root"
Expand Down
28 changes: 25 additions & 3 deletions indexer/readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Setting Up an Indexer

## Prerequisites

Both the indexer and processor compose stacks share a Docker network. Create it before starting:

```bash
docker network create tfgrid_bknd
```

## Configuration

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

Start the indexer stack:

```
```bash
docker compose up -d
```

Stop:

```
```bash
docker compose down
```

### Stack Components

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

### CockroachDB Memory Tuning

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:

```yaml
# Fixed sizes for small VMs:
command: start-single-node --insecure --cache=256MiB --max-sql-memory=256MiB

# Or fractions of total RAM for production (8+ GB):
command: start-single-node --insecure --cache=.25 --max-sql-memory=.25
```

If you see `memory budget exceeded` errors from the gateway or explorer, increase `--max-sql-memory`.

**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).
2 changes: 1 addition & 1 deletion indexer/setup_db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
sleep 2
echo "Set sql.conn.max_read_buffer_message_size to 32 MiB"

HOSTPARAMS="--host db --insecure"
HOSTPARAMS="--host cockroachdb --insecure"
SQL="/cockroach/cockroach.sh sql $HOSTPARAMS"

# https://github.com/threefoldtech/tfchain_graphql/issues/130#issuecomment-1689987550
Expand Down
Loading