Skip to content

Commit

Permalink
feat: add data fetcher service (#145)
Browse files Browse the repository at this point in the history
# What ❔

Add data fetcher service that can be vertically scaled.

## Why ❔

Having data fetching vertically scaled we will be able to keep up with
higher TPS.

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [X] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [X] Tests for the changes have been added / updated.
  • Loading branch information
vasyl-ivanchuk authored and pcheremu committed Feb 15, 2024
1 parent c088201 commit 2a4fb10
Show file tree
Hide file tree
Showing 14 changed files with 1,970 additions and 128 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
}
17 changes: 16 additions & 1 deletion docker-compose-cli.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
version: "3.2"
name: "zkcli-block-explorer"

services:
app:
platform: linux/amd64
Expand Down Expand Up @@ -40,6 +43,18 @@ services:
extra_hosts:
- "host.docker.internal:host-gateway"

data-fetcher:
platform: linux/amd64
image: "matterlabs/block-explorer-data-fetcher:${VERSION}"
environment:
- PORT=3040
- LOG_LEVEL=verbose
- NODE_ENV=development
- BLOCKCHAIN_RPC_URL=http://host.docker.internal:${RPC_PORT}
ports:
- '3040:3040'
restart: unless-stopped

api:
platform: linux/amd64
image: "matterlabs/block-explorer-api:${VERSION}"
Expand Down Expand Up @@ -72,4 +87,4 @@ services:
- POSTGRES_DB=block-explorer

volumes:
postgres:
postgres:
28 changes: 28 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,29 @@ services:
condition: service_healthy
restart: unless-stopped

data-fetcher:
build:
context: .
dockerfile: ./packages/data-fetcher/Dockerfile
target: development-stage
command: npm run --prefix packages/data-fetcher dev:debug
environment:
- PORT=3040
- LOG_LEVEL=verbose
- NODE_ENV=development
- BLOCKCHAIN_RPC_URL=http://zksync:3050
ports:
- '3040:3040'
- '9231:9229'
- '9232:9230'
volumes:
- ./packages/data-fetcher:/usr/src/app/packages/data-fetcher
- /usr/src/app/packages/data-fetcher/node_modules
depends_on:
zksync:
condition: service_healthy
restart: unless-stopped

api:
build:
context: .
Expand All @@ -59,6 +82,11 @@ services:
ports:
- '3020:3020'
- '3005:3005'
- '9233:9229'
- '9234:9230'
volumes:
- ./packages/api:/usr/src/app/packages/api
- /usr/src/app/packages/api/node_modules
depends_on:
- worker
restart: unless-stopped
Expand Down
211 changes: 207 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions packages/app/src/components/NetworkDeprecated.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@

<script lang="ts" setup>
import { computed } from "vue";
import { useRoute } from "vue-router";
import SystemAlert from "@/components/common/SystemAlert.vue";
import useContext from "@/composables/useContext";
import { getWindowLocation } from "@/utils/helpers";
const route = useRoute();
const { networks } = useContext();
const newNetworkUrl = computed(() => {
Expand All @@ -28,7 +26,6 @@ const newNetworkUrl = computed(() => {
const { hostname, origin } = getWindowLocation();
if (hostname === "localhost" || hostname.endsWith("web.app") || !network.hostnames?.length) {
console.log(route);
return `${origin}?network=${network.name}`;
}
return network.hostnames[0];
Expand Down
3 changes: 1 addition & 2 deletions packages/data-fetcher/src/block/block.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class BlockService {
public async getData(blockNumber: number): Promise<BlockData | null> {
const stopDurationMeasuring = this.processingDurationMetric.startTimer();

this.logger.debug({ message: "Getting block data from the blockchain", blockNumber });
this.logger.debug({ message: "Getting block from the blockchain", blockNumber });
const stopGetBlockInfoDurationMetric = this.getBlockInfoDurationMetric.startTimer();
const [block, blockDetails] = await Promise.all([
this.blockchainService.getBlock(blockNumber),
Expand Down Expand Up @@ -90,7 +90,6 @@ export class BlockService {
stopDurationMeasuring({ status: blockProcessingStatus, action: "get" });
}

this.logger.debug({ message: "Successfully generated block data", blockNumber });
return {
block,
blockDetails,
Expand Down
7 changes: 0 additions & 7 deletions packages/data-fetcher/src/health/health.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,4 @@ describe("HealthController", () => {
});
});
});

describe("onApplicationShutdown", () => {
it("defined and returns void", async () => {
const result = healthController.onApplicationShutdown();
expect(result).toBeUndefined();
});
});
});
14 changes: 3 additions & 11 deletions packages/data-fetcher/src/health/health.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Logger, Controller, Get, OnApplicationShutdown } from "@nestjs/common";
import { Logger, Controller, Get } from "@nestjs/common";
import { HealthCheckService, HealthCheck, HealthCheckResult } from "@nestjs/terminus";
import { JsonRpcHealthIndicator } from "./jsonRpcProvider.health";

@Controller(["health", "ready"])
export class HealthController implements OnApplicationShutdown {
export class HealthController {
private readonly logger: Logger;

constructor(
Expand All @@ -17,18 +17,10 @@ export class HealthController implements OnApplicationShutdown {
@HealthCheck()
public async check(): Promise<HealthCheckResult> {
try {
const healthCheckResult = await this.healthCheckService.check([
() => this.jsonRpcHealthIndicator.isHealthy("jsonRpcProvider"),
]);
this.logger.debug({ message: "Health check result", ...healthCheckResult });
return healthCheckResult;
return await this.healthCheckService.check([() => this.jsonRpcHealthIndicator.isHealthy("jsonRpcProvider")]);
} catch (error) {
this.logger.error({ message: error.message, response: error.getResponse() }, error.stack);
throw error;
}
}

onApplicationShutdown(signal?: string): void {
this.logger.debug({ message: "Received a signal", signal });
}
}
Loading

0 comments on commit 2a4fb10

Please sign in to comment.