Newer
Older
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
Luke Schoen
committed
// This file is part of Parity.
//
// SPDX-License-Identifier: BSD-3-Clause
import { isParityRunning, runParity } from '@parity/electron';
Luke Schoen
committed
import getRemainingArgs from 'commander-remaining-args';
import { bundledParityPath } from '../utils/paths';
Luke Schoen
committed
import handleError from '../utils/handleError';
import cli from '../cli';
import Pino from '../utils/pino';
Luke Schoen
committed
const pino = Pino();
let hasCalledInitParityEthereum = false;
class ParityEthereum {
constructor (fetherAppWindow) {
Luke Schoen
committed
if (hasCalledInitParityEthereum) {
throw new Error('Unable to initialise Parity Ethereum more than once');
}
/*
* - If an instance of Parity Ethereum is already running, we connect to it
* and then check in fether-react if the parity_versionInfo RPC returns
* a compatible version; otherwise, we error out.
* - If no instance of Parity Ethereum is running, we run the bundled Parity
* Ethereum binary.
*
* `parity signer new-token` is run on the bundled binary in any case. We
* don't use the $PATH anymore.
*/
Luke Schoen
committed
// Run the bundled Parity Ethereum if needed and wanted
return new Promise(async (resolve, reject) => {
// Parity Ethereum is already running: don't run the bundled binary
if (await this.isRunning()) {
resolve(true);
return;
}
Luke Schoen
committed
// User ran Fether with --no-run-parity: don't run the bundled binary
if (!cli.runParity) {
resolve(false);
return;
}
// Parity Ethereum isn't running: run the bundled binary
await this.run();
pino.info('Running Parity Ethereum');
resolve(true);
})
Luke Schoen
committed
// Notify the renderers
fetherAppWindow.webContents.send('parity-running', isRunning);
global.isParityRunning = isRunning; // Send this variable to renderers via IPC
Luke Schoen
committed
})
.catch(handleError);
Luke Schoen
committed
isRunning = async () => {
return isParityRunning({
wsInterface: cli.wsInterface,
wsPort: cli.wsPort
});
};
Luke Schoen
committed
run = async () => {
return runParity({
Luke Schoen
committed
flags: [
...getRemainingArgs(cli),
'--light',
'--chain',
cli.chain,
'--ws-interface',
cli.wsInterface,
'--ws-port',
cli.wsPort
],
onParityError: err =>
handleError(err, 'An error occured with Parity Ethereum.')
});
};
}
export default ParityEthereum;