This is the backend service for the AdaStat.net Cardano Blockchain Explorer. It is a high-performance Node.js application built with Fastify and TypeScript, featuring built-in clustering, multi-network support, and a scalable database architecture.
- Cluster Management: Utilizes the Node.js cluster module for high availability and automatic worker restarts upon failure.
- Multi-Network Support: Pre-configured network parameters for Cardano Mainnet, Preprod, and Preview.
- Database Scaling: Support for a Primary database (for write operations and event triggers) and a Secondary database (for read-only queries).
- Zero-Downtime Reloads: Supports graceful worker recycling via
SIGUSR2signals.
Before running the backend, ensure you have the following installed and configured:
- Node.js: v22.x or higher (required for native
--env-filesupport). - Package Manager: pnpm is recommended (due to specific native dependency configurations).
- PostgreSQL: v16+ (Both Primary and optional Secondary instances).
- AdaStat Indexer: (Required) This backend depends on the data populated by the AdaStat Indexer. You must set up and run the indexer first to ensure the database schema and data are ready.
Install dependencies:
pnpm installEnvironment Variables:
Create a .env file in the root of the backend directory. You can use .env.example as a starting template:
cp .env.example .envKey Configuration Options:
PORT: Server port (default: 7828).DB_HOST: Primary database host for write operations and event listening.SECONDARY_DB_HOST: Read-only database host (defaults to Primary if not provided).ALLOWED_ORIGINS: Allowed WebSocket and website origins.
Note: Network-specific constants (like Epoch lengths and Mithril aggregators) can be found in .env.mainnet, .env.preprod, and .env.preview.
Start the server in watch mode with debug logging enabled:
pnpm run watchPro Tip: In development mode, you can type r in the terminal to manually trigger a worker restart.
Build the project:
pnpm run buildStart the production server:
pnpm run startpnpm run test: Run unit tests.pnpm run lint: Check and fix code style.pnpm run debug: Run withLOG_LEVEL=debug.pnpm run trace: Run withLOG_LEVEL=trace.
The application uses a Primary/Worker model:
- Primary Process: Manages worker health. If a worker crashes, the Primary process restarts it.
- Crash Protection: To prevent infinite loops, the application will exit if it crashes more than 4 times within a 10-second window.
- Hot Reload: Sending a
SIGUSR2signal to the master process will fork a new worker and gracefully kill the old one.
- Primary DB: The application connects to this instance to handle event triggers via
pg-listen. - Secondary DB: Designed for heavy
SELECTqueries to reduce the load on your primary database.
- License: Apache-2.0.
- Author: AdaStat Team (adastat.net).