Merge pull request #497 from embark-framework/features/test-connect-node
Connect to a node in tests
This commit is contained in:
commit
9f0ab86c2d
|
@ -83,7 +83,7 @@ class Blockchain {
|
|||
}
|
||||
], cb);
|
||||
} else {
|
||||
throw new Error("contracts config error: unknown deployment type " + this.contractsConfig.deployment.type);
|
||||
throw new Error(__("contracts config error: unknown deployment type %s", this.contractsConfig.deployment.type));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,11 +2,11 @@ const async = require('async');
|
|||
const Engine = require('../core/engine.js');
|
||||
const TestLogger = require('./test_logger.js');
|
||||
const Web3 = require('web3');
|
||||
const utils = require('../utils/utils');
|
||||
const constants = require('../constants');
|
||||
const Events = require('../core/events');
|
||||
const cloneDeep = require('clone-deep');
|
||||
const AccountParser = require('../contracts/accountParser');
|
||||
const Provider = require('../contracts/provider');
|
||||
|
||||
function getSimulator() {
|
||||
try {
|
||||
|
@ -29,7 +29,7 @@ function getSimulator() {
|
|||
class Test {
|
||||
constructor(options) {
|
||||
this.options = options || {};
|
||||
this.simOptions = this.options.simulatorOptions || {};
|
||||
this.simOptions = {};
|
||||
this.contracts = {};
|
||||
this.events = new Events();
|
||||
this.ready = true;
|
||||
|
@ -37,42 +37,29 @@ class Test {
|
|||
this.compiledContracts = {};
|
||||
|
||||
this.web3 = new Web3();
|
||||
this.initWeb3Provider();
|
||||
|
||||
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
|
||||
});
|
||||
|
||||
this.engine.init({
|
||||
logger: new TestLogger({logLevel: 'debug'})
|
||||
});
|
||||
|
||||
this.versions_default = this.engine.config.contractsConfig.versions;
|
||||
const deploymentConfig = 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, deployment: deploymentConfig};
|
||||
|
||||
this.engine.startService("libraryManager");
|
||||
this.engine.startService("codeRunner");
|
||||
this.initDeployServices();
|
||||
this.engine.startService("codeGenerator");
|
||||
}
|
||||
|
||||
initWeb3Provider() {
|
||||
if (this.simOptions.node) {
|
||||
this.web3.setProvider(new this.web3.providers.HttpProvider(this.simOptions.node));
|
||||
} else {
|
||||
if (this.simOptions.accounts) {
|
||||
this.simOptions.accounts = this.simOptions.accounts.map((account) => {
|
||||
return {balance: account.hexBalance, secretKey: account.privateKey};
|
||||
});
|
||||
}
|
||||
this.sim = getSimulator();
|
||||
this.web3.setProvider(this.sim.provider(this.simOptions));
|
||||
initWeb3Provider(callback) {
|
||||
if (this.simOptions.host) {
|
||||
const providerOptions = {
|
||||
web3: this.web3,
|
||||
accountsConfig: this.simOptions.accounts,
|
||||
logger: this.engine.logger,
|
||||
isDev: false,
|
||||
web3Endpoint: 'http://' + this.simOptions.host + ':' + this.simOptions.port
|
||||
};
|
||||
this.provider = new Provider(providerOptions);
|
||||
return this.provider.startWeb3Provider(callback);
|
||||
}
|
||||
|
||||
if (this.simOptions.accounts) {
|
||||
this.simOptions.accounts = this.simOptions.accounts.map((account) => {
|
||||
return {balance: account.hexBalance, secretKey: account.privateKey};
|
||||
});
|
||||
}
|
||||
this.sim = getSimulator();
|
||||
this.web3.setProvider(this.sim.provider(this.simOptions));
|
||||
callback();
|
||||
}
|
||||
|
||||
initDeployServices() {
|
||||
|
@ -87,10 +74,39 @@ class Test {
|
|||
|
||||
init(callback) {
|
||||
const self = this;
|
||||
this.engine.contractsManager.build(() => {
|
||||
self.builtContracts = cloneDeep(self.engine.contractsManager.contracts);
|
||||
self.compiledContracts = cloneDeep(self.engine.contractsManager.compiledContracts);
|
||||
callback();
|
||||
|
||||
this.initWeb3Provider((err) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
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
|
||||
});
|
||||
|
||||
this.engine.init({
|
||||
logger: new TestLogger({logLevel: 'debug'})
|
||||
});
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
this.engine.startService("libraryManager");
|
||||
this.engine.startService("codeRunner");
|
||||
this.initDeployServices();
|
||||
this.engine.startService("codeGenerator");
|
||||
|
||||
this.engine.contractsManager.build(() => {
|
||||
self.builtContracts = cloneDeep(self.engine.contractsManager.contracts);
|
||||
self.compiledContracts = cloneDeep(self.engine.contractsManager.compiledContracts);
|
||||
callback();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -104,6 +120,7 @@ class Test {
|
|||
}
|
||||
|
||||
config(options, callback) {
|
||||
const self = this;
|
||||
if (!callback) {
|
||||
callback = function () {
|
||||
};
|
||||
|
@ -111,30 +128,59 @@ class Test {
|
|||
if (!options.contracts) {
|
||||
throw new Error(__('No contracts specified in the options'));
|
||||
}
|
||||
this.options = utils.recursiveMerge(this.options, options);
|
||||
this.simOptions = this.options.simulatorOptions || {};
|
||||
this.ready = false;
|
||||
self.ready = false;
|
||||
|
||||
if (this.options.deployment && this.options.deployment.accounts) {
|
||||
// Account setup
|
||||
this.simOptions.accounts = AccountParser.parseAccountsConfig(this.options.deployment.accounts, this.web3);
|
||||
this.initWeb3Provider();
|
||||
this.initDeployServices();
|
||||
}
|
||||
|
||||
// Reset contracts
|
||||
this.engine.contractsManager.contracts = cloneDeep(this.builtContracts);
|
||||
this.engine.contractsManager.compiledContracts = cloneDeep(this.compiledContracts);
|
||||
|
||||
this._deploy(options, (err, accounts) => {
|
||||
this.ready = true;
|
||||
this.events.emit('ready');
|
||||
if (err) {
|
||||
console.error(err.red);
|
||||
return callback(err);
|
||||
async.waterfall([
|
||||
function checkDeploymentOptions(next) {
|
||||
if (!options.deployment) {
|
||||
if (!self.simOptions.host && !self.simOptions.accounts) {
|
||||
return next();
|
||||
}
|
||||
self.simOptions = {};
|
||||
} else {
|
||||
self.simOptions = {};
|
||||
let resetServices = false;
|
||||
if (options.deployment.accounts) {
|
||||
// Account setup
|
||||
self.simOptions.accounts = AccountParser.parseAccountsConfig(options.deployment.accounts, self.web3);
|
||||
resetServices = true;
|
||||
}
|
||||
if (options.deployment.host && options.deployment.port && options.deployment.type) {
|
||||
if (options.deployment.type !== 'rpc') {
|
||||
throw new Error(__("contracts config error: unknown deployment type %s", options.deployment.type));
|
||||
}
|
||||
Object.assign(self.simOptions, {host: options.deployment.host, port: options.deployment.port});
|
||||
resetServices = true;
|
||||
}
|
||||
if (!resetServices) {
|
||||
return next();
|
||||
}
|
||||
}
|
||||
self.initWeb3Provider((err) => {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
self.initDeployServices();
|
||||
next();
|
||||
});
|
||||
},
|
||||
function resetContracts(next) {
|
||||
self.engine.contractsManager.contracts = cloneDeep(self.builtContracts);
|
||||
self.engine.contractsManager.compiledContracts = cloneDeep(self.compiledContracts);
|
||||
next();
|
||||
},
|
||||
function deploy(next) {
|
||||
self._deploy(options, (err, accounts) => {
|
||||
self.ready = true;
|
||||
self.events.emit('ready');
|
||||
if (err) {
|
||||
console.error(err.red);
|
||||
return next(err);
|
||||
}
|
||||
next(null, accounts);
|
||||
});
|
||||
}
|
||||
callback(null, accounts);
|
||||
});
|
||||
], callback);
|
||||
}
|
||||
|
||||
_deploy(config, callback) {
|
||||
|
|
Loading…
Reference in New Issue