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) { codeRunnerService(_options) {
const CodeRunner = require('./modules/coderunner/codeRunner.js'); const CodeRunner = require('./modules/coderunner/codeRunner.js');
this.codeRunner = new CodeRunner({ this.codeRunner = new CodeRunner({
config: this.config,
plugins: this.plugins, plugins: this.plugins,
events: this.events, events: this.events,
logger: this.logger, logger: this.logger,

View File

@ -5,6 +5,7 @@ const Web3 = require('web3');
class CodeRunner { class CodeRunner {
constructor(options) { constructor(options) {
this.config = options.config;
this.plugins = options.plugins; this.plugins = options.plugins;
this.logger = options.logger; this.logger = options.logger;
this.events = options.events; this.events = options.events;
@ -35,8 +36,28 @@ class CodeRunner {
this.runCode.registerVar('Web3', Web3); this.runCode.registerVar('Web3', Web3);
this.runCode.registerVar('EmbarkJS', EmbarkJS); this.runCode.registerVar('EmbarkJS', EmbarkJS);
this.events.on('code-generator-ready', () => { 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); 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.imports = [];
this.embarkjs_code = []; this.embarkjs_code = [];
this.embarkjs_init_code = {}; this.embarkjs_init_code = {};
this.embarkjs_init_console_code = {};
this.afterContractsDeployActions = []; this.afterContractsDeployActions = [];
this.onDeployActions = []; this.onDeployActions = [];
this.eventActions = {}; this.eventActions = {};
@ -189,6 +190,12 @@ Plugin.prototype.addProviderInit = function(providerType, code, initCondition) {
this.addPluginType('initCode'); 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) { Plugin.prototype.registerImportFile = function(importName, importLocation) {
this.imports.push([importName, importLocation]); this.imports.push([importName, importLocation]);
this.addPluginType('imports'); this.addPluginType('imports');

View File

@ -99,8 +99,8 @@ class CodeGenerator {
self.buildPlaceholderPage(cb); self.buildPlaceholderPage(cb);
}); });
self.events.setCommandHandler('code-generator:embarkjs:initialization-code', (cb) => { self.events.setCommandHandler('code-generator:embarkjs:provider-code', (cb) => {
cb(self.getEmbarkJsInitializationCode()); cb(self.getEmbarkJsProviderCode());
}); });
} }
@ -321,11 +321,8 @@ class CodeGenerator {
}, },
function getJSCode(next) { function getJSCode(next) {
code += "\n" + embarkjsCode + "\n"; 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.generateCommunicationInitialization(true);
code += self.generateStorageInitialization(true); code += self.generateStorageInitialization(true);
code += self.generateNamesInitialization(true); code += self.generateNamesInitialization(true);
@ -342,31 +339,10 @@ class CodeGenerator {
}); });
} }
getEmbarkJsInitializationCode() { getEmbarkJsProviderCode() {
let code = ''; return this.plugins.getPluginsFor('embarkjsCode').reduce((code, plugin) => (
let embarkjsCodes = this.plugins.getPluginsFor('embarkjsCode'); code += plugin.embarkjs_code.join('\n')
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;
} }
buildContractJS(contractName, contractJSON, cb) { buildContractJS(contractName, contractJSON, cb) {

View File

@ -314,6 +314,7 @@ class ENS {
}; };
this.embark.addProviderInit('names', code, shouldInit); 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.addProviderInit('storage', code, shouldInit);
this.embark.addConsoleProviderInit('storage', code, shouldInit);
} }
} }

View File

@ -73,21 +73,22 @@ class Whisper {
addSetProvider() { addSetProvider() {
let connection = this.communicationConfig.connection || {}; let connection = this.communicationConfig.connection || {};
const shouldInit = (communicationConfig) => {
return (communicationConfig.provider === 'whisper' && communicationConfig.enabled === true);
};
// todo: make the add code a function as well // todo: make the add code a function as well
let config = { const config = {
server: canonicalHost(connection.host || defaultHost), server: canonicalHost(connection.host || defaultHost),
port: connection.port || '8546', port: connection.port || '8546',
type: connection.type || 'ws' type: connection.type || 'ws'
}; };
const code = `\nEmbarkJS.Messages.setProvider('whisper', ${JSON.stringify(config)});`;
let code = `\nEmbarkJS.Messages.setProvider('whisper', ${JSON.stringify(config)});`;
let shouldInit = (communicationConfig) => {
return (communicationConfig.provider === 'whisper' && communicationConfig.enabled === true);
};
this.embark.addProviderInit('communication', code, shouldInit); 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; provider = options.server + ':' + options.port;
} }
// TODO: take into account type // 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) { self.getWhisperVersion(function (err, version) {
if (err) { if (err) {
console.log("whisper not available"); console.log("whisper not available");