feat(zcashd): add -conf if default -datadir is set

This commit is contained in:
George Lima 2019-04-08 13:24:37 -03:00
parent a7cbf682ec
commit 6883c8e0cb
2 changed files with 34 additions and 9 deletions

View File

@ -29,10 +29,13 @@ type ZcashConfFile = {
genproclimit: ?string, genproclimit: ?string,
keypool: ?string, keypool: ?string,
paytxfee: ?string, paytxfee: ?string,
datadir?: string,
conf?: string,
}; };
export const parseZcashConf = (): Promise<ZcashConfFile> => new Promise((resolve, reject) => { // eslint-disable-next-line
fs.readFile(locateZcashConf(), (err, file) => { export const parseZcashConf = (customDir: ?string): Promise<ZcashConfFile> => new Promise((resolve, reject) => {
fs.readFile(customDir || locateZcashConf(), (err, file) => {
if (err) return reject(err); if (err) return reject(err);
const fileString = file.toString(); const fileString = file.toString();

View File

@ -3,6 +3,7 @@
import cp from 'child_process'; import cp from 'child_process';
import path from 'path'; import path from 'path';
import os from 'os'; import os from 'os';
import fs from 'fs';
import processExists from 'process-exists'; import processExists from 'process-exists';
/* eslint-disable import/no-extraneous-dependencies */ /* eslint-disable import/no-extraneous-dependencies */
import isDev from 'electron-is-dev'; import isDev from 'electron-is-dev';
@ -18,6 +19,7 @@ import getBinariesPath from './get-binaries-path';
import getOsFolder from './get-os-folder'; import getOsFolder from './get-os-folder';
import getDaemonName from './get-daemon-name'; import getDaemonName from './get-daemon-name';
import fetchParams from './run-fetch-params'; import fetchParams from './run-fetch-params';
import { locateZcashConf } from './locate-zcash-conf';
import log from './logger'; import log from './logger';
import store from '../electron-store'; import store from '../electron-store';
import { parseZcashConf, parseCmdArgs, generateArgsFromConf } from './parse-zcash-conf'; import { parseZcashConf, parseCmdArgs, generateArgsFromConf } from './parse-zcash-conf';
@ -29,7 +31,9 @@ import {
MAINNET, MAINNET,
} from '../../app/constants/zcash-network'; } from '../../app/constants/zcash-network';
const getDaemonOptions = ({ username, password, optionsFromZcashConf }) => { const getDaemonOptions = ({
username, password, useDefaultZcashConf, optionsFromZcashConf,
}) => {
/* /*
-showmetrics -showmetrics
Show metrics on stdout Show metrics on stdout
@ -41,6 +45,8 @@ const getDaemonOptions = ({ username, password, optionsFromZcashConf }) => {
*/ */
const defaultOptions = [ const defaultOptions = [
'-server=1',
'-rest=1',
'-showmetrics', '-showmetrics',
'--metricsui=0', '--metricsui=0',
'-metricsrefreshtime=1', '-metricsrefreshtime=1',
@ -51,6 +57,8 @@ const getDaemonOptions = ({ username, password, optionsFromZcashConf }) => {
...optionsFromZcashConf, ...optionsFromZcashConf,
]; ];
if (useDefaultZcashConf) defaultOptions.push(`-conf=${locateZcashConf()}`);
return Array.from(new Set([...defaultOptions, ...optionsFromZcashConf])); return Array.from(new Set([...defaultOptions, ...optionsFromZcashConf]));
}; };
@ -85,7 +93,23 @@ const runDaemon: () => Promise<?ChildProcess> = () => new Promise(async (resolve
const [, isRunning] = await eres(processExists(ZCASHD_PROCESS_NAME)); const [, isRunning] = await eres(processExists(ZCASHD_PROCESS_NAME));
// This will parse and save rpcuser and rpcpassword in the store // This will parse and save rpcuser and rpcpassword in the store
const [, optionsFromZcashConf] = await eres(parseZcashConf()); let [, optionsFromZcashConf] = await eres(parseZcashConf());
// if the user has a custom datadir and doesn't have a zcash.conf in that folder,
// we need to use the default zcash.conf
let useDefaultZcashConf = false;
if (optionsFromZcashConf.datadir) {
const hasDatadirConf = fs.existsSync(path.join(optionsFromZcashConf.datadir, 'zcash.conf'));
if (hasDatadirConf) {
optionsFromZcashConf = await parseZcashConf(
path.join(String(optionsFromZcashConf.datadir), 'zcash.conf'),
);
} else {
useDefaultZcashConf = true;
}
}
if (optionsFromZcashConf.rpcuser) store.set('rpcuser', optionsFromZcashConf.rpcuser); if (optionsFromZcashConf.rpcuser) store.set('rpcuser', optionsFromZcashConf.rpcuser);
if (optionsFromZcashConf.rpcpassword) store.set('rpcpassword', optionsFromZcashConf.rpcpassword); if (optionsFromZcashConf.rpcpassword) store.set('rpcpassword', optionsFromZcashConf.rpcpassword);
@ -113,10 +137,7 @@ const runDaemon: () => Promise<?ChildProcess> = () => new Promise(async (resolve
store.set(EMBEDDED_DAEMON, true); store.set(EMBEDDED_DAEMON, true);
// Default to mainnet store.set(ZCASH_NETWORK, optionsFromZcashConf.testnet === '1' ? TESTNET : MAINNET);
if (!store.get(ZCASH_NETWORK)) {
store.set(ZCASH_NETWORK, MAINNET);
}
if (!optionsFromZcashConf.rpcuser) store.set('rpcuser', uuid()); if (!optionsFromZcashConf.rpcuser) store.set('rpcuser', uuid());
if (!optionsFromZcashConf.rpcpassword) store.set('rpcpassword', uuid()); if (!optionsFromZcashConf.rpcpassword) store.set('rpcpassword', uuid());
@ -130,8 +151,9 @@ const runDaemon: () => Promise<?ChildProcess> = () => new Promise(async (resolve
const childProcess = cp.spawn( const childProcess = cp.spawn(
processName, processName,
await getDaemonOptions({ getDaemonOptions({
...rpcCredentials, ...rpcCredentials,
useDefaultZcashConf,
optionsFromZcashConf: generateArgsFromConf(optionsFromZcashConf), optionsFromZcashConf: generateArgsFromConf(optionsFromZcashConf),
}), }),
{ {