refactor(@embark/blockchain): Move check for blockchain started

The blockchain module should not contain any ethereum-specific code, and currently it contains a check to see if the blockchain has already been started.

This PR moves this check to blockchain client (ie geth or parity). This check function is registered along with the started callback.

style(@embark/stack/blockchain): add missing semicolon
This commit is contained in:
emizzle 2019-09-11 12:25:33 +10:00 committed by Pascal Precht
parent 0d20cb5d86
commit 7482107374
3 changed files with 98 additions and 87 deletions

View File

@ -23,6 +23,11 @@ class Geth {
}
this.events.request("blockchain:node:register", constants.blockchain.clients.geth, {
isStartedFn: (isStartedCb) => {
this._doCheck((state) => {
return isStartedCb(null, state.status === "on");
});
},
launchFn: (readyCb) => {
this.events.request('processes:register', 'blockchain', {
launchFn: (cb) => {
@ -81,14 +86,22 @@ class Geth {
return cb({ name, status: 'on' });
}
_doCheck(cb) {
const { rpcHost, rpcPort, wsRPC, wsHost, wsPort } = this.blockchainConfig;
if (wsRPC) {
return ws(wsHost, wsPort, (err, version) => this._getNodeState(err, version, cb));
}
rpc(rpcHost, rpcPort, (err, version) => this._getNodeState(err, version, cb));
}
// TODO: need to get correct port taking into account the proxy
registerServiceCheck() {
this.events.request("services:register", 'Ethereum', (cb) => {
const { rpcHost, rpcPort, wsRPC, wsHost, wsPort } = this.blockchainConfig;
if (wsRPC) {
return ws(wsHost, wsPort + 10, (err, version) => this._getNodeState(err, version, cb));
return ws(wsHost, wsPort, (err, version) => this._getNodeState(err, version, cb));
}
rpc(rpcHost, rpcPort + 10, (err, version) => this._getNodeState(err, version, cb));
rpc(rpcHost, rpcPort, (err, version) => this._getNodeState(err, version, cb));
}, 5000, 'off');
}

View File

@ -24,10 +24,14 @@ class Parity {
}
this.events.request("blockchain:node:register", constants.blockchain.clients.parity, {
isStartedFn: (isStartedCb) => {
this._doCheck((state) => {
return isStartedCb(null, state.status === "on");
});
},
launchFn: (readyCb) => {
this.events.request('processes:register', 'blockchain', {
launchFn: (cb) => {
// this.startBlockchainNode(readyCb);
this.startBlockchainNode(cb);
},
stopFn: (cb) => {
@ -65,15 +69,17 @@ class Parity {
return cb({ name, status: 'on' });
}
// TODO: need to get correct port taking into account the proxy
registerServiceCheck() {
this.events.request("services:register", 'Ethereum', (cb) => {
_doCheck(cb) {
const { rpcHost, rpcPort, wsRPC, wsHost, wsPort } = this.blockchainConfig;
if (wsRPC) {
return ws(wsHost, wsPort, (err, version) => this._getNodeState(err, version, cb));
}
rpc(rpcHost, rpcPort, (err, version) => this._getNodeState(err, version, cb));
}, 5000, 'off');
}
// TODO: need to get correct port taking into account the proxy
registerServiceCheck() {
this.events.request("services:register", 'Ethereum', this._doCheck.bind(this), 5000, 'off');
}
startBlockchainNode(callback) {

View File

@ -1,7 +1,6 @@
import async from 'async';
const { __ } = require('embark-i18n');
const constants = require('embark-core/constants');
const Web3RequestManager = require('web3-core-requestmanager');
import BlockchainAPI from "./api";
class Blockchain {
@ -22,57 +21,50 @@ class Blockchain {
embark.registerActionForEvent("pipeline:generateAll:before", this.addArtifactFile.bind(this));
this.blockchainNodes = {};
this.events.setCommandHandler("blockchain:node:register", (clientName, startCb) => {
this.blockchainNodes[clientName] = startCb;
this.events.setCommandHandler("blockchain:node:register", (clientName, { isStartedFn, launchFn, stopFn }) => {
if (!isStartedFn) {
throw new Error(`Blockchain client '${clientName}' must be registered with an 'isStarted' function, client not registered.`);
}
if (!launchFn) {
throw new Error(`Blockchain client '${clientName}' must be registered with a 'launchFn' function, client not registered.`);
}
if (!stopFn) {
throw new Error(`Blockchain client '${clientName}' must be registered with a 'stopFn' function, client not registered.`);
}
this.blockchainNodes[clientName] = { isStartedFn, launchFn, stopFn };
});
this.events.setCommandHandler("blockchain:node:start", async (initialBlockchainConfig, cb) => {
this.plugins.emitAndRunActionsForEvent("blockchain:config:modify", initialBlockchainConfig, (err, blockchainConfig) => {
if (err) {
this.logger.error(__('Error getting modified blockchain config: %s', err.message || err));
blockchainConfig = initialBlockchainConfig;
}
const self = this;
this.events.setCommandHandler("blockchain:node:start", (blockchainConfig, cb) => {
const clientName = blockchainConfig.client;
function started() {
self.startedClient = clientName;
self.events.emit("blockchain:started", clientName);
}
const started = () => {
this.startedClient = clientName;
this.events.emit("blockchain:started", clientName);
};
if (clientName === constants.blockchain.vm) {
started();
return cb();
}
const requestManager = new Web3RequestManager.Manager(blockchainConfig.endpoint);
const ogConsoleError = console.error;
// TODO remove this once we update to web3 2.0
// TODO in web3 1.0, it console.errors "connection not open on send()" even if we catch the error
console.error = (...args) => {
if (args[0].indexOf('connection not open on send()') > -1) {
return;
const client = this.blockchainNodes[clientName];
if (!client) return cb(`Blockchain client '${clientName}' not found, please register this node using 'blockchain:node:register'.`);
// check if we should should start
client.isStartedFn.call(client, (err, isStarted) => {
if (err) {
return cb(err);
}
ogConsoleError(...args);
};
requestManager.send({method: 'eth_accounts'}, (err, _accounts) => {
console.error = ogConsoleError;
if (!err) {
// Node is already started
if (isStarted) {
// Node may already be started
started();
return cb(null, true);
}
const clientFunctions = this.blockchainNodes[clientName];
if (!clientFunctions) {
return cb(__("Client %s not found in registered plugins", clientName));
}
let onStart = () => {
// start node
client.launchFn.call(client, () => {
started();
cb();
};
this.startedClient = clientName;
clientFunctions.launchFn.apply(clientFunctions, [onStart]);
});
});
});