Storage config improvements start. Adjusted the config and have started to support those improvements through the codebase.

Currently stuck on starting multiple storage servcies at once. Might need  a change in storage config spec.

WIP.
This commit is contained in:
emizzle 2018-05-25 17:13:57 +10:00
parent 0cb79d84a0
commit 942a57aa3a
9 changed files with 114 additions and 28 deletions

View File

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

View File

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

View File

@ -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",

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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": {