# How to debug rom with an external db
In some cases we may want to run an input at executorjs using an external database (withouth adding th db at the input). This is very useful to debug the behaviour of the rom with a batchL2data obtained from internal or public testnet/mainnet.
### Steps
### 1- Get the input:
Donwload `hermez-local`repository: https://github.com/hermeznetwork/hermez-local
We want to run docker-compose in folder `mango-sync`:
Configurations:
- From file `config.json` (executor config)
Set `saveResponseToFile`, `saveOutputToFile`, `saveInputToFile`to true. This will make the executor to create a file for each batch.
- At config.toml (synchronizer configuration)
> [Etherman]
URL = "" -> set endpoint to a geth node/gateway
L1ChainID = 5 -> 5 for goerli
PoEAddr = "0x73199676bf39F83b2BCD0365c8Af36a1e6cB033f" -> ZKEVM deployment address
MaticAddr = "0x490ADEc2A7b246327c76e31192884aCF0E166772" -> ERC20 matic address
GlobalExitRootManagerAddr = "0xd675b7722721C689Eb5b461E1e95619cE85fD9e8"
>SyncInterval = "2s"
SyncChunkSize = 100 -> chunks of blocks to analyze
TrustedSequencerURI = ""
GenBlockNumber = 8169034 -> deployment block number
**!!! Remember to set the images with the matching version for the environment to debug at `docker-compose.yml`**
- Run docker compose
```
cd mango-sync
docker-compose up -d
```
It will start to synch L2 and populate a psql database. Once the batch we want to analyze is consolidated, we can retrieve the input from executor container
- Get the input from executor
```
docker exec -it zkevm-executor bash
cd output
ls
```
choose the file to retrieve, the inputs look like this:
XXXXXXXXXXXXXXX...XXXXXX.YYYY.process_batch_input.json
where YYYY is the num batch
To copy a file from a docker container to local:
```
docker cp zkevm-executor:/app/output/20230207_145214_275993_a7ac0fbc-03c3-43cb-b779-e6eab7524708.1108.process_batch_input.json .
```
Now we can use the following file as input for executorjs
### 2- Connect to synched database
To run the input above, we need to run the executorjs connected to the psql database. The params are the following:
```
"-B",
"postgresql://state_user:state_password@127.0.0.1:15432/state_db",
"-n",
"state.nodes",
"-G",
"state.program"
```
Where,
-B: database uri with this format: `postgresql://USERNAME:PASSWORD@IP:PORT/DATABASE`
-n: nodes table name
-G: program/bytecode table name
### 3- Run the input
Finally just run executor js with the obtained input and the correct params to connect to the database, example:
```
"program": "${workspaceFolder}/src/main_executor.js",
"args": [
"${workspaceFolder}/../zkevm-testvectors/inputs-executor/aa/1106-main.json",
"-r ${workspaceFolder}/../zkevm-rom/build/rom.json",
"-d",
"-T",
"-t",
"-P",
"/Users/ignasi/Documents/GitHub/hermez/prover/zkevm-proverjs/testvectors/pilconfig.json",
"-N",
"8388608",
"-B",
"postgresql://state_user:state_password@127.0.0.1:15432/state_db",
"-n",
"state.nodes",
"-G",
"state.program"
]
```
Use verbose and full-tracer to debug the scenario.
If you already have the input and want to debug without synch, you can dump the database in local and test.
Instructions to connect to the database: https://docs.google.com/document/d/11Gzngn5wiVxjIjKNQoiyUC34GlStaWfs5K9Y_wqZ8yM/edit#
After dumping, you will have to connect `executorjs` to your local psql (not the dockerized one).
## Alternative: dumping database locally
### How to dump database
Example: dumping internal testnet
First you need access to ssh providing you rsa key to the sysadmin.
ssh connect + port forwarding:
```
BASTION_HOST=54.73.186.145
PROVERSTATEDB_LOCALPORT=5436
STATEDB_EP=zkevminternalstatedb.cluster-cirmnf3qsupn.eu-west-1.rds.amazonaws.com
ssh ubuntu@$BASTION_HOST -L $PROVERSTATEDB_LOCALPORT:$STATEDB_EP:5432
```
Variables obtained from the gdoc above.
#### Dump the database from server (while ssh connected):
```
pg_dump postgresql://zkevminternalstatedb:vP6mOTKrH7@localhost:5436/zkevminternalstatedb > dump.sql
```
#### Restore database to local:
1- Disconnect from ssh
2- Start psql service locally (if not already running)
3- Restore db:
```
psql template1 -c 'drop database zkevminternalstatedb;'
psql template1 -c 'create database zkevminternalstatedb with owner $user;'
psql zkevminternalstatedb < dump.sql
```
info: template1 is a default database created by psql at installing time. We use this default database to remove and create the new dumped database