Storage and Whisper connect

This commit is contained in:
Anthony Laibe 2018-08-28 14:43:52 +01:00
parent 00ccad1d9b
commit 83c01cffdc
8 changed files with 49 additions and 41 deletions

View File

@ -132,6 +132,7 @@ class Engine {
codeRunnerService(_options) {
const CodeRunner = require('./modules/coderunner/codeRunner.js');
this.codeRunner = new CodeRunner({
config: this.config,
plugins: this.plugins,
events: this.events,
logger: this.logger,

View File

@ -5,6 +5,7 @@ const Web3 = require('web3');
class CodeRunner {
constructor(options) {
this.config = options.config;
this.plugins = options.plugins;
this.logger = options.logger;
this.events = options.events;
@ -35,8 +36,28 @@ class CodeRunner {
this.runCode.registerVar('Web3', Web3);
this.runCode.registerVar('EmbarkJS', EmbarkJS);
this.events.on('code-generator-ready', () => {
this.events.request('code-generator:embarkjs:initialization-code', (code) => {
this.events.request('code-generator:embarkjs:provider-code', (code) => {
this.runCode.doEval(code);
const codeTypes = {
'communication': this.config.communicationConfig || {},
'names': this.config.namesystemConfig || {},
'storage': this.config.storageConfig || {}
};
let initProvidersCode = '';
let initCodes = this.plugins.getPluginsFor('initConsoleCode');
for (let plugin of initCodes) {
for (let codeTypeName of Object.keys(codeTypes)) {
let initCodes = plugin.embarkjs_init_console_code[codeTypeName] || [];
for (let initCode of initCodes) {
let [block, shouldInit] = initCode;
if (shouldInit.call(plugin, codeTypes[codeTypeName])) {
initProvidersCode += block;
}
}
}
}
this.runCode.doEval(initProvidersCode);
});
});
}

View File

@ -25,6 +25,7 @@ var Plugin = function(options) {
this.imports = [];
this.embarkjs_code = [];
this.embarkjs_init_code = {};
this.embarkjs_init_console_code = {};
this.afterContractsDeployActions = [];
this.onDeployActions = [];
this.eventActions = {};
@ -189,6 +190,12 @@ Plugin.prototype.addProviderInit = function(providerType, code, initCondition) {
this.addPluginType('initCode');
};
Plugin.prototype.addConsoleProviderInit = function(providerType, code, initCondition) {
this.embarkjs_init_console_code[providerType] = this.embarkjs_init_console_code[providerType] || [];
this.embarkjs_init_console_code[providerType].push([code, initCondition]);
this.addPluginType('initConsoleCode');
};
Plugin.prototype.registerImportFile = function(importName, importLocation) {
this.imports.push([importName, importLocation]);
this.addPluginType('imports');

View File

@ -99,8 +99,8 @@ class CodeGenerator {
self.buildPlaceholderPage(cb);
});
self.events.setCommandHandler('code-generator:embarkjs:initialization-code', (cb) => {
cb(self.getEmbarkJsInitializationCode());
self.events.setCommandHandler('code-generator:embarkjs:provider-code', (cb) => {
cb(self.getEmbarkJsProviderCode());
});
}
@ -321,11 +321,8 @@ class CodeGenerator {
},
function getJSCode(next) {
code += "\n" + embarkjsCode + "\n";
let pluginsWithCode = self.plugins.getPluginsFor('embarkjsCode');
for (let plugin of pluginsWithCode) {
code += plugin.embarkjs_code.join('\n');
}
code += self.getEmbarkJsProviderCode();
code += self.generateCommunicationInitialization(true);
code += self.generateStorageInitialization(true);
code += self.generateNamesInitialization(true);
@ -342,31 +339,10 @@ class CodeGenerator {
});
}
getEmbarkJsInitializationCode() {
let code = '';
let embarkjsCodes = this.plugins.getPluginsFor('embarkjsCode');
for (let plugin of embarkjsCodes) {
code += plugin.embarkjs_code.join('\n');
}
const codeTypes = {'communication': this.communicationConfig,
'names': this.namesystemConfig,
'storage': this.storageConfig};
let initCodes = this.plugins.getPluginsFor('initCode');
for (let plugin of initCodes) {
for (let codeTypeName of Object.keys(codeTypes)) {
let initCodes = plugin.embarkjs_init_code[codeTypeName] || [];
for (let initCode of initCodes) {
let [block, shouldInit] = initCode;
if (shouldInit.call(plugin, codeTypes[codeTypeName])) {
code += block;
}
}
}
}
return code;
getEmbarkJsProviderCode() {
return this.plugins.getPluginsFor('embarkjsCode').reduce((code, plugin) => (
code += plugin.embarkjs_code.join('\n')
), '');
}
buildContractJS(contractName, contractJSON, cb) {

View File

@ -314,6 +314,7 @@ class ENS {
};
this.embark.addProviderInit('names', code, shouldInit);
this.embark.addConsoleProviderInit('names', code, shouldInit);
}
}

View File

@ -35,6 +35,7 @@ class Storage {
};
this.embark.addProviderInit('storage', code, shouldInit);
this.embark.addConsoleProviderInit('storage', code, shouldInit);
}
}

View File

@ -73,21 +73,22 @@ class Whisper {
addSetProvider() {
let connection = this.communicationConfig.connection || {};
const shouldInit = (communicationConfig) => {
return (communicationConfig.provider === 'whisper' && communicationConfig.enabled === true);
};
// todo: make the add code a function as well
let config = {
const config = {
server: canonicalHost(connection.host || defaultHost),
port: connection.port || '8546',
type: connection.type || 'ws'
};
let code = `\nEmbarkJS.Messages.setProvider('whisper', ${JSON.stringify(config)});`;
let shouldInit = (communicationConfig) => {
return (communicationConfig.provider === 'whisper' && communicationConfig.enabled === true);
};
const code = `\nEmbarkJS.Messages.setProvider('whisper', ${JSON.stringify(config)});`;
this.embark.addProviderInit('communication', code, shouldInit);
const consoleConfig = Object.assign({}, config, {providerOptions: {headers: {Origin: "embark"}}});
const consoleCode = `\nEmbarkJS.Messages.setProvider('whisper', ${JSON.stringify(consoleConfig)});`;
this.embark.addConsoleProviderInit('communication', consoleCode, shouldInit);
}
}

View File

@ -12,7 +12,7 @@ __embarkWhisperNewWeb3.setProvider = function (options) {
provider = options.server + ':' + options.port;
}
// TODO: take into account type
self.web3 = new Web3(new Web3.providers.WebsocketProvider("ws://" + provider));
self.web3 = new Web3(new Web3.providers.WebsocketProvider("ws://" + provider, options.providerOptions));
self.getWhisperVersion(function (err, version) {
if (err) {
console.log("whisper not available");