move code to assert connection to blockchain module
This commit is contained in:
parent
0ec4698e6b
commit
000d7beb27
|
@ -6,6 +6,7 @@ class Blockchain {
|
||||||
this.logger = options.logger;
|
this.logger = options.logger;
|
||||||
this.events = options.events;
|
this.events = options.events;
|
||||||
this.contractsConfig = options.contractsConfig;
|
this.contractsConfig = options.contractsConfig;
|
||||||
|
this.blockchainConfig = options.blockchainConfig;
|
||||||
this.web3 = options.web3;
|
this.web3 = options.web3;
|
||||||
this.addCheck = options.addCheck;
|
this.addCheck = options.addCheck;
|
||||||
|
|
||||||
|
@ -119,6 +120,42 @@ class Blockchain {
|
||||||
}).on('error', cb);
|
}).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;
|
module.exports = Blockchain;
|
||||||
|
|
|
@ -12,7 +12,6 @@ class DeployManager {
|
||||||
this.events = options.events;
|
this.events = options.events;
|
||||||
this.plugins = options.plugins;
|
this.plugins = options.plugins;
|
||||||
this.blockchain = options.blockchain;
|
this.blockchain = options.blockchain;
|
||||||
this.web3 = this.blockchain.web3;
|
|
||||||
this.chainConfig = (options.trackContracts !== false) ? this.config.chainTracker : false;
|
this.chainConfig = (options.trackContracts !== false) ? this.config.chainTracker : false;
|
||||||
this.contractsManager = options.contractsManager;
|
this.contractsManager = options.contractsManager;
|
||||||
this.gasLimit = false;
|
this.gasLimit = false;
|
||||||
|
@ -43,43 +42,21 @@ class DeployManager {
|
||||||
return callback(null, contractsManager);
|
return callback(null, contractsManager);
|
||||||
},
|
},
|
||||||
|
|
||||||
// TODO: remove this
|
// TODO: could be implemented as an event (beforeDeployAll)
|
||||||
// instead it can make a request to check if it's connected to a node
|
function checkIsConnectedToBlockchain(contractsManager, callback) {
|
||||||
|
self.blockchain.assertNodeConnection((err) => {
|
||||||
function checkWeb3IsConnected(contractsManager, callback) {
|
callback(err, contractsManager);
|
||||||
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);
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
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({
|
let deploy = new Deploy({
|
||||||
blockchain: self.blockchain,
|
blockchain: self.blockchain,
|
||||||
contractsManager: contractsManager,
|
contractsManager: contractsManager,
|
||||||
|
@ -98,10 +75,10 @@ class DeployManager {
|
||||||
if (err && self.fatalErrors) {
|
if (err && self.fatalErrors) {
|
||||||
return callback(err);
|
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
|
// TODO: should instead emit a afterDeploy event and/or run a afterDeploy plugin
|
||||||
let afterDeployCmds = self.config.contractsConfig.afterDeploy || [];
|
let afterDeployCmds = self.config.contractsConfig.afterDeploy || [];
|
||||||
|
|
||||||
|
@ -142,7 +119,7 @@ class DeployManager {
|
||||||
for(let cmd of onDeployCode) {
|
for(let cmd of onDeployCode) {
|
||||||
self.logger.info(__("executing") + ": " + cmd);
|
self.logger.info(__("executing") + ": " + cmd);
|
||||||
try {
|
try {
|
||||||
RunCode.doEval(cmd, {web3: web3});
|
RunCode.doEval(cmd, {web3: self.blockchain.web3});
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
if (e.message.indexOf("invalid opcode") >= 0) {
|
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'));
|
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'));
|
||||||
|
|
|
@ -286,6 +286,7 @@ class Engine {
|
||||||
web3Service(options) {
|
web3Service(options) {
|
||||||
this.blockchain = new Blockchain({
|
this.blockchain = new Blockchain({
|
||||||
contractsConfig: this.config.contractsConfig,
|
contractsConfig: this.config.contractsConfig,
|
||||||
|
blockchainConfig: this.config.blockchainConfig,
|
||||||
addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor),
|
addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor),
|
||||||
events: this.events,
|
events: this.events,
|
||||||
logger: this.logger,
|
logger: this.logger,
|
||||||
|
|
Loading…
Reference in New Issue