From 232f6fc88b7223ae64b97099e07a5f122b8d58b9 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 6 Jul 2017 18:48:20 -0400 Subject: [PATCH] support list of fallbacks for dapp web3 connection, support separate connection config for deployment --- lib/contracts/abi.js | 71 +++++++++++++++++++--- lib/core/config.js | 12 +++- lib/core/engine.js | 25 ++++---- lib/core/events.js | 10 +++ lib/core/test.js | 2 +- lib/dashboard/console.js | 7 ++- lib/index.js | 6 +- test_app/app/contracts/another_storage.sol | 3 +- test_app/config/contracts.json | 4 +- 9 files changed, 111 insertions(+), 29 deletions(-) diff --git a/lib/contracts/abi.js b/lib/contracts/abi.js index 3ff3a9453..5bc53a5c5 100644 --- a/lib/contracts/abi.js +++ b/lib/contracts/abi.js @@ -1,19 +1,54 @@ class ABIGenerator { constructor(options) { this.blockchainConfig = options.blockchainConfig || {}; + this.contractsConfig = options.contractsConfig || {}; this.storageConfig = options.storageConfig || {}; this.communicationConfig = options.communicationConfig || {}; this.contractsManager = options.contractsManager; this.rpcHost = options.blockchainConfig && options.blockchainConfig.rpcHost; this.rpcPort = options.blockchainConfig && options.blockchainConfig.rpcPort; this.plugins = options.plugins; + this.events = options.events; } - generateProvider() { + listenToCommands() { + let self = this; + + this.events.setCommandHandler('abi-vanila', function(cb) { + let vanillaABI = self.generateABI({useEmbarkJS: false}); + let contractsJSON = self.generateContractsJSON(); + + cb(vanillaABI, contractsJSON); + }); + + this.events.setCommandHandler('abi', function(cb) { + let embarkJSABI = self.generateABI({useEmbarkJS: true}); + let contractsJSON = self.generateContractsJSON(); + + cb(embarkJSABI, contractsJSON); + }); + + this.events.setCommandHandler('abi-contracts-vanila', function(cb) { + let vanillaContractsABI = self.generateContracts(false); + let contractsJSON = self.generateContractsJSON(); + + cb(vanillaContractsABI, contractsJSON); + }); + + this.events.setCommandHandler('abi-vanila-deployment', function(cb) { + let vanillaABI = self.generateABI({useEmbarkJS: false, deployment: true}); + let contractsJSON = self.generateContractsJSON(); + + cb(vanillaABI, contractsJSON); + }); + } + + generateProvider(isDeployment) { let self = this; let result = ""; let providerPlugins; + // TODO: check contractsConfig for enabled if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) { return ""; } @@ -35,12 +70,32 @@ class ABIGenerator { result += plugin.generateProvider(self) + "\n"; }); } else { - result += "\nwhenEnvIsLoaded(function() {"; - result += "\nif (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {"; - result += '\n\tweb3 = new Web3(web3.currentProvider);'; - result += "\n} else if (typeof Web3 !== 'undefined') {"; - result += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("http://' + this.rpcHost + ':' + this.rpcPort + '"));'; - result += '\n}'; + result += "\nwhenEnvIsLoaded(function() {\n"; + + if (isDeployment) { + + let connection = "http://" + this.contractsConfig.deployment.host + ":" + this.contractsConfig.deployment.port; + result += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("' + connection + '"));'; + } else { + + let connectionCode = this.contractsConfig.dappConnection.map(function(connection) { + let code = ""; + if (connection === '$WEB3') { + code += "if (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {"; + code += '\n\tweb3 = new Web3(web3.currentProvider);'; + code += '\n}'; + } else { + code += "if (typeof Web3 !== 'undefined' && !web3.isConnected()) {"; + code += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("' + connection + '"));'; + code += '\n}'; + } + return code; + }); + + result += connectionCode.join(' else '); + } + + result += "\nweb3.eth.defaultAccount = web3.eth.accounts[0];"; result += '\n})'; } @@ -133,7 +188,7 @@ class ABIGenerator { generateABI(options) { let result = ""; - result += this.generateProvider(); + result += this.generateProvider(options.deployment); result += this.generateContracts(options.useEmbarkJS); result += this.generateStorageInitialization(options.useEmbarkJS); result += this.generateCommunicationInitialization(options.useEmbarkJS); diff --git a/lib/core/config.js b/lib/core/config.js index bd1865a11..16bc8ce69 100644 --- a/lib/core/config.js +++ b/lib/core/config.js @@ -76,11 +76,19 @@ Config.prototype.loadBlockchainConfigFile = function() { Config.prototype.loadContractsConfigFile = function() { var configObject = { - "deployment": { + "versions": { + "web3.js": "0.19.1", + "solc": "0.4.11" + }, + "deployment": { "host": "localhost", "port": 8545, "type": "rpc" - } + }, + "dappConnection": [ + "$WEB3", + "localhost:8545" + ] }; var configPlugins = this.plugins.getPluginsFor('contractsConfig'); diff --git a/lib/core/engine.js b/lib/core/engine.js index a2f8be200..cad71fe83 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -80,11 +80,13 @@ class Engine { logger: this.logger, plugins: this.plugins }); - this.events.on('abi', function (abi, contractsJSON) { - self.currentAbi = abi; - self.contractsJSON = contractsJSON; - pipeline.build(abi, contractsJSON, null, function() { - self.events.emit('outputDone'); + this.events.on('code-generator-ready', function () { + self.events.request('abi', function (abi, contractsJSON) { + self.currentAbi = abi; + self.contractsJSON = contractsJSON; + pipeline.build(abi, contractsJSON, null, function() { + self.events.emit('outputDone'); + }); }); }); // TODO: still need to redeploy contracts because the original contracts @@ -103,19 +105,16 @@ class Engine { let generateABICode = function (contractsManager) { let abiGenerator = new ABIGenerator({ blockchainConfig: self.config.blockchainConfig, + contractsConfig: self.config.contractsConfig, contractsManager: contractsManager, plugins: self.plugins, storageConfig: self.config.storageConfig, - communicationConfig: self.config.communicationConfig + communicationConfig: self.config.communicationConfig, + events: self.events }); - let embarkJSABI = abiGenerator.generateABI({useEmbarkJS: true}); - let vanillaABI = abiGenerator.generateABI({useEmbarkJS: false}); - let vanillaContractsABI = abiGenerator.generateContracts(false); - let contractsJSON = abiGenerator.generateContractsJSON(); + abiGenerator.listenToCommands(); - self.events.emit('abi-contracts-vanila', vanillaContractsABI, contractsJSON); - self.events.emit('abi-vanila', vanillaABI, contractsJSON); - self.events.emit('abi', embarkJSABI, contractsJSON); + self.events.emit('code-generator-ready'); }; this.events.on('contractsDeployed', generateABICode); this.events.on('blockchainDisabled', generateABICode); diff --git a/lib/core/events.js b/lib/core/events.js index 69e0177b8..ea6b84a54 100644 --- a/lib/core/events.js +++ b/lib/core/events.js @@ -1,3 +1,13 @@ var EventEmitter = require('events'); +EventEmitter.prototype.request = function(requestName, cb) { + this.emit('request:' + requestName, cb); +}; + +EventEmitter.prototype.setCommandHandler = function(requestName, cb) { + this.on('request:' + requestName, function(_cb) { + cb.call(this, _cb); + }); +}; + module.exports = EventEmitter; diff --git a/lib/core/test.js b/lib/core/test.js index 39346762e..593f3c4e6 100644 --- a/lib/core/test.js +++ b/lib/core/test.js @@ -72,7 +72,7 @@ Test.prototype.deployAll = function(contractsConfig, cb) { callback(); }, function deploy(callback) { - self.engine.events.on('abi-contracts-vanila', function(vanillaABI) { + self.engine.events.setHandler('abi-contracts-vanila', function(vanillaABI) { callback(null, vanillaABI); }); self.engine.deployManager.deployContracts(function(err, result) { diff --git a/lib/dashboard/console.js b/lib/dashboard/console.js index ca8ccd828..8e5ba80c3 100644 --- a/lib/dashboard/console.js +++ b/lib/dashboard/console.js @@ -8,7 +8,12 @@ class Console { } runCode(code) { - RunCode.doEval(code); // jshint ignore:line + try { + RunCode.doEval(code); // jshint ignore:line + } catch(e) { + console.log(code); + console.debug(e); + } } processEmbarkCmd (cmd) { diff --git a/lib/index.js b/lib/index.js index 6764e4c34..13b6b11e6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -79,8 +79,10 @@ class Embark { env: engine.env }); dashboard.start(function () { - engine.events.on('abi-vanila', function (abi) { - dashboard.console.runCode(abi); + engine.events.on('code-generator-ready', function () { + engine.events.request('abi-vanila-deployment', function (abi) { + dashboard.console.runCode(abi); + }); }); engine.logger.info('dashboard start'); diff --git a/test_app/app/contracts/another_storage.sol b/test_app/app/contracts/another_storage.sol index ba837449c..e00c236a6 100644 --- a/test_app/app/contracts/another_storage.sol +++ b/test_app/app/contracts/another_storage.sol @@ -1,6 +1,7 @@ -pragma solidity ^0.4.7; +pragma solidity ^0.4.11; contract AnotherStorage { address public simpleStorageAddress; + address simpleStorageAddress2; function AnotherStorage(address addr) { simpleStorageAddress = addr; diff --git a/test_app/config/contracts.json b/test_app/config/contracts.json index 1d42028de..ca1813e21 100644 --- a/test_app/config/contracts.json +++ b/test_app/config/contracts.json @@ -11,7 +11,9 @@ }, "dappConnection": [ "$WEB3", - "localhost:8545" + "http://localhost:8550", + "http://localhost:8545", + "http://localhost:8550" ], "gas": "auto", "contracts": {