Skip to content
Open
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
10 changes: 7 additions & 3 deletions app/main_dev/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export const startDcrlnd = async (
}
};

export const startDex = async (walletPath, testnet, locale) => {
export const startDex = (walletPath, testnet, locale) => {
if (GetDexPID()) {
logger.log(
"info",
Expand All @@ -257,7 +257,7 @@ export const startDex = async (walletPath, testnet, locale) => {
}

try {
const started = await launchDex(walletPath, testnet, locale);
const started = launchDex(walletPath, testnet, locale);
return started;
} catch (e) {
logger.log("error", `error launching dex: ${e}`);
Expand Down Expand Up @@ -417,7 +417,11 @@ export const userDex = async () => {
};

export const stopDaemon = () => {
return closeDCRD();
const res = closeDCRD();
if (res) {
dcrdIsRemote = null;
}
return res;
};

export const stopWallet = () => {
Expand Down
52 changes: 27 additions & 25 deletions app/main_dev/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const logLevelsPrintable = {
export const getLogFileName = () =>
path.join(getAppDataDirectory(), "decrediton.log");

class Logger {
export class Logger {
constructor(debug) {
this.debug = debug;
this.logLevels = {
Expand All @@ -68,24 +68,22 @@ class Logger {
this.drained = true;
this.buffer = [];
this.logFile = fs.createWriteStream(getLogFileName());
this.logFile.on("drain", this.dequeue);
}

dequeue() {
if (this.buffer.length === 0) {
this.drained = true;
return;
}

// Keep writing data to the file until we either write all available data or
// the file becomes busy for writes (in which case this function will be
// called again once the file can be written to again).
let drained = true;
while (this.buffer.length() > 0 && drained) {
const data = this.buffer.shift();
this.drained = this.logFile.write(data);
drained = this.drained;
}
this.logFile.on("drain", () => {
if (this.buffer.length === 0) {
this.drained = true;
return;
}

// Keep writing data to the file until we either write all available data or
// the file becomes busy for writes (in which case this function will be
// called again once the file can be written to again).
let drained = true;
while (this.buffer.length > 0 && drained) {
const data = this.buffer.shift();
this.drained = this.logFile.write(data);
drained = this.drained;
}
});
}

queue(data) {
Expand All @@ -100,7 +98,8 @@ class Logger {

log(level, msg) {
const levelLower = level.toLowerCase();
const logLevel = this.logLevels[levelLower] || 3;
const logLevel =
levelLower in this.logLevels ? this.logLevels[levelLower] : 3;

const subsys = "DCTN";
const lvl = logLevelsPrintable[levelLower] || "UNK";
Expand Down Expand Up @@ -197,7 +196,7 @@ const panicErr = "panic";

export function lastLogLine(log) {
const lastLineIdx = log.lastIndexOf(os.EOL, log.length - os.EOL.length - 1);
const lastLineBuff = log.slice(lastLineIdx).toString("utf-8");
const lastLineBuff = log.slice(Math.max(0, lastLineIdx)).toString("utf-8");
return lastLineBuff.trim();
}

Expand All @@ -211,10 +210,13 @@ export function lastErrorLine(log) {
}

export function lastPanicLine(log) {
let lastLineIdx = log.indexOf(panicErr);
if (lastLineIdx < 0) lastLineIdx = log.indexOf("goroutine");
const lastLineBuff = log.slice(lastLineIdx).toString("utf-8");
return lastLineBuff;
let lastLineIdx = log.lastIndexOf(panicErr);
if (lastLineIdx < 0) lastLineIdx = log.lastIndexOf("goroutine");
const endOfErrorLineIdx = log.indexOf(os.EOL, lastLineIdx);
const lastLineBuff = log
.slice(lastLineIdx, endOfErrorLineIdx)
.toString("utf-8");
return lastLineBuff.trim();
}

export function ClearDcrwalletLogs() {
Expand Down
21 changes: 7 additions & 14 deletions app/main_dev/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ export function getWalletsDirectoryPath() {
// getWalletsDirectoryPathNetwork gets the wallets directory.
// Example in unix if testnet equals true: ~/.config/decrediton/wallets/testnet
export function getWalletsDirectoryPathNetwork(testnet) {
return path.join(
getAppDataDirectory(),
"wallets",
testnet ? TESTNET : MAINNET
);
return path.join(getWalletsDirectoryPath(), testnet ? TESTNET : MAINNET);
}

// getWalletPath returns the directory of a selected wallet byt its name.
Expand All @@ -54,9 +50,7 @@ export function getWalletPath(testnet, walletName = "") {
// walletPath represents the wallet name decrediton has loaded.
export function getWalletDb(testnet, walletPath) {
return path.join(
getWalletsDirectoryPath(),
testnet ? TESTNET : MAINNET,
walletPath,
getWalletPath(testnet, walletPath),
testnet ? "testnet3" : MAINNET,
"wallet.db"
);
Expand Down Expand Up @@ -90,12 +84,11 @@ export function getDcrdRpcCert(appDataPath) {
return path.resolve(appDataPath ? appDataPath : getDcrdPath(), "rpc.cert");
}

export function getCertsPath(name, custombinpath) {
const binPath = custombinpath
? custombinpath
: process.env.NODE_ENV === "development"
? path.join(__dirname, "..", "certs")
: path.join(process.resourcesPath, "certs");
export function getCertsPath() {
const binPath =
process.env.NODE_ENV === "development"
? path.join(__dirname, "..", "certs")
: path.join(process.resourcesPath, "certs");

return binPath;
}
Expand Down
2 changes: 1 addition & 1 deletion app/main_dev/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const setupProxy = (logger) =>
default:
if (proxyType) {
logger.log("error", "Unknown proxy type " + proxyType);
reject("Unknown proxy type: " + proxyType);
return reject("Unknown proxy type: " + proxyType);
}
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"^dex$": "<rootDir>/test/mocks/dexMock.js",
"^walletCrypto$": "<rootDir>/app/wallet/crypto.js",
"^fetchModule$": "<rootDir>/app/helpers/fetchModule.js",
"wallet-preload-shim$": "<rootDir>/test/mocks/walletPreloadShimMock.js"
"wallet-preload-shim$": "<rootDir>/test/mocks/walletPreloadShimMock.js",
"logging$": "<rootDir>/test/mocks/loggingMock.js"
},
"transformIgnorePatterns": [
"/node_modules/",
Expand Down
22 changes: 22 additions & 0 deletions test/mocks/electronMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,25 @@ export const ipcRenderer = {
export const clipboard = {
readText: jest.fn(() => "")
};

export let onBeforeSendHeadersListener;
export let onHeadersReceivedListener;

export const session = {
defaultSession: {
setProxy: jest.fn(() => Promise.resolve()),
webRequest: {
onBeforeSendHeaders: jest.fn((_, cb) => {
onBeforeSendHeadersListener = cb;
}),
onHeadersReceived: jest.fn((cb) => {
onHeadersReceivedListener = cb;
})
}
}
};

export const app = {
name: "testAppName",
getVersion: () => "testVersion"
};
7 changes: 7 additions & 0 deletions test/mocks/loggingMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function createLogger() {
return {
log: () => {}
};
}

export function trace() {}
33 changes: 33 additions & 0 deletions test/unit/main_dev/constants.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as con from "../../../app/main_dev/constants";
const constants = con;

test("test USAGE_MESSAGE", () => {
expect(constants.USAGE_MESSAGE).toMatchInlineSnapshot(`
"testAppName version testVersion
Usage
$ testAppName [OPTIONS]

Options
--help -h Show help and exit.
--version -v Show version and exit.
--debug -d Debug daemon/wallet messages.
--testnet Connect to testnet.
--mainnet Connect to mainnet.
--advanced Start in advanced daemon mode.
--spv Start in SPV mode (cannot be used at the same time as advanced daemon mode).
--spvconnect Specify direct peer for SPV connection in 'host:port' or 'host' format (latter uses the default SPV port). Supports comma-separated list of peers. Always use with --spv.
--rpcuser Specify RPC username for advanced daemon mode connection
--rpcpass Specify RPC password
--rpccert Specify RPC Certificate
--rpcconnect Specify RPC connection in 'host:port' or 'host' format (latter uses the default RPC port). Note that different ports are used for RPC and SPV connections.
--extrawalletargs Pass extra arguments to dcrwallet.
--custombinpath Custom path for dcrd/dcrwallet/dcrctl binaries.
"
`);
});

test("test VERSION_MESSAGE", () => {
expect(constants.VERSION_MESSAGE).toMatchInlineSnapshot(
'"testAppName version testVersion"'
);
});
Loading