# fixturenet-optimism (L2-only)

Instructions to setup and deploy L2 fixturenet using [Optimism](https://stack.optimism.io)

## Setup

Prerequisite: An L1 Ethereum RPC endpoint

Clone required repositories:

```bash
laconic-so --stack fixturenet-optimism setup-repositories --exclude git.vdb.to/cerc-io/go-ethereum

# If this throws an error as a result of being already checked out to a branch/tag in a repo, remove the repositories mentioned below and re-run the command
```

Build the container images:

```bash
laconic-so --stack fixturenet-optimism build-containers --include cerc/foundry,cerc/optimism-contracts,cerc/optimism-op-node,cerc/optimism-l2geth,cerc/optimism-op-batcher,cerc/optimism-op-proposer
```

This should create the required docker images in the local image registry:
* `cerc/foundry`
* `cerc/optimism-contracts`
* `cerc/optimism-l2geth`
* `cerc/optimism-op-node`
* `cerc/optimism-op-batcher`
* `cerc/optimism-op-proposer`

## Create a deployment

First, create a spec file for the deployment, which will map the stack's ports and volumes to the host:
```bash
laconic-so --stack fixturenet-optimism deploy init --map-ports-to-host any-fixed-random --output fixturenet-optimism-spec.yml
```
### Ports
It is usually necessary to expose certain container ports on one or more the host's addresses to allow incoming connections.
Any ports defined in the Docker compose file are exposed by default with random port assignments, bound to "any" interface (IP address 0.0.0.0), but the port mappings can be customized by editing the "spec" file generated by `laconic-so deploy init`.

In addition, a stack-wide port mapping "recipe" can be applied at the time the
`laconic-so deploy init` command is run, by supplying the  desired recipe with the `--map-ports-to-host` option. The following recipes are supported:
| Recipe | Host Port Mapping |
|--------|-------------------|
| any-variable-random | Bind to 0.0.0.0 using a random port assigned at start time (default) |
| localhost-same | Bind to 127.0.0.1 using the same port number as exposed by the containers |
| any-same | Bind to 0.0.0.0 using the same port number as exposed by the containers |
| localhost-fixed-random | Bind to 127.0.0.1 using a random port number selected at the time the command is run (not checked for already in use)|
| any-fixed-random | Bind to 0.0.0.0 using a random port number selected at the time the command is run (not checked for already in use) |

For example, you may wish to use `any-fixed-random` to generate the initial mappings and then edit the spec file to set the `op-geth` RPC to an easy to remember port like 8545 or 9545 on the host.

### Data volumes
Container data volumes are bind-mounted to specified paths in the host filesystem.
The default setup (generated by `laconic-so deploy init`) places the volumes in the `./data` subdirectory of the deployment directory. The default mappings can be customized by editing the "spec" file generated by `laconic-so deploy init`.

---
Once you've made any needed changes to the spec file, create a deployment from it:
```bash
laconic-so --stack fixturenet-optimism deploy create --spec-file fixturenet-optimism-spec.yml --deployment-dir fixturenet-optimism-deployment
```

Finally, open the `stack.yml` file inside your deployment directory and, under the `pods:` section, remove (or comment out) the entry for `fixturenet-eth`. This will prevent the deployment from trying to spin up a new L1 chain when starting the stack.

## Set chain env variables

Inside the deployment directory, open the file `config.env` and add the following variables to point the stack at your L1 rpc and provide account credentials ([defaults](../../config/fixturenet-optimism/l1-params.env)):

  ```bash
  # External L1 endpoint
  CERC_L1_CHAIN_ID=
  CERC_L1_RPC=
  CERC_L1_HOST=
  CERC_L1_PORT=

  # URL to get CSV with credentials for accounts on L1
  # that are used to send balance to Optimism Proxy contract
  # (enables them to do transactions on L2)
  CERC_L1_ACCOUNTS_CSV_URL=

  # OR
  # Specify the required account credentials for the Admin account
  # Other generated accounts will be funded from this account, so it should contain ~20 Eth
  CERC_L1_ADDRESS=
  CERC_L1_PRIV_KEY=
  ```

* NOTE: If L1 is running on the host machine, use `host.docker.internal` as the hostname to access the host port, or use the `ip a` command to find the IP address of the `docker0` interface (this will usually be something like `172.17.0.1` or `172.18.0.1`)

## Start the stack
Start the deployment:
```bash
laconic-so deployment --dir fixturenet-optimism-deployment start
```
1. The stack will check for a response from the L1 endpoint specified in your env file.
2. The `fixturenet-optimism-contracts` service will configure and deploy the Optimism contracts to L1, exiting when complete. This may take several minutes; you can follow the progress by following the container's logs (see below).
3. The `op-node` and `op-geth` services will initialize themselves (if not already initialized) and start
4. The remaining services, `op-batcher` and `op-proposer` will start

### Logs
To list and monitor the running containers:

```bash
laconic-so --stack fixturenet-optimism deploy ps

# With status
docker ps

# Check logs for a container
docker logs -f <CONTAINER_ID>
```

## Clean up

To stop all L2 services running in the background, while preserving chain data:

```bash
laconic-so deployment --dir fixturenet-optimism-deployment stop
```

To stop all L2 services and also delete chain data:

```bash
laconic-so deployment --dir fixturenet-optimism-deployment stop --delete-volumes
```

## Troubleshooting

See [Troubleshooting](./README.md#troubleshooting)
