From 4f6d6d933ab214eb3d208b7e99f1765403c5ad69 Mon Sep 17 00:00:00 2001 From: Adam A Date: Fri, 22 Nov 2024 12:33:18 +0100 Subject: [PATCH] clean up control services --- Dockerfile | 2 +- Dockerfile.parameterized | 2 +- README.md | 28 ++++---- control.js | 92 --------------------------- control_out.js | 28 -------- control_out_multi.js | 36 ----------- control_in.js => rpc_proxy.js | 0 control_status.js => status.js | 0 control_out_proxy.js => sync_proxy.js | 0 9 files changed, 16 insertions(+), 172 deletions(-) delete mode 100755 control.js delete mode 100755 control_out.js delete mode 100755 control_out_multi.js rename control_in.js => rpc_proxy.js (100%) rename control_status.js => status.js (100%) rename control_out_proxy.js => sync_proxy.js (100%) diff --git a/Dockerfile b/Dockerfile index 3c31fce..d1a5fb3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,6 +27,6 @@ RUN sed -i.bak "s#^laddr = \"tcp://127.0.0.1:26657\"#laddr = \"tcp://0.0.0.0:266 # Install control script ADD deno.json deno.lock deps.js / RUN deno cache --import-map=/deno.json --lock=/deno.lock deps.js -ADD lib.js services.js control.js control_status.js control_in.js control_node.js control_out.js control_out_proxy.js / +ADD lib.js services.js status.js rpc_proxy.js control_node.js sync_proxy.js / ENTRYPOINT [ "/bin/bash" ] CMD [ "-c", "/control.js" ] diff --git a/Dockerfile.parameterized b/Dockerfile.parameterized index c284261..90f5c3e 100644 --- a/Dockerfile.parameterized +++ b/Dockerfile.parameterized @@ -32,6 +32,6 @@ RUN sed -i.bak "s#^persistent_peers *=.*#persistent_peers = \"$PEERS\"#" $CONFIG # Install control script ADD deno.json deno.lock deps.js / RUN deno cache --import-map=/deno.json --lock=/deno.lock deps.js -ADD lib.js services.js control.js control_status.js control_in.js control_node.js control_out.js control_out_proxy.js / +ADD lib.js services.js status.js rpc_proxy.js control_node.js sync_proxy.js / ENTRYPOINT [ "/bin/bash" ] CMD [ "-c", "/control.js" ] diff --git a/README.md b/README.md index e7aacac..98a9985 100644 --- a/README.md +++ b/README.md @@ -23,20 +23,20 @@ available during sync (see [`anoma/namada#3810`](https://github.com/anoma/namada control_node.js ⚠️ Internal only No -Manages the Namada node. MUST run in isolated network (no Internet access except through node-in and node-out). -Parses node log; when epoch increments, node messages node-out +Manages the Namada node. MUST run in isolated network (no Internet access except through rpc-proxy and sync-proxy). +Parses node log; when epoch increments, node messages sync-proxy to pause the sync. -node-in -control_in.js +rpc-proxy +rpc_proxy.js Internal, External ⚠️ Yes Allows connections from indexer to node. -node-out -control_out.js +sync-proxy +sync_proxy.js Internal, External ⚠️ Yes Allows connections from node to peers. By pausing the contained proxy, @@ -44,16 +44,16 @@ node sync is paused, so that the indexer can catch up. node-status -control_status.js +status.js Internal, External No Manages the other three. When indexing has caught up, Undexer tells node-status -to resume sync, and node-status tells node-out to restart the proxy. +to resume sync, and node-status tells sync-proxy to restart the proxy. -Note that `node-in` and `node-out` require an init process in the container +Note that `rpc-proxy` and `sync-proxy` require an init process in the container (`docker run --init`, or `init: true` in `docker-compose.yml`) so that Docker is able to reap the zombie processes that are created when the internal `simpleproxy` is killed by the managing script. @@ -91,9 +91,9 @@ services: image: ghcr.io/hackbg/undexer:v4 restart: unless-stopped depends_on: { postgres: { condition: service_healthy } } - environment: { RPC_URL: "http://node-in:26657" } - node-in: - entrypoint: /control_in.js + environment: { RPC_URL: "http://rpc-proxy:26657" } + rpc-proxy: + entrypoint: /rpc_proxy.js networks: [ external, internal ] image: ghcr.io/hackbg/namada-for-undexer:main init: true @@ -103,8 +103,8 @@ services: networks: [ internal ] image: ghcr.io/hackbg/namada-for-undexer:main restart: unless-stopped - node-out: - entrypoint: /control_out.js + sync-proxy: + entrypoint: /sync_proxy.js networks: [ external, internal ] image: ghcr.io/hackbg/namada-for-undexer:main init: true diff --git a/control.js b/control.js deleted file mode 100755 index e20e359..0000000 --- a/control.js +++ /dev/null @@ -1,92 +0,0 @@ -#!/usr/bin/env -S deno run --allow-net --allow-run=namada,simpleproxy,pkill --allow-env=NAMADA,PROXY,LOCAL,REMOTE,CONTROL_HOST,CONTROL_PORT,CHAIN_ID --allow-write=/home/namada/.local/share/namada - -import { initialize, environment, ServiceManager } from './lib.js' -import { NamadaService, SimpleProxyService } from './services.js' - -if (import.meta.main) main() - -function main () { - initialize() - const { NAMADA, PROXY, LOCAL, REMOTE, CONTROL_HOST, CONTROL_PORT, CHAIN_ID } = environment({ - NAMADA: "namada", - PROXY: "simpleproxy", - LOCAL: ":26666", - REMOTE: "namada-peer-housefire.mandragora.io:26656", - CONTROL_HOST: "127.0.0.1", - CONTROL_PORT: "25555", - CHAIN_ID: "housefire-reduce.e51ecf4264fc3", - }) - - // Define the services - const services = { - node: new NamadaService(NAMADA, CHAIN_ID), - proxy: new SimpleProxyService(PROXY, LOCAL, REMOTE), - } - - // Every time the epoch increments, proxy disconnects and sync stops. - // The indexer must send HTTP /proxy/start or WS {"resume":{}} to continue - // once it's done with indexindg the current epoch. - let currentEpoch = 0n - services.node.events.addEventListener('synced', async ({ detail: { epoch } }) => { - epoch = BigInt(epoch) - if (epoch > currentEpoch) { - console.log('\n🟠 Epoch has increased. Pausing until indexer catches up.\n') - await services.proxy.stop() - currentEpoch = epoch - } - }) - - // Create the service manager. - const server = new ServiceManager(services) - - // Add the websocket endpoint. - server.routes.push(['/ws', (req) => { - if (req.headers.get("upgrade") != "websocket") { - return new Response(null, { status: 400 }) - } - const { socket, response } = Deno.upgradeWebSocket(req) - socket.addEventListener("open", () => { - services.node.events.addEventListener('synced', send) - console.log("client connected to websocket") - }) - socket.addEventListener("close", () => { - services.node.events.removeEventListener('synced', send) - console.log("client disconnected from websocket") - }) - socket.addEventListener("message", async (event) => { - console.log("message received over websocket", event.data) - const data = JSON.parse(event.data) - if (data.restart) { - console.log('restarting sync from beginning...') - await services.node.stop() - await services.node.deleteData() - services.node.start() - } - if (data.resume) { - console.log('resuming sync...') - services.proxy.start() - } - }) - return response - - function send ({ type, detail }) { - if (socket.readyState === WebSocket.OPEN) { - socket.send(JSON.stringify({ [type]: detail })) - } - } - }]) - - // Run the service manager: - server.listen({ - host: CONTROL_HOST, - port: CONTROL_PORT, - }, () => ({ - config: { LOCAL, REMOTE }, - services: { - proxy: services.proxy.status, - node: services.node.status - }, - routes: server.routes.map(route=>route[0]) - })) - -} diff --git a/control_out.js b/control_out.js deleted file mode 100755 index 7de2e4d..0000000 --- a/control_out.js +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env -S deno run --allow-net --allow-run=simpleproxy,pkill,pgrep --allow-env=HOST,PORT,PROXY,LOCAL,REMOTE -// This service manages a `simpleproxy` that receives outgoing connections -// from the node over the internal network, and proxies them to the outside world. -import { initialize, environment, api } from './lib.js' -import { Service } from './services.js' -if (import.meta.main) main() -function main () { - initialize() - const { HOST, PORT, PROXY, LOCAL, REMOTE } = environment({ - HOST: "0.0.0.0", - PORT: "25552", - PROXY: "simpleproxy", - LOCAL: ":26666", - REMOTE: "namada-peer.mandragora.io:26656", - }) - const name = `Sync proxy (${LOCAL} -> ${REMOTE})` - const service = new Service(name, PROXY, '-v', '-L', LOCAL, '-R', REMOTE) - service.start() - api('Sync', HOST, PORT, service.routes(), { - onMessage: async ({ event }) => { - const data = JSON.parse(event.data) - if (data.resume) { - console.log('🟢 Resuming sync...') - await service.start() - } - } - }) -} diff --git a/control_out_multi.js b/control_out_multi.js deleted file mode 100755 index 499f5cf..0000000 --- a/control_out_multi.js +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env -S deno run --allow-net --allow-run=simpleproxy,pkill,pgrep --allow-env=HOST,PORT,PROXY,LOCAL1,LOCAL2,LOCAL3,REMOTE1,REMOTE2,REMOTE3 -// This service manages a `simpleproxy` that receives outgoing connections -// from the node over the internal network, and proxies them to the outside world. -import { initialize, environment, api } from './lib.js' -import { MultiService } from './services.js' -if (import.meta.main) main() -function main () { - initialize() - const { HOST, PORT, PROXY, LOCAL1, LOCAL2, LOCAL3, REMOTE1, REMOTE2, REMOTE3 } = environment({ - HOST: "0.0.0.0", - PORT: "25552", - PROXY: "simpleproxy", - LOCAL1: ":26666", - LOCAL2: ":26667", - LOCAL3: ":26668", - REMOTE1: "74.50.93.254:26656", - REMOTE2: "64.118.250.82:46656", - REMOTE3: "138.197.133.118:26656", - }) - const name = `Sync proxy (3x)` - const service = new MultiService(name, [ - [PROXY, '-v', '-L', LOCAL1, '-R', REMOTE1], - [PROXY, '-v', '-L', LOCAL2, '-R', REMOTE2], - [PROXY, '-v', '-L', LOCAL3, '-R', REMOTE3], - ]) - service.start() - api('Sync', HOST, PORT, service.routes(), { - onMessage: async ({ event }) => { - const data = JSON.parse(event.data) - if (data.resume) { - console.log('🟢 Resuming sync...') - await service.start() - } - } - }) -} diff --git a/control_in.js b/rpc_proxy.js similarity index 100% rename from control_in.js rename to rpc_proxy.js diff --git a/control_status.js b/status.js similarity index 100% rename from control_status.js rename to status.js diff --git a/control_out_proxy.js b/sync_proxy.js similarity index 100% rename from control_out_proxy.js rename to sync_proxy.js