From 94e38ceabaa89a3ec54e25f4a03cf253d5fae5f9 Mon Sep 17 00:00:00 2001 From: Nabarun Gogoi Date: Wed, 5 Apr 2023 17:26:38 +0530 Subject: [PATCH] Add ability to run mobymask-v2 stack with external optimism endpoint (#279) * Set optimism geth endpoint from env file * Set L1 account private keys from env * Only deploy contract and generate invite in mobymask container * Add readme for running mobymask v2 stack independently * Modify mobymask container to stop running server and update readmes * Check deployer account balance before deploying contract * Fix for checking account balance before deploying * Update readme description * Update MobyMask repo tag in readme --- .../docker-compose-fixturenet-optimism.yml | 4 +- .../docker-compose-mobymask-laconicd.yml | 25 ------ .../docker-compose-watcher-mobymask-v2.yml | 39 ++++----- .../config/fixturenet-optimism/l1-params.env | 7 +- .../deploy-and-generate-invite.sh | 38 +++++++-- .../watcher-mobymask-v2/mobymask-app-start.sh | 3 + .../watcher-mobymask-v2/optimism-params.env | 12 +++ .../watcher-mobymask-v2/secrets-template.json | 2 +- .../watcher-mobymask-v2/server-start.sh | 18 +++- .../watcher-config-template.toml | 2 +- .../container-build/cerc-mobymask/Dockerfile | 2 +- app/data/pod-list.txt | 3 +- app/data/stacks/fixturenet-optimism/README.md | 6 +- .../{L2-ONLY.md => l2-only.md} | 6 +- app/data/stacks/mobymask-v2/README.md | 23 ++--- app/data/stacks/mobymask-v2/mobymask-only.md | 85 +++++++++++++++++++ 16 files changed, 194 insertions(+), 81 deletions(-) delete mode 100644 app/data/compose/docker-compose-mobymask-laconicd.yml mode change 100644 => 100755 app/data/config/watcher-mobymask-v2/deploy-and-generate-invite.sh create mode 100644 app/data/config/watcher-mobymask-v2/optimism-params.env rename app/data/stacks/fixturenet-optimism/{L2-ONLY.md => l2-only.md} (86%) create mode 100644 app/data/stacks/mobymask-v2/mobymask-only.md diff --git a/app/data/compose/docker-compose-fixturenet-optimism.yml b/app/data/compose/docker-compose-fixturenet-optimism.yml index d2e96d3f..142a2251 100644 --- a/app/data/compose/docker-compose-fixturenet-optimism.yml +++ b/app/data/compose/docker-compose-fixturenet-optimism.yml @@ -55,7 +55,7 @@ services: entrypoint: "sh" command: "/run-op-geth.sh" ports: - - "8545" + - "0.0.0.0:8545:8545" healthcheck: test: ["CMD", "nc", "-vz", "localhost:8545"] interval: 30s @@ -77,7 +77,7 @@ services: - l2_accounts:/l2-accounts:ro command: ["sh", "/app/run-op-node.sh"] ports: - - "8547" + - "0.0.0.0:8547:8547" healthcheck: test: ["CMD", "nc", "-vz", "localhost:8547"] interval: 30s diff --git a/app/data/compose/docker-compose-mobymask-laconicd.yml b/app/data/compose/docker-compose-mobymask-laconicd.yml deleted file mode 100644 index 4847aeb3..00000000 --- a/app/data/compose/docker-compose-mobymask-laconicd.yml +++ /dev/null @@ -1,25 +0,0 @@ -version: "3.2" - -services: - laconicd: - restart: unless-stopped - image: cerc/laconicd:local - command: ["sh", "/docker-entrypoint-scripts.d/create-fixturenet.sh"] - volumes: - - ../config/fixturenet-laconicd/create-fixturenet.sh:/docker-entrypoint-scripts.d/create-fixturenet.sh - ports: - - "9473" - - "8545" - - "8546" - - "1317" - healthcheck: - test: ["CMD", "nc", "-v", "localhost", "8545"] - interval: 20s - timeout: 5s - retries: 15 - start_period: 10s - -networks: - # https://docs.docker.com/compose/networking/#configure-the-default-network - default: - name: mobymask-v2-network diff --git a/app/data/compose/docker-compose-watcher-mobymask-v2.yml b/app/data/compose/docker-compose-watcher-mobymask-v2.yml index 8478a4d9..667191a1 100644 --- a/app/data/compose/docker-compose-watcher-mobymask-v2.yml +++ b/app/data/compose/docker-compose-watcher-mobymask-v2.yml @@ -22,45 +22,40 @@ services: start_period: 10s mobymask: - restart: unless-stopped image: cerc/mobymask:local working_dir: /app/packages/server - depends_on: - op-node: - condition: service_healthy - op-geth: - condition: service_healthy - # TODO: Configure env file for ETH RPC URL & private key + env_file: + - ../config/watcher-mobymask-v2/optimism-params.env environment: - ENV=PROD - command: ["sh", "./deploy-and-generate-invite.sh"] + command: + - sh + - -c + - | + ./wait-for-it.sh -h $${L2_GETH_HOST} -p $${L2_GETH_PORT} -s -t 0 && \ + ./wait-for-it.sh -h $${L2_NODE_HOST} -p $${L2_NODE_PORT} -s -t 0 && \ + ./deploy-and-generate-invite.sh volumes: - # TODO: add a script to set RPC endpoint from env - # add manually if running seperately + - ../config/wait-for-it.sh:/app/packages/server/wait-for-it.sh - ../config/watcher-mobymask-v2/secrets-template.json:/app/packages/server/secrets-template.json - ../config/watcher-mobymask-v2/deploy-and-generate-invite.sh:/app/packages/server/deploy-and-generate-invite.sh - moby_data_server:/app/packages/server - fixturenet_geth_accounts:/geth-accounts:ro - healthcheck: - test: ["CMD", "nc", "-v", "localhost", "3330"] - interval: 20s - timeout: 5s - retries: 15 - start_period: 10s + extra_hosts: + - "host.docker.internal:host-gateway" mobymask-watcher-server: - # TODO: pass optimism RPC endpoint restart: unless-stopped depends_on: mobymask-watcher-db: condition: service_healthy mobymask: - condition: service_healthy + condition: service_completed_successfully image: cerc/watcher-mobymask-v2:local + env_file: + - ../config/watcher-mobymask-v2/optimism-params.env command: ["sh", "server-start.sh"] volumes: - # TODO: add a script to set RPC endpoint from env - # add manually if running seperately - ../config/watcher-mobymask-v2/watcher-config-template.toml:/app/packages/mobymask-v2-watcher/environments/watcher-config-template.toml - ../config/watcher-mobymask-v2/peer.env:/app/packages/peer/.env - ../config/watcher-mobymask-v2/relay-id.json:/app/packages/mobymask-v2-watcher/relay-id.json @@ -78,6 +73,8 @@ services: timeout: 5s retries: 15 start_period: 5s + extra_hosts: + - "host.docker.internal:host-gateway" # TODO: Move to a separate pod mobymask-app: @@ -85,7 +82,7 @@ services: mobymask-watcher-server: condition: service_healthy mobymask: - condition: service_healthy + condition: service_completed_successfully image: cerc/mobymask-ui:local command: ["sh", "mobymask-app-start.sh"] volumes: diff --git a/app/data/config/fixturenet-optimism/l1-params.env b/app/data/config/fixturenet-optimism/l1-params.env index 31876523..93c58805 100644 --- a/app/data/config/fixturenet-optimism/l1-params.env +++ b/app/data/config/fixturenet-optimism/l1-params.env @@ -1,8 +1,13 @@ # Change if pointing to an external L1 endpoint -L1_RPC="http://fixturenet-eth-geth-1:8545" + +# L1 endpoint L1_CHAIN_ID=1212 +L1_RPC="http://fixturenet-eth-geth-1:8545" L1_HOST="fixturenet-eth-geth-1" L1_PORT=8545 + +# Credentials for accounts on L1 to send balance to Optimism Proxy contract from +# (enables them to do transactions on L2) L1_ADDRESS= L1_PRIV_KEY= L1_ADDRESS_2= diff --git a/app/data/config/watcher-mobymask-v2/deploy-and-generate-invite.sh b/app/data/config/watcher-mobymask-v2/deploy-and-generate-invite.sh old mode 100644 new mode 100755 index 2a8e0eb5..b53b6b56 --- a/app/data/config/watcher-mobymask-v2/deploy-and-generate-invite.sh +++ b/app/data/config/watcher-mobymask-v2/deploy-and-generate-invite.sh @@ -1,11 +1,39 @@ #!/bin/sh set -e +if [ -n "$CERC_SCRIPT_DEBUG" ]; then + set -x +fi -# Read the private key of L1 account to deploy contract -# TODO: Take from env if /geth-accounts volume doesn't exist to allow using separately running L1 -L1_PRIV_KEY=$(head -n 1 /geth-accounts/accounts.csv | cut -d ',' -f 3) + +if [ -f /geth-accounts/accounts.csv ]; then + echo "Using L1 private key from the mounted volume" + # Read the private key of L1 account to deploy contract + PRIVATE_KEY_DEPLOYER=$(head -n 1 /geth-accounts/accounts.csv | cut -d ',' -f 3) +else + echo "Using PRIVATE_KEY_DEPLOYER from env" +fi # Set the private key -jq --arg privateKey "$L1_PRIV_KEY" '.privateKey = ($privateKey)' secrets-template.json > secrets.json +jq --arg privateKey "$PRIVATE_KEY_DEPLOYER" '.privateKey = $privateKey' secrets-template.json > secrets.json -npm start +export L2_GETH_URL="http://${L2_GETH_HOST}:${L2_GETH_PORT}" +jq --arg rpcUrl "$L2_GETH_URL" '.rpcUrl = $rpcUrl' secrets.json > secrets_updated.json && mv secrets_updated.json secrets.json + +cd ../hardhat +export RPC_URL="${L2_GETH_URL}" + +while true; do + ACCOUNT_BALANCE=$(yarn hardhat --network optimism balance $PRIVATE_KEY_DEPLOYER | grep ETH) + + if [ "$ACCOUNT_BALANCE" != "0.0 ETH" ]; then + echo "Account balance updated: $ACCOUNT_BALANCE" + break # exit the loop + fi + + echo "Account balance not updated: $ACCOUNT_BALANCE" + echo "Checking after 2 seconds" + sleep 2 +done + +cd ../server +npm run deployAndGenerateInvite diff --git a/app/data/config/watcher-mobymask-v2/mobymask-app-start.sh b/app/data/config/watcher-mobymask-v2/mobymask-app-start.sh index fa3870cf..efc4ed96 100755 --- a/app/data/config/watcher-mobymask-v2/mobymask-app-start.sh +++ b/app/data/config/watcher-mobymask-v2/mobymask-app-start.sh @@ -1,5 +1,8 @@ #!/bin/sh set -e +if [ -n "$CERC_SCRIPT_DEBUG" ]; then + set -x +fi # Merging config files to get deployed contract address jq -s '.[0] * .[1]' /app/src/mobymask-app-config.json /server/config.json > /app/src/config.json diff --git a/app/data/config/watcher-mobymask-v2/optimism-params.env b/app/data/config/watcher-mobymask-v2/optimism-params.env new file mode 100644 index 00000000..bafb37a8 --- /dev/null +++ b/app/data/config/watcher-mobymask-v2/optimism-params.env @@ -0,0 +1,12 @@ +# Change if pointing to an external optimism geth endpoint + +# L2 endpoints +# TODO: Add another env for complete URL to handle https +L2_GETH_HOST="op-geth" +L2_GETH_PORT=8545 +L2_NODE_HOST="op-node" +L2_NODE_PORT=8547 + +# Credentials for accounts to perform txs on L2 +PRIVATE_KEY_DEPLOYER= +PRIVATE_KEY_PEER= diff --git a/app/data/config/watcher-mobymask-v2/secrets-template.json b/app/data/config/watcher-mobymask-v2/secrets-template.json index 220efc8e..1397bbf1 100644 --- a/app/data/config/watcher-mobymask-v2/secrets-template.json +++ b/app/data/config/watcher-mobymask-v2/secrets-template.json @@ -1,5 +1,5 @@ { - "rpcUrl": "http://op-geth:8545", + "rpcUrl": "", "privateKey": "", "baseURI": "http://127.0.0.1:3002/#" } diff --git a/app/data/config/watcher-mobymask-v2/server-start.sh b/app/data/config/watcher-mobymask-v2/server-start.sh index 6b4cd345..5fe9ab84 100755 --- a/app/data/config/watcher-mobymask-v2/server-start.sh +++ b/app/data/config/watcher-mobymask-v2/server-start.sh @@ -1,11 +1,25 @@ #!/bin/sh +set -e +if [ -n "$CERC_SCRIPT_DEBUG" ]; then + set -x +fi # Assign deployed contract address from server config CONTRACT_ADDRESS=$(jq -r '.address' /server/config.json | tr -d '"') -L1_PRIV_KEY_2=$(awk -F, 'NR==2{print $NF}' /geth-accounts/accounts.csv) -sed "s/REPLACE_WITH_PRIVATE_KEY/${L1_PRIV_KEY_2}/" environments/watcher-config-template.toml > environments/local.toml +if [ -f /geth-accounts/accounts.csv ]; then + echo "Using L1 private key from the mounted volume" + # Read the private key of L1 account for sending txs from peer + PRIVATE_KEY_PEER=$(awk -F, 'NR==2{print $NF}' /geth-accounts/accounts.csv) +else + echo "Using PRIVATE_KEY_PEER from env" +fi + +sed "s/REPLACE_WITH_PRIVATE_KEY/${PRIVATE_KEY_PEER}/" environments/watcher-config-template.toml > environments/local.toml sed -i "s/REPLACE_WITH_CONTRACT_ADDRESS/${CONTRACT_ADDRESS}/" environments/local.toml +export L2_GETH_URL="http://${L2_GETH_HOST}:${L2_GETH_PORT}" +sed -i 's|REPLACE_WITH_L2_GETH_URL|'"${L2_GETH_URL}"'|' environments/local.toml + echo 'yarn server' yarn server diff --git a/app/data/config/watcher-mobymask-v2/watcher-config-template.toml b/app/data/config/watcher-mobymask-v2/watcher-config-template.toml index ff937678..05554e3a 100644 --- a/app/data/config/watcher-mobymask-v2/watcher-config-template.toml +++ b/app/data/config/watcher-mobymask-v2/watcher-config-template.toml @@ -59,7 +59,7 @@ [upstream] [upstream.ethServer] gqlApiEndpoint = "http://ipld-eth-server:8083/graphql" - rpcProviderEndpoint = "http://op-geth:8545" + rpcProviderEndpoint = "REPLACE_WITH_L2_GETH_URL" blockDelayInMilliSecs = 60000 [upstream.cache] diff --git a/app/data/container-build/cerc-mobymask/Dockerfile b/app/data/container-build/cerc-mobymask/Dockerfile index c94a1cea..caa3e6b5 100644 --- a/app/data/container-build/cerc-mobymask/Dockerfile +++ b/app/data/container-build/cerc-mobymask/Dockerfile @@ -1,6 +1,6 @@ FROM node:16.17.1-alpine3.16 -RUN apk --update --no-cache add python3 alpine-sdk jq +RUN apk --update --no-cache add python3 alpine-sdk jq bash WORKDIR /app diff --git a/app/data/pod-list.txt b/app/data/pod-list.txt index 79c4171a..f24c9ed0 100644 --- a/app/data/pod-list.txt +++ b/app/data/pod-list.txt @@ -15,7 +15,8 @@ watcher-erc20 watcher-erc721 watcher-uniswap-v3 watcher-mobymask-v2 -mobymask-laconicd +mobymask-app +peer-test-app test eth-probe keycloak diff --git a/app/data/stacks/fixturenet-optimism/README.md b/app/data/stacks/fixturenet-optimism/README.md index 65839e70..61ca97b3 100644 --- a/app/data/stacks/fixturenet-optimism/README.md +++ b/app/data/stacks/fixturenet-optimism/README.md @@ -2,7 +2,7 @@ Instructions to setup and deploy an end-to-end L1+L2 stack with [fixturenet-eth](../fixturenet-eth/) (L1) and [Optimism](https://stack.optimism.io) (L2) -We support running just the L2 part of stack, given an external L1 endpoint. Follow [L2-ONLY](./L2-ONLY.md) for the same. +We support running just the L2 part of stack, given an external L1 endpoint. Follow [l2-only](./l2-only.md) for the same. ## Setup @@ -74,10 +74,10 @@ Clear volumes created by this stack: ```bash # List all relevant volumes -docker volume ls -q --filter name=laconic* +docker volume ls -q --filter "name=.*fixturenet_geth_accounts|.*l1_deployment|.*l2_accounts|.*l2_config|.*l2_geth_data" # Remove all the listed volumes -docker volume rm $(docker volume ls -q --filter name=laconic*) +docker volume rm $(docker volume ls -q --filter "name=.*fixturenet_geth_accounts|.*l1_deployment|.*l2_accounts|.*l2_config|.*l2_geth_data") ``` ## Troubleshooting diff --git a/app/data/stacks/fixturenet-optimism/L2-ONLY.md b/app/data/stacks/fixturenet-optimism/l2-only.md similarity index 86% rename from app/data/stacks/fixturenet-optimism/L2-ONLY.md rename to app/data/stacks/fixturenet-optimism/l2-only.md index 55eab385..a1c33717 100644 --- a/app/data/stacks/fixturenet-optimism/L2-ONLY.md +++ b/app/data/stacks/fixturenet-optimism/l2-only.md @@ -55,7 +55,7 @@ The `fixturenet-optimism-contracts` service may take a while (`~15 mins`) to com To list down and monitor the running containers: ```bash -laconic-so --stack fixturenet-optimism deploy ps +laconic-so --stack fixturenet-optimism deploy --include fixturenet-optimism ps # With status docker ps @@ -76,10 +76,10 @@ Clear volumes created by this stack: ```bash # List all relevant volumes -docker volume ls -q --filter name=laconic* +docker volume ls -q --filter "name=.*fixturenet_geth_accounts|.*l1_deployment|.*l2_accounts|.*l2_config|.*l2_geth_data" # Remove all the listed volumes -docker volume rm $(docker volume ls -q --filter name=laconic*) +docker volume rm $(docker volume ls -q --filter "name=.*fixturenet_geth_accounts|.*l1_deployment|.*l2_accounts|.*l2_config|.*l2_geth_data") ``` ## Troubleshooting diff --git a/app/data/stacks/mobymask-v2/README.md b/app/data/stacks/mobymask-v2/README.md index 68b9b61c..513bd431 100644 --- a/app/data/stacks/mobymask-v2/README.md +++ b/app/data/stacks/mobymask-v2/README.md @@ -1,6 +1,9 @@ # MobyMask v2 watcher -Instructions to deploy MobyMask v2 watcher stack using [laconic-stack-orchestrator](/README.md#install) +Instructions to setup and deploy an end-to-end MobyMask v2 stack ([L1](../fixturenet-eth/) + [L2](../fixturenet-optimism/) chains + watcher) using [laconic-stack-orchestrator](/README.md#install) + +We support running just the watcher part of stack, given an external L2 Optimism endpoint. +Follow [mobymask-only](./mobymask-only.md) for the same. ## Setup @@ -30,7 +33,7 @@ git checkout laconic # MobyMask cd ~/cerc/MobyMask -git checkout v0.1.1 +git checkout v0.1.2 # Optimism cd ~/cerc/optimism @@ -68,19 +71,9 @@ Deploy the stack: Find the watcher container's id and export it for later use: ```bash -laconic-so --stack mobymask-v2 deploy-system ps | grep "mobymask-watcher-server" - -export CONTAINER_ID= +export CONTAINER_ID=$(docker ps -q --filter "name=mobymask-watcher-server") ``` -Example output: - -``` -id: 5d3aae4b22039fcd1c9b18feeb91318ede1100581e75bb5ac54f9e436066b02c, name: laconic-bfb01caf98b1b8f7c8db4d33f11b905a-mobymask-watcher-server-1, ports: 0.0.0.0:3001->3001/tcp, 0.0.0.0:9001->9001/tcp, 0.0.0.0:9090->9090/tcp -``` - -In above output the container ID is `5d3aae4b22039fcd1c9b18feeb91318ede1100581e75bb5ac54f9e436066b02c` - Run the peer tests: ```bash @@ -128,11 +121,11 @@ Clear volumes: * List all relevant volumes: ```bash - docker volume ls -q --filter name=laconic* + docker volume ls -q --filter "name=.*mobymask_watcher_db_data|.*moby_data_server|.*fixturenet_geth_accounts|.*l1_deployment|.*l2_accounts|.*l2_config|.*l2_geth_data" ``` * Remove all the listed volumes: ```bash - docker volume rm $(docker volume ls -q --filter name=laconic*) + docker volume rm $(docker volume ls -q --filter "name=.*mobymask_watcher_db_data|.*moby_data_server|.*fixturenet_geth_accounts|.*l1_deployment|.*l2_accounts|.*l2_config|.*l2_geth_data") ``` diff --git a/app/data/stacks/mobymask-v2/mobymask-only.md b/app/data/stacks/mobymask-v2/mobymask-only.md new file mode 100644 index 00000000..843e7533 --- /dev/null +++ b/app/data/stacks/mobymask-v2/mobymask-only.md @@ -0,0 +1,85 @@ +# MobyMask v2 watcher + +Instructions to setup and deploy MobyMask v2 watcher independently + +## Setup + +Prerequisite: An L2 Optimism RPC endpoint + +Clone required repositories: + +```bash +laconic-so --stack mobymask-v2 setup-repositories --include cerc-io/MobyMask,cerc-io/watcher-ts,cerc-io/react-peer,cerc-io/mobymask-ui +``` + +Checkout to the required versions and branches in repos: + +```bash +```bash +# watcher-ts +cd ~/cerc/watcher-ts +git checkout v0.2.34 + +# react-peer +cd ~/cerc/react-peer +git checkout v0.2.31 + +# mobymask-ui +cd ~/cerc/mobymask-ui +git checkout laconic + +# MobyMask +cd ~/cerc/MobyMask +git checkout v0.1.2 +``` + +Build the container images: + +```bash +laconic-so --stack mobymask-v2 build-containers --include cerc/watcher-mobymask-v2,cerc/react-peer,cerc/mobymask-ui,cerc/mobymask +``` + +This should create the required docker images in the local image registry + +## Deploy + +Update the [optimism-params.env](../../config/watcher-mobymask-v2/optimism-params.env) file with Optimism endpoints and other params if running Optimism separately + +* NOTE: + * Stack Orchestrator needs to be run in [`dev`](/docs/CONTRIBUTING.md#install-developer-mode) mode to be able to edit the env file + * If Optimism is running on the host machine, use `host.docker.internal` as the hostname to access the host port + +Deploy the stack: + +```bash +laconic-so --stack mobymask-v2 deploy --include watcher-mobymask-v2 up +``` + +To list down and monitor the running containers: + +```bash +laconic-so --stack mobymask-v2 deploy --include watcher-mobymask-v2 ps +# With status +docker ps +# Check logs for a container +docker logs -f +``` + +See [Tests](./README.md#tests) and [Demo](./README.md#demo) to interact with stack + +## Clean up + +Stop all services running in the background: + +```bash +laconic-so --stack mobymask-v2 deploy down --include watcher-mobymask-v2 +``` + +Clear volumes created by this stack: + +```bash +# List all relevant volumes +docker volume ls -q --filter "name=.*mobymask_watcher_db_data|.*moby_data_server|.*fixturenet_geth_accounts" +# Remove all the listed volumes +docker volume rm $(docker volume ls -q --filter "name=.*mobymask_watcher_db_data|.*moby_data_server|.*fixturenet_geth_accounts") +```