refactor to not use the blockchain_connector directly

This commit is contained in:
Jonathan Rainville 2018-10-12 11:23:12 -04:00 committed by Pascal Precht
parent e450c0484a
commit 75d9998c57
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
3 changed files with 38 additions and 36 deletions

View File

@ -554,7 +554,7 @@ class EmbarkController {
function startServices(callback) { function startServices(callback) {
engine.startService("processManager"); engine.startService("processManager");
engine.startService("libraryManager"); engine.startService("libraryManager");
engine.startService("web3", {wait: true}); // Empty web3 as Test changes it engine.startService("web3", {wait: true});
engine.startService("deployment"); engine.startService("deployment");
engine.startService("codeGenerator"); engine.startService("codeGenerator");
engine.startService("codeRunner"); engine.startService("codeRunner");

View File

@ -14,8 +14,7 @@ class BlockchainConnector {
this.plugins = options.plugins; this.plugins = options.plugins;
this.logger = embark.logger; this.logger = embark.logger;
this.events = embark.events; this.events = embark.events;
this.contractsConfig = embark.config.contractsConfig; this.config = embark.config;
this.blockchainConfig = embark.config.blockchainConfig;
this.web3 = options.web3; this.web3 = options.web3;
this.isDev = options.isDev; this.isDev = options.isDev;
this.web3Endpoint = ''; this.web3Endpoint = '';
@ -55,12 +54,12 @@ class BlockchainConnector {
const self = this; const self = this;
this.web3 = new Web3(); this.web3 = new Web3();
// TODO find a better way
if (self.wait) { if (self.wait) {
self.wait = false;
return cb(); return cb();
} }
let {type, host, port, accounts, protocol} = this.contractsConfig.deployment; let {type, host, port, accounts, protocol, coverage} = this.config.contractsConfig.deployment;
if (!BlockchainConnector.ACCEPTED_TYPES.includes(type)) { if (!BlockchainConnector.ACCEPTED_TYPES.includes(type)) {
this.logger.error(__("contracts config error: unknown deployment type %s", type)); this.logger.error(__("contracts config error: unknown deployment type %s", type));
@ -69,9 +68,9 @@ class BlockchainConnector {
if (type === 'vm') { if (type === 'vm') {
const sim = self._getSimulator(); const sim = self._getSimulator();
self.provider = sim.provider(self.contractsConfig.deployment); self.provider = sim.provider(self.config.contractsConfig.deployment);
if (self.coverage) { if (coverage) {
// Here we patch the sendAsync method on the provider. The goal behind this is to force pure/constant/view calls to become // Here we patch the sendAsync method on the provider. The goal behind this is to force pure/constant/view calls to become
// transactions, so that we can pull in execution traces and account for those executions in code coverage. // transactions, so that we can pull in execution traces and account for those executions in code coverage.
// //
@ -118,7 +117,7 @@ class BlockchainConnector {
const providerOptions = { const providerOptions = {
web3: this.web3, web3: this.web3,
accountsConfig: accounts, accountsConfig: accounts,
blockchainConfig: this.blockchainConfig, blockchainConfig: this.config.blockchainConfig,
logger: this.logger, logger: this.logger,
isDev: this.isDev, isDev: this.isDev,
type: type, type: type,
@ -130,9 +129,9 @@ class BlockchainConnector {
self.provider.startWeb3Provider(() => { self.provider.startWeb3Provider(() => {
this.getNetworkId() this.getNetworkId()
.then(id => { .then(id => {
let networkId = self.blockchainConfig.networkId; let networkId = self.config.blockchainConfig.networkId;
if (!networkId && constants.blockchain.networkIds[self.blockchainConfig.networkType]) { if (!networkId && constants.blockchain.networkIds[self.config.blockchainConfig.networkType]) {
networkId = constants.blockchain.networkIds[self.blockchainConfig.networkType]; networkId = constants.blockchain.networkIds[self.config.blockchainConfig.networkType];
} }
if (networkId && id.toString() !== networkId.toString()) { 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(__('Connected to a blockchain node on network {{realId}} while your config specifies {{configId}}', {realId: id, configId: networkId}));
@ -244,6 +243,11 @@ class BlockchainConnector {
registerRequests() { registerRequests() {
const self = this; const self = this;
this.events.setCommandHandler("blockchain:reset", function(cb) {
self.isWeb3Ready = false;
self.initWeb3(cb);
});
this.events.setCommandHandler("blockchain:get", function(cb) { this.events.setCommandHandler("blockchain:get", function(cb) {
cb(self.web3); cb(self.web3);
}); });
@ -344,7 +348,7 @@ class BlockchainConnector {
self.logger.error(err); self.logger.error(err);
return cb(new Error(err)); return cb(new Error(err));
} }
let accountConfig = self.blockchainConfig.account; let accountConfig = self.config.blockchainConfig.account;
let selectedAccount = accountConfig && accountConfig.address; let selectedAccount = accountConfig && accountConfig.address;
self.setDefaultAccount(selectedAccount || accounts[0]); self.setDefaultAccount(selectedAccount || accounts[0]);
cb(); cb();

View File

@ -17,27 +17,22 @@ class Test {
this.contracts = {}; this.contracts = {};
this.firstDeployment = true; this.firstDeployment = true;
this.needConfig = true; this.needConfig = true;
this.blockchainConnector = null;
this.provider = null; this.provider = null;
this.accounts = []; this.accounts = [];
} }
init(callback) { init(callback) {
this.gasLimit = 6000000; this.gasLimit = 6000000;
this.events.request('deploy:setGasLimit', this.gasLimit); this.events.request('deploy:setGasLimit', this.gasLimit);
this.events.request('blockchain:object', (connector) => { if (this.options.node !== 'embark') {
this.blockchainConnector = connector; this.showNodeHttpWarning();
if (this.options.node !== 'embark') { return callback();
this.showNodeHttpWarning(); }
return callback(); if (!this.ipc.connected) {
} this.engine.logger.error("Could not connect to Embark's IPC. Is embark running?");
if (!this.ipc.connected) { process.exit(1);
this.engine.logger.error("Could not connect to Embark's IPC. Is embark running?"); }
process.exit(1); return this.connectToIpcNode(callback);
}
return this.connectToIpcNode(callback);
});
} }
connectToIpcNode(cb) { connectToIpcNode(cb) {
@ -62,7 +57,6 @@ class Test {
}); });
} }
// TODO use event for this
if (!this.simOptions.host && (this.options.node && this.options.node === 'vm')) { if (!this.simOptions.host && (this.options.node && this.options.node === 'vm')) {
this.simOptions.type = 'vm'; this.simOptions.type = 'vm';
} else if (this.simOptions.host || (this.options.node && this.options.node !== 'vm')) { } else if (this.simOptions.host || (this.options.node && this.options.node !== 'vm')) {
@ -78,18 +72,22 @@ class Test {
} }
this.configObj.contractsConfig.deployment = this.simOptions; this.configObj.contractsConfig.deployment = this.simOptions;
this.blockchainConnector.contractsConfig = this.configObj.contractsConfig; this.configObj.contractsConfig.deployment.coverage = this.options.coverage;
this.blockchainConnector.isWeb3Ready = false; this.events.request("config:contractsConfig:set", this.configObj.contractsConfig, () => {
this.blockchainConnector.wait = false; this.events.request('blockchain:reset', (err) => {
this.blockchainConnector.coverage = this.options.coverage; if (err) {
this.logger.error('Error restarting the blockchain connection');
}
callback(err);
});
});
this.blockchainConnector.initWeb3(callback);
} }
showNodeHttpWarning() { showNodeHttpWarning() {
if (this.options.node.startsWith('http')) { if (this.options.node.startsWith('http')) {
this.logger.warn("You are using http to connect to the node, as a result the gas details won't be correct." + this.logger.warn("You are using http to connect to the node, as a result the gas details won't be correct." +
" For correct gas details reporting, please use a websockets connection to your node."); " For correct gas details reporting, please use a websockets connection to your node.");
} }
} }
@ -127,7 +125,7 @@ class Test {
return callback(__("contracts config error: unknown deployment type %s", type)); return callback(__("contracts config error: unknown deployment type %s", type));
} }
if(accounts || port !== this.simOptions.port || type !== this.simOptions.type || host !== this.simOptions.host) { if (accounts || port !== this.simOptions.port || type !== this.simOptions.type || host !== this.simOptions.host) {
resetServices = true; resetServices = true;
} }
@ -166,7 +164,7 @@ class Test {
options = {}; options = {};
} }
if (!callback) { if (!callback) {
callback = function () { callback = function() {
}; };
} }
if (!options.contracts) { if (!options.contracts) {
@ -276,7 +274,7 @@ class Test {
}); });
} }
], function (err, accounts) { ], function(err, accounts) {
if (err) { if (err) {
self.logger.error(__('terminating due to error')); self.logger.error(__('terminating due to error'));
self.logger.error(err.message || err); self.logger.error(err.message || err);