From 0d1e3b6fa2ca7e22ffdf4c0c366f1c5f40669fc9 Mon Sep 17 00:00:00 2001 From: prathamesh0 Date: Wed, 12 Apr 2023 15:27:38 +0530 Subject: [PATCH] Create and use peer ids from a mounted volume --- .../compose/docker-compose-mobymask-app.yml | 2 ++ .../compose/docker-compose-peer-test-app.yml | 4 +++ .../docker-compose-watcher-mobymask-v2.yml | 4 +-- .../mobymask-app-config.json | 4 +-- .../watcher-mobymask-v2/mobymask-app-start.sh | 10 ++++++- .../watcher-mobymask-v2/mobymask-params.env | 4 +-- .../config/watcher-mobymask-v2/peer-id.json | 5 ---- app/data/config/watcher-mobymask-v2/peer.env | 1 - .../config/watcher-mobymask-v2/relay-id.json | 5 ---- .../watcher-mobymask-v2/start-server.sh | 26 ++++++++++++++++++- .../watcher-mobymask-v2/test-app-config.json | 4 +-- .../watcher-mobymask-v2/test-app-start.sh | 8 ++++++ .../watcher-config-template.toml | 6 ++--- app/data/stacks/mobymask-v2/web-apps.md | 4 +-- 14 files changed, 59 insertions(+), 28 deletions(-) delete mode 100644 app/data/config/watcher-mobymask-v2/peer-id.json delete mode 100644 app/data/config/watcher-mobymask-v2/peer.env delete mode 100644 app/data/config/watcher-mobymask-v2/relay-id.json diff --git a/app/data/compose/docker-compose-mobymask-app.yml b/app/data/compose/docker-compose-mobymask-app.yml index 1cd4f9ab..df14d7db 100644 --- a/app/data/compose/docker-compose-mobymask-app.yml +++ b/app/data/compose/docker-compose-mobymask-app.yml @@ -22,6 +22,7 @@ services: - ../config/wait-for-it.sh:/app/wait-for-it.sh - ../config/watcher-mobymask-v2/mobymask-app-config.json:/app/src/mobymask-app-config.json - ../config/watcher-mobymask-v2/mobymask-app-start.sh:/app/mobymask-app-start.sh + - peers_ids:/peers - mobymask_deployment:/server ports: - "0.0.0.0:3002:3000" @@ -37,3 +38,4 @@ services: volumes: mobymask_deployment: + peers_ids: diff --git a/app/data/compose/docker-compose-peer-test-app.yml b/app/data/compose/docker-compose-peer-test-app.yml index c0cc68cb..7d1b89d6 100644 --- a/app/data/compose/docker-compose-peer-test-app.yml +++ b/app/data/compose/docker-compose-peer-test-app.yml @@ -14,6 +14,7 @@ services: volumes: - ../config/watcher-mobymask-v2/test-app-config.json:/app/packages/test-app/src/test-app-config.json - ../config/watcher-mobymask-v2/test-app-start.sh:/app/packages/test-app/test-app-start.sh + - peers_ids:/peers ports: - "0.0.0.0:3003:3000" healthcheck: @@ -24,3 +25,6 @@ services: start_period: 10s extra_hosts: - "host.docker.internal:host-gateway" + +volumes: + peers_ids: diff --git a/app/data/compose/docker-compose-watcher-mobymask-v2.yml b/app/data/compose/docker-compose-watcher-mobymask-v2.yml index 4d2a0394..36cf2cf3 100644 --- a/app/data/compose/docker-compose-watcher-mobymask-v2.yml +++ b/app/data/compose/docker-compose-watcher-mobymask-v2.yml @@ -78,9 +78,8 @@ services: volumes: - ../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 - - ../config/watcher-mobymask-v2/peer-id.json:/app/packages/mobymask-v2-watcher/peer-id.json - ../config/watcher-mobymask-v2/start-server.sh:/app/packages/mobymask-v2-watcher/start-server.sh + - peers_ids:/app/packages/mobymask-v2-watcher/peers - mobymask_deployment:/server - fixturenet_geth_accounts:/geth-accounts:ro # Expose GQL, metrics and relay node ports @@ -99,5 +98,6 @@ services: volumes: mobymask_watcher_db_data: + peers_ids: mobymask_deployment: fixturenet_geth_accounts: diff --git a/app/data/config/watcher-mobymask-v2/mobymask-app-config.json b/app/data/config/watcher-mobymask-v2/mobymask-app-config.json index 2ccb816d..ed913b51 100644 --- a/app/data/config/watcher-mobymask-v2/mobymask-app-config.json +++ b/app/data/config/watcher-mobymask-v2/mobymask-app-config.json @@ -1,8 +1,6 @@ { "name": "MobyMask", - "relayNodes": [ - "/ip4/127.0.0.1/tcp/9090/ws/p2p/12D3KooWSPCsVkHVyLQoCqhu2YRPvvM7o6r6NRYyLM5zeA6Uig5t" - ], + "relayNodes": [], "peer": { "enableDebugInfo": true } 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 fbe2fbf9..578d86f9 100755 --- a/app/data/config/watcher-mobymask-v2/mobymask-app-start.sh +++ b/app/data/config/watcher-mobymask-v2/mobymask-app-start.sh @@ -9,6 +9,14 @@ CERC_DEPLOYED_CONTRACT="${CERC_DEPLOYED_CONTRACT:-${DEFAULT_CERC_DEPLOYED_CONTRA CERC_RELAY_NODES="${CERC_RELAY_NODES:-${DEFAULT_CERC_RELAY_NODES}}" CERC_APP_WATCHER_URL="${CERC_APP_WATCHER_URL:-${DEFAULT_CERC_APP_WATCHER_URL}}" +# If not set (or []), check the mounted volume for relay peer id +if [ -z "$CERC_RELAY_NODES" ] || [ "$CERC_RELAY_NODES" = "[]" ]; then + echo "CERC_RELAY_NODES not provided, taking from the mounted volume" + CERC_RELAY_NODES="[\"/ip4/127.0.0.1/tcp/9090/ws/p2p/$(jq -r '.id' /peers/relay-id.json)\"]" +fi + +echo "Using CERC_RELAY_NODES $CERC_RELAY_NODES" + # Use config from mounted volume if available (when running web-app along with watcher stack) if [ -f /server/config.json ]; then echo "Merging config for deployed contract from mounted volume" @@ -19,7 +27,7 @@ else # Set config values from environment variables jq --arg address "$CERC_DEPLOYED_CONTRACT" \ - --argjson chainId $CERC_CHAIN_ID \ + --argjson chainId "$CERC_CHAIN_ID" \ --argjson relayNodes "$CERC_RELAY_NODES" \ '.address = $address | .chainId = $chainId | .relayNodes = $relayNodes' \ /app/src/mobymask-app-config.json > /app/src/config.json diff --git a/app/data/config/watcher-mobymask-v2/mobymask-params.env b/app/data/config/watcher-mobymask-v2/mobymask-params.env index 7131003e..22681036 100644 --- a/app/data/config/watcher-mobymask-v2/mobymask-params.env +++ b/app/data/config/watcher-mobymask-v2/mobymask-params.env @@ -24,5 +24,5 @@ DEFAULT_CERC_DEPLOYED_CONTRACT= # Chain ID is used by mobymask web-app for txs DEFAULT_CERC_CHAIN_ID=42069 -# Set relay nodes to be used by web-apps -DEFAULT_CERC_RELAY_NODES=["/ip4/127.0.0.1/tcp/9090/ws/p2p/12D3KooWSPCsVkHVyLQoCqhu2YRPvvM7o6r6NRYyLM5zeA6Uig5t"] +# Set of relay nodes to be used by web-apps +DEFAULT_CERC_RELAY_NODES=[] diff --git a/app/data/config/watcher-mobymask-v2/peer-id.json b/app/data/config/watcher-mobymask-v2/peer-id.json deleted file mode 100644 index ceb55fc0..00000000 --- a/app/data/config/watcher-mobymask-v2/peer-id.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "id": "12D3KooWK6myjc8r1KBnfP9igp31qJkPaVfsKDjKrjoSefV5SDEo", - "privKey": "CAESQJMHbMaH+UEOtjGOzXYtoPO/cdHakCtN1hcnknIWzx/6ie1lxb+8kfzBjwt7apfj8fHlTCYSIVK8Q2AWu9a2h3g=", - "pubKey": "CAESIIntZcW/vJH8wY8Le2qX4/Hx5UwmEiFSvENgFrvWtod4" -} diff --git a/app/data/config/watcher-mobymask-v2/peer.env b/app/data/config/watcher-mobymask-v2/peer.env deleted file mode 100644 index 4cce4d9a..00000000 --- a/app/data/config/watcher-mobymask-v2/peer.env +++ /dev/null @@ -1 +0,0 @@ -RELAY="/ip4/127.0.0.1/tcp/9090/ws/p2p/12D3KooWSPCsVkHVyLQoCqhu2YRPvvM7o6r6NRYyLM5zeA6Uig5t" diff --git a/app/data/config/watcher-mobymask-v2/relay-id.json b/app/data/config/watcher-mobymask-v2/relay-id.json deleted file mode 100644 index d7edc6dc..00000000 --- a/app/data/config/watcher-mobymask-v2/relay-id.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "id": "12D3KooWSPCsVkHVyLQoCqhu2YRPvvM7o6r6NRYyLM5zeA6Uig5t", - "privKey": "CAESQGsqG5o4VlWJZM9XlA3MjabyZOXWQ2MLZU5AhBQsjXGt9iSlGtTuNOrHX5xSRgLBxLuMoqWsjGxE/dDB9c46RI8=", - "pubKey": "CAESIPYkpRrU7jTqx1+cUkYCwcS7jKKlrIxsRP3QwfXOOkSP" -} diff --git a/app/data/config/watcher-mobymask-v2/start-server.sh b/app/data/config/watcher-mobymask-v2/start-server.sh index 9cef1e58..3c23642b 100755 --- a/app/data/config/watcher-mobymask-v2/start-server.sh +++ b/app/data/config/watcher-mobymask-v2/start-server.sh @@ -14,6 +14,27 @@ CERC_DEPLOYED_CONTRACT="${CERC_DEPLOYED_CONTRACT:-${DEFAULT_CERC_DEPLOYED_CONTRA echo "Using L2 RPC endpoint ${CERC_L2_GETH_RPC}" +# Check for peer ids in ./peers folder, create if not present +if [ -f ./peers/relay-id.json ]; then + echo "Using peer id for relay node from the mounted volume" +else + echo "Creating a new peer id for relay node" + cd ../peer + yarn create-peer -f ../mobymask-v2-watcher/peers/relay-id.json + cd ../mobymask-v2-watcher +fi + +if [ -f ./peers/peer-id.json ]; then + echo "Using peer id for peer node from the mounted volume" +else + echo "Creating a new peer id for peer node" + cd ../peer + yarn create-peer -f ../mobymask-v2-watcher/peers/peer-id.json + cd ../mobymask-v2-watcher +fi + +CERC_RELAY_MULTIADDR="/dns4/mobymask-watcher-server/tcp/9090/ws/p2p/$(jq -r '.id' ./peers/relay-id.json)" + # Use contract address from environment variable or set from config.json in mounted volume if [ -n "$CERC_DEPLOYED_CONTRACT" ]; then CONTRACT_ADDRESS="${CERC_DEPLOYED_CONTRACT}" @@ -30,12 +51,12 @@ else echo "Using CERC_PRIVATE_KEY_PEER from env" fi - # Read in the config template TOML file and modify it WATCHER_CONFIG_TEMPLATE=$(cat environments/watcher-config-template.toml) WATCHER_CONFIG=$(echo "$WATCHER_CONFIG_TEMPLATE" | \ sed -E "s/REPLACE_WITH_CERC_RELAY_PEERS/$(echo "${CERC_RELAY_PEERS}" | sed 's/\//\\\//g')/g; \ s/REPLACE_WITH_CERC_RELAY_ANNOUNCE_DOMAIN/${CERC_RELAY_ANNOUNCE_DOMAIN}/g; \ + s/REPLACE_WITH_CERC_RELAY_MULTIADDR/${CERC_RELAY_MULTIADDR}/g; \ s/REPLACE_WITH_CERC_ENABLE_PEER_L2_TXS/${CERC_ENABLE_PEER_L2_TXS}/g; \ s/REPLACE_WITH_CERC_PRIVATE_KEY_PEER/${CERC_PRIVATE_KEY_PEER}/g; \ s/REPLACE_WITH_CONTRACT_ADDRESS/${CONTRACT_ADDRESS}/g; \ @@ -44,5 +65,8 @@ WATCHER_CONFIG=$(echo "$WATCHER_CONFIG_TEMPLATE" | \ # Write the modified content to a new file echo "$WATCHER_CONFIG" > environments/local.toml +# Write the relay node's multiaddr to /app/packages/peer/.env for running tests +echo "RELAY=\"$CERC_RELAY_MULTIADDR\"" > /app/packages/peer/.env + echo 'yarn server' yarn server diff --git a/app/data/config/watcher-mobymask-v2/test-app-config.json b/app/data/config/watcher-mobymask-v2/test-app-config.json index 3b00674f..cce15fd8 100644 --- a/app/data/config/watcher-mobymask-v2/test-app-config.json +++ b/app/data/config/watcher-mobymask-v2/test-app-config.json @@ -1,7 +1,5 @@ { - "relayNodes": [ - "/ip4/127.0.0.1/tcp/9090/ws/p2p/12D3KooWSPCsVkHVyLQoCqhu2YRPvvM7o6r6NRYyLM5zeA6Uig5t" - ], + "relayNodes": [], "peer": { "enableDebugInfo": true } diff --git a/app/data/config/watcher-mobymask-v2/test-app-start.sh b/app/data/config/watcher-mobymask-v2/test-app-start.sh index 2f45da03..a7934934 100755 --- a/app/data/config/watcher-mobymask-v2/test-app-start.sh +++ b/app/data/config/watcher-mobymask-v2/test-app-start.sh @@ -6,6 +6,14 @@ fi CERC_RELAY_NODES="${CERC_RELAY_NODES:-${DEFAULT_CERC_RELAY_NODES}}" +# If not set (or []), check the mounted volume for relay peer id +if [ -z "$CERC_RELAY_NODES" ] || [ "$CERC_RELAY_NODES" = "[]" ]; then + echo "CERC_RELAY_NODES not provided, taking from the mounted volume" + CERC_RELAY_NODES="[\"/ip4/127.0.0.1/tcp/9090/ws/p2p/$(jq -r '.id' /peers/relay-id.json)\"]" +fi + +echo "Using CERC_RELAY_NODES $CERC_RELAY_NODES" + # Set relay nodes in config from CERC_RELAY_NODES environment variable jq --argjson relayNodes "$CERC_RELAY_NODES" \ '.relayNodes = $relayNodes' \ 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 cf05d6e2..e6ce0750 100644 --- a/app/data/config/watcher-mobymask-v2/watcher-config-template.toml +++ b/app/data/config/watcher-mobymask-v2/watcher-config-template.toml @@ -27,14 +27,14 @@ host = "0.0.0.0" port = 9090 relayPeers = REPLACE_WITH_CERC_RELAY_PEERS - peerIdFile = './relay-id.json' + peerIdFile = './peers/relay-id.json' announce = 'REPLACE_WITH_CERC_RELAY_ANNOUNCE_DOMAIN' enableDebugInfo = true [server.p2p.peer] - relayMultiaddr = '/dns4/mobymask-watcher-server/tcp/9090/ws/p2p/12D3KooWSPCsVkHVyLQoCqhu2YRPvvM7o6r6NRYyLM5zeA6Uig5t' + relayMultiaddr = 'REPLACE_WITH_CERC_RELAY_MULTIADDR' pubSubTopic = 'mobymask' - peerIdFile = './peer-id.json' + peerIdFile = './peers/peer-id.json' enableDebugInfo = true enableL2Txs = REPLACE_WITH_CERC_ENABLE_PEER_L2_TXS diff --git a/app/data/stacks/mobymask-v2/web-apps.md b/app/data/stacks/mobymask-v2/web-apps.md index 2c1ed253..bfe08f93 100644 --- a/app/data/stacks/mobymask-v2/web-apps.md +++ b/app/data/stacks/mobymask-v2/web-apps.md @@ -41,8 +41,8 @@ This should create the required docker images in the local image registry Create and update an env file to be used in the next step ([defaults](../../config/watcher-mobymask-v2/mobymask-params.env)): ```bash - # Set relay nodes to be used by the web-app - CERC_RELAY_NODES=["/ip4/127.0.0.1/tcp/9090/ws/p2p/12D3KooWSPCsVkHVyLQoCqhu2YRPvvM7o6r6NRYyLM5zeA6Uig5t"] + # Set of relay nodes to be used by the web-app + CERC_RELAY_NODES=[] # Also add if running MobyMask app: