From 1057fc26ffdcc14127f500559b0c64dbf68fb164 Mon Sep 17 00:00:00 2001 From: rBurgett Date: Fri, 22 Oct 2021 18:03:17 -0400 Subject: [PATCH] Log all rpc request payloads when XL_DEBUG argument or environment variable set to true --- src/server/constants/index.js | 2 ++ src/server/index.js | 15 +++++++++++++-- src/server/modules/rpc-controller.js | 21 ++++++++++++++++----- src/server/modules/wallet-controller.js | 12 ++++++++++-- src/server/modules/wallet.js | 12 ++++++++++-- 5 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/server/constants/index.js b/src/server/constants/index.js index a3016c0..9844d7b 100644 --- a/src/server/constants/index.js +++ b/src/server/constants/index.js @@ -8,6 +8,8 @@ import {app} from 'electron'; import path from 'path'; import fs from 'fs-extra'; +export const DEBUG_ENV = 'XL_DEBUG'; + export const DATA_DIR = app ? app.getPath('userData') : ''; export const getLocaleData = locale => { diff --git a/src/server/index.js b/src/server/index.js index f42c595..746e0c9 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -5,7 +5,7 @@ import {publicPath} from './util/public-path'; // must be at top import Api from './modules/api'; import {apiConstants} from '../app/api'; import CloudChains from './modules/cloudchains'; -import {DATA_DIR, getLocaleData, storageKeys} from './constants'; +import {DATA_DIR, DEBUG_ENV, getLocaleData, storageKeys} from './constants'; import {DEFAULT_LOCALE, DEFAULT_ZOOM_FACTOR} from '../app/constants'; import Localize from '../app/components/shared/localize'; import {logger} from './modules/logger'; @@ -26,6 +26,17 @@ import isDev from 'electron-is-dev'; import path from 'path'; import ContextMenu from './modules/context-menu'; +const debugArgPatt = new RegExp(`${DEBUG_ENV}=(\\w+)`); +const foundArg = process.argv.find(str => debugArgPatt.test(str)); +let debug = false; +if(foundArg && foundArg.match(debugArgPatt)[1] === 'true') { + logger.info(`Command line arg ${DEBUG_ENV} set to true`); + debug = true; +} else if(process.env[DEBUG_ENV] && process.env[DEBUG_ENV] === 'true') { + logger.info(`Environment variable ${DEBUG_ENV} set to true`); + debug = true; +} + // Handle any uncaught exceptions process.on('uncaughtException', err => { logger.error('', err); @@ -196,7 +207,7 @@ const startup = async () => { // Create the token manifest from the raw manifest data and fee information const tokenManifest = new TokenManifest(confController.getManifest(), confController.getXBridgeInfo()); // Create the wallet controller - walletController = new WalletController(cloudChains, tokenManifest, storage); + walletController = new WalletController(cloudChains, tokenManifest, storage, debug); }; // Start the application diff --git a/src/server/modules/rpc-controller.js b/src/server/modules/rpc-controller.js index b5011b4..cbfc0b8 100644 --- a/src/server/modules/rpc-controller.js +++ b/src/server/modules/rpc-controller.js @@ -39,15 +39,23 @@ class RPCController { */ _password = ''; + /** + * @type {boolean} + * @private + */ + _debugMode = false; + /** * @param port {number} * @param username {string} * @param password {string} + * @param debugMode {boolean} */ - constructor(port, username, password) { + constructor(port, username, password, debugMode = false) { this._port = port; this._username = username; this._password = password; + this._debugMode = debugMode; } /** @@ -68,13 +76,16 @@ class RPCController { */ async _makeRequest(method, params = [], options = {timeout: HTTP_REQUEST_TIMEOUT}) { return new Promise((resolve, reject) => { + const requestBody = JSON.stringify({ + method, + params + }); + if(this._debugMode) + logger.info(`${method} request to port ${this._port} with body: ${requestBody}`); request .post(`http://127.0.0.1:${this._port}`) .auth(this._username, this._password) - .send(JSON.stringify({ - method, - params - })) + .send(requestBody) .timeout(options.timeout) .then(res => { const { statusCode } = res; diff --git a/src/server/modules/wallet-controller.js b/src/server/modules/wallet-controller.js index a7c4472..bf69fca 100644 --- a/src/server/modules/wallet-controller.js +++ b/src/server/modules/wallet-controller.js @@ -37,6 +37,12 @@ class WalletController { */ _storage = null; + /** + * @type {boolean} + * @private + */ + _debugMode = false; + /** * Default request for currency pricing information. * @param ticker @@ -52,11 +58,13 @@ class WalletController { * @param cloudChains {CloudChains} * @param manifest {TokenManifest} * @param storage {SimpleStorage} + * @param debugMode {boolean} */ - constructor(cloudChains, manifest, storage) { + constructor(cloudChains, manifest, storage, debugMode = false) { this._cloudChains = cloudChains; this._manifest = manifest; this._storage = storage; + this._debugMode = debugMode; } /** @@ -123,7 +131,7 @@ class WalletController { logger.info(`failed to load wallet for token: ${conf.ticker()}`); continue; } - this._wallets.set(conf.ticker(), new Wallet(token, conf, this._storage)); + this._wallets.set(conf.ticker(), new Wallet(token, conf, this._storage, this._debugMode)); } } diff --git a/src/server/modules/wallet.js b/src/server/modules/wallet.js index d90f624..ee2a4f4 100644 --- a/src/server/modules/wallet.js +++ b/src/server/modules/wallet.js @@ -110,13 +110,20 @@ class Wallet { */ _websiteLink = ''; + /** + * @type {boolean} + * @private + */ + _debugMode = false; + /** * Constructs a wallet * @param token {Token} * @param conf {CCWalletConf} * @param storage {Object} + * @param debugMode {boolean} */ - constructor(token, conf, storage) { + constructor(token, conf, storage, debugMode = false) { this._token = token; this._conf = conf; this._storage = storage; @@ -126,6 +133,7 @@ class Wallet { this._explorerLink = explorer; this._explorerTxLink = explorerTx; this._websiteLink = website; + this._debugMode = debugMode; this.initRpcIfEnabled(); } @@ -138,7 +146,7 @@ class Wallet { return; // Set the default port to the xbridge conf port settings if the // cloudchains conf port is invalid. - this.rpc = new RPCController(this._conf.rpcPort, this._conf.rpcUsername, this._conf.rpcPassword); + this.rpc = new RPCController(this._conf.rpcPort, this._conf.rpcUsername, this._conf.rpcPassword, this._debugMode); } /**