From 0639717d0c82390cb5eec247570c0ad5aa032a15 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Tue, 26 Feb 2019 14:09:47 -0500 Subject: [PATCH] fix(blockchain): check if node is synched after connection --- .../lib/modules/blockchain_connector/index.js | 50 ++++++++++++------- .../modules/deployment/contract_deployer.js | 5 +- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/packages/embark/src/lib/modules/blockchain_connector/index.js b/packages/embark/src/lib/modules/blockchain_connector/index.js index e98e9fb72..554600cf5 100644 --- a/packages/embark/src/lib/modules/blockchain_connector/index.js +++ b/packages/embark/src/lib/modules/blockchain_connector/index.js @@ -174,23 +174,39 @@ class BlockchainConnector { if (err) { return self.logger.error(err); } - self.provider.startWeb3Provider(() => { - this.getNetworkId() - .then(id => { - let networkId = self.config.blockchainConfig.networkId; - if (!networkId && constants.blockchain.networkIds[self.config.blockchainConfig.networkType]) { - networkId = constants.blockchain.networkIds[self.config.blockchainConfig.networkType]; - } - if (networkId && id.toString() !== networkId.toString()) { - self.logger.warn(__('Connected to a blockchain node on network {{realId}} while your config specifies {{configId}}', {realId: id, configId: networkId})); - self.logger.warn(__('Make sure you started the right blockchain node')); - } - }) - .catch(console.error); - self.provider.fundAccounts(() => { - self._emitWeb3Ready(); - cb(); - }); + self.provider.startWeb3Provider(async () => { + try { + const blockNumber = await self.web3.eth.getBlockNumber(); + await self.web3.eth.getBlock(blockNumber); + self.provider.fundAccounts(() => { + self._emitWeb3Ready(); + cb(); + }); + } catch (e) { + const errorMessage = e.message || e; + if (errorMessage.indexOf('no suitable peers available') > 0) { + self.logger.warn(errorMessage); + self.logger.warn(__('Your node is probably not synchronized. Wait until your node is synchronized before deploying')); + process.exit(1); + } + self.logger.error(errorMessage); + cb(errorMessage); + } + + try { + const id = await this.getNetworkId(); + let networkId = self.config.blockchainConfig.networkId; + if (!networkId && + constants.blockchain.networkIds[self.config.blockchainConfig.networkType]) { + networkId = constants.blockchain.networkIds[self.config.blockchainConfig.networkType]; + } + if (networkId && id.toString() !== networkId.toString()) { + self.logger.warn(__('Connected to a blockchain node on network {{realId}} while your config specifies {{configId}}', {realId: id, configId: networkId})); + self.logger.warn(__('Make sure you started the right blockchain node')); + } + } catch (e) { + console.error(e); + } }); }); } diff --git a/packages/embark/src/lib/modules/deployment/contract_deployer.js b/packages/embark/src/lib/modules/deployment/contract_deployer.js index cda74eabc..0124cc9c3 100644 --- a/packages/embark/src/lib/modules/deployment/contract_deployer.js +++ b/packages/embark/src/lib/modules/deployment/contract_deployer.js @@ -185,7 +185,10 @@ class ContractDeployer { return self.deployContract(contract, next); } - self.blockchain.getCode(trackedContract.address, function(_getCodeErr, codeInChain) { + self.blockchain.getCode(trackedContract.address, function(getCodeErr, codeInChain) { + if (getCodeErr) { + return next(getCodeErr); + } if (codeInChain.length > 3 || skipBytecodeCheck) { // it is "0x" or "0x0" for empty code, depending on web3 version self.contractAlreadyDeployed(contract, trackedContract, next); } else {