move code to assert connection to blockchain module

This commit is contained in:
Iuri Matias 2018-05-20 09:08:03 -04:00 committed by Jonathan Rainville
parent 0ec4698e6b
commit 000d7beb27
3 changed files with 53 additions and 38 deletions

View File

@ -6,6 +6,7 @@ class Blockchain {
this.logger = options.logger;
this.events = options.events;
this.contractsConfig = options.contractsConfig;
this.blockchainConfig = options.blockchainConfig;
this.web3 = options.web3;
this.addCheck = options.addCheck;
@ -119,6 +120,42 @@ class Blockchain {
}).on('error', cb);
}
assertNodeConnection(cb) {
const self = this;
if (!self.web3) {
return cb(Error("no web3 instance found"));
}
if (self.web3.currentProvider === undefined) {
self.logger.error(("Couldn't connect to an Ethereum node are you sure it's on?").red);
self.logger.info("make sure you have an Ethereum node or simulator running. e.g 'embark blockchain'".magenta);
return cb(Error("error connecting to blockchain node"));
}
self.getAccounts(function(err, _accounts) {
if (err) {
self.logger.error(("Couldn't connect to an Ethereum node are you sure it's on?").red);
self.logger.info("make sure you have an Ethereum node or simulator running. e.g 'embark blockchain'".magenta);
return cb(Error("error connecting to blockchain node"));
}
return cb();
});
}
determineDefaultAccount(cb) {
const self = this;
self.getAccounts(function (err, accounts) {
if (err) {
self.logger.error(err);
return cb(new Error(err));
}
let accountConfig = self.blockchainConfig.account;
let selectedAccount = accountConfig && accountConfig.address;
self.setDefaultAccount(selectedAccount || accounts[0]);
cb();
});
}
}
module.exports = Blockchain;

View File

@ -12,7 +12,6 @@ class DeployManager {
this.events = options.events;
this.plugins = options.plugins;
this.blockchain = options.blockchain;
this.web3 = this.blockchain.web3;
this.chainConfig = (options.trackContracts !== false) ? this.config.chainTracker : false;
this.contractsManager = options.contractsManager;
this.gasLimit = false;
@ -43,43 +42,21 @@ class DeployManager {
return callback(null, contractsManager);
},
// TODO: remove this
// instead it can make a request to check if it's connected to a node
function checkWeb3IsConnected(contractsManager, callback) {
if (!self.web3) {
return callback(Error("no web3 instance found"));
}
if (self.web3.currentProvider === undefined) {
self.logger.error(__("Couldn't connect to an Ethereum node are you sure it's on?").red);
self.logger.info(__("make sure you have an Ethereum node or simulator running. e.g '%s'", 'embark blockchain').magenta);
return callback(Error("error connecting to blockchain node"));
}
self.blockchain.getAccounts(function(err, _accounts) {
if (err) {
self.logger.error(__("Couldn't connect to an Ethereum node are you sure it's on?").red);
self.logger.info(__("make sure you have an Ethereum node or simulator running. e.g '%s'", 'embark blockchain').magenta);
return callback(Error("error connecting to blockchain node"));
}
return callback(null, contractsManager, self.web3);
});
},
function setDefaultAccount(contractsManager, web3, callback) {
self.blockchain.getAccounts(function (err, accounts) {
if (err) {
self.logger.error(err);
return callback(new Error(err));
}
let accountConfig = self.config.blockchainConfig.account;
let selectedAccount = accountConfig && accountConfig.address;
self.blockchain.setDefaultAccount(selectedAccount || accounts[0]);
callback(null, contractsManager, web3);
// TODO: could be implemented as an event (beforeDeployAll)
function checkIsConnectedToBlockchain(contractsManager, callback) {
self.blockchain.assertNodeConnection((err) => {
callback(err, contractsManager);
});
},
function deployAllContracts(contractsManager, web3, callback) {
// TODO: this can be done on the fly or as part of the initialization
function determineDefaultAccount(contractsManager, callback) {
self.blockchain.determineDefaultAccount((err) => {
callback(err, contractsManager);
});
},
function deployAllContracts(contractsManager, callback) {
let deploy = new Deploy({
blockchain: self.blockchain,
contractsManager: contractsManager,
@ -98,10 +75,10 @@ class DeployManager {
if (err && self.fatalErrors) {
return callback(err);
}
callback(null, contractsManager, web3);
callback(null, contractsManager);
});
},
function runAfterDeployCommands(contractsManager, web3, callback) {
function runAfterDeployCommands(contractsManager, callback) {
// TODO: should instead emit a afterDeploy event and/or run a afterDeploy plugin
let afterDeployCmds = self.config.contractsConfig.afterDeploy || [];
@ -142,7 +119,7 @@ class DeployManager {
for(let cmd of onDeployCode) {
self.logger.info(__("executing") + ": " + cmd);
try {
RunCode.doEval(cmd, {web3: web3});
RunCode.doEval(cmd, {web3: self.blockchain.web3});
} catch(e) {
if (e.message.indexOf("invalid opcode") >= 0) {
self.logger.error(__('the transaction was rejected; this usually happens due to a throw or a require, it can also happen due to an invalid operation'));

View File

@ -286,6 +286,7 @@ class Engine {
web3Service(options) {
this.blockchain = new Blockchain({
contractsConfig: this.config.contractsConfig,
blockchainConfig: this.config.blockchainConfig,
addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor),
events: this.events,
logger: this.logger,