support list of fallbacks for dapp web3 connection, support separate connection config for deployment

This commit is contained in:
Iuri Matias 2017-07-06 18:48:20 -04:00
parent f882b3486b
commit 232f6fc88b
9 changed files with 111 additions and 29 deletions

View File

@ -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);

View File

@ -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');

View File

@ -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);

View File

@ -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;

View File

@ -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) {

View File

@ -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) {

View File

@ -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');

View File

@ -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;

View File

@ -11,7 +11,9 @@
},
"dappConnection": [
"$WEB3",
"localhost:8545"
"http://localhost:8550",
"http://localhost:8545",
"http://localhost:8550"
],
"gas": "auto",
"contracts": {