2018-06-01 17:42:05 +00:00
|
|
|
const async = require('async');
|
2018-06-01 17:33:11 +00:00
|
|
|
const Engine = require('../core/engine.js');
|
|
|
|
const TestLogger = require('./test_logger.js');
|
|
|
|
const Web3 = require('web3');
|
2018-06-01 17:42:05 +00:00
|
|
|
const constants = require('../constants');
|
2018-05-30 20:17:17 +00:00
|
|
|
const Events = require('../core/events');
|
2018-06-01 17:44:49 +00:00
|
|
|
const cloneDeep = require('clone-deep');
|
2018-07-27 18:43:39 +00:00
|
|
|
const AccountParser = require('../utils/accountParser');
|
2018-07-27 20:54:33 +00:00
|
|
|
// TODO: breaks module isolation; tests need to be refactored to use the engine and avoid this
|
|
|
|
const Provider = require('../modules/blockchain_connector/provider.js');
|
2018-06-19 13:24:36 +00:00
|
|
|
const utils = require('../utils/utils');
|
2017-06-26 13:01:54 +00:00
|
|
|
|
2018-07-16 09:07:10 +00:00
|
|
|
const EmbarkJS = require('embarkjs');
|
2018-06-10 16:11:34 +00:00
|
|
|
|
2018-06-01 17:33:11 +00:00
|
|
|
function getSimulator() {
|
2017-03-04 02:06:44 +00:00
|
|
|
try {
|
2018-06-01 17:33:11 +00:00
|
|
|
return require('ganache-cli');
|
2017-03-04 02:06:44 +00:00
|
|
|
} catch (e) {
|
2018-06-01 17:33:11 +00:00
|
|
|
const moreInfo = 'For more information see https://github.com/trufflesuite/ganache-cli';
|
2017-03-04 02:06:44 +00:00
|
|
|
if (e.code === 'MODULE_NOT_FOUND') {
|
2018-06-01 17:33:11 +00:00
|
|
|
console.error(__('Simulator not found; Please install it with "%s"', 'npm install ganache-cli --save'));
|
|
|
|
console.error(moreInfo);
|
|
|
|
throw e;
|
2017-03-04 02:06:44 +00:00
|
|
|
}
|
2018-06-01 17:33:11 +00:00
|
|
|
console.error("==============");
|
|
|
|
console.error(__("Tried to load Ganache CLI (testrpc), but an error occurred. This is a problem with Ganache CLI"));
|
|
|
|
console.error(moreInfo);
|
|
|
|
console.error("==============");
|
2017-03-04 02:06:44 +00:00
|
|
|
throw e;
|
|
|
|
}
|
2018-06-01 17:33:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Test {
|
|
|
|
constructor(options) {
|
|
|
|
this.options = options || {};
|
2018-06-07 20:07:58 +00:00
|
|
|
this.simOptions = {};
|
2018-05-30 20:17:17 +00:00
|
|
|
this.events = new Events();
|
|
|
|
this.ready = true;
|
2018-07-30 08:27:59 +00:00
|
|
|
this.firstRunConfig = true;
|
2018-06-08 16:30:44 +00:00
|
|
|
this.error = false;
|
2018-07-30 08:27:59 +00:00
|
|
|
this.contracts = {};
|
2018-06-01 15:30:02 +00:00
|
|
|
this.builtContracts = {};
|
2018-06-04 14:45:50 +00:00
|
|
|
this.compiledContracts = {};
|
2018-06-29 14:29:07 +00:00
|
|
|
this.logsSubscription = null;
|
2018-07-31 09:48:04 +00:00
|
|
|
this.needConfig = true;
|
2018-06-01 17:33:11 +00:00
|
|
|
this.web3 = new Web3();
|
2018-06-06 19:33:48 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 20:07:58 +00:00
|
|
|
initWeb3Provider(callback) {
|
2018-07-06 19:16:04 +00:00
|
|
|
const self = this;
|
2018-06-22 17:19:32 +00:00
|
|
|
if (this.provider) {
|
|
|
|
this.provider.stop();
|
|
|
|
}
|
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-06-07 20:07:58 +00:00
|
|
|
if (this.simOptions.host) {
|
2018-07-03 20:39:17 +00:00
|
|
|
let {host, port, type, protocol, accounts} = this.simOptions;
|
2018-06-19 13:24:36 +00:00
|
|
|
if (!protocol) {
|
|
|
|
protocol = (this.simOptions.type === "rpc") ? 'http' : 'ws';
|
|
|
|
}
|
|
|
|
const endpoint = `${protocol}://${host}:${port}`;
|
2018-06-07 20:07:58 +00:00
|
|
|
const providerOptions = {
|
|
|
|
web3: this.web3,
|
2018-06-19 13:24:36 +00:00
|
|
|
type,
|
|
|
|
accountsConfig: accounts,
|
2018-06-15 19:16:55 +00:00
|
|
|
blockchainConfig: this.engine.config.blockchainConfig,
|
2018-06-07 20:07:58 +00:00
|
|
|
logger: this.engine.logger,
|
|
|
|
isDev: false,
|
2018-06-19 13:24:36 +00:00
|
|
|
web3Endpoint: endpoint
|
2018-06-07 20:07:58 +00:00
|
|
|
};
|
2018-06-19 13:24:36 +00:00
|
|
|
console.info(`Connecting to node at ${endpoint}`.cyan);
|
2018-06-29 21:09:19 +00:00
|
|
|
|
2018-06-19 13:24:36 +00:00
|
|
|
return utils.pingEndpoint(host, port, type, protocol, this.engine.config.blockchainConfig.wsOrigins.split(',')[0], (err) => {
|
|
|
|
if (err) {
|
|
|
|
console.error(`Error connecting to the node, there might be an error in ${endpoint}`.red);
|
|
|
|
return callback(err);
|
|
|
|
}
|
2018-06-29 21:09:19 +00:00
|
|
|
|
2018-07-06 19:30:16 +00:00
|
|
|
self.provider = new Provider(providerOptions);
|
2018-07-06 19:16:04 +00:00
|
|
|
return self.provider.startWeb3Provider((err) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
2018-06-19 13:24:36 +00:00
|
|
|
});
|
2018-06-06 19:33:48 +00:00
|
|
|
}
|
2018-06-07 20:07:58 +00:00
|
|
|
|
2018-06-22 17:19:32 +00:00
|
|
|
if (!this.sim) {
|
|
|
|
this.sim = getSimulator();
|
|
|
|
}
|
2018-06-07 20:07:58 +00:00
|
|
|
this.web3.setProvider(this.sim.provider(this.simOptions));
|
2018-06-29 14:29:07 +00:00
|
|
|
this.subscribeToPendingTransactions();
|
2018-06-07 20:07:58 +00:00
|
|
|
callback();
|
2018-06-06 19:33:48 +00:00
|
|
|
}
|
|
|
|
|
2018-06-29 14:29:07 +00:00
|
|
|
subscribeToPendingTransactions() {
|
|
|
|
const self = this;
|
|
|
|
if (self.logsSubscription) {
|
|
|
|
self.logsSubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
self.logsSubscription = self.web3.eth
|
|
|
|
.subscribe('newBlockHeaders')
|
|
|
|
.on("data", function (blockHeader) {
|
|
|
|
self.engine.events.emit('block:header', blockHeader);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-06 19:33:48 +00:00
|
|
|
initDeployServices() {
|
2018-06-01 17:42:05 +00:00
|
|
|
this.engine.startService("web3", {
|
|
|
|
web3: this.web3
|
|
|
|
});
|
|
|
|
this.engine.startService("deployment", {
|
2018-08-07 15:37:46 +00:00
|
|
|
trackContracts: false,
|
|
|
|
ipcRole: 'client'
|
2018-06-01 17:42:05 +00:00
|
|
|
});
|
2018-07-27 16:44:08 +00:00
|
|
|
this.events.request('deploy:setGasLimit', 6000000);
|
2018-06-01 18:12:10 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 17:43:43 +00:00
|
|
|
init(callback) {
|
2018-06-15 19:16:55 +00:00
|
|
|
this.engine = new Engine({
|
|
|
|
env: this.options.env || 'test',
|
|
|
|
// TODO: config will need to detect if this is a obj
|
|
|
|
embarkConfig: this.options.embarkConfig || 'embark.json',
|
|
|
|
interceptLogs: false
|
|
|
|
});
|
|
|
|
|
2018-07-30 08:27:59 +00:00
|
|
|
this.engine.init({
|
|
|
|
logger: new TestLogger({logLevel: this.options.loglevel})
|
|
|
|
});
|
2018-06-07 20:07:58 +00:00
|
|
|
|
2018-07-30 08:27:59 +00:00
|
|
|
this.versions_default = this.engine.config.contractsConfig.versions;
|
|
|
|
// Reset contract config to nothing to make sure we deploy only what we want
|
|
|
|
this.engine.config.contractsConfig = {
|
|
|
|
contracts: {},
|
|
|
|
versions: this.versions_default
|
|
|
|
};
|
2018-06-07 20:07:58 +00:00
|
|
|
|
2018-07-30 08:27:59 +00:00
|
|
|
this.engine.startService("libraryManager");
|
|
|
|
this.engine.startService("codeRunner");
|
|
|
|
this.initDeployServices();
|
|
|
|
this.engine.startService("codeGenerator");
|
2018-06-07 20:07:58 +00:00
|
|
|
|
2018-07-30 08:27:59 +00:00
|
|
|
callback();
|
2018-06-01 17:43:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 20:17:17 +00:00
|
|
|
onReady(callback) {
|
2018-06-08 16:30:44 +00:00
|
|
|
const self = this;
|
2018-05-30 20:17:17 +00:00
|
|
|
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) => {
|
|
|
|
self.events.removeListener('ready', readyCallback);
|
2018-06-08 10:14:46 +00:00
|
|
|
callback(err);
|
2018-06-08 16:51:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
readyCallback = () => {
|
|
|
|
self.events.removeListener('deployError', errorCallback);
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
|
|
|
|
this.events.once('ready', readyCallback);
|
|
|
|
this.events.once('deployError', errorCallback);
|
2018-05-30 20:17:17 +00:00
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
callback(__("contracts config error: unknown deployment type %s", type));
|
2018-06-15 19:16:55 +00:00
|
|
|
}
|
2018-07-30 08:27:59 +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
|
|
|
}
|
|
|
|
if (accounts) {
|
|
|
|
self.simOptions.accounts = AccountParser.parseAccountsConfig(accounts, self.web3);
|
2018-06-15 19:16:55 +00:00
|
|
|
} else {
|
2018-07-31 10:15:49 +00:00
|
|
|
self.simOptions.account = null;
|
2018-07-31 09:16:56 +00:00
|
|
|
}
|
2018-07-31 10:15:49 +00:00
|
|
|
|
2018-07-31 09:16:56 +00:00
|
|
|
Object.assign(self.simOptions, {
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
type
|
|
|
|
});
|
|
|
|
|
2018-07-31 10:17:50 +00:00
|
|
|
if (!resetServices && !self.firstRunConfig) {
|
2018-07-30 08:27:59 +00:00
|
|
|
return callback();
|
2018-06-15 19:16:55 +00:00
|
|
|
}
|
2018-07-30 08:27:59 +00:00
|
|
|
|
2018-06-15 19:16:55 +00:00
|
|
|
self.initWeb3Provider((err) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2018-07-30 08:27:59 +00:00
|
|
|
self.firstRunConfig = false;
|
2018-06-15 19:16:55 +00:00
|
|
|
self.initDeployServices();
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-01 17:42:05 +00:00
|
|
|
config(options, callback) {
|
2018-06-07 20:07:58 +00:00
|
|
|
const self = this;
|
2018-07-31 09:48:04 +00:00
|
|
|
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 = {};
|
|
|
|
}
|
2018-05-30 20:17:17 +00:00
|
|
|
if (!callback) {
|
2018-06-01 18:12:10 +00:00
|
|
|
callback = function () {
|
|
|
|
};
|
2018-05-30 20:17:17 +00:00
|
|
|
}
|
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
|
|
|
}
|
2018-06-07 20:07:58 +00:00
|
|
|
self.ready = false;
|
2018-06-01 17:44:49 +00:00
|
|
|
|
2018-06-07 20:07:58 +00:00
|
|
|
async.waterfall([
|
2018-06-15 19:16:55 +00:00
|
|
|
function checkDeploymentOpts(next) {
|
|
|
|
self.checkDeploymentOptions(options, next);
|
2018-06-07 20:07:58 +00:00
|
|
|
},
|
2018-07-30 08:27:59 +00:00
|
|
|
function compileContracts(next) {
|
2018-07-31 09:48:04 +00:00
|
|
|
if (Object.keys(self.builtContracts).length > 0) {
|
2018-07-30 08:27:59 +00:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
console.info('Compiling contracts'.cyan);
|
|
|
|
self.engine.contractsManager.build(() => {
|
|
|
|
self.builtContracts = cloneDeep(self.engine.contractsManager.contracts);
|
|
|
|
let className;
|
|
|
|
for (className in self.builtContracts) {
|
|
|
|
self.builtContracts[className].dependencyCount = null;
|
|
|
|
}
|
|
|
|
self.compiledContracts = cloneDeep(self.engine.contractsManager.compiledContracts);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
2018-06-07 20:07:58 +00:00
|
|
|
function resetContracts(next) {
|
|
|
|
self.engine.contractsManager.contracts = cloneDeep(self.builtContracts);
|
|
|
|
self.engine.contractsManager.compiledContracts = cloneDeep(self.compiledContracts);
|
2018-06-20 17:56:43 +00:00
|
|
|
self.engine.contractsManager.contractDependencies = {};
|
2018-06-07 20:07:58 +00:00
|
|
|
next();
|
|
|
|
},
|
|
|
|
function deploy(next) {
|
|
|
|
self._deploy(options, (err, accounts) => {
|
|
|
|
if (err) {
|
2018-06-08 10:14:46 +00:00
|
|
|
self.events.emit('deployError', err);
|
2018-06-08 16:30:44 +00:00
|
|
|
self.error = err;
|
2018-06-07 20:07:58 +00:00
|
|
|
return next(err);
|
|
|
|
}
|
2018-06-08 10:14:46 +00:00
|
|
|
self.ready = true;
|
2018-06-08 16:30:44 +00:00
|
|
|
self.error = false;
|
2018-06-08 10:14:46 +00:00
|
|
|
self.events.emit('ready');
|
2018-06-07 20:07:58 +00:00
|
|
|
next(null, accounts);
|
|
|
|
});
|
2018-06-01 17:42:05 +00:00
|
|
|
}
|
2018-06-19 13:24:36 +00:00
|
|
|
], (err, accounts) => {
|
|
|
|
if (err) {
|
|
|
|
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-06-01 15:30:02 +00:00
|
|
|
self.engine.config.contractsConfig = {contracts: config.contracts, versions: self.versions_default};
|
2018-06-01 17:42:05 +00:00
|
|
|
self.engine.events.emit(constants.events.contractConfigChanged, self.engine.config.contractsConfig);
|
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-05-30 12:55:34 +00:00
|
|
|
self.web3.eth.getAccounts(function (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;
|
|
|
|
self.web3.eth.defaultAccount = accounts[0];
|
2018-06-06 17:54:20 +00:00
|
|
|
next(null, accounts);
|
2018-06-01 17:42:05 +00:00
|
|
|
});
|
2018-06-01 17:43:43 +00:00
|
|
|
},
|
2018-06-11 18:39:17 +00:00
|
|
|
function getBalance(accounts, next) {
|
2018-06-11 21:22:08 +00:00
|
|
|
self.web3.eth.getBalance(self.web3.eth.defaultAccount).then((balance) => {
|
|
|
|
if (parseInt(balance, 10) === 0) {
|
2018-06-11 18:39:17 +00:00
|
|
|
console.warn("Warning: default account has no funds");
|
|
|
|
}
|
|
|
|
next(null, accounts);
|
2018-07-03 20:39:17 +00:00
|
|
|
}).catch((err) => {
|
|
|
|
next(err);
|
|
|
|
});
|
2018-06-11 18:39:17 +00:00
|
|
|
},
|
|
|
|
function deploy(accounts, next) {
|
2018-07-27 17:57:39 +00:00
|
|
|
self.engine.events.request('deploy:contracts:test', () => {
|
2018-06-11 18:39:17 +00:00
|
|
|
next(null, accounts);
|
|
|
|
});
|
|
|
|
},
|
2018-06-06 17:54:20 +00:00
|
|
|
function createContractObject(accounts, next) {
|
2018-06-01 17:44:49 +00:00
|
|
|
async.each(Object.keys(self.engine.contractsManager.contracts), (contractName, eachCb) => {
|
2018-06-01 17:43:43 +00:00
|
|
|
const contract = self.engine.contractsManager.contracts[contractName];
|
2018-06-01 17:44:49 +00:00
|
|
|
if (!self.contracts[contractName]) {
|
|
|
|
self.contracts[contractName] = {};
|
|
|
|
}
|
2018-07-30 08:27:59 +00:00
|
|
|
|
|
|
|
let newContract = new EmbarkJS.Contract({
|
2018-07-03 20:39:17 +00:00
|
|
|
abi: contract.abiDefinition,
|
|
|
|
address: contract.deployedAddress,
|
|
|
|
from: self.web3.eth.defaultAccount,
|
|
|
|
gas: 6000000,
|
|
|
|
web3: self.web3
|
2018-07-30 08:27:59 +00:00
|
|
|
});
|
2018-06-10 16:11:34 +00:00
|
|
|
|
2018-07-30 08:27:59 +00:00
|
|
|
if (newContract.options) {
|
|
|
|
newContract.options.from = self.web3.eth.defaultAccount;
|
|
|
|
newContract.options.data = contract.code;
|
|
|
|
newContract.options.gas = 6000000;
|
2018-06-07 23:08:18 +00:00
|
|
|
}
|
2018-07-30 08:27:59 +00:00
|
|
|
|
|
|
|
Object.setPrototypeOf(self.contracts[contractName], newContract);
|
|
|
|
Object.assign(self.contracts[contractName], newContract);
|
|
|
|
|
2018-06-01 17:43:43 +00:00
|
|
|
eachCb();
|
2018-06-06 17:54:20 +00:00
|
|
|
}, (err) => {
|
|
|
|
next(err, accounts);
|
|
|
|
});
|
2018-06-01 17:42:05 +00:00
|
|
|
}
|
2018-06-06 17:54:20 +00:00
|
|
|
], function (err, accounts) {
|
2018-06-01 18:12:10 +00:00
|
|
|
if (err) {
|
2018-06-01 17:42:05 +00:00
|
|
|
console.log(__('terminating due to error'));
|
2018-06-01 17:44:49 +00:00
|
|
|
return callback(err);
|
2018-06-01 18:12:10 +00:00
|
|
|
}
|
2018-06-06 17:54:20 +00:00
|
|
|
callback(null, accounts);
|
2018-06-01 18:12:10 +00:00
|
|
|
});
|
2018-06-01 17:35:15 +00:00
|
|
|
}
|
2018-06-01 17:43:43 +00:00
|
|
|
|
2018-07-30 08:27:59 +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
|
|
|
}
|
2018-07-30 08:27:59 +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;
|