From bcd55930404356a5d7f35bcb239aa82aa8f7786e Mon Sep 17 00:00:00 2001 From: emizzle Date: Tue, 26 Jun 2018 12:59:14 +1000 Subject: [PATCH] Updated online/offline event checks so they are run during initialisation. Changed the online event to `once` and set it to be bound every time the node goes offline. The above changes handle the case where: 1) `embark run` runs and starts geth. 2) geth is killed manually 3) `embark blockchain` is run in separate process to restart geth 4) the `embark run` process detects this change and restarts the web3 provider and recompiles/deploys/builds Every time `embark blochain` is restarted, an error is appended and all are emitted from the `eth-block-tracker`. This is a bug but can't figure out where it originates. The downside is that if, for example, `embark blockchain` is restarted 4 times, there will be 4 errors emitted from the `eth-block-tracker`. Because of this, errors emitted from `eth-block-tracker` have been reduced to trace to avoid clogging the logs. --- lib/cmds/blockchain/blockchain.js | 2 +- lib/contracts/blockchain.js | 22 ++++++++++++++-------- lib/contracts/provider.js | 8 ++++++-- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/cmds/blockchain/blockchain.js b/lib/cmds/blockchain/blockchain.js index fa8f88a9..bc693463 100644 --- a/lib/cmds/blockchain/blockchain.js +++ b/lib/cmds/blockchain/blockchain.js @@ -183,7 +183,7 @@ Blockchain.prototype.run = function() { console.log('Geth: ' + data); }); self.child.on('exit', (code) => { - let strCode = ''; + let strCode; if (code) { strCode = ' with error code ' + code; } else { diff --git a/lib/contracts/blockchain.js b/lib/contracts/blockchain.js index 0d61b20e..df4a247b 100644 --- a/lib/contracts/blockchain.js +++ b/lib/contracts/blockchain.js @@ -73,6 +73,20 @@ class Blockchain { if (!err) { self.isWeb3Ready = true; self.events.emit(WEB3_READY); + // if the ethereum node goes offline, we need a check to ensure + // the provider is also stopped + self.events.on('check:wentOffline:Ethereum', () => { + self.logger.trace('Ethereum went offline: stopping web3 provider...'); + self.provider.stop(); + + // once the node goes back online, we can restart the provider + self.events.once('check:backOnline:Ethereum', () => { + self.logger.trace('Ethereum back online: starting web3 provider...'); + self.provider.startWeb3Provider(() => { + self.logger.trace('web3 provider restarted after ethereum node came back online'); + }); + }); + }); return next(); } self.web3StartedInProcess = true; @@ -161,14 +175,6 @@ class Blockchain { if (err && err !== NO_NODE) { return cb(err); } - else if ((statusObj && statusObj.status === 'off') || err === NO_NODE){ - self.provider.stop(); - self.events.on('check:backOnline:Ethereum', () => { - self.provider.startWeb3Provider(() => { - self.logger.trace('web3 provider restarted after ethereum node came back online'); - }); - }); - } cb(statusObj); }); }, 5000, 'off'); diff --git a/lib/contracts/provider.js b/lib/contracts/provider.js index c4981a1b..d35b5c89 100644 --- a/lib/contracts/provider.js +++ b/lib/contracts/provider.js @@ -39,8 +39,12 @@ class Provider { // network connectivity error self.engine.on('error', (err) => { - // report connectivity errors - self.logger.error(err); + // report connectivity errors as trace due to polling + self.logger.trace('web3 provider error: ', err); + self.logger.trace('stopping web3 provider due to error'); + + // prevent continuous polling errors + self.engine.stop(); }); self.engine.start();