Skip to content

Commit 39733ac

Browse files
glpecileuri-99entropidelicMauroToscanoJuArce
authored
feat(explorer): add ecto postgres db (#265)
Co-authored-by: Urix <[email protected]> Co-authored-by: Mariano Nicolini <[email protected]> Co-authored-by: Mauro Toscano <[email protected]> Co-authored-by: Julian Arce <[email protected]>
1 parent ef6d5b2 commit 39733ac

37 files changed

+711
-180
lines changed

Makefile

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,6 @@ build_binaries:
497497
@go build -o ./task_sender/build/aligned-task-sender ./task_sender/cmd/main.go
498498
@echo "Task sender built into /task_sender/build/aligned-task-sender"
499499

500-
run_local:
501-
./scripts/run_local.sh
502-
503500
__SP1_FFI__: ##
504501
build_sp1_macos:
505502
@cd operator/sp1/lib && cargo build --release
@@ -671,13 +668,51 @@ build_all_ffi_linux: ## Build all FFIs for Linux
671668

672669

673670
__EXPLORER__:
674-
run_devnet_explorer:
671+
run_devnet_explorer: run_db ecto_setup_db
675672
@cd explorer/ && \
676673
mix setup && \
677674
cp .env.dev .env && \
678675
./start.sh
679676

680-
run_explorer:
677+
run_explorer: run_db ecto_setup_db
681678
@cd explorer/ && \
682679
mix setup && \
683680
./start.sh
681+
682+
build_db:
683+
@cd explorer && \
684+
docker build -t explorer-postgres-image .
685+
686+
run_db: remove_db_container
687+
@cd explorer && \
688+
docker run -d --name explorer-postgres-container -p 5432:5432 -v explorer-postgres-data:/var/lib/postgresql/data explorer-postgres-image
689+
690+
ecto_setup_db:
691+
@cd explorer/ && \
692+
./ecto_setup_db.sh
693+
694+
remove_db_container:
695+
@cd explorer && \
696+
docker stop explorer-postgres-container || true && \
697+
docker rm explorer-postgres-container || true
698+
699+
clean_db: remove_db_container
700+
@cd explorer && \
701+
docker volume rm explorer-postgres-data || true
702+
703+
dump_db:
704+
@cd explorer && \
705+
docker exec -t explorer-postgres-container pg_dumpall -c -U explorer_user > dump.$$(date +\%Y\%m\%d_\%H\%M\%S).sql
706+
@echo "Dumped database successfully to /explorer"
707+
708+
recover_db: run_db
709+
@read -p $$'\e[32mEnter the dump file to recover (e.g., dump.20230607_123456.sql): \e[0m' DUMP_FILE && \
710+
cd explorer && \
711+
docker cp $$DUMP_FILE explorer-postgres-container:/dump.sql && \
712+
docker exec -t explorer-postgres-container psql -U explorer_user -d explorer_db -f /dump.sql && \
713+
echo "Recovered database successfully from $$DUMP_FILE"
714+
715+
explorer_fetch_old_batches:
716+
@cd explorer && \
717+
./scripts/fetch_old_batches.sh 1600000 1716277
718+

README.md

Lines changed: 102 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ aligned \
9090
--proof_generator_addr [proof_generator_addr]
9191
```
9292

93-
**Examples**
93+
**Examples**:
9494

9595
```bash
9696
aligned \
@@ -866,12 +866,97 @@ To install Grafana, you can follow the instructions on the [official website](ht
866866

867867
- [Erlang 26](https://github.com/asdf-vm/asdf-erlang)
868868
- [Elixir 1.16.2](https://elixir-ko.github.io/install.html), compiled with OTP 26
869-
- [Phoenix 1.7.12](https://hexdocs.pm/phoenix/installation.html)
870-
- [Ecto 3.11.2](https://hexdocs.pm/ecto/getting-started.html)
869+
- [Docker](https://docs.docker.com/get-docker/)
870+
871+
### DB Setup
872+
873+
To setup the explorer, an installation of the DB is needed.
874+
875+
First you'll need to install docker if you don't have it already. You can follow the instructions [here](https://docs.docker.com/get-docker/).
876+
877+
The explorer uses a PostgreSQL database. To build and start the DB using docker, just run:
878+
879+
```bash
880+
make build_db
881+
```
882+
883+
This will build the docker image to be used as our database.
884+
885+
After this, both `make run_explorer` and `make run_devnet_explorer` (see [this](#running-for-local-devnet) for more details) will automatically start, setup and connect to the database, which will be available on `localhost:5432` and the data is persisted in a volume.
886+
887+
<details>
888+
889+
<summary>
890+
(Optional) The steps to manually execute the database are as follows...
891+
</summary>
892+
893+
- Run the database container, opening port `5432`:
894+
895+
```bash
896+
make run_db
897+
```
898+
899+
- Configure the database with ecto running `ecto.create` and `ecto.migrate`:
900+
901+
```bash
902+
make ecto_setup_db
903+
```
904+
905+
- Start the explorer:
906+
907+
```bash
908+
make run_explorer # or make run_devnet_explorer
909+
```
910+
911+
</details>
912+
913+
<br>
914+
915+
In order to clear the DB, you can run:
916+
917+
```bash
918+
make clean_db
919+
```
920+
921+
If you need to dumb the data from the DB, you can run:
922+
923+
```bash
924+
make dump_db
925+
```
926+
927+
This will create a `dump.$date.sql` SQL script on the `explorer` directory with all the existing data.
928+
929+
Data can be recovered from a `dump.$date.sql` using the following command:
930+
931+
```bash
932+
make recover_db
933+
```
934+
935+
Then you'll be requested to enter the file name of the dump you want to recover already positioned in the `/explorer` directory.
936+
937+
This will update your database with the dumped database data.
938+
939+
### Extra scripts
940+
941+
If you want to fetch past batches that for any reason were not inserted into the DB, you will first need to make sure you have the ELIXIR_HOSTNAME .env variable configured. You can get the hostname of your elixir by running `elixir -e 'IO.puts(:inet.gethostname() |> elem(1))'`
942+
943+
Then you can run:
944+
945+
```bash
946+
make explorer_fetch_old_batches
947+
```
948+
949+
You can modify which blocks are being fetched by modify the parameters the `explorer_fetch_old_batches.sh` is being recieved
871950

872951
### Running for local devnet
873952

874-
```make run_devnet_explorer```
953+
To run the explorer for the local devnet, you'll need to have the devnet running (see [local devnet setup](#local-devnet-setup)) and the DB already setup.
954+
955+
To run the explorer, just run:
956+
957+
```bash
958+
make run_devnet_explorer
959+
```
875960

876961
Now you can visit [`localhost:4000`](http://localhost:4000) from your browser.
877962
You can access to a tasks information by visiting `localhost:4000/batches/:merkle_root`.
@@ -885,6 +970,13 @@ Create a `.env` file in the `/explorer` directory of the project. The `.env` fil
885970
| `RPC_URL` | The RPC URL of the network you want to connect to. |
886971
| `ENVIRONMENT` | The environment you want to run the application in. It can be `devnet`, `holesky` or `mainnet`. |
887972
| `PHX_HOST` | The host URL where the Phoenix server will be running. |
973+
| `DB_NAME` | The name of the postgres database. |
974+
| `DB_USER` | The username of the postgres database. |
975+
| `DB_PASS` | The password of the postgres database. |
976+
| `DB_HOST` | The host URL where the postgres database will be running. |
977+
| `ELIXIR_HOSTNAME` | The hostname of your running elixir. Read [Extra Scripts](#extra-scripts) section for more details |
978+
979+
Then you can run the explorer with this env file config by entering the following command:
888980

889981
```make run_explorer```
890982

@@ -935,11 +1027,14 @@ make test
9351027

9361028
### SP1
9371029

938-
#### Dependencies
1030+
#### SP1 Dependencies
1031+
9391032
This guide assumes that:
1033+
9401034
- sp1 prover installed (instructions [here](https://succinctlabs.github.io/sp1/getting-started/install.html))
9411035
- sp1 project to generate the proofs (instructions [here](https://succinctlabs.github.io/sp1/generating-proofs/setup.html))
9421036
- aligned layer repository cloned:
1037+
9431038
```bash
9441039
git clone https://github.com/yetanotherco/aligned_layer.git
9451040
```
@@ -953,13 +1048,15 @@ and check that the proof is generated with `client.prove_compressed` instead of
9531048
First, open a terminal and navigate to the script folder in the sp1 project directory
9541049

9551050
Then, run the following command to generate a proof:
1051+
9561052
```bash
9571053
cargo run --release
9581054
```
9591055

9601056
#### How to get the proof verified by AlignedLayer
9611057

9621058
After generating the proof, you will have to find two different files:
1059+
9631060
- proof file: usually found under `script` directory, with the name `proof.json` or similar
9641061
- elf file: usually found under `program/elf/` directory
9651062

explorer/.env.dev

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# Network
12
RPC_URL=http://localhost:8545
23
ENVIRONMENT=devnet
4+
5+
# Elixir
36
PHX_HOST=http://localhost:4000
7+
ELIXIR_HOSTNAME=MAC
8+
9+
# Database
10+
DB_NAME=explorer_db
11+
DB_USER=explorer_user
12+
DB_PASS=explorer_pass
13+
DB_HOST=localhost

explorer/.env.example

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# Network
12
RPC_URL=<rpc_url>
23
ENVIRONMENT=<devnet|holesky|mainnet>
4+
5+
# Elixir
36
PHX_HOST=<phx_host_url>
7+
ELIXIR_HOSTNAME=<iex_hostname>
8+
9+
# Database
10+
DB_NAME=<db_name>
11+
DB_USER=<db_user>
12+
DB_PASS=<db_pass>
13+
DB_HOST=<db_host>

explorer/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ explorer-*.tar
3535
npm-debug.log
3636
/assets/node_modules/
3737

38-
3938
# Environment Variables
4039
/.env
40+
41+
dump.*.sql

explorer/Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# https://hub.docker.com/_/postgres
2+
FROM postgres:16.3
3+
4+
# Environment variables
5+
ENV POSTGRES_USER=explorer_user
6+
ENV POSTGRES_PASSWORD=explorer_pass
7+
ENV POSTGRES_DB=explorer_db
8+
9+
# Expose the default PostgreSQL port
10+
EXPOSE 5432

explorer/assets/js/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ topbar.config({
4141
shadowColor: "rgba(0, 0, 0, .3)"
4242
});
4343
window.addEventListener("phx:page-loading-start", (_info) =>
44-
topbar.show(150)
44+
topbar.show(50)
4545
);
4646
window.addEventListener("phx:page-loading-stop", (_info) =>
4747
topbar.hide()

explorer/config/config.exs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,10 @@ config :explorer, ExplorerWeb.Endpoint,
2121
pubsub_server: Explorer.PubSub,
2222
live_view: [signing_salt: "XkOXIXZ0"]
2323

24-
# Configures the mailer
25-
#
26-
# By default it uses the "Local" adapter which stores the emails
27-
# locally. You can see the emails in your browser, at "/dev/mailbox".
28-
#
29-
# For production it's recommended to configure a different adapter
30-
# at the `config/runtime.exs`.
31-
config :explorer, Explorer.Mailer, adapter: Swoosh.Adapters.Local
24+
# Configures the database
25+
config :explorer,
26+
ecto_repos: [Explorer.Repo],
27+
env: Mix.env()
3228

3329
# Configure esbuild (the version is required)
3430
config :esbuild,

explorer/config/dev.exs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ config :explorer, ExplorerWeb.Endpoint,
1919
tailwind: {Tailwind, :install_and_run, [:explorer, ~w(--watch)]}
2020
]
2121

22+
2223
# ## SSL Support
2324
#
2425
# In order to use HTTPS in development, a self-signed
@@ -52,8 +53,8 @@ config :explorer, ExplorerWeb.Endpoint,
5253
]
5354
]
5455

55-
# Enable dev routes for dashboard and mailbox
56-
config :explorer, dev_routes: true
56+
# Enable dev routes (for dashboard, etc). only enable behind auth
57+
# config :explorer, dev_routes: true
5758

5859
# Do not include metadata nor timestamps in development logs
5960
config :logger, :console, format: "[$level] $message\n"
@@ -70,6 +71,3 @@ config :phoenix_live_view,
7071
debug_heex_annotations: true,
7172
# Enable helpful, but potentially expensive runtime checks
7273
enable_expensive_runtime_checks: true
73-
74-
# Disable swoosh api client as it is only required for production adapters.
75-
config :swoosh, :api_client, false

explorer/config/prod.exs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ import Config
77
# before starting your production server.
88
config :explorer, ExplorerWeb.Endpoint, cache_static_manifest: "priv/static/cache_manifest.json"
99

10-
# Configures Swoosh API Client
11-
config :swoosh, api_client: Swoosh.ApiClient.Finch, finch_name: Explorer.Finch
12-
13-
# Disable Swoosh Local Memory Storage
14-
config :swoosh, local: false
15-
1610
# Do not print debug messages in production
1711
config :logger, level: :info
1812

0 commit comments

Comments
 (0)