From a6a5bac053f24ec27efdee020b0a95fad3eb1f5e Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 7 Jun 2018 16:07:58 -0400 Subject: [PATCH 1/2] use new provider when using account with a node specified --- lib/contracts/blockchain.js | 2 +- lib/tests/test.js | 170 +++++++++++++++++++++++------------- 2 files changed, 110 insertions(+), 62 deletions(-) diff --git a/lib/contracts/blockchain.js b/lib/contracts/blockchain.js index e2403c0c..1fbf4dde 100644 --- a/lib/contracts/blockchain.js +++ b/lib/contracts/blockchain.js @@ -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)); } } diff --git a/lib/tests/test.js b/lib/tests/test.js index 86e52ca0..332e4edc 100644 --- a/lib/tests/test.js +++ b/lib/tests/test.js @@ -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,41 @@ 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; + 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"); + + this.engine.contractsManager.build(() => { + self.builtContracts = cloneDeep(self.engine.contractsManager.contracts); + self.compiledContracts = cloneDeep(self.engine.contractsManager.compiledContracts); + callback(); + }); }); } @@ -104,6 +122,7 @@ class Test { } config(options, callback) { + const self = this; if (!callback) { callback = function () { }; @@ -111,30 +130,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) { From cbe456bf1ea89f2c4c95c359af23456a4e126414 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 7 Jun 2018 16:09:54 -0400 Subject: [PATCH 2/2] remove useless config --- lib/tests/test.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/tests/test.js b/lib/tests/test.js index 332e4edc..b89739d8 100644 --- a/lib/tests/test.js +++ b/lib/tests/test.js @@ -91,12 +91,10 @@ class Test { }); 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 + versions: this.versions_default }; this.engine.startService("libraryManager");