Skip to content

Commit e82adee

Browse files
committed
Upgrade tracker
1 parent 5eeb764 commit e82adee

38 files changed

+855
-449
lines changed

packages/tracker/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ RPC_PASSWORD=opcatAwesome
1212

1313
NETWORK=mainnet
1414
API_PORT=3000
15+
WORKER_PORT=3001
1516
GENESIS_BLOCK_HEIGHT=0 # the height of block that CAT protocol launches

packages/tracker/.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,5 @@ pids
5555
# Diagnostic reports (https://nodejs.org/api/report.html)
5656
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
5757

58-
/docker/data/*
58+
/docker/*
5959
!/docker/data/bitcoin.conf
60-
/docker/pgdata/*
61-
!/docker/pgdata/.gitkeep

packages/tracker/README.md

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,68 @@
11
# CAT Tracker
22

3-
The tracker reads CAT token transactions from the blockchain, stores them in a database (`Postgres`) in a structured way, which can be quickly retrieved via RESTful APIs. The Swagger documentation for all the APIs can be found at http://127.0.0.1:3000 after running.
3+
The tracker reads CAT token transactions from the blockchain, stores them in Postgres in a structured way, which can be quickly retrieved via RESTful APIs. The Swagger documentation for all the APIs can be found at http://127.0.0.1:3000 after running.
44

55
## Installation
66

77
```bash
8-
yarn install
8+
yarn install && yarn build
99
```
1010

11-
## Build
12-
13-
```sh
14-
yarn build
15-
```
16-
17-
## Before Run
11+
## Prerequisite
1812

1913
The tracker needs a full node and Postgres. We use Fractal node as an example here.
2014

21-
Make sure you have `docker` installed, you can follow this [guide](https://docs.docker.com/engine/install/) to install it.
15+
Make sure you have `docker` installed, or follow this [guide](https://docs.docker.com/engine/install/) to install it.
2216

2317
1. Update `.env` file with your own configuration.
24-
2. Update directory permission
18+
19+
2. Update directory permission to avoid `docker` doesn't have rights to write data.
2520

2621
```bash
27-
sudo chmod 777 docker/data
28-
sudo chmod 777 docker/pgdata
22+
sudo chmod o+w docker/data
2923
```
3024

31-
3. Run `postgresql` and `bitcoind`:
25+
3. Run `postgresql` and `bitcoind`.
3226

3327
```bash
3428
docker compose up -d
3529
```
3630

37-
## Run the tracker service
38-
39-
### Use `Docker` (Recommended)
31+
## Migration
4032

41-
1. Build docker image under the project root directory
33+
Run the following command to initialize database tables for the first-time run, or migrate data when upgrading from a previous version.
4234

4335
```bash
44-
cd ../../ && docker build -t tracker:latest .
36+
yarn migration:run
4537
```
4638

47-
2. Run the container
39+
**Note: If upgrading the tracker from a previous version, migrating the table data may take a few hours. Make a [database backup](https://www.postgresql.org/docs/current/app-pgdump.html) before this.**
4840

49-
```bash
50-
docker run -d \
51-
--name tracker \
52-
--add-host="host.docker.internal:host-gateway" \
53-
-e DATABASE_HOST="host.docker.internal" \
54-
-e RPC_HOST="host.docker.internal" \
55-
-p 3000:3000 \
56-
tracker:latest
57-
```
41+
## Run
5842

59-
3. Check tracker logs
43+
The tracker consists of two modules:
6044

61-
```bash
62-
docker logs -f tracker
63-
```
45+
- `worker`, reads CAT token transactions from the blockchain and stores them in a database.
46+
- `api`, reads data from the database and serves the RESTful APIs.
6447

65-
### Use `yarn`
48+
To run the `worker` module:
6649

67-
* development mode
6850
```bash
69-
yarn run start
51+
# development
52+
yarn start:worker
53+
54+
# production mode
55+
yarn start:worker:prod
7056
```
7157

72-
* production mode
58+
To run the `api` module:
59+
7360
```bash
74-
yarn run start:prod
61+
# development
62+
yarn start:api
63+
64+
# production mode
65+
yarn start:api:prod
7566
```
7667

77-
> **Note:** Make sure the tracker syncs to the latest block before you run CLI over it. The sync-up progress can be found at http://127.0.0.1:3000/api after running.
68+
**Note:** Make sure the tracker syncs to the latest block before you run CLI over it. The sync-up progress can be found at http://127.0.0.1:3000/api after running.

packages/tracker/docker-compose.yml

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,11 @@ services:
1515
bitcoind:
1616
image: fractalbitcoin/fractal:v0.2.1
1717
restart: always
18-
entrypoint: ["bitcoind", "-datadir=/data/", "-maxtipage=504576000"]
19-
command: ""
18+
entrypoint: ["bitcoind", "-datadir=/data/"]
2019
healthcheck:
2120
test: ["CMD", "bitcoin-cli", "-datadir=/data/", "getblockchaininfo"]
2221
ports:
23-
- "8330:8330"
24-
- "8331:8331"
2522
- "8332:8332"
2623
- "8333:8333"
27-
deploy:
28-
resources:
29-
limits:
30-
memory: 40G
31-
memswap_limit: 60G
32-
mem_swappiness: 100
3324
volumes:
3425
- ./docker/data:/data
35-
logging:
36-
driver: "json-file"
37-
options:
38-
labels: "env,filebeat,name"
39-
max-size: "1g"
40-
max-file: "3"

packages/tracker/docker/data/bitcoin.conf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,4 @@ rpcworkqueue=2048
1313
rpcthreads=32
1414
rpcservertimeout=120
1515

16-
zmqpubhashblock=tcp://0.0.0.0:8330
17-
zmqpubrawtx=tcp://0.0.0.0:8331
18-
1916
maxconnections=50
20-
21-
debug=bench
22-
debug=netmsg

packages/tracker/nest-cli.json renamed to packages/tracker/nest-cli-api.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"$schema": "https://json.schemastore.org/nest-cli",
33
"collection": "@nestjs/schematics",
44
"sourceRoot": "src",
5+
"entryFile": "main-api",
56
"compilerOptions": {
67
"deleteOutDir": true
78
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"$schema": "https://json.schemastore.org/nest-cli",
3+
"collection": "@nestjs/schematics",
4+
"sourceRoot": "src",
5+
"entryFile": "main-worker",
6+
"compilerOptions": {
7+
"deleteOutDir": true
8+
}
9+
}

packages/tracker/package.json

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,22 @@
88
"scripts": {
99
"build": "nest build",
1010
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
11-
"start": "nest start",
12-
"start:dev": "nest start --watch",
13-
"start:debug": "nest start --debug --watch",
14-
"start:prod": "node dist/main",
11+
12+
"start:worker": "nest start -c nest-cli-worker.json",
13+
"start:worker:dev": "nest start --watch -c nest-cli-worker.json",
14+
"start:worker:debug": "nest start --debug --watch -c nest-cli-worker.json",
15+
"start:worker:prod": "yarn build && node dist/main-worker",
16+
17+
"start:api": "nest start -c nest-cli-api.json",
18+
"start:api:dev": "nest start --watch -c nest-cli-api.json",
19+
"start:api:debug": "nest start --debug --watch -c nest-cli-api.json",
20+
"start:api:prod": "yarn build && node dist/main-api",
21+
22+
"typeorm": "npx typeorm-ts-node-commonjs -d src/config/db.config.ts",
23+
"migration:generate": "npm run typeorm -- migration:generate",
24+
"migration:run": "npm run typeorm -- migration:run",
25+
"migration:revert": "npm run typeorm -- migration:revert",
26+
1527
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
1628
"test": "jest",
1729
"test:watch": "jest --watch",
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Module } from '@nestjs/common';
2+
import { TypeOrmModule } from '@nestjs/typeorm';
3+
import { ConfigModule } from '@nestjs/config';
4+
// config
5+
import { appConfig } from './config/app.config';
6+
import { ormConfig } from './config/db.config';
7+
// routes
8+
import { HealthCheckModule } from './routes/healthCheck/healthCheck.module';
9+
import { TokenModule } from './routes/token/token.module';
10+
import { MinterModule } from './routes/minter/minter.module';
11+
import { AddressModule } from './routes/address/address.module';
12+
// serivces
13+
import { CommonModule } from './services/common/common.module';
14+
15+
// eslint-disable-next-line @typescript-eslint/no-var-requires
16+
require('dotenv').config();
17+
18+
@Module({
19+
imports: [
20+
ConfigModule.forRoot({
21+
load: [appConfig],
22+
}),
23+
TypeOrmModule.forRoot(ormConfig),
24+
25+
HealthCheckModule,
26+
TokenModule,
27+
MinterModule,
28+
AddressModule,
29+
30+
CommonModule,
31+
],
32+
controllers: [],
33+
providers: [],
34+
})
35+
export class AppApiModule {}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Module } from '@nestjs/common';
2+
import { TypeOrmModule } from '@nestjs/typeorm';
3+
import { ConfigModule } from '@nestjs/config';
4+
// config
5+
import { appConfig } from './config/app.config';
6+
import { ormConfig } from './config/db.config';
7+
// services
8+
import { RpcModule } from './services/rpc/rpc.module';
9+
import { BlockModule } from './services/block/block.module';
10+
import { TxModule } from './services/tx/tx.module';
11+
import { CommonModule } from './services/common/common.module';
12+
13+
// eslint-disable-next-line @typescript-eslint/no-var-requires
14+
require('dotenv').config();
15+
16+
@Module({
17+
imports: [
18+
ConfigModule.forRoot({
19+
load: [appConfig],
20+
}),
21+
TypeOrmModule.forRoot(ormConfig),
22+
23+
RpcModule,
24+
BlockModule,
25+
TxModule,
26+
CommonModule,
27+
],
28+
controllers: [],
29+
providers: [],
30+
})
31+
export class AppWorkerModule {}

0 commit comments

Comments
 (0)