2022-03-28 20:39:08 -07:00
|
|
|
//This has to run first so that the process variables are set up when the other modules are instantiated.
|
|
|
|
require("./helpers/loadConfig");
|
|
|
|
|
|
|
|
import { setDefaultWasm } from "@certusone/wormhole-sdk/lib/cjs/solana/wasm";
|
|
|
|
import { getCommonEnvironment } from "./configureEnv";
|
|
|
|
import { getLogger } from "./helpers/logHelper";
|
|
|
|
import { PromHelper, PromMode } from "./helpers/promHelpers";
|
|
|
|
import * as redisHelper from "./helpers/redisHelper";
|
|
|
|
import * as restListener from "./listener/rest_listen";
|
|
|
|
import * as spyListener from "./listener/spy_listen";
|
|
|
|
import * as relayWorker from "./relayer/relay_worker";
|
2022-04-20 16:51:12 -07:00
|
|
|
import * as walletMonitor from "./monitor";
|
2022-03-28 20:39:08 -07:00
|
|
|
|
2022-04-20 16:51:12 -07:00
|
|
|
const ARG_LISTEN_ONLY = "--listen_only";
|
|
|
|
const ARG_RELAY_ONLY = "--relay_only";
|
|
|
|
const ARG_WALLET_MONITOR_ONLY = "--wallet_monitor_only";
|
|
|
|
const ONLY_ONE_ARG_ERROR_MSG = `May only specify one of ${ARG_LISTEN_ONLY}, ${ARG_RELAY_ONLY}, or ${ARG_WALLET_MONITOR_ONLY}`;
|
|
|
|
const ONLY_ONE_ARG_ERROR_RESULT = `Multiple args found of ${ARG_LISTEN_ONLY}, ${ARG_RELAY_ONLY}, ${ARG_WALLET_MONITOR_ONLY}`;
|
2022-03-28 20:39:08 -07:00
|
|
|
|
|
|
|
setDefaultWasm("node");
|
|
|
|
const logger = getLogger();
|
|
|
|
|
|
|
|
// Load the relay config data.
|
|
|
|
let runListen: boolean = true;
|
|
|
|
let runWorker: boolean = true;
|
|
|
|
let runRest: boolean = true;
|
2022-04-20 16:51:12 -07:00
|
|
|
let runWalletMonitor: boolean = true;
|
2022-03-28 20:39:08 -07:00
|
|
|
let foundOne: boolean = false;
|
|
|
|
let error: string = "";
|
|
|
|
|
|
|
|
for (let idx = 0; idx < process.argv.length; ++idx) {
|
2022-04-20 16:51:12 -07:00
|
|
|
if (process.argv[idx] === ARG_LISTEN_ONLY) {
|
2022-03-28 20:39:08 -07:00
|
|
|
if (foundOne) {
|
2022-04-20 16:51:12 -07:00
|
|
|
logger.error(ONLY_ONE_ARG_ERROR_MSG);
|
|
|
|
error = ONLY_ONE_ARG_ERROR_RESULT;
|
2022-03-28 20:39:08 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info("spy_relay is running in listen only mode");
|
|
|
|
runWorker = false;
|
2022-04-20 16:51:12 -07:00
|
|
|
runWalletMonitor = false;
|
2022-03-28 20:39:08 -07:00
|
|
|
foundOne = true;
|
|
|
|
}
|
|
|
|
|
2022-04-20 16:51:12 -07:00
|
|
|
if (process.argv[idx] === ARG_RELAY_ONLY) {
|
2022-03-28 20:39:08 -07:00
|
|
|
if (foundOne) {
|
2022-04-20 16:51:12 -07:00
|
|
|
logger.error(ONLY_ONE_ARG_ERROR_MSG);
|
|
|
|
error = ONLY_ONE_ARG_ERROR_RESULT;
|
2022-03-28 20:39:08 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info("spy_relay is running in relay only mode");
|
|
|
|
runListen = false;
|
|
|
|
runRest = false;
|
2022-04-20 16:51:12 -07:00
|
|
|
runWalletMonitor = false;
|
|
|
|
foundOne = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.argv[idx] === ARG_WALLET_MONITOR_ONLY) {
|
|
|
|
if (foundOne) {
|
|
|
|
logger.error(ONLY_ONE_ARG_ERROR_MSG);
|
|
|
|
error = ONLY_ONE_ARG_ERROR_RESULT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info("spy_relay is running in wallet monitor only mode");
|
|
|
|
runListen = false;
|
|
|
|
runRest = false;
|
|
|
|
runWorker = false;
|
2022-03-28 20:39:08 -07:00
|
|
|
foundOne = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!foundOne) {
|
|
|
|
logger.info("spy_relay is running both the listener and relayer");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
!error &&
|
|
|
|
spyListener.init(runListen) &&
|
|
|
|
relayWorker.init(runWorker) &&
|
2022-04-20 16:51:12 -07:00
|
|
|
restListener.init(runRest) &&
|
|
|
|
walletMonitor.init(runWalletMonitor)
|
2022-03-28 20:39:08 -07:00
|
|
|
) {
|
|
|
|
const commonEnv = getCommonEnvironment();
|
|
|
|
const { promPort, readinessPort } = commonEnv;
|
|
|
|
logger.info("prometheus client listening on port " + promPort);
|
|
|
|
let promClient: PromHelper;
|
2022-04-20 16:51:12 -07:00
|
|
|
const runAll: boolean = runListen && runWorker && runWalletMonitor;
|
|
|
|
if (runAll) {
|
|
|
|
promClient = new PromHelper("spy_relay", promPort, PromMode.All);
|
2022-03-28 20:39:08 -07:00
|
|
|
} else if (runListen) {
|
|
|
|
promClient = new PromHelper("spy_relay", promPort, PromMode.Listen);
|
|
|
|
} else if (runWorker) {
|
|
|
|
promClient = new PromHelper("spy_relay", promPort, PromMode.Relay);
|
2022-04-20 16:51:12 -07:00
|
|
|
} else if (runWalletMonitor) {
|
|
|
|
promClient = new PromHelper("spy_relay", promPort, PromMode.WalletMonitor);
|
2022-03-28 20:39:08 -07:00
|
|
|
} else {
|
|
|
|
logger.error("Invalid run mode for Prometheus");
|
2022-04-20 16:51:12 -07:00
|
|
|
promClient = new PromHelper("spy_relay", promPort, PromMode.All);
|
2022-03-28 20:39:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
redisHelper.init(promClient);
|
|
|
|
|
|
|
|
if (runListen) spyListener.run(promClient);
|
|
|
|
if (runWorker) relayWorker.run(promClient);
|
|
|
|
if (runRest) restListener.run();
|
2022-04-20 16:51:12 -07:00
|
|
|
if (runWalletMonitor) walletMonitor.run(promClient);
|
2022-03-28 20:39:08 -07:00
|
|
|
|
|
|
|
if (readinessPort) {
|
|
|
|
const Net = require("net");
|
|
|
|
const readinessServer = new Net.Server();
|
|
|
|
readinessServer.listen(readinessPort, function () {
|
|
|
|
logger.info("listening for readiness requests on port " + readinessPort);
|
|
|
|
});
|
|
|
|
|
|
|
|
readinessServer.on("connection", function (socket: any) {
|
|
|
|
//logger.debug("readiness connection");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.error("Initialization failed.");
|
|
|
|
}
|