embark/lib/modules/tests/test.js

335 lines
10 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');
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;
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.blockchainConnector = null;
this.provider = null;
this.accounts = [];
}
init(callback) {
this.showNodeHttpWarning();
this.events.request('blockchain:object', (connector) => {
this.blockchainConnector = connector;
callback();
});
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) {
account.hexBalance = '0x8AC7230489E80000'; // 10 ether
}
return {balance: account.hexBalance, secretKey: account.privateKey};
});
}
2018-10-09 20:43:38 +00:00
// TODO use event for this
if (!this.simOptions.host && (this.options.node && this.options.node === 'vm')) {
2018-10-09 20:43:38 +00:00
this.simOptions.type = 'vm';
}
this.configObj.contractsConfig.deployment = this.simOptions;
this.blockchainConnector.contractsConfig = this.configObj.contractsConfig;
this.blockchainConnector.isWeb3Ready = false;
2018-10-10 15:34:56 +00:00
this.blockchainConnector.wait = false;
2018-10-09 20:43:38 +00:00
// TODO change this
/*if (this.options.coverage) {
2018-09-17 13:12:20 +00:00
// 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.
//
// Instead of a simple call, here's what happens:
//
// 1) A transaction is sent with the same payload, and a pre-defined gas price;
// 2) We wait for the transaction to be mined by asking for the receipt;
// 3) Once we get the receipt back, we dispatch the real call and pass the original callback;
//
// This will still allow tests to get the return value from the call and run contracts unmodified.
simProvider.realSendAsync = simProvider.sendAsync.bind(simProvider);
simProvider.sendAsync = function(payload, cb) {
if(payload.method !== 'eth_call') {
return simProvider.realSendAsync(payload, cb);
}
2018-10-09 20:43:38 +00:00
self.events.request('reporter:toggleGasListener');
2018-09-17 13:12:20 +00:00
let newParams = Object.assign({}, payload.params[0], {gasPrice: '0x77359400'});
let newPayload = {
id: payload.id + 1,
method: 'eth_sendTransaction',
params: [newParams],
jsonrpc: payload.jsonrpc
};
simProvider.realSendAsync(newPayload, (_err, response) => {
let txHash = response.result;
self.web3.eth.getTransactionReceipt(txHash, (_err, _res) => {
2018-10-09 20:43:38 +00:00
self.events.request('reporter:toggleGasListener');
2018-09-17 13:12:20 +00:00
simProvider.realSendAsync(payload, cb);
});
2018-09-11 18:28:31 +00:00
});
2018-09-17 13:12:20 +00:00
};
2018-10-09 20:43:38 +00:00
}*/
2018-09-10 18:42:16 +00:00
2018-10-09 20:43:38 +00:00
this.blockchainConnector.initWeb3(callback);
2018-06-06 19:33:48 +00:00
}
2018-10-09 20:43:38 +00:00
/*initDeployServices() {
2018-06-01 17:42:05 +00:00
this.engine.startService("web3", {
web3: this.web3
});
this.engine.startService("deployment", {
2018-08-24 13:25:47 +00:00
trackContracts: false,
compileOnceOnly: true,
disableOptimizations: this.options.coverage
2018-06-01 17:42:05 +00:00
});
this.gasLimit = 6000000;
this.engine.events.request('deploy:setGasLimit', this.gasLimit);
2018-10-09 20:43:38 +00:00
}*/
2018-06-01 17:43:43 +00:00
2018-10-09 20:43:38 +00:00
/*connectToIpcNode(cb) {
2018-09-10 10:52:06 +00:00
this.engine.ipc.request('blockchain:node', {}, (err, node) => {
if (err) {
2018-09-10 16:05:11 +00:00
this.engine.logger.error(err.message || err);
return cb();
2018-09-10 10:52:06 +00:00
}
this.options.node = node;
2018-09-19 10:51:29 +00:00
this.showNodeHttpWarning();
2018-09-10 10:52:06 +00:00
cb();
});
2018-10-09 20:43:38 +00:00
}*/
2018-09-10 10:52:06 +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." +
2018-09-19 12:46:39 +00:00
" 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
}
2018-07-31 09:16:56 +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 compileContracts(next) {
if (!self.firstDeployment) {
return next();
}
2018-10-09 20:43:38 +00:00
self.logger.info('Compiling contracts'.cyan);
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) {
2018-10-09 20:43:38 +00:00
// TODO use events instead of modifying directly
self.configObj.contractsConfig = {contracts: config.contracts, versions: self.versions_default};
2018-06-01 17:43:43 +00:00
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-06-06 17:54:20 +00:00
function createContractObject(accounts, 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-10 16:09:56 +00:00
self.events.request('blockchain:contract:create', {
abi: contract.abiDefinition,
2018-10-10 16:09:56 +00:00
address: contract.deployedAddress
}, (newContract) => {
if (newContract.options) {
newContract.options.data = contract.code;
newContract.options.from = accounts[0];
if (!newContract.options.data.startsWith('0x')) {
newContract.options.data = '0x' + newContract.options.data;
}
newContract.options.gas = 6000000;
}
2018-10-10 16:09:56 +00:00
Object.setPrototypeOf(self.contracts[contract.className], newContract);
2018-10-10 16:09:56 +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
}
2018-06-06 17:54:20 +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
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;