Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log all rpc request payloads when XL_DEBUG argument or environment va… #163

Merged
merged 1 commit into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/server/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
15 changes: 13 additions & 2 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down
21 changes: 16 additions & 5 deletions src/server/modules/rpc-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;
Expand Down
12 changes: 10 additions & 2 deletions src/server/modules/wallet-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class WalletController {
*/
_storage = null;

/**
* @type {boolean}
* @private
*/
_debugMode = false;

/**
* Default request for currency pricing information.
* @param ticker
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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));
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/server/modules/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -126,6 +133,7 @@ class Wallet {
this._explorerLink = explorer;
this._explorerTxLink = explorerTx;
this._websiteLink = website;
this._debugMode = debugMode;
this.initRpcIfEnabled();
}

Expand All @@ -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);
}

/**
Expand Down