2018-11-30 17:33:29 -08:00
|
|
|
// @flow
|
|
|
|
import cp from 'child_process';
|
|
|
|
import path from 'path';
|
2018-12-04 11:13:16 -08:00
|
|
|
import os from 'os';
|
2018-11-30 17:33:29 -08:00
|
|
|
import processExists from 'process-exists';
|
2018-12-05 08:34:49 -08:00
|
|
|
/* eslint-disable import/no-extraneous-dependencies */
|
2018-11-30 21:21:12 -08:00
|
|
|
import isDev from 'electron-is-dev';
|
2018-11-30 17:33:29 -08:00
|
|
|
import type { ChildProcess } from 'child_process';
|
2018-12-03 18:34:41 -08:00
|
|
|
import eres from 'eres';
|
2018-12-05 08:34:49 -08:00
|
|
|
/* eslint-disable-next-line import/named */
|
|
|
|
import { mainWindow } from '../electron';
|
2018-11-30 17:33:29 -08:00
|
|
|
|
|
|
|
import getBinariesPath from './get-binaries-path';
|
2018-11-30 21:21:12 -08:00
|
|
|
import getOsFolder from './get-os-folder';
|
2018-11-30 17:33:29 -08:00
|
|
|
import getDaemonName from './get-daemon-name';
|
|
|
|
import fetchParams from './run-fetch-params';
|
|
|
|
import log from './logger';
|
2018-12-05 11:43:47 -08:00
|
|
|
import store from '../electron-store';
|
2018-12-15 13:26:10 -08:00
|
|
|
import generateRandomString from '../../app/utils/generate-random-string';
|
2018-11-30 17:33:29 -08:00
|
|
|
|
2018-12-05 11:43:47 -08:00
|
|
|
const getDaemonOptions = ({ username, password }) => {
|
2018-12-04 08:29:16 -08:00
|
|
|
/*
|
|
|
|
-showmetrics
|
|
|
|
Show metrics on stdout
|
|
|
|
-metricsui
|
|
|
|
Set to 1 for a persistent metrics screen, 0 for sequential metrics
|
|
|
|
output
|
|
|
|
-metricsrefreshtime
|
|
|
|
Number of seconds between metrics refreshes
|
|
|
|
*/
|
2018-12-05 11:43:47 -08:00
|
|
|
const defaultOptions = [
|
|
|
|
'-showmetrics',
|
|
|
|
'--metricsui=0',
|
|
|
|
'-metricsrefreshtime=3',
|
|
|
|
`-rpcuser=${username}`,
|
|
|
|
`-rpcpassword=${password}`,
|
|
|
|
];
|
2018-12-15 07:10:39 -08:00
|
|
|
return isDev
|
|
|
|
? defaultOptions.concat(['-testnet', '-addnode=testnet.z.cash'])
|
|
|
|
: defaultOptions;
|
2018-12-04 08:29:16 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
let resolved = false;
|
2018-11-30 21:21:12 -08:00
|
|
|
|
2018-12-15 07:10:39 -08:00
|
|
|
// eslint-disable-next-line
|
2018-12-03 18:34:41 -08:00
|
|
|
const runDaemon: () => Promise<?ChildProcess> = () => new Promise(async (resolve, reject) => {
|
2018-12-15 07:10:39 -08:00
|
|
|
const processName = path.join(
|
|
|
|
getBinariesPath(),
|
|
|
|
getOsFolder(),
|
|
|
|
getDaemonName(),
|
|
|
|
);
|
2018-11-30 17:33:29 -08:00
|
|
|
|
2018-12-03 18:34:41 -08:00
|
|
|
const [err] = await eres(fetchParams());
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
log('Something went wrong fetching params: ', err);
|
|
|
|
return reject(new Error(err));
|
|
|
|
}
|
|
|
|
|
|
|
|
log('Fetch Params finished!');
|
|
|
|
|
|
|
|
const [, isRunning] = await eres(processExists(processName));
|
|
|
|
|
|
|
|
if (isRunning) {
|
|
|
|
log('Already is running!');
|
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
|
2018-12-05 11:43:47 -08:00
|
|
|
const hasCredentials = store.has('rpcuser') && store.has('rpcpassword');
|
|
|
|
|
|
|
|
const rpcCredentials = hasCredentials
|
|
|
|
? {
|
|
|
|
username: store.get('rpcuser'),
|
|
|
|
password: store.get('rpcpassword'),
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
username: generateRandomString(),
|
|
|
|
password: generateRandomString(),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!hasCredentials) {
|
|
|
|
store.set('rpcuser', rpcCredentials.username);
|
|
|
|
store.set('rpcpassword', rpcCredentials.password);
|
|
|
|
}
|
|
|
|
|
2018-12-15 07:10:39 -08:00
|
|
|
const childProcess = cp.spawn(
|
|
|
|
processName,
|
|
|
|
getDaemonOptions(rpcCredentials),
|
|
|
|
{
|
|
|
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
|
|
},
|
|
|
|
);
|
2018-12-03 18:34:41 -08:00
|
|
|
|
2018-12-05 08:34:49 -08:00
|
|
|
childProcess.stdout.on('data', (data) => {
|
|
|
|
if (mainWindow) mainWindow.webContents.send('zcashd-log', data.toString());
|
2018-12-04 08:29:16 -08:00
|
|
|
if (!resolved) {
|
|
|
|
resolve(childProcess);
|
|
|
|
resolved = true;
|
|
|
|
}
|
2018-12-03 18:34:41 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
childProcess.stderr.on('data', (data) => {
|
|
|
|
log(data.toString());
|
|
|
|
reject(new Error(data.toString()));
|
|
|
|
});
|
|
|
|
|
|
|
|
childProcess.on('error', reject);
|
2018-12-04 11:13:16 -08:00
|
|
|
|
|
|
|
if (os.platform() === 'win32') {
|
|
|
|
resolved = true;
|
|
|
|
resolve(childProcess);
|
|
|
|
}
|
2018-11-30 17:33:29 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
export default runDaemon;
|