embark-area-51/lib/modules/tests/test.js

330 lines
9.4 KiB
JavaScript
Raw Normal View History

2018-06-01 17:42:05 +00:00
const async = require('async');
2018-10-09 17:17:19 +00:00
const AccountParser = require('../../utils/accountParser');
const EmbarkJS = require('embarkjs');
2018-10-18 12:38:29 +00:00
const utils = require('../../utils/utils');
2018-10-16 15:16:47 +00:00
const constants = require('../../constants');
const BALANCE_10_ETHER_IN_HEX = '0x8AC7230489E80000';
2017-06-26 13:01:54 +00:00
2018-06-01 17:33:11 +00:00
class Test {
constructor(options) {
this.options = options || {};
this.simOptions = {};
2018-10-09 18:36:14 +00:00
this.events = options.events;
2018-10-09 20:43:38 +00:00
this.logger = options.logger;
2018-10-18 12:38:29 +00:00
this.ipc = options.ipc;
2018-10-09 20:43:38 +00:00
this.configObj = options.config;
this.ready = true;
this.firstRunConfig = true;
2018-06-08 16:30:44 +00:00
this.error = false;
this.contracts = {};
this.firstDeployment = true;
this.needConfig = true;
2018-10-09 20:43:38 +00:00
this.provider = null;
this.accounts = [];
}
init(callback) {
2018-10-16 15:16:47 +00:00
this.gasLimit = constants.tests.gasLimit;
2018-10-12 14:17:16 +00:00
this.events.request('deploy:setGasLimit', this.gasLimit);
if (this.options.node !== 'embark') {
this.showNodeHttpWarning();
return callback();
}
if (!this.ipc.connected) {
this.engine.logger.error("Could not connect to Embark's IPC. Is embark running?");
process.exit(1);
}
return this.connectToIpcNode(callback);
2018-10-18 12:38:29 +00:00
}
connectToIpcNode(cb) {
this.ipc.request('blockchain:node', {}, (err, node) => {
if (err) {
this.logger.error(err.message || err);
return cb();
}
this.options.node = node;
this.showNodeHttpWarning();
cb();
});
2018-06-06 19:33:48 +00:00
}
initWeb3Provider(callback) {
2018-07-31 10:15:49 +00:00
if (this.simOptions.accounts) {
this.simOptions.accounts = this.simOptions.accounts.map((account) => {
if (!account.hexBalance) {
2018-10-16 15:16:47 +00:00
account.hexBalance = BALANCE_10_ETHER_IN_HEX;
2018-07-31 10:15:49 +00:00
}
return {balance: account.hexBalance, secretKey: account.privateKey};
});
}
if (!this.simOptions.host && (this.options.node && this.options.node === 'vm')) {
2018-10-09 20:43:38 +00:00
this.simOptions.type = 'vm';
2018-10-18 12:38:29 +00:00
} else if (this.simOptions.host || (this.options.node && this.options.node !== 'vm')) {
let options = this.simOptions;
if (this.options.node) {
options = utils.deconstructUrl(this.options.node);
}
if (!options.protocol) {
options.protocol = (options.type === "rpc") ? 'http' : 'ws';
}
Object.assign(this.simOptions, options);
2018-10-09 20:43:38 +00:00
}
2018-10-18 12:38:29 +00:00
2018-10-09 20:43:38 +00:00
this.configObj.contractsConfig.deployment = this.simOptions;
this.configObj.contractsConfig.deployment.coverage = this.options.coverage;
this.events.request("config:contractsConfig:set", this.configObj.contractsConfig, () => {
this.events.request('blockchain:reset', (err) => {
if (err) {
this.logger.error('Error restarting the blockchain connection');
}
callback(err);
});
});
2018-10-09 20:43:38 +00:00
2018-06-06 19:33:48 +00:00
}
2018-09-19 10:51:29 +00:00
showNodeHttpWarning() {
if (this.options.node.startsWith('http')) {
2018-10-09 20:43:38 +00:00
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.");
2018-09-19 10:51:29 +00:00
}
}
onReady(callback) {
2018-06-08 16:30:44 +00:00
const self = this;
if (this.ready) {
return callback();
}
2018-06-08 16:30:44 +00:00
if (this.error) {
return callback(this.error);
}
2018-06-08 16:51:04 +00:00
let errorCallback, readyCallback;
errorCallback = (err) => {
2018-10-09 18:36:14 +00:00
self.events.removeListener('tests:ready', readyCallback);
callback(err);
2018-06-08 16:51:04 +00:00
};
readyCallback = () => {
2018-10-09 18:36:14 +00:00
self.events.removeListener('tests:deployError', errorCallback);
2018-06-08 16:51:04 +00:00
callback();
};
2018-10-09 18:36:14 +00:00
this.events.once('tests:ready', readyCallback);
this.events.once('tests:deployError', errorCallback);
}
2018-06-15 19:16:55 +00:00
checkDeploymentOptions(options, callback) {
const self = this;
2018-07-31 10:17:50 +00:00
let resetServices = false;
2018-07-31 09:16:56 +00:00
const {host, port, type, accounts} = options.deployment || {};
if (host && port && !['rpc', 'ws'].includes(type)) {
2018-10-10 16:09:56 +00:00
return callback(__("contracts config error: unknown deployment type %s", type));
2018-06-15 19:16:55 +00:00
}
if (accounts || port !== this.simOptions.port || type !== this.simOptions.type || host !== this.simOptions.host) {
2018-07-31 10:17:50 +00:00
resetServices = true;
2018-07-31 09:16:56 +00:00
}
2018-07-31 10:15:49 +00:00
2018-10-10 16:09:56 +00:00
this.events.request("blockchain:get", (web3) => {
if (accounts) {
self.simOptions.accounts = AccountParser.parseAccountsConfig(accounts, web3);
} else {
self.simOptions.accounts = null;
}
2018-07-31 09:16:56 +00:00
2018-10-10 16:09:56 +00:00
Object.assign(self.simOptions, {
host,
port,
type
});
2018-10-10 16:09:56 +00:00
if (!resetServices && !self.firstRunConfig) {
return callback();
2018-06-15 19:16:55 +00:00
}
2018-10-10 16:09:56 +00:00
self.initWeb3Provider((err) => {
if (err) {
return callback(err);
}
self.firstRunConfig = false;
callback();
});
2018-06-15 19:16:55 +00:00
});
}
2018-06-01 17:42:05 +00:00
config(options, callback) {
const self = this;
self.needConfig = false;
2018-06-08 10:17:51 +00:00
if (typeof (options) === 'function') {
2018-06-08 00:11:49 +00:00
callback = options;
options = {};
}
if (!callback) {
callback = function() {
};
}
2018-05-30 21:35:54 +00:00
if (!options.contracts) {
2018-06-08 00:09:07 +00:00
options.contracts = {};
2018-05-30 21:35:54 +00:00
}
self.ready = false;
2018-06-01 17:44:49 +00:00
async.waterfall([
2018-06-15 19:16:55 +00:00
function checkDeploymentOpts(next) {
self.checkDeploymentOptions(options, next);
},
function changeGlobalWeb3(next) {
self.events.request('blockchain:get', (web3) => {
global.web3 = web3;
next();
});
},
function compileContracts(next) {
if (!self.firstDeployment) {
return next();
}
2018-10-12 14:17:16 +00:00
console.info('Compiling contracts'.cyan);
2018-10-09 20:43:38 +00:00
self.events.request("contracts:build", false, (err) => {
self.firstDeployment = false;
next(err);
});
},
2018-07-31 09:16:56 +00:00
function resetContracts(next) {
2018-10-09 20:43:38 +00:00
self.events.request("contracts:reset:dependencies", next);
2018-07-31 09:16:56 +00:00
},
function deploy(next) {
self._deploy(options, (err, accounts) => {
if (err) {
2018-10-09 18:36:14 +00:00
self.events.emit('tests:deployError', err);
2018-06-08 16:30:44 +00:00
self.error = err;
return next(err);
}
self.ready = true;
2018-06-08 16:30:44 +00:00
self.error = false;
2018-10-09 18:36:14 +00:00
self.events.emit('tests:ready');
next(null, accounts);
});
2018-06-01 17:42:05 +00:00
}
], (err, accounts) => {
if (err) {
2018-10-09 20:43:38 +00:00
// TODO Do not exit in case of not a normal run (eg after a change)
process.exit(1);
}
callback(null, accounts);
});
2018-06-01 17:42:05 +00:00
}
2018-06-01 17:43:43 +00:00
_deploy(config, callback) {
2018-06-01 17:42:05 +00:00
const self = this;
async.waterfall([
2018-06-01 17:43:43 +00:00
function getConfig(next) {
self.events.request('config:contractsConfig:set',
{contracts: config.contracts, versions: self.versions_default}, next);
2018-06-01 17:42:05 +00:00
},
2018-06-01 17:43:43 +00:00
function getAccounts(next) {
2018-10-10 16:09:56 +00:00
self.events.request('blockchain:getAccounts', (err, accounts) => {
2018-06-01 17:42:05 +00:00
if (err) {
2018-06-01 17:43:43 +00:00
return next(err);
2018-06-01 17:42:05 +00:00
}
2018-06-01 17:43:43 +00:00
self.accounts = accounts;
2018-10-10 16:09:56 +00:00
self.events.request('blockchain:defaultAccount:set', accounts[0], () => {
next(null, accounts);
});
2018-06-01 17:42:05 +00:00
});
2018-06-01 17:43:43 +00:00
},
function getBalance(accounts, next) {
2018-10-10 16:09:56 +00:00
self.events.request('blockchain:getBalance', self.accounts[0], (err, balance) => {
if (err) {
return next(err);
}
2018-06-11 21:22:08 +00:00
if (parseInt(balance, 10) === 0) {
2018-10-09 20:43:38 +00:00
self.logger.warn("Warning: default account has no funds");
}
next(null, accounts);
});
},
function deploy(accounts, next) {
2018-10-09 20:43:38 +00:00
self.events.request('deploy:contracts:test', () => {
next(null, accounts);
});
},
2018-10-18 12:37:28 +00:00
function getWeb3Object(accounts, next) {
self.events.request('blockchain:get', (web3) => {
next(null, accounts, web3);
});
},
function createContractObject(accounts, web3, next) {
2018-10-09 20:43:38 +00:00
self.events.request('contracts:all', (err, contracts) => {
async.each(contracts, (contract, eachCb) => {
if (!self.contracts[contract.className]) {
self.contracts[contract.className] = {};
}
2018-10-18 12:37:28 +00:00
const newContract = Test.getWeb3Contract(contract, web3);
Object.setPrototypeOf(self.contracts[contract.className], newContract);
2018-10-18 12:37:28 +00:00
eachCb();
}, (err) => {
next(err, accounts);
});
2018-06-10 16:11:34 +00:00
2018-06-06 17:54:20 +00:00
});
2018-06-01 17:42:05 +00:00
}
], function(err, accounts) {
if (err) {
2018-10-10 15:34:56 +00:00
self.logger.error(__('terminating due to error'));
self.logger.error(err.message || err);
2018-06-01 17:44:49 +00:00
return callback(err);
}
2018-06-06 17:54:20 +00:00
callback(null, accounts);
});
2018-06-01 17:35:15 +00:00
}
2018-06-01 17:43:43 +00:00
2018-10-18 12:37:28 +00:00
static getWeb3Contract(contract, web3) {
const newContract = new EmbarkJS.Blockchain.Contract({
abi: contract.abiDefinition,
address: contract.deployedAddress,
from: contract.deploymentAccount || web3.eth.defaultAccount,
2018-10-16 15:16:47 +00:00
gas: constants.tests.gasLimit,
2018-10-18 12:37:28 +00:00
web3: web3
});
newContract.filename = contract.filename;
if (newContract.options) {
newContract.options.from = contract.deploymentAccount || web3.eth.defaultAccount;
newContract.options.data = contract.code;
if (!newContract.options.data.startsWith('0x')) {
newContract.options.data = '0x' + newContract.options.data;
}
2018-10-16 15:16:47 +00:00
newContract.options.gas = constants.tests.gasLimit;
2018-10-18 12:37:28 +00:00
}
return newContract;
}
require(path) {
const prefix = 'Embark/contracts/';
if (!path.startsWith(prefix)) {
throw new Error(__('Unknown module %s', path));
2018-06-01 17:43:43 +00:00
}
let contractName = path.replace(prefix, "");
let contract = this.contracts[contractName];
if (contract) {
return contract;
}
let newContract = {};
this.contracts[contractName] = newContract;
return newContract;
2018-06-01 17:43:43 +00:00
}
2018-06-01 17:33:11 +00:00
}
2016-08-21 22:05:35 +00:00
module.exports = Test;