From c22901ab95d1b273af0889aad671a3f11fd06176 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Thu, 13 Sep 2018 11:13:52 +0100 Subject: [PATCH] Fix circular JSON and swarm api require --- cmd/cmd_controller.js | 33 +++++++++++++++++--------- lib/core/ipc.js | 30 +++++++++++++---------- lib/core/modules/coderunner/runCode.js | 2 +- lib/modules/swarm/embarkjs.js | 2 +- package-lock.json | 7 +++++- package.json | 1 + 6 files changed, 49 insertions(+), 26 deletions(-) diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index 18a28c477..a5de68cbc 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -296,20 +296,31 @@ class EmbarkController { if(!engine.ipc.connected || engine.ipc.isServer()) { return callback(); } + const Provider = require('../lib/modules/blockchain_connector/provider'); const Web3 = require('web3'); let web3 = new Web3(); engine.ipc.request("runcode:getCommands", null, (_, {web3Config, commands}) => { - web3.setProvider(web3Config.provider.host); - web3.eth.defaultAccount = web3Config.defaultAccount; - engine.events.emit("runcode:register", "web3", web3); - async.each(commands, ({varName, code}, next) => { - if (varName) { - engine.events.emit("runcode:register", varName, code); - } else { - engine.events.request("runcode:eval", code); - } - next(); - }, callback); + const providerOptions = { + web3: web3, + accountsConfig: engine.config.contractsConfig.deployment.accounts, + blockchainConfig: engine.config.blockchainConfig, + logger: engine.logger, + isDev: engine.isDev, + type: engine.config.contractsConfig.deployment.type, + web3Endpoint: web3Config.providerUrl + }; + const provider = new Provider(providerOptions); + provider.startWeb3Provider(() => { + engine.events.emit("runcode:register", "web3", web3); + async.each(commands, ({varName, code}, next) => { + if (varName) { + engine.events.emit("runcode:register", varName, code); + } else { + engine.events.request("runcode:eval", code); + } + next(); + }, callback); + }); }); }, function deploy(callback) { diff --git a/lib/core/ipc.js b/lib/core/ipc.js index a046817cb..520836d14 100644 --- a/lib/core/ipc.js +++ b/lib/core/ipc.js @@ -1,5 +1,6 @@ -let fs = require('./fs.js'); -let ipc = require('node-ipc'); +const fs = require('./fs.js'); +const ipc = require('node-ipc'); +const {parse, stringify} = require('flatted/cjs'); class IPC { @@ -47,35 +48,40 @@ class IPC { on(action, done) { const self = this; - ipc.server.on('message', function(data, socket) { - if (data.action !== action) { + ipc.server.on('message', function(messageString, socket) { + const message = parse(messageString); + if (message.action !== action) { return; } let reply = function(error, replyData) { self.reply(socket, action, error, replyData); }; - done(data.message, reply, socket); + done(message.data, reply, socket); }); } reply(client, action, error, data) { - ipc.server.emit(client, 'message', {action: action, message: data, error: (error && error.stack)}); + const message = stringify({action, data, error: (error && error.stack)}); + ipc.server.emit(client, 'message', message); } listenTo(action, callback) { - ipc.of['embark'].on(action, callback); + ipc.of['embark'].on(action, (messageString) => { + callback(parse(messageString)); + }); } broadcast(action, data) { - ipc.server.broadcast(action, data); + ipc.server.broadcast(action, stringify(data)); } once(action, cb) { - ipc.of['embark'].once('message', function(msg) { - if (msg.action !== action) { + ipc.of['embark'].once('message', function(messageString) { + const message = parse(messageString); + if (message.action !== action) { return; } - cb(msg.error, msg.message); + cb(message.error, message.data); }); } @@ -83,7 +89,7 @@ class IPC { if (cb) { this.once(action, cb); } - ipc.of['embark'].emit('message', {action: action, message: data}); + ipc.of['embark'].emit('message', stringify({action: action, data: data})); } isClient() { diff --git a/lib/core/modules/coderunner/runCode.js b/lib/core/modules/coderunner/runCode.js index facd08b46..746dd0d58 100644 --- a/lib/core/modules/coderunner/runCode.js +++ b/lib/core/modules/coderunner/runCode.js @@ -29,7 +29,7 @@ class RunCode { } getWeb3Config() { - return {defaultAccount: this.context.web3.eth.defaultAccount, provider: this.context.web3.currentProvider}; + return {defaultAccount: this.context.web3.eth.defaultAccount, providerUrl: this.context.web3.currentProvider.connection._url}; } } diff --git a/lib/modules/swarm/embarkjs.js b/lib/modules/swarm/embarkjs.js index 4049df54e..e8e36cec2 100644 --- a/lib/modules/swarm/embarkjs.js +++ b/lib/modules/swarm/embarkjs.js @@ -1,6 +1,6 @@ /*global web3 */ let __embarkSwarm = {_swarmConnection: undefined}; -import SwarmAPI from 'swarm-api'; +const SwarmAPI = require('swarm-api'); __embarkSwarm.setProvider = function (options) { let protocol = options.protocol || 'http'; diff --git a/package-lock.json b/package-lock.json index c40d58b5e..d7acde2f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3687,6 +3687,11 @@ "resolved": "https://registry.npmjs.org/flatmap/-/flatmap-0.0.3.tgz", "integrity": "sha1-Hxik2TgVLUlZZfnJWNkjqy3WabQ=" }, + "flatted": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/flatted/-/flatted-0.2.3.tgz", + "integrity": "sha512-C4B5UtK3kOrLAyZ1ftqEWprxCfLmCIqEcNufZrtsJhiZ/fcI5mvCgtAtC3pu7BC9KE7aUIrPXwTgcT1fiI7QhA==" + }, "flatten": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/flatten/-/flatten-1.0.2.tgz", @@ -7926,7 +7931,7 @@ }, "readable-stream": { "version": "2.3.6", - "resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==", "requires": { "core-util-is": "~1.0.0", diff --git a/package.json b/package.json index a66c905f2..927f1a9b8 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "ethereumjs-wallet": "0.6.0", "file-loader": "^1.1.5", "finalhandler": "^1.1.1", + "flatted": "^0.2.3", "follow-redirects": "^1.2.4", "fs-extra": "^2.0.0", "ganache-cli": "^6.1.6",