From c83b6dde60213f77c6cd0b2ad62a3a269b6ee9fa Mon Sep 17 00:00:00 2001 From: emizzle Date: Fri, 22 Jun 2018 22:52:15 +1000 Subject: [PATCH] Handle geth process exit via crash/kill and also via killing `embark blockchain` First case - run `embark run` which starts a blockchain node, then manually kill the `geth` process. Would throw `{ [Error: connect ECONNREFUSED 127.0.0.1:8543] message: 'connect ECONNREFUSED 127.0.0.1:8543', code: -32603 }` error and ruins the dashboard. Second case, 1) run `embark blockchain` 2) run `embark run` 3) kill `embark blockchain` throws the error `{ [Error: connect ECONNREFUSED 127.0.0.1:8543] message: 'connect ECONNREFUSED 127.0.0.1:8543', code: -32603 }` and ruins the dashboard. The first case was solved by having the child blockchain process that spawns geth listen for geth exit, then kill itself. The second case required updating of `eth-block-tracker` to v4.0.1 inside of the `embark-web3-provider-engine`. v4.0.1 was a major version update and introduced breaking changes. Those changes were handled inside of `embark-web3-provider-engine`, covered in **blocker** PR https://github.com/jrainville/provider-engine/pull/1. --- lib/contracts/provider.js | 113 ++++++++++++++++++++++ lib/modules/blockchain_connector/index.js | 1 - 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 lib/contracts/provider.js diff --git a/lib/contracts/provider.js b/lib/contracts/provider.js new file mode 100644 index 00000000..7d9b088d --- /dev/null +++ b/lib/contracts/provider.js @@ -0,0 +1,113 @@ +const ProviderEngine = require('embark-web3-provider-engine'); +const RpcSubprovider = require('embark-web3-provider-engine/subproviders/rpc'); +const WsSubprovider = require('embark-web3-provider-engine/subproviders/websocket'); +const async = require('async'); +const AccountParser = require('./accountParser'); +const fundAccount = require('./fundAccount'); + +const NO_ACCOUNTS = 'noAccounts'; + +class Provider { + constructor(options) { + this.web3 = options.web3; + this.accountsConfig = options.accountsConfig; + this.blockchainConfig = options.blockchainConfig; + this.type = options.type; + this.web3Endpoint = options.web3Endpoint; + this.logger = options.logger; + this.isDev = options.isDev; + this.engine = new ProviderEngine(); + this.asyncMethods = {}; + } + + startWeb3Provider(callback) { + const self = this; + + if (this.type === 'rpc') { + self.engine.addProvider(new RpcSubprovider({ + rpcUrl: self.web3Endpoint + })); + } else if (this.type === 'ws') { + self.engine.addProvider(new WsSubprovider({ + rpcUrl: self.web3Endpoint, + origin: this.blockchainConfig.wsOrigins.split(',')[0] + })); + } else { + return callback(__("contracts config error: unknown deployment type %s", this.type)); + } + + + // network connectivity error + self.engine.on('error', (err) => { + // report connectivity errors + self.logger.error(err); + }); + + self.engine.start(); + self.web3.setProvider(self); + + self.accounts = AccountParser.parseAccountsConfig(self.accountsConfig, self.web3, self.logger); + self.addresses = []; + async.waterfall([ + function populateWeb3Wallet(next) { + if (!self.accounts.length) { + return next(NO_ACCOUNTS); + } + self.accounts.forEach(account => { + self.addresses.push(account.address); + self.web3.eth.accounts.wallet.add(account); + }); + self.asyncMethods = { + eth_accounts: self.eth_accounts.bind(self) + }; + next(); + } + ], function (err) { + if (err && err !== NO_ACCOUNTS) { + self.logger.error((err)); + } + callback(); + }); + } + + fundAccounts(callback) { + const self = this; + if (!self.accounts.length) { + return callback(); + } + if (!self.isDev) { + return callback(); + } + async.each(self.accounts, (account, eachCb) => { + fundAccount(self.web3, account.address, account.hexBalance, eachCb); + }, callback); + } + + stop() { + this.engine.stop(); + } + + eth_accounts(payload, cb) { + return cb(null, this.addresses); + } + + sendAsync(payload, callback) { + let method = this.asyncMethods[payload.method]; + if (method) { + return method.call(method, payload, (err, result) => { + if (err) { + return callback(err); + } + let response = {'id': payload.id, 'jsonrpc': '2.0', 'result': result}; + callback(null, response); + }); + } + this.engine.sendAsync.apply(this.engine, arguments); + } + + send() { + return this.engine.send.apply(this.engine, arguments); + } +} + +module.exports = Provider; diff --git a/lib/modules/blockchain_connector/index.js b/lib/modules/blockchain_connector/index.js index 233117c3..82b45151 100644 --- a/lib/modules/blockchain_connector/index.js +++ b/lib/modules/blockchain_connector/index.js @@ -380,4 +380,3 @@ class BlockchainConnector { BlockchainConnector.ACCEPTED_TYPES = ['rpc', 'ws', 'vm']; module.exports = BlockchainConnector; -