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