From c72070a0802a726b7d408d2b28176add9170926e Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Fri, 15 Sep 2023 12:38:31 +0530 Subject: [PATCH] Fix Nitro deployment script and add watcher container --- .../docker-compose-watcher-mobymask-v3.yml | 62 +++++++++++-- .../deploy-nitro-contracts.sh | 45 ++++++---- .../deploy-nitro-contracts.ts | 1 + .../watcher-mobymask-v3/mobymask-params.env | 37 ++++++++ .../read-nitro-contracts.sh | 18 ++++ .../watcher-config-template.toml | 80 +++++++++++++++++ .../cerc-watcher-mobymask-v3/Dockerfile | 20 +++++ .../cerc-watcher-mobymask-v3/build.sh | 9 ++ app/data/container-image-list.txt | 1 + app/data/stacks/mobymask-v3/stack.yml | 6 +- app/data/stacks/mobymask-v3/watcher.md | 89 ++++++++++++++++++- 11 files changed, 340 insertions(+), 28 deletions(-) create mode 100644 app/data/config/watcher-mobymask-v3/mobymask-params.env create mode 100755 app/data/config/watcher-mobymask-v3/read-nitro-contracts.sh create mode 100644 app/data/config/watcher-mobymask-v3/watcher-config-template.toml create mode 100644 app/data/container-build/cerc-watcher-mobymask-v3/Dockerfile create mode 100755 app/data/container-build/cerc-watcher-mobymask-v3/build.sh diff --git a/app/data/compose/docker-compose-watcher-mobymask-v3.yml b/app/data/compose/docker-compose-watcher-mobymask-v3.yml index 6fbe71df..0702c93e 100644 --- a/app/data/compose/docker-compose-watcher-mobymask-v3.yml +++ b/app/data/compose/docker-compose-watcher-mobymask-v3.yml @@ -28,8 +28,7 @@ services: image: cerc/mobymask:local working_dir: /app/packages/server env_file: - - ../config/watcher-mobymask-v2/optimism-params.env - - ../config/watcher-mobymask-v2/mobymask-params.env + - ../config/watcher-mobymask-v3/mobymask-params.env environment: CERC_SCRIPT_DEBUG: ${CERC_SCRIPT_DEBUG} ENV: "PROD" @@ -57,14 +56,65 @@ services: restart: on-failure environment: CERC_SCRIPT_DEBUG: ${CERC_SCRIPT_DEBUG} - # working_dir: /app/packages/peer - working_dir: /app/packages/ - command: ["sh", "generate-peer-ids.sh && deploy-nitro-contracts.sh"] + working_dir: /app/packages/peer + command: ["sh", "generate-peer-ids.sh"] volumes: - ../config/watcher-mobymask-v2/generate-peer-ids.sh:/app/packages/peer/generate-peer-ids.sh - peers_ids:/peer-ids - - ../config/watcher-mobymask-v3/deploy-nitro-contracts.sh:/app/packages/peer/deploy-nitro-contracts.sh + + # Optionally deploys the Nitro contracts and sets them at the required path + # Starts the MobyMask v3 watcher server + mobymask-watcher-server: + image: cerc/watcher-mobymask-v3:local + restart: unless-stopped + depends_on: + mobymask-watcher-db: + condition: service_healthy + peer-ids-gen: + condition: service_completed_successfully + mobymask: + condition: service_completed_successfully + env_file: + - ../config/watcher-mobymask-v3/mobymask-params.env + environment: + CERC_SCRIPT_DEBUG: ${CERC_SCRIPT_DEBUG} + CERC_L2_GETH_RPC: ${CERC_L2_GETH_RPC} + CERC_L1_ACCOUNTS_CSV_URL: ${CERC_L1_ACCOUNTS_CSV_URL} + CERC_PRIVATE_KEY_DEPLOYER: ${CERC_PRIVATE_KEY_DEPLOYER} + CERC_PRIVATE_KEY_PEER: ${CERC_PRIVATE_KEY_PEER} + CERC_RELAY_PEERS: ${CERC_RELAY_PEERS} + CERC_DENY_MULTIADDRS: ${CERC_DENY_MULTIADDRS} + CERC_PUBSUB: ${CERC_PUBSUB} + CERC_RELAY_ANNOUNCE_DOMAIN: ${CERC_RELAY_ANNOUNCE_DOMAIN} + CERC_ENABLE_PEER_L2_TXS: ${CERC_ENABLE_PEER_L2_TXS} + CERC_DEPLOYED_CONTRACT: ${CERC_DEPLOYED_CONTRACT} + CERC_NA_ADDRESS: ${CERC_NA_ADDRESS} + CERC_VPA_ADDRESS: ${CERC_VPA_ADDRESS} + CERC_CA_ADDRESS: ${CERC_CA_ADDRESS} + entrypoint: ["bash", "-c"] + command: ["./deploy-nitro-contracts.sh && ./read-nitro-contracts.sh && ./start-server.sh"] + volumes: + - ../config/watcher-mobymask-v3/deploy-nitro-contracts.sh:/app/deploy-nitro-contracts.sh + - ../config/watcher-mobymask-v3/deploy-nitro-contracts.ts:/app/deploy-nitro-contracts.ts + - ../config/watcher-mobymask-v3/read-nitro-contracts.sh:/app/read-nitro-contracts.sh + - ../config/watcher-mobymask-v3/watcher-config-template.toml:/app/environments/watcher-config-template.toml + - ../config/watcher-mobymask-v2/start-server.sh:/app/start-server.sh + - peers_ids:/app/peers - nitro_addresses:/nitro + - mobymask_deployment:/server + # Expose GQL, metrics and relay node ports + ports: + - "127.0.0.1:3001:3001" + - "127.0.0.1:9001:9001" + - "127.0.0.1:9090:9090" + healthcheck: + test: ["CMD", "busybox", "nc", "localhost", "9090"] + interval: 20s + timeout: 5s + retries: 15 + start_period: 5s + extra_hosts: + - "host.docker.internal:host-gateway" volumes: mobymask_watcher_db_data: diff --git a/app/data/config/watcher-mobymask-v3/deploy-nitro-contracts.sh b/app/data/config/watcher-mobymask-v3/deploy-nitro-contracts.sh index e2cd687c..e22eae5c 100755 --- a/app/data/config/watcher-mobymask-v3/deploy-nitro-contracts.sh +++ b/app/data/config/watcher-mobymask-v3/deploy-nitro-contracts.sh @@ -4,17 +4,34 @@ if [ -n "$CERC_SCRIPT_DEBUG" ]; then set -x fi -CERC_L2_GETH_RPC="${CERC_L2_GETH_RPC:-${DEFAULT_CERC_L2_GETH_RPC}}" -CERC_L1_ACCOUNTS_CSV_URL="${CERC_L1_ACCOUNTS_CSV_URL:-${DEFAULT_CERC_L1_ACCOUNTS_CSV_URL}}" -CERC_NITRO_CONTRACTS="${CERC_DEPLOYED_CONTRACT:-${DEFAULT_CERC_NITRO_CONTRACTS}}" +CERC_NA_ADDRESS="${CERC_NA_ADDRESS:-${DEFAULT_CERC_NA_ADDRESS}}" +CERC_VPA_ADDRESS="${CERC_VPA_ADDRESS:-${DEFAULT_CERC_VPA_ADDRESS}}" +CERC_CA_ADDRESS="${CERC_CA_ADDRESS:-${DEFAULT_CERC_CA_ADDRESS}}" NITRO_ADDRESSES_FILE_PATH="/nitro/nitro-addresses.json" # Check if CERC_NITRO_CONTRACTS environment variable set to skip contract deployment -if [ -n "$CERC_NITRO_CONTRACTS" ]; then - echo "CERC_NITRO_CONTRACTS is set to '$CERC_NITRO_CONTRACTS'" - echo "Skipping Nitro contracts deployment" - exit 0 +if [ -n "$CERC_NA_ADDRESS" ]; then + echo "CERC_NA_ADDRESS is set to '$CERC_NA_ADDRESS'" + echo "CERC_VPA_ADDRESS is set to '$CERC_VPA_ADDRESS'" + echo "CERC_CA_ADDRESS is set to '$CERC_CA_ADDRESS'" + echo "Using the above addresses and skipping Nitro contracts deployment" + + # Create the required JSON and write it to a file + nitro_addresses_json="{ + \"nitroAdjudicatorAddress\": \"$CERC_NA_ADDRESS\", + \"virtualPaymentAppAddress\": \"$CERC_VPA_ADDRESS\", + \"consensusAppAddress\": \"$CERC_CA_ADDRESS\" + }" + echo "$nitro_addresses_json" > "${NITRO_ADDRESSES_FILE_PATH}" + + exit +fi + +# Check and exit if a deployment already exists (on restarts) +if [ -f ${NITRO_ADDRESSES_FILE_PATH} ]; then + echo "${NITRO_ADDRESSES_FILE_PATH} already exists, skipping Nitro contracts deployment" + exit fi echo "Using L2 RPC endpoint ${CERC_L2_GETH_RPC}" @@ -33,14 +50,8 @@ else echo "Couldn't fetch L1 account credentials, using CERC_PRIVATE_KEY_DEPLOYER from env" fi -# Check and exit if a deployment already exists (on restarts) -if [ -f ${NITRO_ADDRESSES_FILE_PATH} ]; then - echo "${NITRO_ADDRESSES_FILE_PATH} already exists, skipping Nitro contracts deployment" - exit -fi +echo "RPC_URL=${CERC_L2_GETH_RPC}" > .env +echo "NITRO_ADDRESSES_FILE_PATH=${NITRO_ADDRESSES_FILE_PATH}" >> .env +echo "PRIVATE_KEY=${CERC_PRIVATE_KEY_DEPLOYER}" >> .env -export RPC_URL="${CERC_L2_GETH_RPC}" -export NITRO_ADDRESSES_FILE_PATH="${NITRO_ADDRESSES_FILE_PATH}" -export PRIVATE_KEY="${CERC_PRIVATE_KEY_DEPLOYER}" - -yarn ts-node --esm ./src/deploy-nitro-contracts.ts +yarn ts-node --esm deploy-nitro-contracts.ts diff --git a/app/data/config/watcher-mobymask-v3/deploy-nitro-contracts.ts b/app/data/config/watcher-mobymask-v3/deploy-nitro-contracts.ts index bd41f734..c738083d 100644 --- a/app/data/config/watcher-mobymask-v3/deploy-nitro-contracts.ts +++ b/app/data/config/watcher-mobymask-v3/deploy-nitro-contracts.ts @@ -1,3 +1,4 @@ +import 'dotenv/config'; import fs from 'fs'; import { providers, Wallet } from 'ethers'; import { deployContracts } from '@cerc-io/nitro-util'; diff --git a/app/data/config/watcher-mobymask-v3/mobymask-params.env b/app/data/config/watcher-mobymask-v3/mobymask-params.env new file mode 100644 index 00000000..a346baac --- /dev/null +++ b/app/data/config/watcher-mobymask-v3/mobymask-params.env @@ -0,0 +1,37 @@ +# Defaults + +# Watcher endpoint +DEFAULT_CERC_APP_WATCHER_URL="http://localhost:3001" + +# Set of relay peers to connect to from the relay node +DEFAULT_CERC_RELAY_PEERS=[] + +# Domain to be used in the relay node's announce address +DEFAULT_CERC_RELAY_ANNOUNCE_DOMAIN= + +# Base URI for mobymask-app (used for generating invite) +DEFAULT_CERC_MOBYMASK_APP_BASE_URI="http://127.0.0.1:3004/#" + +# Set to false for disabling watcher peer to send txs to L2 +DEFAULT_CERC_ENABLE_PEER_L2_TXS=true + +# Set deployed MobyMask contract address to avoid deploying contract in stack +# mobymask-app will use this contract address in config if run separately +DEFAULT_CERC_DEPLOYED_CONTRACT= + +# Chain ID is used by mobymask web-app for txs +DEFAULT_CERC_CHAIN_ID=42069 + +# Set of relay nodes to be used by web-apps +DEFAULT_CERC_RELAY_NODES=[] + +# Set of multiaddrs to be avoided while dialling +DEFAULT_CERC_DENY_MULTIADDRS=[] + +# Type of pubsub to be used +DEFAULT_CERC_PUBSUB="" + +# Set deployed Nitro addresses to avoid deploying them in the stack +DEFAULT_CERC_NA_ADDRESS= +DEFAULT_CERC_VPA_ADDRESS= +DEFAULT_CERC_CA_ADDRESS= diff --git a/app/data/config/watcher-mobymask-v3/read-nitro-contracts.sh b/app/data/config/watcher-mobymask-v3/read-nitro-contracts.sh new file mode 100755 index 00000000..93655e7c --- /dev/null +++ b/app/data/config/watcher-mobymask-v3/read-nitro-contracts.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +set -e +if [ -n "$CERC_SCRIPT_DEBUG" ]; then + set -x +fi + +NITRO_ADDRESSES_FILE_PATH="/nitro/nitro-addresses.json" +DESTINATION_FILE_PATH="./src/nitro-addresses.json" + +# Check if the file exists +if [ -f "$NITRO_ADDRESSES_FILE_PATH" ]; then + cat "$NITRO_ADDRESSES_FILE_PATH" > "$DESTINATION_FILE_PATH" + echo "Nitro addresses set to ${DESTINATION_FILE_PATH}" +else + echo "File ${NITRO_ADDRESSES_FILE_PATH} does not exist" + exit 1 +fi diff --git a/app/data/config/watcher-mobymask-v3/watcher-config-template.toml b/app/data/config/watcher-mobymask-v3/watcher-config-template.toml new file mode 100644 index 00000000..a0dd3b34 --- /dev/null +++ b/app/data/config/watcher-mobymask-v3/watcher-config-template.toml @@ -0,0 +1,80 @@ +[server] + host = "0.0.0.0" + port = 3001 + kind = "lazy" + + # Checkpointing state. + checkpointing = true + + # Checkpoint interval in number of blocks. + checkpointInterval = 2000 + + # Enable state creation + enableState = true + + # Boolean to filter logs by contract. + filterLogs = true + + # Max block range for which to return events in eventsInRange GQL query. + # Use -1 for skipping check on block range. + maxEventsBlockRange = -1 + + [server.p2p] + enableRelay = true + enablePeer = true + + [server.p2p.relay] + host = "0.0.0.0" + port = 9090 + relayPeers = REPLACE_WITH_CERC_RELAY_PEERS + denyMultiaddrs = REPLACE_WITH_CERC_DENY_MULTIADDRS + peerIdFile = './peers/relay-id.json' + announce = 'REPLACE_WITH_CERC_RELAY_ANNOUNCE_DOMAIN' + pubsub = 'REPLACE_WITH_CERC_PUBSUB' + enableDebugInfo = true + + [server.p2p.peer] + relayMultiaddr = 'REPLACE_WITH_CERC_RELAY_MULTIADDR' + pubSubTopic = 'mobymask' + denyMultiaddrs = REPLACE_WITH_CERC_DENY_MULTIADDRS + peerIdFile = './peers/peer-id.json' + pubsub = 'REPLACE_WITH_CERC_PUBSUB' + enableDebugInfo = true + enableL2Txs = REPLACE_WITH_CERC_ENABLE_PEER_L2_TXS + + [server.p2p.peer.l2TxsConfig] + privateKey = 'REPLACE_WITH_CERC_PRIVATE_KEY_PEER' + contractAddress = 'REPLACE_WITH_CONTRACT_ADDRESS' + +[metrics] + host = "0.0.0.0" + port = 9000 + [metrics.gql] + port = 9001 + +[database] + type = "postgres" + host = "mobymask-watcher-db" + port = 5432 + database = "mobymask-watcher" + username = "vdbm" + password = "password" + synchronize = true + logging = false + +[upstream] + [upstream.ethServer] + gqlApiEndpoint = "http://ipld-eth-server:8083/graphql" + rpcProviderEndpoint = "REPLACE_WITH_CERC_L2_GETH_RPC_ENDPOINT" + blockDelayInMilliSecs = 60000 + + [upstream.cache] + name = "requests" + enabled = false + deleteOnStart = false + +[jobQueue] + dbConnectionString = "postgres://vdbm:password@mobymask-watcher-db/mobymask-watcher-job-queue" + maxCompletionLagInSecs = 300 + jobDelayInMilliSecs = 100 + eventsInBatch = 50 diff --git a/app/data/container-build/cerc-watcher-mobymask-v3/Dockerfile b/app/data/container-build/cerc-watcher-mobymask-v3/Dockerfile new file mode 100644 index 00000000..3e07eec9 --- /dev/null +++ b/app/data/container-build/cerc-watcher-mobymask-v3/Dockerfile @@ -0,0 +1,20 @@ +FROM ubuntu:22.04 + +RUN apt-get update \ + && apt-get install -y curl wget gnupg build-essential \ + && curl --silent --location https://deb.nodesource.com/setup_18.x | bash - \ + && apt-get update \ + && apt-get install -y nodejs git busybox jq \ + && node -v + +RUN corepack enable \ + && yarn --version + +WORKDIR /app + +COPY . . + +RUN echo "Building mobymask-v2-watcher-ts" && \ + yarn && yarn build + +WORKDIR /app diff --git a/app/data/container-build/cerc-watcher-mobymask-v3/build.sh b/app/data/container-build/cerc-watcher-mobymask-v3/build.sh new file mode 100755 index 00000000..1c26a4ce --- /dev/null +++ b/app/data/container-build/cerc-watcher-mobymask-v3/build.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +# Build cerc/watcher-mobymask-v3 + +source ${CERC_CONTAINER_BASE_DIR}/build-base.sh + +# See: https://stackoverflow.com/a/246128/1701505 +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + +docker build -t cerc/watcher-mobymask-v3:local -f ${SCRIPT_DIR}/Dockerfile ${build_command_args} ${CERC_REPO_BASE_DIR}/mobymask-v2-watcher-ts diff --git a/app/data/container-image-list.txt b/app/data/container-image-list.txt index 9ed473ba..ab8bc3ce 100644 --- a/app/data/container-image-list.txt +++ b/app/data/container-image-list.txt @@ -50,3 +50,4 @@ cerc/watcher-sushiswap cerc/graph-node cerc/sushiswap-subgraphs cerc/webapp-base +cerc/watcher-mobymask-v3 diff --git a/app/data/stacks/mobymask-v3/stack.yml b/app/data/stacks/mobymask-v3/stack.yml index 22bcb39e..4f08ea7f 100644 --- a/app/data/stacks/mobymask-v3/stack.yml +++ b/app/data/stacks/mobymask-v3/stack.yml @@ -8,9 +8,9 @@ repos: - github.com/cerc-io/mobymask-ui containers: - cerc/watcher-ts - - cerc/watcher-mobymask-v2 - - cerc/mobymask-ui + - cerc/watcher-mobymask-v3 - cerc/mobymask + - cerc/mobymask-ui pods: - watcher-mobymask-v3 - - mobymask-app-v3 + # - mobymask-app-v3 diff --git a/app/data/stacks/mobymask-v3/watcher.md b/app/data/stacks/mobymask-v3/watcher.md index b666c331..6ee069b3 100644 --- a/app/data/stacks/mobymask-v3/watcher.md +++ b/app/data/stacks/mobymask-v3/watcher.md @@ -5,13 +5,98 @@ Clone required repositories: ```bash -laconic-so --stack mobymask-v3 setup-repositories +laconic-so --stack mobymask-v3 setup-repositories --pull --exclude github.com/cerc-io/mobymask-ui ``` Build the container images: ```bash -laconic-so --stack mobymask-v3 build-containers +laconic-so --stack mobymask-v3 build-containers --exclude cerc/mobymask-ui ``` ## Deploy + +### Configuration + +Create and update an env file to be used in the next step ([defaults](../../config/watcher-mobymask-v3/mobymask-params.env)): + + ```bash + # External L2 endpoints + CERC_L2_GETH_RPC= + + # Endpoints waited on before contract deployment + CERC_L2_GETH_HOST= + CERC_L2_GETH_PORT= + + CERC_L2_NODE_HOST= + CERC_L2_NODE_PORT= + + # URL (fixturenet-eth-bootnode-lighthouse) to get CSV with credentials for accounts on L1 to perform txs on L2 + CERC_L1_ACCOUNTS_CSV_URL= + + # OR + # Specify the required account credentials + CERC_PRIVATE_KEY_DEPLOYER= + CERC_PRIVATE_KEY_PEER= + + # Base URI for mobymask-app + # (used for generating a root invite link after deploying the contract) + CERC_MOBYMASK_APP_BASE_URI="http://127.0.0.1:3004/#" + + # (Optional) Domain to be used in the relay node's announce address + CERC_RELAY_ANNOUNCE_DOMAIN= + + # (Optional) Set of relay peers to connect to from the relay node + CERC_RELAY_PEERS=[] + + # (Optional) Set of multiaddrs to be avoided while dialling + CERC_DENY_MULTIADDRS=[] + + # (Optional) Type of pubsub to be used + CERC_PUBSUB="" + + # Set to false for disabling watcher peer to send txs to L2 + CERC_ENABLE_PEER_L2_TXS=true + + # (Optional) Set already deployed MobyMask contract address to avoid deploying contract in the stack + CERC_DEPLOYED_CONTRACT= + + # (Optional) Set already deployed Nitro addresses to avoid deploying them in the stack + CERC_NA_ADDRESS= + CERC_VPA_ADDRESS= + CERC_CA_ADDRESS= + ``` + +* NOTE: 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-v3 deploy --cluster mobymask_v3 --include watcher-mobymask-v3 --env-file up +``` + +* To list down and monitor the running containers: + + ```bash + laconic-so --stack mobymask-v3 deploy --cluster mobymask_v3 --include watcher-mobymask-v3 ps + + # With status + docker ps -a + + # Check logs for a container + docker logs -f + ``` + +* The watcher endpoint is exposed on host port `3001` and the relay node endpoint is exposed on host port `9090` + +* Check the logs of the MobyMask contract deployment container to get the deployed contract's address and generated root invite link: + + ```bash + docker logs -f $(docker ps -aq --filter name="mobymask-1") + ``` + +* Check the logs of the watcher server container to get the deployed Nitro contracts' addresses: + +```bash +docker exec -it $(docker ps -q --filter name="mobymask-watcher-server") bash -c "cat /nitro/nitro-addresses.json" +```