Dynamic selection of storage provider now working based on improved storage config.

swarm plugin now re-initialises the bzz object when it's availability is checked. this creates a much more stable swarm implementation on the dapp side.

surrounded the storage provider init code block with embark env ready

added alternate swarm gateway url

can now upload dapp to ipfs and run swarm storage and vice versa
This commit is contained in:
emizzle 2018-05-30 16:34:36 +10:00
parent 6ac8672a31
commit 85117cf55c
10 changed files with 90 additions and 87 deletions

View File

@ -230,18 +230,6 @@ 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")');

View File

@ -135,7 +135,6 @@ 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) {

View File

@ -132,6 +132,11 @@
"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.",
"deploying to swarm!": "deploying to swarm!",
"adding %s to swarm": "adding %s to swarm",
"successfully uploaded to swarm": "successfully uploaded to swarm",
"Cannot upload: {{platform}} node is not running on {{protocol}}://{{host}}{{port}}.": "Cannot upload: {{platform}} node is not running on {{protocol}}://{{host}}{{port}}."
"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: \"",

View File

@ -24,7 +24,7 @@ __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'));
}
});

View File

@ -1,8 +1,8 @@
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 UploadIPFS = require('./upload.js');
const utils = require('../../utils/utils.js');
const fs = require('../../core/fs.js');
const RunCode = require('../../coderunner/runCode');
const IpfsApi = require('ipfs-api');
const _ = require('underscore');
const UploadIPFS = require('./upload.js');
const utils = require('../../utils/utils.js');
@ -22,12 +22,6 @@ class IPFS {
this.protocol = options.protocol || this.storageConfig.upload.protocol;
this.addCheck = options.addCheck;
this.embark = embark;
// this.commandlineDeploy();
// this.setServiceCheck();
// this.addProviderToEmbarkJS();
// this.addSetProvider();
// this.addObjectToConsole();
}
commandlineDeploy() {
@ -132,7 +126,7 @@ class IPFS {
// }
addObjectToConsole() {
let ipfs = IpfsApi(this.storageConfig.host, this.storageConfig.port);
let ipfs = IpfsApi(this.host, this.port);
this.events.emit("runcode:register", "ipfs", ipfs);
}

View File

@ -5,20 +5,25 @@ import {findSeries} from 'p-iteration';
let __embarkStorage = {};
__embarkStorage.setProviders = async function (dappConnOptions) {
var self = this;
try {
let workingConnFound = await findSeries(dappConnOptions, async (dappConn) => {
if(dappConn === '$BZZ' || dappConn.provider === 'swarm'){
let options = dappConnOptions;
options.useOnlyCurrentProvider = dappConn === '$BZZ';
let options = dappConn;
if(dappConn === '$BZZ') options = {"useOnlyGivenProvider": true};
try{
await EmbarkJS.Storage.setProvider('swarm', options);
return EmbarkJS.Storage.isAvailable();
let isAvailable = await EmbarkJS.Storage.isAvailable();
return isAvailable;
}catch(err){
return false; // catch errors for when bzz object not initialised but config has requested it to be used
}
}
else if(dappConn.provider === 'ipfs') {
// set the provider then check the connection, if true, use that provider, else, check next provider
try{
await EmbarkJS.Storage.setProvider('ipfs', dappConn);
return EmbarkJS.Storage.isAvailable();
let isAvailable = await EmbarkJS.Storage.isAvailable();
return isAvailable;
} catch(err) {
return false;
}
@ -26,7 +31,6 @@ __embarkStorage.setProviders = async function (dappConnOptions) {
});
if(!workingConnFound) throw new Error('Could not connect to a storage provider using any of the dappConnections in the storage config');
} catch (err) {
self.ipfsConnection = null;
throw new Error('Failed to connect to IPFS: ' + err.message);
throw new Error('Failed to connect to a storage provider: ' + err.message);
}
};

View File

@ -5,28 +5,52 @@ let _ = require('underscore');
class Storage {
constructor(embark, options){
this._embark = embark;
this._storageConfig = options.storageConfig;
let storageProviderCls = require(`../${this._storageConfig.upload.provider}/index.js`);
this._uploadProvider = new storageProviderCls(embark, this._storageConfig); /*eslint no-new: "off"*/
let uploadProvider = new storageProviderCls(embark, options); /*eslint no-new: "off"*/
if(typeof this._uploadProvider.initProvider == 'function') this._uploadProvider.initProvider();
if(typeof this._uploadProvider.commandlineDeploy == 'function') this._uploadProvider.commandlineDeploy();
if(typeof this._uploadProvider.setServiceCheck == 'function') this._uploadProvider.setServiceCheck();
if(typeof this._uploadProvider.addObjectToConsole == 'function') this._uploadProvider.addObjectToConsole();
if(typeof uploadProvider.commandlineDeploy == 'function') uploadProvider.commandlineDeploy();
if(typeof uploadProvider.setServiceCheck == 'function') uploadProvider.setServiceCheck();
if(typeof uploadProvider.addObjectToConsole == 'function') uploadProvider.addObjectToConsole();
// loop through all available providers and add "register provider" code in EmbarkJS
// which allows the provider to be set in the DApp
// loop through all available providers and add the provider code to embarkjs
this._storageConfig.available_providers.forEach(providerStr => {
let storageProvider;
// check if we've already instantiated our storage class and reuse
if(providerStr === this._storageConfig.upload.provider){
storageProvider = uploadProvider;
}
// otherwise instantiate the storage provider
else {
let storageProviderCls = require(`../${providerStr}/index.js`);
this._storageProvider = new storageProviderCls(this.storageConfig); /*eslint no-new: "off"*/
if(typeof this._storageProvider.addProviderToEmbarkJS == 'function') this._storageProvider.addProviderToEmbarkJS();
storageProvider = new storageProviderCls(embark, options); /*eslint no-new: "off"*/
}
// add __embarkSwarm and __embarkIPFS objects to EmbarkJS
if(typeof storageProvider.addProviderToEmbarkJS == 'function') storageProvider.addProviderToEmbarkJS();
});
// add the code to call setProviders in embarkjs
// add the storage provider code (__embarkStorage) to embarkjs
this.addProviderToEmbarkJS();
// add the code to call setProviders in embarkjs after embark is ready
this.addSetProviders();
}
addProviderToEmbarkJS(){
// TODO: make this a shouldAdd condition
if (this._storageConfig === {} || !this._storageConfig.dappConnection || !this._storageConfig.dappConnection.length) {
return;
}
let code = "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString();
this._embark.addCodeToEmbarkJS(code);
}
addSetProviders() {
// TODO: make this a shouldAdd condition
if (this._storageConfig === {} || !this._storageConfig.dappConnection || !this._storageConfig.dappConnection.length) {
@ -39,11 +63,13 @@ class Storage {
return _.contains(this._storageConfig.available_providers, conn.provider) || (conn === '$BZZ' && hasSwarm);
});
let code = "";
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString();
code += `\n__embarkStorage.setProviders(${JSON.stringify(connectionsToSet)}));`;
let code = `\n__embarkStorage.setProviders(${JSON.stringify(connectionsToSet)});`;
this.embark.addCodeToEmbarkJS(code);
let shouldInit = (storageConfig) => {
return (connectionsToSet !== undefined && connectionsToSet.length > 0 && storageConfig.enabled === true);
};
this._embark.addProviderInit('storage', code, shouldInit);
}
}

View File

@ -1,14 +1,14 @@
/*global web3, some */
/*global web3 */
let __embarkSwarm = {};
const bytes = require("eth-lib/lib/bytes");
__embarkSwarm.setProvider = function (options) {
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.useOnlyCurrentProvider = options.useOnlyCurrentProvider;
//this._getUrl = options.getUrl || `${this.connectUrl}/bzzr:/`;
let protocol = options.protocol || 'http';
let port = options.port ? `:${options.port}` : '';
this._config = options;
this._connectUrl = `${protocol}://${options.host}${port}`;
this._connectError = new Error(`Cannot connect to Swarm node on ${this._connectUrl}`);
this._config = options;
this._connectUrl = `${protocol}://${options.host}${port}`;
@ -16,8 +16,11 @@ __embarkSwarm.setProvider = function (options) {
return new Promise((resolve, reject) => {
try {
if (!this.bzz.currentProvider && !this.useOnlyCurrentProvider) {
this.bzz.setProvider(this.connectUrl);
if (!web3.bzz.currentProvider && !options.useOnlyGivenProvider) {
web3.bzz.setProvider(this._connectUrl);
}
else if(options.useOnlyGivenProvider && web3.bzz.givenProvider !== null){
web3.bzz.setProvider(web3.bzz.givenProvider);
}
else if(options.useOnlyGivenProvider && web3.bzz.givenProvider !== null){
web3.bzz.setProvider(web3.bzz.givenProvider);

View File

@ -10,24 +10,18 @@ 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;
this.bzz = options.bzz;
// this.initProvider();
// this.commandlineDeploy();
// this.setServiceCheck();
// this.addProviderToEmbarkJS();
// this.addSetProvider();
}
let host = options.host || options.storageConfig.upload.host;
let port = options.port || options.storageConfig.upload.port;
if(port) port = ':' + port;
else port = '';
let protocol = options.protocol || options.storageConfig.upload.protocol || 'http';
this.providerUrl = `${protocol}://${host}${port}`;
this.getUrl = options.storageConfig.upload.getUrl || this.providerUrl + '/bzzr:/';
initProvider(){
if(!this.bzz.currentProvider) {
this.bzz.setProvider(`${this.protocol}://${this.host}:${this.port}`);
}
this.bzz = new Web3Bzz(this.providerUrl);
}
commandlineDeploy() {
@ -105,16 +99,6 @@ 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;

View File

@ -20,10 +20,10 @@
"development": {
"enabled": true,
"upload": {
"provider": "ipfs",
"provider": "swarm",
"host": "localhost",
"port": 5001,
"getUrl": "http://localhost:8080/ipfs/"
"port": 8500,
"getUrl": "http://localhost:8500/bzzr:/"
}
},
"livenet": {