diff --git a/js/embark.js b/js/embark.js index 45d22c333..d86617664 100644 --- a/js/embark.js +++ b/js/embark.js @@ -230,6 +230,18 @@ EmbarkJS.Storage.setProvider = function(provider, options) { return providerObj.setProvider(options); }; +EmbarkJS.Storage.setProviders = function(provider, dappConnOptions) { + let providerObj = this.Providers[provider]; + + if (!providerObj) { + throw new Error('Unknown storage provider'); + } + + this.currentStorage = providerObj; + + return providerObj.setProviders(dappConnOptions); +}; + EmbarkJS.Storage.isAvailable = function(){ if (!this.currentStorage) { throw new Error('Storage provider not set; e.g EmbarkJS.Storage.setProvider("ipfs")'); diff --git a/lib/core/engine.js b/lib/core/engine.js index 78b125198..388bc0425 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -135,6 +135,7 @@ class Engine { plugins: this.plugins }); this.events.on('code-generator-ready', function () { + console.log('CODE GENERATOR READY EVENT FIRED'); self.events.request('code', function (abi, contractsJSON) { pipeline.build(abi, contractsJSON, null, () => { if (self.watch) { @@ -263,24 +264,18 @@ class Engine { } storageService(_options) { - this.registerModule('storage', { + this.registerModule('ipfs', { addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), host: _options.host, port: _options.port }); - // this.registerModule('ipfs', { - // addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), - // storageConfig: this.config.storageConfig, - // host: _options.host, - // port: _options.port - // }); - // this.registerModule('swarm', { - // addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), - // storageConfig: this.config.storageConfig, - // // TODO: this should not be needed and should be deducted from the config instead - // // the eth provider is not necessary the same as the swarm one - // bzz: this.blockchain.web3.bzz - // }); + this.registerModule('swarm', { + addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), + storageConfig: this.config.storageConfig, + // TODO: this should not be needed and should be deducted from the config instead + // the eth provider is not necessary the same as the swarm one + bzz: this.blockchain.web3.bzz + }); } web3Service(options) { diff --git a/lib/i18n/locales/en.json b/lib/i18n/locales/en.json index e81ff233e..f502a89e8 100644 --- a/lib/i18n/locales/en.json +++ b/lib/i18n/locales/en.json @@ -132,6 +132,9 @@ "IPFS node detected": "IPFS node detected", "Webserver is offline": "Webserver is offline", "DApp path length is too long: \"": "DApp path length is too long: \"", + "This is known to cause issues with some applications, please consider reducing your DApp path's length to 66 characters or less.": "This is known to cause issues with some applications, please consider reducing your DApp path's length to 66 characters or less." + "no contracts found": "no contracts found", + "DApp path length is too long: \"": "DApp path length is too long: \"", "This is known to cause issues with some applications, please consider reducing your DApp path's length to 66 characters or less.": "This is known to cause issues with some applications, please consider reducing your DApp path's length to 66 characters or less.", "deploying to swarm!": "deploying to swarm!", "adding %s to swarm": "adding %s to swarm", diff --git a/lib/index.js b/lib/index.js index 9ee88d17d..cfe13efb1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -162,6 +162,7 @@ class Embark { engine.startService('storage'); engine.startService("codeGenerator"); engine.startService("namingSystem"); + engine.events.on('check:backOnline:Ethereum', function () { engine.logger.info(__('Ethereum node detected') + '..'); engine.config.reloadConfig(); diff --git a/lib/modules/ipfs/embarkjs.js b/lib/modules/ipfs/embarkjs.js index 82bf19324..f0eb3fee4 100644 --- a/lib/modules/ipfs/embarkjs.js +++ b/lib/modules/ipfs/embarkjs.js @@ -1,4 +1,5 @@ import IpfsApi from 'ipfs-api'; +//import {some} from 'p-iteration'; let __embarkIPFS = {}; @@ -24,13 +25,36 @@ __embarkIPFS.setProvider = function (options) { resolve(self); } catch (err) { console.error(err); - self._ipfsConnection = null; + self.ipfsConnection = null; reject(new Error('Failed to connect to IPFS')); } }); return promise; }; +__embarkIPFS.setProviders = async function (dappConnOptions) { + var self = this; + try { + let workingConnFound = await some(dappConnOptions, async (dappConn) => { + if(dappConn === '$BZZ' || dappConn.provider !== 'ipfs') return false; // swarm has no bearing for ipfs plugin, continue + else { + // set the provider then check the connection, if true, use that provider, else, check next provider + try{ + await self.setProvider(dappConn); + return await self.isAvailable(); + } catch(err) { + return false; + } + } + }); + if(!workingConnFound) throw new Error('Could not connect to IPFS using any of the dappConnections in the storage config'); + else return self; + } catch (err) { + self.ipfsConnection = null; + throw new Error('Failed to connect to IPFS: ' + err.message); + } +}; + __embarkIPFS.saveText = function (text) { const self = this; var promise = new Promise(function (resolve, reject) { diff --git a/lib/modules/ipfs/index.js b/lib/modules/ipfs/index.js index 722e44d3c..5484bb060 100644 --- a/lib/modules/ipfs/index.js +++ b/lib/modules/ipfs/index.js @@ -1,3 +1,9 @@ +let UploadIPFS = require('./upload.js'); +let utils = require('../../utils/utils.js'); +let fs = require('../../core/fs.js'); +let RunCode = require('../../coderunner/runCode'); +let IpfsApi = require('ipfs-api'); +const _ = require('underscore'); const UploadIPFS = require('./upload.js'); const utils = require('../../utils/utils.js'); const fs = require('../../core/fs.js'); @@ -109,12 +115,12 @@ class IPFS { this.embark.addCodeToEmbarkJS(code); } - // addSetProvider() { - // let code = "\nEmbarkJS.Storage.setProviders('ipfs'," + JSON.stringify(this.storageConfig.dappConnection) + ");"; + addSetProvider() { + let code = "\nEmbarkJS.Storage.setProviders('ipfs'," + JSON.stringify(this.storageConfig.dappConnection) + ");"; - // let shouldInit = (storageConfig) => { - // return (this.storageConfig.dappConnection !== undefined && this.storageConfig.dappConnection.some((dappConn) => dappConn.provider === 'ipfs') && storageConfig.enabled === true); - // }; + let shouldInit = (storageConfig) => { + return (this.storageConfig.dappConnection !== undefined && this.storageConfig.dappConnection.some((dappConn) => dappConn.provider === 'ipfs') && storageConfig.enabled === true); + }; // this.embark.addProviderInit('storage', code, shouldInit); // } diff --git a/lib/modules/swarm/embarkjs.js b/lib/modules/swarm/embarkjs.js index 7f5dc4a5e..1ef85e179 100644 --- a/lib/modules/swarm/embarkjs.js +++ b/lib/modules/swarm/embarkjs.js @@ -1,10 +1,14 @@ /*global web3 */ let __embarkSwarm = {}; const bytes = require("eth-lib/lib/bytes"); +import {some} from 'p-iteration'; __embarkSwarm.setProvider = function (options) { - let protocol = options.protocol || 'http'; - let port = options.port ? `:${options.port}` : ''; + this.bzz = web3.bzz; + this.protocol = options.protocol || 'http'; + this.connectUrl = `${this.protocol}://${options.host}:${options.port}`; + this.connectError = new Error(`Cannot connect to Swarm node on ${this.connectUrl}`); + //this._getUrl = options.getUrl || `${this.connectUrl}/bzzr:/`; this._config = options; this._connectUrl = `${protocol}://${options.host}${port}`; @@ -12,8 +16,8 @@ __embarkSwarm.setProvider = function (options) { return new Promise((resolve, reject) => { try { - if (!web3.bzz.currentProvider && !options.useOnlyGivenProvider) { - web3.bzz.setProvider(this._connectUrl); + if (!this.bzz.currentProvider) { + this.bzz.setProvider(this.connectUrl); } else if(options.useOnlyGivenProvider && web3.bzz.givenProvider !== null){ web3.bzz.setProvider(web3.bzz.givenProvider); @@ -26,6 +30,28 @@ __embarkSwarm.setProvider = function (options) { }); }; +__embarkSwarm.setProviders = async function (dappConnOptions) { + var self = this; + try { + let workingConnFound = await some(dappConnOptions, async (dappConn) => { + if(dappConn === '$BZZ'){ + return self.isAvailable(); + } + else if(dappConn.provider === 'swarm') + { + // set the provider then check the connection, if true, use that provider, else, check next provider + await self.setProvider(dappConn); + return self.isAvailable(); + } + else return false; + }); + if(!workingConnFound) throw new Error('Could not connect to Swarm using any of the dappConnections in the storage config'); + else return self; + } catch (err) { + throw new Error('Failed to connect to Swarm: ' + err.message); + } +}; + __embarkSwarm.isAvailable = function () { return new Promise((resolve, reject) => { // if web3 swarm object doesn't exist diff --git a/lib/modules/swarm/index.js b/lib/modules/swarm/index.js index b40d61c83..d137cfd65 100644 --- a/lib/modules/swarm/index.js +++ b/lib/modules/swarm/index.js @@ -10,6 +10,9 @@ class Swarm { this.events = embark.events; this.buildDir = options.buildDir; this.storageConfig = options.storageConfig; + this.host = options.host || options.storageConfig.upload.host; + this.port = options.port || options.storageConfig.upload.port; + this.protocol = options.protocol || options.storageConfig.upload.protocol; this.addCheck = options.addCheck; this.embark = embark; @@ -24,6 +27,12 @@ class Swarm { this.bzz = new Web3Bzz(this.providerUrl); } + initSwarmProvider(){ + if(!this.bzz.currentProvider) { + this.bzz.setProvider(`${this.protocol}://${this.host}:${this.port}`); + } + } + commandlineDeploy() { this.upload_swarm = new UploadSwarm({ buildDir: this.buildDir || 'dist/', @@ -73,7 +82,7 @@ class Swarm { }); } - addProviderToEmbarkJS() { + addSwarmToEmbarkJS() { let self = this; // TODO: make this a shouldAdd condition if (this.storageConfig === {}) { @@ -99,6 +108,16 @@ class Swarm { this.embark.addCodeToEmbarkJS(code); } + + addSetProvider() { + let code = "\nEmbarkJS.Storage.setProviders('swarm'," + JSON.stringify(this.storageConfig.dappConnection) + ");"; + + let shouldInit = (storageConfig) => { + return (this.storageConfig.dappConnection !== undefined && this.storageConfig.dappConnection.some((dappConn) => dappConn.provider === 'swarm') && storageConfig.enabled === true); + }; + + this.embark.addProviderInit('storage', code, shouldInit); + } } module.exports = Swarm; diff --git a/test_apps/test_app/config/storage.json b/test_apps/test_app/config/storage.json index 139121561..6d06e3c74 100644 --- a/test_apps/test_app/config/storage.json +++ b/test_apps/test_app/config/storage.json @@ -20,10 +20,10 @@ "development": { "enabled": true, "upload": { - "provider": "swarm", + "provider": "ipfs", "host": "localhost", - "port": 8500, - "getUrl": "http://localhost:8500/bzzr:/" + "port": 5001, + "getUrl": "http://localhost:8080/ipfs/" } }, "livenet": {