Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/docker #9

Merged
merged 8 commits into from
Apr 10, 2024
Merged
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
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# both daemon and cli work mode
ETH_NETWORK=1
EL_RPC_URLS=https://mainnet.infura.io/v3/...
CL_API_URLS=https://quiknode.pro/...
CSM_ADDRESS=0x9D4AF1Ee19Dad8857db3a45B0374c81c8A1C6320
VERIFIER_ADDRESS=0x9D4AF1Ee19Dad8857db3a45B0374c81c8A1C6321
TX_SIGNER_PRIVATE_KEY=0x...

# only for daemon mode
KEYSAPI_API_URLS=https://keys-api.lido.fi/
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
# ENV
/.env

# Storage
/.keys-indexer-*
/.roots-stack-*
# Application storage
/storage

# Logs
logs
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v20.11.0
v20.12.1
22 changes: 22 additions & 0 deletions Dockerfile.cli
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM node:20.12.1-alpine as building

WORKDIR /app

COPY package.json yarn.lock ./
COPY ./tsconfig*.json ./
COPY ./src ./src

RUN yarn install --frozen-lockfile --non-interactive && yarn cache clean && yarn typechain
RUN yarn build

FROM node:20.12.1-alpine

WORKDIR /app

COPY --from=building /app/dist ./dist
COPY --from=building /app/node_modules ./node_modules
COPY ./package.json ./

USER node

ENTRYPOINT ["yarn"]
26 changes: 26 additions & 0 deletions Dockerfile.daemon
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM node:20.12.1-alpine as building

WORKDIR /app

COPY package.json yarn.lock ./
COPY ./tsconfig*.json ./
COPY ./src ./src

RUN yarn install --frozen-lockfile --non-interactive && yarn cache clean && yarn typechain
RUN yarn build

FROM node:20.12.1-alpine

WORKDIR /app

COPY --from=building /app/dist ./dist
COPY --from=building /app/node_modules ./node_modules
COPY ./package.json ./
RUN mkdir -p ./storage/ && chown -R node:node ./storage/

USER node

HEALTHCHECK --interval=60s --timeout=10s --retries=3 \
CMD sh -c "wget -nv -t1 --spider http://localhost:$HTTP_PORT/health" || exit 1

CMD ["yarn", "start:prod"]
74 changes: 74 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
services:

prometheus:
image: prom/prometheus:latest
container_name: prometheus
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
command:
- --config.file=/etc/prometheus/prometheus.yml

daemon:
build:
context: .
dockerfile: ./Dockerfile.daemon
container_name: prover-daemon
restart: unless-stopped
environment:
- DRY_RUN=${DRY_RUN:-false}
- ETH_NETWORK=${ETH_NETWORK}
- KEYSAPI_API_URLS=${KEYSAPI_API_URLS}
- EL_RPC_URLS=${EL_RPC_URLS}
- CL_API_URLS=${CL_API_URLS}
- CSM_ADDRESS=${CSM_ADDRESS}
- VERIFIER_ADDRESS=${VERIFIER_ADDRESS}
- TX_SIGNER_PRIVATE_KEY=${TX_SIGNER_PRIVATE_KEY:-}
expose:
- "${HTTP_PORT:-8080}"
ports:
- "${EXTERNAL_HTTP_PORT:-${HTTP_PORT:-8080}}:${HTTP_PORT:-8080}"
volumes:
- ./storage/:/app/storage/
depends_on:
- prometheus

#
# CLI tools
#
withdrawal:
build:
context: .
dockerfile: ./Dockerfile.cli
entrypoint:
- yarn
- withdrawal
container_name: prover-cli-withdrawal
restart: no
environment:
- DRY_RUN=${DRY_RUN:-false}
- ETH_NETWORK=${ETH_NETWORK}
- EL_RPC_URLS=${EL_RPC_URLS}
- CL_API_URLS=${CL_API_URLS}
- CSM_ADDRESS=${CSM_ADDRESS}
- VERIFIER_ADDRESS=${VERIFIER_ADDRESS}
- TX_SIGNER_PRIVATE_KEY=${TX_SIGNER_PRIVATE_KEY:-}

slashing:
build:
context: .
dockerfile: ./Dockerfile.cli
entrypoint:
- yarn
- slashing
container_name: prover-cli-slashing
restart: no
environment:
- DRY_RUN=${DRY_RUN:-false}
- ETH_NETWORK=${ETH_NETWORK}
- EL_RPC_URLS=${EL_RPC_URLS}
- CL_API_URLS=${CL_API_URLS}
- CSM_ADDRESS=${CSM_ADDRESS}
- VERIFIER_ADDRESS=${VERIFIER_ADDRESS}
- TX_SIGNER_PRIVATE_KEY=${TX_SIGNER_PRIVATE_KEY:-}
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"private": true,
"license": "GPL-3.0",
"scripts": {
"prove": "nest build && NODE_OPTIONS=--max_old_space_size=8192 WORKING_MODE=cli node dist/main prove",
"prove:debug": "nest build && NODE_OPTIONS=--max_old_space_size=8192 WORKING_MODE=cli node --inspect dist/main prove",
"prove": "NODE_OPTIONS=--max_old_space_size=8192 WORKING_MODE=cli node dist/main prove",
"prove:debug": "NODE_OPTIONS=--max_old_space_size=8192 WORKING_MODE=cli node --inspect dist/main prove",
"slashing": "yarn prove slashing",
"slashing:debug": "yarn prove:debug slashing",
"withdrawal": "yarn prove withdrawal",
Expand All @@ -19,14 +19,12 @@
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"start:prod": "NODE_OPTIONS=--max_old_space_size=8192 node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\"",
"lint:fix": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
"test-daemon": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest --config test/jest-daemon-e2e.json",
"test-cli": "WORKING_MODE=cli NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest --config test/jest-cli-e2e.json"
},
"dependencies": {
"@huanshiwushuang/lowdb": "^6.0.2",
Expand All @@ -39,14 +37,18 @@
"@nestjs/config": "^3.1.1",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/terminus": "^10.2.3",
"@typechain/ethers-v5": "^11.1.2",
"@types/cli-progress": "^3.11.5",
"@willsoto/nestjs-prometheus": "^6.0.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"ethers": "^5.7.2",
"nest-commander": "^3.12.5",
"nest-commander-testing": "^3.3.0",
"nest-winston": "^1.9.4",
"ora-classic": "^5.4.2",
"prom-client": "^15.1.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"stream-chain": "^2.2.5",
Expand Down
13 changes: 13 additions & 0 deletions prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
global:
scrape_interval: 10s
evaluation_interval: 15s

scrape_configs:

- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']

- job_name: 'daemon'
static_configs:
- targets: ['daemon:8080']
21 changes: 0 additions & 21 deletions src/cli/cli.service.spec.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/cli/commands/prove.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { CsmContract } from '../../common/contracts/csm-contract.service';
import { ProverService } from '../../common/prover/prover.service';
import { KeyInfoFn } from '../../common/prover/types';
import { Consensus } from '../../common/providers/consensus/consensus';
import { Execution } from '../../common/providers/execution/execution';

type ProofOptions = {
nodeOperatorId: string;
Expand Down Expand Up @@ -35,7 +34,6 @@ export class ProveCommand extends CommandRunner {
protected readonly inquirerService: InquirerService,
protected readonly csm: CsmContract,
protected readonly consensus: Consensus,
protected readonly execution: Execution,
protected readonly prover: ProverService,
) {
super();
Expand Down
19 changes: 0 additions & 19 deletions src/common/config/config.service.spec.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/common/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class ConfigService extends ConfigServiceSource<EnvironmentVariables> {
* List of env variables that should be hidden
*/
public get secrets(): string[] {
return [...this.get('EL_RPC_URLS'), ...this.get('CL_API_URLS'), ...this.get('KEYSAPI_API_URLS')];
return [...this.get('EL_RPC_URLS'), ...this.get('CL_API_URLS'), ...(this.get('KEYSAPI_API_URLS') ?? [])];
}

public get<T extends keyof EnvironmentVariables>(key: T): EnvironmentVariables[T] {
Expand Down
3 changes: 2 additions & 1 deletion src/common/config/env.validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ export class EnvironmentVariables {
@IsArray()
@ArrayMinSize(1)
@Transform(({ value }) => value.split(','))
public KEYSAPI_API_URLS!: string[];
@ValidateIf((vars) => vars.WORKING_MODE === WorkingMode.Daemon)
public KEYSAPI_API_URLS: string[];

@IsInt()
@Transform(({ value }) => parseInt(value, 10), { toClassOnly: true })
Expand Down
1 change: 1 addition & 0 deletions src/common/health/health.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const HEALTH_URL = 'health';
23 changes: 23 additions & 0 deletions src/common/health/health.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as v8 from 'v8';

import { Controller, Get } from '@nestjs/common';
import { HealthCheck, HealthCheckService, MemoryHealthIndicator } from '@nestjs/terminus';

import { HEALTH_URL } from './health.constants';

@Controller(HEALTH_URL)
export class HealthController {
private readonly maxHeapSize: number;
constructor(
private health: HealthCheckService,
private memory: MemoryHealthIndicator,
) {
this.maxHeapSize = v8.getHeapStatistics().heap_size_limit;
}

@Get()
@HealthCheck()
check() {
return this.health.check([async () => this.memory.checkHeap('memoryHeap', this.maxHeapSize)]);
}
}
7 changes: 5 additions & 2 deletions src/common/health/health.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Module } from '@nestjs/common';
import { TerminusModule } from '@nestjs/terminus';

import { HealthService } from './health.service';
import { HealthController } from './health.controller';

@Module({
providers: [HealthService],
providers: [],
controllers: [HealthController],
imports: [TerminusModule],
})
export class HealthModule {}
19 changes: 0 additions & 19 deletions src/common/health/health.service.spec.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/common/health/health.service.ts

This file was deleted.

3 changes: 3 additions & 0 deletions src/common/health/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './health.constants';
export * from './health.controller';
export * from './health.module';
4 changes: 4 additions & 0 deletions src/common/prometheus/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './prometheus.constants';
export * from './prometheus.controller';
export * from './prometheus.module';
export * from './prometheus.service';
1 change: 1 addition & 0 deletions src/common/prometheus/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './prometheus.interface';
19 changes: 19 additions & 0 deletions src/common/prometheus/interfaces/prometheus.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Metrics } from '@willsoto/nestjs-prometheus';
import * as client from 'prom-client';
export { Metrics } from '@willsoto/nestjs-prometheus';

export type Options<T extends string> =
| client.GaugeConfiguration<T>
| client.SummaryConfiguration<T>
| client.CounterConfiguration<T>
| client.HistogramConfiguration<T>;

export type Metric<T extends Metrics, S extends string> = T extends 'Gauge'
? client.Gauge<S>
: T extends 'Summary'
? client.Summary<S>
: T extends 'Counter'
? client.Counter<S>
: T extends 'Histogram'
? client.Histogram<S>
: never;
Loading
Loading