Skip to content

Commit

Permalink
feat: porting changes from release/testnet-beta (matter-labs#1)
Browse files Browse the repository at this point in the history
* feat: adopt style for zulu
* feat: add scripts
* feat: add docker file for other network

---------

Co-authored-by: zh-kai <[email protected]>
Co-authored-by: Iron Lu <[email protected]>
  • Loading branch information
3 people authored Jul 23, 2024
1 parent 18925c4 commit b7ed739
Show file tree
Hide file tree
Showing 45 changed files with 1,566 additions and 1,460 deletions.
2 changes: 1 addition & 1 deletion .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx --no-install commitlint --edit
#npx --no-install commitlint --edit
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged
#npx lint-staged
32 changes: 32 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
help: ## Display this help screen
@grep -h \
-E '^[a-zA-Z_0-9-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

login_db: ## if need use pswd: notsecurepassword
@psql -h localhost -U postgres -p 5433


login_alpha_db: ## if need use pswd: notsecurepassword
@psql -h localhost -U postgres -p 5434 -d block-explorer-alpha

start_local: ## run explorer with local db
@docker compose -f docker-compose-local.yaml up -d

start: ## run explorer with external db
@docker compose -f docker-compose.yaml up -d


start_alpha_api_local: ## run explorer with local db
@docker compose -f docker-compose-local-alpha.yaml up -d

start_alpha_api: ## run explorer with local db
@docker compose -f docker-compose-local-alpha.yaml up -d alpha-api


clear: ## clear docker
docker compose down --volumes
docker compose pull


.PHONY: clippy fmt test
71 changes: 18 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<h1 align="center">zkSync Era Block Explorer</h1>
<h1 align="center">ZuluPrime Block Explorer</h1>

<p align="center">Online blockchain browser for viewing and analyzing <a href="https://zksync.io">zkSync Era</a> blockchain.</p>
<p align="center">Online blockchain browser for viewing and analyzing <a href="https://zulunetwork.io">ZuluPrime</a> blockchain.</p>

## 📌 Overview
This repository is a monorepo consisting of 4 packages:
- [Worker](./packages/worker) - an indexer service for [zkSync Era](https://zksync.io) blockchain data. The purpose of the service is to read blockchain data in real time, transform it and fill in it's database with the data in a way that makes it easy to be queried by the [API](./packages/api) service.
- [Worker](./packages/worker) - an indexer service for [ZuluPrime](https://zulunetwork.io) blockchain data. The purpose of the service is to read blockchain data in real time, transform it and fill in it's database with the data in a way that makes it easy to be queried by the [API](./packages/api) service.
- [Data Fetcher](./packages/data-fetcher) - a service that exposes and implements an HTTP endpoint to retrieve aggregated data for a certain block / range of blocks from the blockchain. This endpoint is called by the [Worker](./packages/worker) service.
- [API](./packages/api) - a service providing Web API for retrieving structured [zkSync Era](https://zksync.io) blockchain data collected by [Worker](./packages/worker). It connects to the Worker's database to be able to query the collected data.
- [API](./packages/api) - a service providing Web API for retrieving structured [ZuluPrime](https://zulunetwork.io) blockchain data collected by [Worker](./packages/worker). It connects to the Worker's database to be able to query the collected data.
- [App](./packages/app) - a front-end app providing an easy-to-use interface for users to view and inspect transactions, blocks, contracts and more. It makes requests to the [API](./packages/api) to get the data and presents it in a way that's easy to read and understand.

## 🏛 Architecture
Expand All @@ -15,7 +15,7 @@ The following diagram illustrates how are the block explorer components connecte
```mermaid
flowchart
subgraph blockchain[Blockchain]
Blockchain[zkSync Era JSON-RPC API]
Blockchain[ZuluPrime JSON-RPC API]
end
subgraph explorer[Block explorer]
Expand All @@ -37,7 +37,7 @@ flowchart
Worker-."Request data (HTTP)".->Blockchain
```

[Worker](./packages/worker) service retrieves aggregated data from the [Data Fetcher](./packages/data-fetcher) via HTTP and also directly from the blockchain using [zkSync Era JSON-RPC API](https://era.zksync.io/docs/api/api.html), processes it and saves into the database. [API](./packages/api) service is connected to the same database where it gets the data from to handle API requests. It performs only read requests to the database. The front-end [App](./packages/app) makes HTTP calls to the Block Explorer [API](./packages/api) to get blockchain data and to the [zkSync Era JSON-RPC API](https://era.zksync.io/docs/api/api.html) for reading contracts, performing transactions etc.
[Worker](./packages/worker) service retrieves aggregated data from the [Data Fetcher](./packages/data-fetcher) via HTTP and also directly from the blockchain using [ZuluPrime JSON-RPC API](https://era.zulunetwork.io/docs/api/api.html), processes it and saves into the database. [API](./packages/api) service is connected to the same database where it gets the data from to handle API requests. It performs only read requests to the database. The front-end [App](./packages/app) makes HTTP calls to the Block Explorer [API](./packages/api) to get blockchain data and to the [ZuluPrime JSON-RPC API](https://era.zksync.io/docs/api/api.html) for reading contracts, performing transactions etc.

## 🚀 Features

Expand All @@ -48,31 +48,19 @@ flowchart
- ✅ Standalone HTTP API.
- ✅ Local node support.

## 📋 Prerequisites

## 🛠 How to RUn
### 📋 Prerequisites

- Ensure you have `node >= 18.0.0` and `npm >= 9.0.0` installed.

## 🛠 Installation
#### 🛠 Installation

```bash
npm install
```

## ⚙️ Setting up env variables

### Manually set up env variables
Make sure you have set up all the necessary env variables. Follow setting up env variables instructions for [Worker](./packages/worker#setting-up-env-variables), [Data Fetcher](./packages/data-fetcher#setting-up-env-variables) and [API](./packages/api#setting-up-env-variables). For the [App](./packages/app) package you might want to edit environment config, see [Environment configs](./packages/app#environment-configs).

### Build env variables based on your [zksync-era](https://github.com/matter-labs/zksync-era) local repo setup
Make sure you have [zksync-era](https://github.com/matter-labs/zksync-era) repo set up locally. You must have your environment variables files present in the [zksync-era](https://github.com/matter-labs/zksync-era) repo at `/etc/env/*.env` for the build envs script to work.

The following script sets `.env` files for [Worker](./packages/worker), [Data Fetcher](./packages/data-fetcher) and [API](./packages/api) packages as well as environment configuration file for [App](./packages/app) package based on your local [zksync-era](https://github.com/matter-labs/zksync-era) repo setup.
```bash
npm run hyperchain:configure
```
You can review and edit generated files if you need to change any settings.

## 👨‍💻 Running locally
### 👨‍💻 Running locally

Before running the solution, make sure you have a database server up and running, you have created a database and set up all the required environment variables.
To create a database run the following command:
Expand All @@ -93,21 +81,13 @@ npm run start

Each component can also be started individually. Follow individual packages `README` for details.

## 🐳 Running in Docker
There is a docker compose configuration that allows you to run Block Explorer and all its dependencies in docker. Just run the following command to spin up the whole environment:
```
docker compose up
```
It will run local Ethereum node, ZkSync Era, Postgres DB and all Block Explorer services.

## ⛓️ Connection to your Hyperchain
To get block-explorer connected to your ZK Stack Hyperchain you need to set up all the the necessary environment and configuration files with your Hyperchain settings. You can use a script to build them. See [Setting up env variables](#%EF%B8%8F-setting-up-env-variables).
### 🐳 Running in Docker

## 🔍 Verify Block Explorer is up and running

To verify front-end `App` is running open http://localhost:3010 in your browser. `API` should be available at http://localhost:3020, `Worker` at http://localhost:3001 and `Data Fetcher` at http://localhost:3040.
```shell
make start_local
```

## 🕵️‍♂️ Testing
### 🕵️‍♂️ Testing
Run unit tests for all packages:
```bash
npm run test
Expand All @@ -122,21 +102,6 @@ npm run test -w {package}
```
For more details on testing please check individual packages `README`.

## 💻 Conventional Commits
We follow the Conventional Commits [specification](https://www.conventionalcommits.org/en/v1.0.0/#specification).

## 📘 License
zkSync Era Block Explorer is distributed under the terms of either

- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)

at your option.

## 🔗 Production links
- Testnet Goerli API: https://block-explorer-api.testnets.zksync.dev
- Testnet Sepolia API: https://block-explorer-api.sepolia.zksync.dev
- Mainnet API: https://block-explorer-api.mainnet.zksync.io
- Testnet Goerli App: https://goerli.explorer.zksync.io
- Testnet Sepolia App: https://sepolia.explorer.zksync.io
- Mainnet App: https://explorer.zksync.io
## Acknowledge
ZuluPrime Block Explorer leverages the power of [`zksync-block-explorer`](https://github.com/matter-labs/block-explorer) from [Matter Labs](https://github.com/matter-labs).
124 changes: 124 additions & 0 deletions docker-compose-demo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# run with sequencer and db
services:
app:
build:
context: .
dockerfile: ./packages/app/Dockerfile
ports:
- '3010:3010'
depends_on:
- api
restart: unless-stopped

worker:
build:
context: .
dockerfile: ./packages/worker/Dockerfile
environment:
- PORT=3001
- LOG_LEVEL=verbose
- DATABASE_HOST=postgres
- DATABASE_USER=postgres
- DATABASE_PASSWORD=postgres
- DATABASE_NAME=block-explorer
- BLOCKCHAIN_RPC_URL=http://zksync:3050
- DATA_FETCHER_URL=http://data-fetcher:3040
- BATCHES_PROCESSING_POLLING_INTERVAL=1000
ports:
- '3001:3001'
depends_on:
zksync:
condition: service_healthy
restart: unless-stopped

data-fetcher:
build:
context: .
dockerfile: ./packages/data-fetcher/Dockerfile
environment:
- PORT=3040
- LOG_LEVEL=verbose
- BLOCKCHAIN_RPC_URL=http://zksync:3050
ports:
- '3040:3040'
depends_on:
zksync:
condition: service_healthy
restart: unless-stopped

api:
build:
context: .
dockerfile: ./packages/api/Dockerfile
environment:
- PORT=3020
- METRICS_PORT=3005
- LOG_LEVEL=verbose
- DATABASE_URL=postgres://postgres:postgres@postgres:5432/block-explorer
ports:
- '3020:3020'
- '3005:3005'
depends_on:
- worker
restart: unless-stopped

postgres:
image: "postgres:14"
logging:
driver: none
volumes:
- postgres:/var/lib/postgresql/data
ports:
- "5432:5432"
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
interval: 5s
timeout: 5s
retries: 5
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=block-explorer

geth:
image: "matterlabs/geth:latest"
logging:
driver: none
ports:
- "8545:8545"
- "8546:8546"
volumes:
- geth:/var/lib/geth/data

zksync:
stdin_open: true
tty: true
image: matterlabs/local-node:latest2.0
depends_on:
postgres:
condition: service_healthy
geth:
condition: service_started
ports:
- "3050:3050" # JSON RPC HTTP port
- "3051:3051" # JSON RPC WS port
volumes:
# Configs folder bind
- zksync-config:/etc/env/
# Storage folder bind
- zksync-data:/var/lib/zksync/data
environment:
- DATABASE_URL=postgres://postgres:postgres@postgres:5432/zksync_local
- ETH_CLIENT_WEB3_URL=http://geth:8545
healthcheck:
test: "curl -H \"Content-Type: application/json\" -X POST --data '{\"jsonrpc\":\"2.0\",\"method\":\"web3_clientVersion\",\"params\":[],\"id\":67}' 127.0.0.1:3050 || exit 1"
interval: 5s
timeout: 5s
retries: 300
restart: unless-stopped

volumes:
geth:
postgres:
zksync-config:
zksync-data:
37 changes: 37 additions & 0 deletions docker-compose-local-alpha.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Run the explorer with db
services:
alpha-api:
container_name: "block-explorer-alpha-api"
image: zulunetwork/zulu-prime-block-explorer-api:latest
environment:
- PORT=3022
- METRICS_PORT=3007
- LOG_LEVEL=verbose
- DATABASE_URL=postgres://postgres:postgres@localhost:5434/block-explorer-alpha
ports:
- '3022:3022'
- '3007:3007'
restart: unless-stopped
alpha-postgres:
container_name: "block-explorer-alpha-api-postgres"
image: "postgres:16"
logging:
driver: none
volumes:
- ~/volumes/postgres-16/data:/var/lib/postgresql/data
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
interval: 5s
timeout: 5s
retries: 5
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=block-explorer-alpha
expose:
- "5434" # Publishes 5434 to other containers but NOT to host machine
ports:
- "5434:5434"
command: -p 5434


Loading

0 comments on commit b7ed739

Please sign in to comment.