From 82042c01c6f5d322438244409cd6c6297ab0ab73 Mon Sep 17 00:00:00 2001 From: Andy Nogueira Date: Wed, 8 Mar 2017 09:46:15 -0500 Subject: [PATCH 01/45] Fixed issue with logic to detect if IPFS node is online. If the IPFS node is not running and there's another service in the computer running on port 5001 it could cause an exception. --- lib/core/services.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/core/services.js b/lib/core/services.js index a04f672e..ad2f8c07 100644 --- a/lib/core/services.js +++ b/lib/core/services.js @@ -70,18 +70,23 @@ ServicesMonitor.prototype.check = function() { body += d; }); res.on('end', function() { - var parsed = JSON.parse(body); - if(parsed.Version){ - result.push(("IPFS " + parsed.Version).green); + try{ + var parsed = JSON.parse(body); + if(parsed.Version){ + result.push(("IPFS " + parsed.Version).green); + } + else{ + result.push("IPFS".green); + } } - else{ - result.push("IPFS".green); + catch (e){ + result.push("IPFS".red); } callback(null, result); }); res.on('error', function(err) { self.logger.trace("Check IPFS version error: " + err); - result.push("IPFS".green); + result.push("IPFS".red); callback(null, result); }); }); @@ -93,8 +98,8 @@ ServicesMonitor.prototype.check = function() { }); }, function checkDevServer(result, callback) { - var host = self.serverHost || self.config.webServerConfig.host; - var port = self.serverPort || self.config.webServerConfig.port; + var host = self.config.webServerConfig.host || self.serverHost; + var port = self.config.webServerConfig.port || self.serverPort; self.logger.trace('checkDevServer'); var devServer = 'Webserver (http://' + host + ':' + port + ')'; devServer = (self.runWebserver) ? devServer.green : devServer.red; From f5aafa424220a2d95bfc947e752aa562d121a52b Mon Sep 17 00:00:00 2001 From: Andy Nogueira Date: Wed, 8 Mar 2017 09:51:58 -0500 Subject: [PATCH 02/45] Setting host and port parameters back according to develop branch --- lib/core/services.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/core/services.js b/lib/core/services.js index ad2f8c07..90fb5e0b 100644 --- a/lib/core/services.js +++ b/lib/core/services.js @@ -98,8 +98,8 @@ ServicesMonitor.prototype.check = function() { }); }, function checkDevServer(result, callback) { - var host = self.config.webServerConfig.host || self.serverHost; - var port = self.config.webServerConfig.port || self.serverPort; + var host = self.serverHost || self.config.webServerConfig.host; + var port = self.serverPort || self.config.webServerConfig.port; self.logger.trace('checkDevServer'); var devServer = 'Webserver (http://' + host + ':' + port + ')'; devServer = (self.runWebserver) ? devServer.green : devServer.red; From 511c92bd9cfbbc88d2d835281bfef87e67c5112f Mon Sep 17 00:00:00 2001 From: Andy Nogueira Date: Wed, 8 Mar 2017 22:37:04 -0500 Subject: [PATCH 03/45] Updated the link on boilerplate app (generated by embark new) to point to the latest documentation instead of old wiki --- boilerplate/app/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boilerplate/app/index.html b/boilerplate/app/index.html index 3585f080..8068e7c7 100644 --- a/boilerplate/app/index.html +++ b/boilerplate/app/index.html @@ -6,6 +6,6 @@

Welcome to Embark!

-

See the Wiki to see what you can do with Embark!

+

See the Embark's documentation to see what you can do with Embark!

From 1b4e1c209bc2047fed6defc2b0dc806afe3edf94 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 9 Mar 2017 07:10:25 -0500 Subject: [PATCH 04/45] add downloads shiled --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ac3cab76..86483a60 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![npm](https://img.shields.io/npm/dm/embark.svg)]() [![Join the chat at https://gitter.im/iurimatias/embark-framework](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/iurimatias/embark-framework?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Build Status](https://travis-ci.org/iurimatias/embark-framework.svg?branch=develop)](https://travis-ci.org/iurimatias/embark-framework) From 5b900fc278c885a3fa7cbfd3767bd77d7fcd4498 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 9 Mar 2017 07:12:24 -0500 Subject: [PATCH 05/45] update gitter shield --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 86483a60..6e2edae7 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ [![npm](https://img.shields.io/npm/dm/embark.svg)]() -[![Join the chat at https://gitter.im/iurimatias/embark-framework](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/iurimatias/embark-framework?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +[![Gitter](https://img.shields.io/gitter/room/iurimatias/embark-framework.svg)]() [![Build Status](https://travis-ci.org/iurimatias/embark-framework.svg?branch=develop)](https://travis-ci.org/iurimatias/embark-framework) [![Code Climate](https://codeclimate.com/github/iurimatias/embark-framework/badges/gpa.svg)](https://codeclimate.com/github/iurimatias/embark-framework) From 862b102c3b3259a103ab9dd19ddcc6bc5387f1d5 Mon Sep 17 00:00:00 2001 From: Todd Baur Date: Fri, 10 Mar 2017 12:11:54 +0900 Subject: [PATCH 06/45] a good start on a contrib.md --- CONTRIBUTING.md | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..0af79571 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,92 @@ +# Contributing + +When contributing to this repository, please first discuss the change you wish to make via issue, +email, or any other method with the owners of this repository before making a change. + +Please note we have a code of conduct, please follow it in all your interactions with the project. + +## Pull Request Process + +1. Ensure any install or build dependencies are removed before the end of the layer when doing a + build. +2. Update the README.md with details of changes to the interface, this includes new environment + variables, exposed ports, useful file locations and container parameters. +3. Increase the version numbers in any examples files and the README.md to the new version that this + Pull Request would represent. The versioning scheme we use is [SemVer](http://semver.org/). +4. You may merge the Pull Request in once you have the sign-off of two other developers, or if you + do not have permission to do that, you may request the second reviewer to merge it for you. + +## Code of Conduct + +### Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, gender identity and expression, level of experience, +nationality, personal appearance, race, religion, or sexual identity and +orientation. + +### Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or +advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic + address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +### Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or +reject comments, commits, code, wiki edits, issues, and other contributions +that are not aligned to this Code of Conduct, or to ban temporarily or +permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +### Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +### Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by contacting the project team at [INSERT EMAIL ADDRESS]. All +complaints will be reviewed and investigated and will result in a response that +is deemed necessary and appropriate to the circumstances. The project team is +obligated to maintain confidentiality with regard to the reporter of an incident. +Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good +faith may face temporary or permanent repercussions as determined by other +members of the project's leadership. + +### Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, +available at [http://contributor-covenant.org/version/1/4][version] + +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/4/ \ No newline at end of file From a4369ecd4ed91b7308a39fadc67048106a457e40 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 10 Mar 2017 06:44:17 -0500 Subject: [PATCH 07/45] implement (previously empty) stopMonitor method --- lib/core/services.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/core/services.js b/lib/core/services.js index 90fb5e0b..6d4cce51 100644 --- a/lib/core/services.js +++ b/lib/core/services.js @@ -20,6 +20,7 @@ ServicesMonitor.prototype.startMonitor = function() { }; ServicesMonitor.prototype.stopMonitor = function() { + clearInterval(this.monitor); }; ServicesMonitor.prototype.check = function() { From 835899cf6e43c23a73880fd6d55f2c2df6494832 Mon Sep 17 00:00:00 2001 From: Todd Baur Date: Fri, 10 Mar 2017 23:43:52 +0900 Subject: [PATCH 08/45] this is a quick attempt at speeding up the init of CLI by changing Embark.js to a reveal module pattern and moving the requires into the functions that need them. It removes the circular reference between Cmd and Embark, which is the biggest hit to speed. It also changes how the binary starts by importing Cmd instead of Embark. --- bin/embark | 6 ++--- lib/cmd.js | 3 ++- lib/index.js | 74 +++++++++++++++++++++++++--------------------------- 3 files changed, 41 insertions(+), 42 deletions(-) diff --git a/bin/embark b/bin/embark index eb0b4d55..c9173a48 100755 --- a/bin/embark +++ b/bin/embark @@ -1,4 +1,4 @@ #!/usr/bin/env node - -var Embark = require('..'); -Embark.process(process.argv); +var Cmd = require('../lib/cmd'); +var cli = new Cmd(); +cli.process(process.argv); diff --git a/lib/cmd.js b/lib/cmd.js index b60ac4ff..e60c5e22 100644 --- a/lib/cmd.js +++ b/lib/cmd.js @@ -1,8 +1,9 @@ var program = require('commander'); var colors = require('colors'); var shelljs = require('shelljs'); +var Embark = require('../lib/index'); -var Cmd = function(Embark) { +var Cmd = function() { this.Embark = Embark; program.version(Embark.version); }; diff --git a/lib/index.js b/lib/index.js index 6b358d23..1df81b4b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,63 +2,50 @@ var async = require('async'); //require("./core/debug_util.js")(__filename, async); -var Web3 = require('web3'); var colors = require('colors'); var Engine = require('./core/engine.js'); -var Blockchain = require('./cmds/blockchain/blockchain.js'); -var Simulator = require('./cmds/simulator.js'); -var TemplateGenerator = require('./cmds/template_generator.js'); - var Test = require('./core/test.js'); -var Logger = require('./core/logger.js'); -var Config = require('./core/config.js'); -var Events = require('./core/events.js'); - -var Dashboard = require('./dashboard/dashboard.js'); var IPFS = require('./upload/ipfs.js'); var Swarm = require('./upload/swarm.js'); -var Cmd = require('./cmd.js'); var version = require('../package.json').version; -var Embark = { +var Embark = function () { + function initConfig (env, options) { + var Events = require('./core/events.js'); + var Logger = require('./core/logger.js'); + var Config = require('./core/config.js'); - version: version, - - process: function(args) { - var cmd = new Cmd(Embark); - cmd.process(args); - }, - - initConfig: function(env, options) { this.events = new Events(); this.logger = new Logger({logLevel: 'debug'}); this.config = new Config({env: env, logger: this.logger, events: this.events}); this.config.loadConfigFiles(options); this.plugins = this.config.plugins; - }, + } - blockchain: function(env, client) { - var blockchain = Blockchain(this.config.blockchainConfig, client, env); - blockchain.run(); - }, + function blockchain (env, client) { + return require('./cmds/blockchain/blockchain.js')(this.config.blockchainConfig, client, env).run(); + } - simulator: function(options) { + function simulator (options) { + var Simulator = require('./cmds/simulator.js'); var simulator = new Simulator({blockchainConfig: this.config.blockchainConfig}); simulator.run(options); - }, + } - generateTemplate: function(templateName, destinationFolder, name) { + function generateTemplate (templateName, destinationFolder, name) { + var TemplateGenerator = require('./cmds/template_generator.js'); var templateGenerator = new TemplateGenerator(templateName); templateGenerator.generate(destinationFolder, name); - }, + } + + function run (options) { + var Dashboard = require('./dashboard/dashboard.js'); - run: function(options) { - var self = this; var env = options.env; var engine = new Engine({ @@ -131,10 +118,9 @@ var Embark = { engine.events.emit('firstDeploymentDone'); } }); - }, + } - build: function(options) { - var self = this; + function build (options) { var engine = new Engine({ env: options.env, @@ -169,14 +155,14 @@ var Embark = { // needed due to child processes process.exit(); }); - }, + } - initTests: function(options) { + function initTests (options) { return new Test(options); - }, + } // TODO: should deploy if it hasn't already - upload: function(platform) { + function upload (platform) { if (platform === 'ipfs') { var ipfs = new IPFS({buildDir: 'dist/', plugins: this.plugins, storageConfig: this.config.storageConfig}); ipfs.deploy(); @@ -189,6 +175,18 @@ var Embark = { } } + return { + version: version, + initConfig: initConfig, + blockchain: blockchain, + simulator: simulator, + generateTemplate: generateTemplate, + run: run, + build: build, + initTests: initTests, + upload: upload + }; + }; module.exports = Embark; From dc9084b8a673c9e1856348d15187e1fbffc127eb Mon Sep 17 00:00:00 2001 From: Todd Baur Date: Sat, 11 Mar 2017 00:07:08 +0900 Subject: [PATCH 09/45] Dropped 'this' in Cmd and set internal var Embar to self execute --- lib/cmd.js | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/lib/cmd.js b/lib/cmd.js index e60c5e22..46883c33 100644 --- a/lib/cmd.js +++ b/lib/cmd.js @@ -1,10 +1,9 @@ var program = require('commander'); var colors = require('colors'); var shelljs = require('shelljs'); -var Embark = require('../lib/index'); +var Embark = require('../lib/index')(); var Cmd = function() { - this.Embark = Embark; program.version(Embark.version); }; @@ -28,7 +27,7 @@ Cmd.prototype.process = function(args) { }; Cmd.prototype.newApp = function() { - var self = this; + program .command('new [name]') .description('new application') @@ -40,32 +39,32 @@ Cmd.prototype.newApp = function() { console.log("e.g embark new --help for more information".green); process.exit(9); } - self.Embark.generateTemplate('boilerplate', './', name); + Embark.generateTemplate('boilerplate', './', name); }); }; Cmd.prototype.demo = function() { - var self = this; + program .command('demo') .description('create a working dapp with a SimpleStorage contract') .action(function() { - self.Embark.generateTemplate('demo', './', 'embark_demo'); + Embark.generateTemplate('demo', './', 'embark_demo'); }); }; Cmd.prototype.build = function() { - var self = this; + program .command('build [environment]') .description('deploy and build dapp at dist/ (default: development)') .action(function(env, options) { - self.Embark.build({env: env || 'development'}); + Embark.build({env: env || 'development'}); }); }; Cmd.prototype.run = function() { - var self = this; + program .command('run [environment]') .option('-p, --port [port]', 'port to run the dev webserver (default: 8000)') @@ -75,7 +74,7 @@ Cmd.prototype.run = function() { .option('--no-color', 'no colors in case it\'s needed for compatbility purposes') .description('run dapp (default: development)') .action(function(env, options) { - self.Embark.run({ + Embark.run({ env: env || 'development', serverPort: options.port, serverHost: options.host, @@ -86,22 +85,22 @@ Cmd.prototype.run = function() { }; Cmd.prototype.blockchain = function() { - var self = this; + program .command('blockchain [environment]') .option('-c, --client [client]', 'Use a specific ethereum client or simulator (supported: geth, parity, ethersim, testrpc') .description('run blockchain server (default: development)') .action(function(env ,options) { - self.Embark.initConfig(env || 'development', { + Embark.initConfig(env || 'development', { embarkConfig: 'embark.json', interceptLogs: false }); - self.Embark.blockchain(env || 'development', options.client || 'geth'); + Embark.blockchain(env || 'development', options.client || 'geth'); }); }; Cmd.prototype.simulator = function() { - var self = this; + program .command('simulator [environment]') .description('run a fast ethereum rpc simulator') @@ -109,11 +108,11 @@ Cmd.prototype.simulator = function() { .option('-p, --port [port]', 'port to run the rpc simulator (default: 8000)') .option('-h, --host [host]', 'host to run the rpc simulator (default: localhost)') .action(function(env, options) { - self.Embark.initConfig(env || 'development', { + Embark.initConfig(env || 'development', { embarkConfig: 'embark.json', interceptLogs: false }); - self.Embark.simulator({port: options.port, host: options.host}); + Embark.simulator({port: options.port, host: options.host}); }); }; @@ -127,16 +126,16 @@ Cmd.prototype.test = function() { }; Cmd.prototype.upload = function() { - var self = this; + program .command('upload [platform] [environment]') .description('upload your dapp to a decentralized storage. possible options: ipfs, swarm (e.g embark upload swarm)') .action(function(platform, env, options) { // TODO: get env in cmd line as well - self.Embark.initConfig(env || 'development', { + Embark.initConfig(env || 'development', { embarkConfig: 'embark.json', interceptLogs: false }); - self.Embark.upload(platform); + Embark.upload(platform); }); }; From b590312e20c93e7fdb40f45ade8795eaaede4767 Mon Sep 17 00:00:00 2001 From: Todd Baur Date: Sat, 11 Mar 2017 00:13:59 +0900 Subject: [PATCH 10/45] cleanup empty lines after function --- lib/cmd.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/lib/cmd.js b/lib/cmd.js index 46883c33..da226271 100644 --- a/lib/cmd.js +++ b/lib/cmd.js @@ -27,7 +27,6 @@ Cmd.prototype.process = function(args) { }; Cmd.prototype.newApp = function() { - program .command('new [name]') .description('new application') @@ -44,7 +43,6 @@ Cmd.prototype.newApp = function() { }; Cmd.prototype.demo = function() { - program .command('demo') .description('create a working dapp with a SimpleStorage contract') @@ -54,7 +52,6 @@ Cmd.prototype.demo = function() { }; Cmd.prototype.build = function() { - program .command('build [environment]') .description('deploy and build dapp at dist/ (default: development)') @@ -64,7 +61,6 @@ Cmd.prototype.build = function() { }; Cmd.prototype.run = function() { - program .command('run [environment]') .option('-p, --port [port]', 'port to run the dev webserver (default: 8000)') @@ -85,7 +81,6 @@ Cmd.prototype.run = function() { }; Cmd.prototype.blockchain = function() { - program .command('blockchain [environment]') .option('-c, --client [client]', 'Use a specific ethereum client or simulator (supported: geth, parity, ethersim, testrpc') @@ -100,7 +95,6 @@ Cmd.prototype.blockchain = function() { }; Cmd.prototype.simulator = function() { - program .command('simulator [environment]') .description('run a fast ethereum rpc simulator') @@ -126,7 +120,6 @@ Cmd.prototype.test = function() { }; Cmd.prototype.upload = function() { - program .command('upload [platform] [environment]') .description('upload your dapp to a decentralized storage. possible options: ipfs, swarm (e.g embark upload swarm)') From 173c5711475e6274915aa70f1147f62308e92755 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 10 Mar 2017 22:00:30 -0500 Subject: [PATCH 11/45] remove old services monitor; add new services monitor to engine; move previous checks to their modules inits --- lib/core/engine.js | 91 +++++++++++++++++++++------ lib/core/logger.js | 1 - lib/core/services.js | 119 ----------------------------------- lib/core/services_monitor.js | 84 +++++++++++++++++++++++++ lib/dashboard/dashboard.js | 1 - lib/dashboard/monitor.js | 10 ++- lib/index.js | 15 ++--- 7 files changed, 173 insertions(+), 148 deletions(-) delete mode 100644 lib/core/services.js create mode 100644 lib/core/services_monitor.js diff --git a/lib/core/engine.js b/lib/core/engine.js index ad248a7c..8765d6d2 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -1,11 +1,13 @@ +var http = require('http'); +var utils = require('./utils.js'); + var Events = require('./events.js'); var Logger = require('./logger.js'); var Config = require('./config.js'); var DeployManager = require('../contracts/deploy_manager.js'); var ABIGenerator = require('../contracts/abi.js'); -var Dashboard = require('../dashboard/dashboard.js'); -var ServicesMonitor = require('./services.js'); +var ServicesMonitor = require('./services_monitor.js'); var Pipeline = require('../pipeline/pipeline.js'); var Server = require('../pipeline/server.js'); var Watch = require('../pipeline/watch.js'); @@ -19,24 +21,34 @@ var Engine = function(options) { }; Engine.prototype.init = function(_options) { + var self = this; var options = _options || {}; this.events = new Events(); this.logger = options.logger || new Logger({logLevel: 'debug'}); this.config = new Config({env: this.env, logger: this.logger, events: this.events}); this.config.loadConfigFiles({embarkConfig: this.embarkConfig, interceptLogs: this.interceptLogs}); this.plugins = this.config.plugins; + + this.servicesMonitor = new ServicesMonitor({events: this.events, logger: this.logger}); + this.servicesMonitor.addCheck('embarkVersion', function(cb) { + return cb({name: 'Embark ' + self.version, status: 'green'}); + }); +}; + +Engine.prototype.startMonitor = function() { + this.servicesMonitor.startMonitor(); }; Engine.prototype.startService = function(serviceName, _options) { var options = _options || {}; var services = { - "monitor": this.monitorService, "pipeline": this.pipelineService, "abi": this.abiService, "deployment": this.deploymentService, "fileWatcher": this.fileWatchService, - "webServer": this.webServerService + "webServer": this.webServerService, + "ipfs": this.ipfsService }; var service = services[serviceName]; @@ -50,18 +62,6 @@ Engine.prototype.startService = function(serviceName, _options) { return service.apply(this, [options]); }; -Engine.prototype.monitorService = function(options) { - var servicesMonitor = new ServicesMonitor({ - logger: this.logger, - config: this.config, - serverHost: options.serverHost, - serverPort: options.serverPort, - runWebserver: options.runWebserver, - version: this.version - }); - servicesMonitor.startMonitor(); -}; - Engine.prototype.pipelineService = function(options) { var self = this; this.logger.setStatus("Building Assets"); @@ -134,15 +134,68 @@ Engine.prototype.fileWatchService = function(options) { }; Engine.prototype.webServerService = function(options) { + var self = this; var webServerConfig = this.config.webServerConfig; if (!webServerConfig.enabled) { return; } + + var host = options.host || webServerConfig.host; + var port = options.port || webServerConfig.port; + this.logger.setStatus("Starting Server"); var server = new Server({ logger: this.logger, - host: options.host || webServerConfig.host, - port: options.port || webServerConfig.port + host: host, + port: port + }); + + self.servicesMonitor.addCheck('Webserver', function(cb) { + var devServer = 'Webserver (http://' + host + ':' + port + ')'; + return cb({name: devServer, status: 'green'}); + }); + + server.start(function(){ + }); +}; + +Engine.prototype.ipfsService = function(options) { + var self = this; + self.servicesMonitor.addCheck('IPFS', function(cb) { + + utils.checkIsAvailable('http://localhost:5001', function(available) { + if (available) { + //Ideally this method should be in an IPFS API JSONRPC wrapper + //The URL should also be flexible to accept non-default IPFS url + self.logger.trace("Checking IPFS version..."); + http.get('http://localhost:5001/api/v0/version', function(res) { + var body = ''; + res.on('data', function(d) { + body += d; + }); + res.on('end', function() { + try{ + var parsed = JSON.parse(body); + if(parsed.Version){ + return cb({name: ("IPFS " + parsed.Version), status: 'green'}); + } + else{ + return cb({name: "IPFS ", status: 'green'}); + } + } + catch (e){ + return cb({name: "IPFS ", status: 'red'}); + } + }); + res.on('error', function(err) { + self.logger.trace("Check IPFS version error: " + err); + return cb({name: "IPFS ", status: 'red'}); + }); + }); + } + else { + return cb({name: "IPFS ", status: 'red'}); + } + }); }); - server.start(function(){}); }; module.exports = Engine; diff --git a/lib/core/logger.js b/lib/core/logger.js index 5007977c..442544a7 100644 --- a/lib/core/logger.js +++ b/lib/core/logger.js @@ -5,7 +5,6 @@ var Logger = function(options) { this.logLevel = options.logLevel || 'info'; this.logFunction = options.logFunction || console.log; this.contractsState = options.contractsState || function() {}; - this.availableServices = options.availableServices || function() {}; this.setStatus = options.setStatus || console.log; }; diff --git a/lib/core/services.js b/lib/core/services.js deleted file mode 100644 index 6d4cce51..00000000 --- a/lib/core/services.js +++ /dev/null @@ -1,119 +0,0 @@ -var Web3 = require('web3'); -var async = require('async'); -var http = require('http'); -var utils = require('./utils.js'); - -// TODO: needs a refactor and be done in a different way -var ServicesMonitor = function(options) { - this.logger = options.logger; - this.interval = options.interval || 5000; - this.config = options.config; - this.serverHost = options.serverHost || 'localhost'; - this.serverPort = options.serverPort || 8000; - this.runWebserver = options.runWebserver; - this.version = options.version; -}; - -ServicesMonitor.prototype.startMonitor = function() { - this.check(); - this.monitor = setInterval(this.check.bind(this), this.interval); -}; - -ServicesMonitor.prototype.stopMonitor = function() { - clearInterval(this.monitor); -}; - -ServicesMonitor.prototype.check = function() { - var self = this; - async.waterfall([ - function connectWeb3(callback) { - self.logger.trace('connectWeb3'); - var web3 = new Web3(); - var web3Endpoint = 'http://' + self.config.blockchainConfig.rpcHost + ':' + self.config.blockchainConfig.rpcPort; - web3.setProvider(new web3.providers.HttpProvider(web3Endpoint)); - callback(null, web3, []); - }, - function addEmbarkVersion(web3, result, callback) { - self.logger.trace('addEmbarkVersion'); - result.push(('Embark ' + self.version).green); - callback(null, web3, result); - }, - function checkEthereum(web3, result, callback) { - self.logger.trace('checkEthereum'); - var service; - if (web3.isConnected()) { - service = (web3.version.node.split("/")[0] + " " + web3.version.node.split("/")[1].split("-")[0] + " (Ethereum)").green; - } else { - service = "No Blockchain node found".red; - } - result.push(service); - callback(null, web3, result); - }, - function checkWhisper(web3, result, callback) { - self.logger.trace('checkWhisper'); - web3.version.getWhisper(function(err, res) { - var service = 'Whisper'; - result.push(err ? service.red : service.green); - callback(null, result); - }); - }, - function checkIPFS(result, callback) { - self.logger.trace('checkIPFS'); - - utils.checkIsAvailable('http://localhost:5001', function(available) { - if (available) { - //Ideally this method should be in an IPFS API JSONRPC wrapper - //The URL should also be flexible to accept non-default IPFS url - self.logger.trace("Checking IPFS version..."); - http.get('http://localhost:5001/api/v0/version', function(res) { - var body = ''; - res.on('data', function(d) { - body += d; - }); - res.on('end', function() { - try{ - var parsed = JSON.parse(body); - if(parsed.Version){ - result.push(("IPFS " + parsed.Version).green); - } - else{ - result.push("IPFS".green); - } - } - catch (e){ - result.push("IPFS".red); - } - callback(null, result); - }); - res.on('error', function(err) { - self.logger.trace("Check IPFS version error: " + err); - result.push("IPFS".red); - callback(null, result); - }); - }); - } - else { - result.push('IPFS'.red); - return callback(null, result); - } - }); - }, - function checkDevServer(result, callback) { - var host = self.serverHost || self.config.webServerConfig.host; - var port = self.serverPort || self.config.webServerConfig.port; - self.logger.trace('checkDevServer'); - var devServer = 'Webserver (http://' + host + ':' + port + ')'; - devServer = (self.runWebserver) ? devServer.green : devServer.red; - result.push(devServer); - callback(null, result); - } - ], function(err, result) { - if (err) { - self.logger.error(err.message); - } else { - self.logger.availableServices(result); - } - }); -}; - -module.exports = ServicesMonitor; diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js new file mode 100644 index 00000000..65673a8b --- /dev/null +++ b/lib/core/services_monitor.js @@ -0,0 +1,84 @@ +var Web3 = require('web3'); +var async = require('async'); +var http = require('http'); +var utils = require('./utils.js'); + +var ServicesMonitor = function(options) { + this.events = options.events; + this.logger = options.logger; + this.checkList = {}; + this.checkTimers = {}; + this.checkState = {}; +}; + +ServicesMonitor.prototype.addCheck = function(name, checkFn, time) { + this.logger.info('add check'); + // TODO: check if a service with the same name already exists + this.checkList[name] = {fn: checkFn, interval: time || 5000}; +}; + +ServicesMonitor.prototype.startMonitor = function() { + var self = this; + var checkName; + + for (checkName in this.checkList) { + var check = this.checkList[checkName]; + + self.events.on('check:' + checkName, function(obj) { + self.logger.info(JSON.stringify(obj)); + self.checkState[checkName] = obj.name[obj.status]; + self.events.emit("servicesState", self.checkState); + }); + + this.checkTimers[checkName] = setInterval(function() { + check.fn.call(check.fn, function(obj) { + self.events.emit('check:' + checkName, obj); + }); + }, check.interval); + + check.fn.call(check.fn, function(obj) { + self.events.emit('check:' + checkName, obj); + }); + } +}; + +// TODO: old checks to be moved +ServicesMonitor.prototype.check = function() { + var self = this; + async.waterfall([ + function connectWeb3(callback) { + self.logger.trace('connectWeb3'); + var web3 = new Web3(); + var web3Endpoint = 'http://' + self.config.blockchainConfig.rpcHost + ':' + self.config.blockchainConfig.rpcPort; + web3.setProvider(new web3.providers.HttpProvider(web3Endpoint)); + callback(null, web3, []); + }, + function checkEthereum(web3, result, callback) { + self.logger.trace('checkEthereum'); + var service; + if (web3.isConnected()) { + service = (web3.version.node.split("/")[0] + " " + web3.version.node.split("/")[1].split("-")[0] + " (Ethereum)").green; + } else { + service = "No Blockchain node found".red; + } + result.push(service); + callback(null, web3, result); + }, + function checkWhisper(web3, result, callback) { + self.logger.trace('checkWhisper'); + web3.version.getWhisper(function(err, res) { + var service = 'Whisper'; + result.push(err ? service.red : service.green); + callback(null, result); + }); + } + ], function(err, result) { + if (err) { + self.logger.error(err.message); + } else { + self.logger.availableServices(result); + } + }); +}; + +module.exports = ServicesMonitor; diff --git a/lib/dashboard/dashboard.js b/lib/dashboard/dashboard.js index d67b142d..367b77b8 100644 --- a/lib/dashboard/dashboard.js +++ b/lib/dashboard/dashboard.js @@ -23,7 +23,6 @@ Dashboard.prototype.start = function(done) { monitor = new Monitor({env: self.env, console: console}); self.logger.logFunction = monitor.logEntry; self.logger.contractsState = monitor.setContracts; - self.logger.availableServices = monitor.availableServices; self.logger.setStatus = monitor.setStatus.bind(monitor); self.logger.info('========================'.bold.green); diff --git a/lib/dashboard/monitor.js b/lib/dashboard/monitor.js index 210fa7ce..c1ce4c8d 100644 --- a/lib/dashboard/monitor.js +++ b/lib/dashboard/monitor.js @@ -40,7 +40,15 @@ function Dashboard(options) { this.input.focus(); } -Dashboard.prototype.availableServices = function(services) { +Dashboard.prototype.availableServices = function(_services) { + var services = []; + var checkName; + for (checkName in _services) { + services.push(_services[checkName]); + } + + console.log(services); + this.progress.setContent(services.join('\n')); this.screen.render(); }; diff --git a/lib/index.js b/lib/index.js index 6b358d23..926dc6e1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -90,6 +90,12 @@ var Embark = { dashboard.console.runCode(abi); }); + engine.logger.info('dashboard start'); + engine.events.on('servicesState', function(servicesState) { + engine.logger.info('servicesState event'); + dashboard.monitor.availableServices(servicesState); + }); + callback(); }); }, @@ -99,16 +105,10 @@ var Embark = { engine.logger.info("loaded plugins: " + pluginList.join(", ")); } - if (options.useDashboard) { - engine.startService("monitor", { - serverHost: options.serverHost, - serverPort: options.serverPort, - runWebserver: options.runWebserver - }); - } engine.startService("pipeline"); engine.startService("abi"); engine.startService("deployment"); + engine.startService("ipfs"); engine.deployManager.deployContracts(function() { engine.startService("fileWatcher"); @@ -118,6 +118,7 @@ var Embark = { port: options.serverPort }); } + engine.startMonitor(); callback(); }); } From d8b41162da44c57b82a95b2a1c520663fe1ee411 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E4=BF=8A=E9=92=A6?= Date: Sat, 11 Mar 2017 16:54:59 +0800 Subject: [PATCH 12/45] Update README.md Correct directory, update dapp structure, update deployment usage guide --- README.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6e2edae7..449de065 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ Table of Contents * [Testing Contracts](#tests) * [Working with different chains](#working-with-different-chains) * [Custom Application Structure](#structuring-application) -* [Deploying to IPFS](#deploying-to-ipfs) +* [Deploying to IPFS](#deploying-to-ipfs-and-swarm) * [Extending Functionality with Plugins](#plugins) * [Donations](#donations) @@ -147,6 +147,9 @@ DApp Structure config/ |___ blockchain.json #environments configuration |___ contracts.json #contracts configuration + |___ storage.json #storage configuration + |___ webserver.json #webserver configuration + |___ communication.json #communication configuration test/ |___ #contracts tests ``` @@ -323,14 +326,20 @@ events: Client side deployment will be automatically available in Embark for existing contracts: ```Javascript - SimpleStorage.deploy().then(function(anotherSimpleStorage) {}); + SimpleStorage.deploy([args], {options}).then(function(anotherSimpleStorage) {}); ``` or it can be manually definied as ```Javascript var myContract = new EmbarkJS.Contract({abi: abiObject, code: code}); - myContract.deploy().then(function(anotherMyContractObject) {}); + myContract.deploy([args], {options}).then(function(anotherMyContractObject) {}); +``` + +so you can define your gas as + +```Javascript + myContract.deploy([100, "seconde argument"], {gas: 800000}).then(function(anotherMyContractObject) {}); ``` EmbarkJS - Storage From 15e8b4692340f967f1846eddb1dff68a4f684620 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E4=BF=8A=E9=92=A6?= Date: Sat, 11 Mar 2017 17:04:51 +0800 Subject: [PATCH 13/45] Update README.md --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 449de065..3fa98e19 100644 --- a/README.md +++ b/README.md @@ -145,11 +145,11 @@ DApp Structure |___ css/ |___ js/ config/ - |___ blockchain.json #environments configuration - |___ contracts.json #contracts configuration - |___ storage.json #storage configuration - |___ webserver.json #webserver configuration - |___ communication.json #communication configuration + |___ blockchain.json #rpc and blockchain configuration + |___ contracts.json #ethereum contracts configuration + |___ storage.json #ipfs configuration + |___ communication.json #whisper/orbit configuration + |___ webserver.json #dev webserver configuration test/ |___ #contracts tests ``` From f8322ba665378b314e153858f594487f3ff94d86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E4=BF=8A=E9=92=A6?= Date: Sat, 11 Mar 2017 17:07:34 +0800 Subject: [PATCH 14/45] Update embarkjs.rst update deploy usage --- docs/embarkjs.rst | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/docs/embarkjs.rst b/docs/embarkjs.rst index 50c0e747..dd6a39fc 100644 --- a/docs/embarkjs.rst +++ b/docs/embarkjs.rst @@ -20,11 +20,17 @@ existing contracts: .. code:: javascript - SimpleStorage.deploy().then(function(anotherSimpleStorage) {}); + SimpleStorage.deploy([args], {options}).then(function(anotherSimpleStorage) {}); or it can be manually definied as .. code:: javascript var myContract = new EmbarkJS.Contract({abi: abiObject, code: code}); - myContract.deploy().then(function(anotherMyContractObject) {}); + myContract.deploy([args], {options}).then(function(anotherMyContractObject) {}); + +so you can define your gas as + +.. code:: javascript + + myContract.deploy([100, "seconde argument"], {gas: 800000}).then(function(anotherMyContractObject) {}); From 4bf31328e2dfdb183b9adcef28349b66c57794c0 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 07:32:16 -0500 Subject: [PATCH 15/45] use eachObject to avoid async issues; support non-recurrent checks --- lib/core/engine.js | 3 +-- lib/core/services_monitor.js | 40 +++++++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/lib/core/engine.js b/lib/core/engine.js index 8765d6d2..eee754a4 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -32,7 +32,7 @@ Engine.prototype.init = function(_options) { this.servicesMonitor = new ServicesMonitor({events: this.events, logger: this.logger}); this.servicesMonitor.addCheck('embarkVersion', function(cb) { return cb({name: 'Embark ' + self.version, status: 'green'}); - }); + }, 0); }; Engine.prototype.startMonitor = function() { @@ -160,7 +160,6 @@ Engine.prototype.webServerService = function(options) { Engine.prototype.ipfsService = function(options) { var self = this; self.servicesMonitor.addCheck('IPFS', function(cb) { - utils.checkIsAvailable('http://localhost:5001', function(available) { if (available) { //Ideally this method should be in an IPFS API JSONRPC wrapper diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index 65673a8b..25e24c62 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -3,6 +3,18 @@ var async = require('async'); var http = require('http'); var utils = require('./utils.js'); +// TODO: repeated, add this to an async extensions file +function asyncEachObject(object, iterator, callback) { + async.each( + Object.keys(object || {}), + function(key, next){ + iterator(key, object[key], next); + }, + callback + ); +} +async.eachObject = asyncEachObject; + var ServicesMonitor = function(options) { this.events = options.events; this.logger = options.logger; @@ -12,34 +24,38 @@ var ServicesMonitor = function(options) { }; ServicesMonitor.prototype.addCheck = function(name, checkFn, time) { - this.logger.info('add check'); + this.logger.info('add check: ' + name); // TODO: check if a service with the same name already exists this.checkList[name] = {fn: checkFn, interval: time || 5000}; }; ServicesMonitor.prototype.startMonitor = function() { + this.logger.info('startMonitor'); var self = this; - var checkName; - - for (checkName in this.checkList) { - var check = this.checkList[checkName]; + async.eachObject(this.checkList, function(checkName, check, callback) { self.events.on('check:' + checkName, function(obj) { - self.logger.info(JSON.stringify(obj)); + //self.logger.info('checked ' + checkName); + //self.logger.info(JSON.stringify(obj)); + //self.logger.info(JSON.stringify(self.checkState)); self.checkState[checkName] = obj.name[obj.status]; self.events.emit("servicesState", self.checkState); }); - this.checkTimers[checkName] = setInterval(function() { - check.fn.call(check.fn, function(obj) { - self.events.emit('check:' + checkName, obj); - }); - }, check.interval); + if (check.interval !== 0) { + self.checkTimers[checkName] = setInterval(function() { + check.fn.call(check.fn, function(obj) { + self.events.emit('check:' + checkName, obj); + }); + }, check.interval); + } check.fn.call(check.fn, function(obj) { self.events.emit('check:' + checkName, obj); }); - } + }, function(err) { + }); + this.logger.info(JSON.stringify(this.checkState)); }; // TODO: old checks to be moved From ea70d09e8c3be3b52fa394dce402727bdbf9e9b7 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 10:29:45 -0500 Subject: [PATCH 16/45] move ethereum and whisper checks; refactor web3 connection --- lib/contracts/deploy_manager.js | 32 ++++++++++++++------------ lib/core/engine.js | 35 +++++++++++++++++++++++++++-- lib/core/services_monitor.js | 40 --------------------------------- lib/dashboard/monitor.js | 2 -- lib/index.js | 2 +- 5 files changed, 52 insertions(+), 59 deletions(-) diff --git a/lib/contracts/deploy_manager.js b/lib/contracts/deploy_manager.js index 0293b97c..04ce315f 100644 --- a/lib/contracts/deploy_manager.js +++ b/lib/contracts/deploy_manager.js @@ -33,21 +33,25 @@ DeployManager.prototype.deployContracts = function(done) { }); contractsManager.build(callback); }, - function connectWithWeb3(contractsManager, callback) { - var web3; - if (self.web3) { - web3 = self.web3; - } else { - web3 = new Web3(); - var web3Endpoint = 'http://' + self.config.blockchainConfig.rpcHost + ':' + self.config.blockchainConfig.rpcPort; - web3.setProvider(new web3.providers.HttpProvider(web3Endpoint)); - if (!web3.isConnected()) { - self.logger.error(("Couldn't connect to " + web3Endpoint.underline + " are you sure it's on?").red); - self.logger.info("make sure you have an ethereum node or simulator running. e.g 'embark blockchain'".magenta); - return callback(Error("error connecting to blockchain node")); - } + function checkWeb3IsConnected(contractsManager, callback) { + if (!self.web3) { + return callback(Error("no web3 instance found")); + } + if (self.web3.currentProvider.isConnected !== undefined && !self.web3.isConnected()) { + self.logger.error(("Couldn't connect to an Ethereum node are you sure it's on?").red); + self.logger.info("make sure you have an Ethereum node or simulator running. e.g 'embark blockchain'".magenta); + return callback(Error("error connecting to blockchain node")); + } + if (self.web3.currentProvider.isConnected === undefined) { + self.web3.version.getNode(function(err, version) { + if (err) { + return callback(Error("error connecting to blockchain node")); + } + return callback(null, contractsManager, self.web3); + }); + } else { + return callback(null, contractsManager, self.web3); } - callback(null, contractsManager, web3); }, function setDefaultAccount(contractsManager, web3, callback) { web3.eth.getAccounts(function(err, accounts) { diff --git a/lib/core/engine.js b/lib/core/engine.js index eee754a4..3a821ea9 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -1,4 +1,5 @@ var http = require('http'); +var Web3 = require('web3'); var utils = require('./utils.js'); var Events = require('./events.js'); @@ -48,7 +49,8 @@ Engine.prototype.startService = function(serviceName, _options) { "deployment": this.deploymentService, "fileWatcher": this.fileWatchService, "webServer": this.webServerService, - "ipfs": this.ipfsService + "ipfs": this.ipfsService, + "web3": this.web3Service }; var service = services[serviceName]; @@ -111,7 +113,7 @@ Engine.prototype.abiService = function(options) { Engine.prototype.deploymentService = function(options) { var self = this; this.deployManager = new DeployManager({ - web3: options.web3, + web3: options.web3 || self.web3, trackContracts: options.trackContracts, config: this.config, logger: this.logger, @@ -197,4 +199,33 @@ Engine.prototype.ipfsService = function(options) { }); }; +Engine.prototype.web3Service = function(options) { + var self = this; + this.web3 = options.web3; + if (this.web3 === undefined) { + this.web3 = new Web3(); + var web3Endpoint = 'http://' + this.config.blockchainConfig.rpcHost + ':' + this.config.blockchainConfig.rpcPort; + this.web3.setProvider(new this.web3.providers.HttpProvider(web3Endpoint)); + } + + self.servicesMonitor.addCheck('Ethereum', function(cb) { + if (self.web3.isConnected()) { + return cb({name: (self.web3.version.node.split("/")[0] + " " + self.web3.version.node.split("/")[1].split("-")[0] + " (Ethereum)"), status: 'green'}); + } else { + return cb({name: "No Blockchain node found", status: 'red'}); + } + }); + + self.servicesMonitor.addCheck('Whisper', function(cb) { + self.web3.version.getWhisper(function(err, res) { + if (err) { + return cb({name: 'Whisper', status: 'red'}); + } else { + return cb({name: 'Whisper', status: 'green'}); + } + }); + }); +}; + + module.exports = Engine; diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index 25e24c62..f0f61b76 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -55,46 +55,6 @@ ServicesMonitor.prototype.startMonitor = function() { }); }, function(err) { }); - this.logger.info(JSON.stringify(this.checkState)); -}; - -// TODO: old checks to be moved -ServicesMonitor.prototype.check = function() { - var self = this; - async.waterfall([ - function connectWeb3(callback) { - self.logger.trace('connectWeb3'); - var web3 = new Web3(); - var web3Endpoint = 'http://' + self.config.blockchainConfig.rpcHost + ':' + self.config.blockchainConfig.rpcPort; - web3.setProvider(new web3.providers.HttpProvider(web3Endpoint)); - callback(null, web3, []); - }, - function checkEthereum(web3, result, callback) { - self.logger.trace('checkEthereum'); - var service; - if (web3.isConnected()) { - service = (web3.version.node.split("/")[0] + " " + web3.version.node.split("/")[1].split("-")[0] + " (Ethereum)").green; - } else { - service = "No Blockchain node found".red; - } - result.push(service); - callback(null, web3, result); - }, - function checkWhisper(web3, result, callback) { - self.logger.trace('checkWhisper'); - web3.version.getWhisper(function(err, res) { - var service = 'Whisper'; - result.push(err ? service.red : service.green); - callback(null, result); - }); - } - ], function(err, result) { - if (err) { - self.logger.error(err.message); - } else { - self.logger.availableServices(result); - } - }); }; module.exports = ServicesMonitor; diff --git a/lib/dashboard/monitor.js b/lib/dashboard/monitor.js index c1ce4c8d..b1a243c4 100644 --- a/lib/dashboard/monitor.js +++ b/lib/dashboard/monitor.js @@ -47,8 +47,6 @@ Dashboard.prototype.availableServices = function(_services) { services.push(_services[checkName]); } - console.log(services); - this.progress.setContent(services.join('\n')); this.screen.render(); }; diff --git a/lib/index.js b/lib/index.js index 926dc6e1..91b671af 100644 --- a/lib/index.js +++ b/lib/index.js @@ -92,7 +92,6 @@ var Embark = { engine.logger.info('dashboard start'); engine.events.on('servicesState', function(servicesState) { - engine.logger.info('servicesState event'); dashboard.monitor.availableServices(servicesState); }); @@ -105,6 +104,7 @@ var Embark = { engine.logger.info("loaded plugins: " + pluginList.join(", ")); } + engine.startService("web3"); engine.startService("pipeline"); engine.startService("abi"); engine.startService("deployment"); From c2c41549c17ae1ec35decd1e060dec81deecddb1 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 10:38:05 -0500 Subject: [PATCH 17/45] cleanup; remove unneded requires --- lib/contracts/deploy_manager.js | 1 - lib/core/services_monitor.js | 6 ------ lib/core/test.js | 8 -------- lib/index.js | 1 - 4 files changed, 16 deletions(-) diff --git a/lib/contracts/deploy_manager.js b/lib/contracts/deploy_manager.js index 04ce315f..ebbe5d08 100644 --- a/lib/contracts/deploy_manager.js +++ b/lib/contracts/deploy_manager.js @@ -1,5 +1,4 @@ var async = require('async'); -var Web3 = require('web3'); var Deploy = require('./deploy.js'); var ContractsManager = require('./contracts.js'); diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index f0f61b76..402eb1e7 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -1,7 +1,4 @@ -var Web3 = require('web3'); var async = require('async'); -var http = require('http'); -var utils = require('./utils.js'); // TODO: repeated, add this to an async extensions file function asyncEachObject(object, iterator, callback) { @@ -35,9 +32,6 @@ ServicesMonitor.prototype.startMonitor = function() { async.eachObject(this.checkList, function(checkName, check, callback) { self.events.on('check:' + checkName, function(obj) { - //self.logger.info('checked ' + checkName); - //self.logger.info(JSON.stringify(obj)); - //self.logger.info(JSON.stringify(self.checkState)); self.checkState[checkName] = obj.name[obj.status]; self.events.emit("servicesState", self.checkState); }); diff --git a/lib/core/test.js b/lib/core/test.js index ac1b5e37..fe3b0406 100644 --- a/lib/core/test.js +++ b/lib/core/test.js @@ -1,15 +1,7 @@ var async = require('async'); var Web3 = require('web3'); -var Embark = require('../index.js'); - var Engine = require('./engine.js'); - -var ABIGenerator = require('../contracts/abi.js'); -var ContractsManager = require('../contracts/contracts.js'); -var Deploy = require('../contracts/deploy.js'); - -var Config = require('./config.js'); var RunCode = require('./runCode.js'); var TestLogger = require('./test_logger.js'); diff --git a/lib/index.js b/lib/index.js index 91b671af..6a71a03f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,7 +2,6 @@ var async = require('async'); //require("./core/debug_util.js")(__filename, async); -var Web3 = require('web3'); var colors = require('colors'); var Engine = require('./core/engine.js'); From 92f9df411716b2bcd4ac253e3667e0eeae912787 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 10:52:02 -0500 Subject: [PATCH 18/45] remove async extension to a module --- lib/contracts/compiler.js | 13 +------------ lib/core/async_extend.js | 14 ++++++++++++++ lib/core/services_monitor.js | 14 +------------- 3 files changed, 16 insertions(+), 25 deletions(-) create mode 100644 lib/core/async_extend.js diff --git a/lib/contracts/compiler.js b/lib/contracts/compiler.js index f7a7c70e..3d282266 100644 --- a/lib/contracts/compiler.js +++ b/lib/contracts/compiler.js @@ -1,18 +1,7 @@ /*jshint esversion: 6, loopfunc: true */ -var async = require('async'); +var async = require('../core/async_extend.js'); var SolcW = require('./solcW.js'); -function asyncEachObject(object, iterator, callback) { - async.each( - Object.keys(object || {}), - function(key, next){ - iterator(key, object[key], next); - }, - callback - ); -} -async.eachObject = asyncEachObject; - var Compiler = function(options) { this.plugins = options.plugins; this.logger = options.logger; diff --git a/lib/core/async_extend.js b/lib/core/async_extend.js new file mode 100644 index 00000000..1c6db4f5 --- /dev/null +++ b/lib/core/async_extend.js @@ -0,0 +1,14 @@ +var async = require('async'); + +function asyncEachObject(object, iterator, callback) { + async.each( + Object.keys(object || {}), + function(key, next){ + iterator(key, object[key], next); + }, + callback + ); +} +async.eachObject = asyncEachObject; + +module.exports = async; diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index 402eb1e7..0dd11919 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -1,16 +1,4 @@ -var async = require('async'); - -// TODO: repeated, add this to an async extensions file -function asyncEachObject(object, iterator, callback) { - async.each( - Object.keys(object || {}), - function(key, next){ - iterator(key, object[key], next); - }, - callback - ); -} -async.eachObject = asyncEachObject; +var async = require('./async_extend.js'); var ServicesMonitor = function(options) { this.events = options.events; From c514765d0bceb68a3d07c9389f14d0715d36d614 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 11:03:20 -0500 Subject: [PATCH 19/45] move misc utils modules to their own folder --- lib/cmds/template_generator.js | 2 +- lib/contracts/compiler.js | 2 +- lib/contracts/solcW.js | 2 +- lib/core/config.js | 2 +- lib/core/engine.js | 2 +- lib/core/fs.js | 2 +- lib/core/plugin.js | 2 +- lib/core/plugins.js | 2 +- lib/core/services_monitor.js | 2 +- lib/dashboard/console.js | 2 +- lib/index.js | 2 +- lib/{core => utils}/async_extend.js | 0 lib/{core => utils}/debug_util.js | 0 lib/{core => utils}/utils.js | 0 14 files changed, 11 insertions(+), 11 deletions(-) rename lib/{core => utils}/async_extend.js (100%) rename lib/{core => utils}/debug_util.js (100%) rename lib/{core => utils}/utils.js (100%) diff --git a/lib/cmds/template_generator.js b/lib/cmds/template_generator.js index a0aa482f..42c63288 100644 --- a/lib/cmds/template_generator.js +++ b/lib/cmds/template_generator.js @@ -1,5 +1,5 @@ var fs = require('../core/fs.js'); -var utils = require('../core/utils.js'); +var utils = require('../utils/utils.js'); var TemplateGenerator = function(templateName) { this.templateName = templateName; diff --git a/lib/contracts/compiler.js b/lib/contracts/compiler.js index 3d282266..3c258294 100644 --- a/lib/contracts/compiler.js +++ b/lib/contracts/compiler.js @@ -1,5 +1,5 @@ /*jshint esversion: 6, loopfunc: true */ -var async = require('../core/async_extend.js'); +var async = require('../utils/async_extend.js'); var SolcW = require('./solcW.js'); var Compiler = function(options) { diff --git a/lib/contracts/solcW.js b/lib/contracts/solcW.js index eec22b10..e9a0f9ad 100644 --- a/lib/contracts/solcW.js +++ b/lib/contracts/solcW.js @@ -1,4 +1,4 @@ -var utils = require('../core/utils.js'); +var utils = require('../utils/utils.js'); var solcProcess; var compilerLoaded = false; diff --git a/lib/core/config.js b/lib/core/config.js index 3db31a9a..c87b6761 100644 --- a/lib/core/config.js +++ b/lib/core/config.js @@ -1,6 +1,6 @@ var fs = require('./fs.js'); var Plugins = require('./plugins.js'); -var utils = require('./utils.js'); +var utils = require('../utils/utils.js'); // TODO: add wrapper for fs so it can also work in the browser // can work with both read and save diff --git a/lib/core/engine.js b/lib/core/engine.js index 3a821ea9..6122bf8b 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -1,6 +1,6 @@ var http = require('http'); var Web3 = require('web3'); -var utils = require('./utils.js'); +var utils = require('../utils/utils.js'); var Events = require('./events.js'); var Logger = require('./logger.js'); diff --git a/lib/core/fs.js b/lib/core/fs.js index dbbf7480..a88816e4 100644 --- a/lib/core/fs.js +++ b/lib/core/fs.js @@ -1,5 +1,5 @@ var fs = require('fs-extra'); -var utils = require('./utils.js'); +var utils = require('../utils/utils.js'); function mkdirpSync() { return fs.mkdirpSync.apply(fs.mkdirpSync, arguments); diff --git a/lib/core/plugin.js b/lib/core/plugin.js index 32765ea3..9d6ff37a 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -1,6 +1,6 @@ /*jshint esversion: 6, loopfunc: true */ var fs = require('./fs.js'); -var utils = require('./utils.js'); +var utils = require('../utils/utils.js'); // TODO: pass other params like blockchainConfig, contract files, etc.. var Plugin = function(options) { diff --git a/lib/core/plugins.js b/lib/core/plugins.js index 4132a280..c3319183 100644 --- a/lib/core/plugins.js +++ b/lib/core/plugins.js @@ -1,5 +1,5 @@ var Plugin = require('./plugin.js'); -var utils = require('./utils.js'); +var utils = require('../utils/utils.js'); var Plugins = function(options) { this.pluginList = options.plugins || []; diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index 0dd11919..4049e320 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -1,4 +1,4 @@ -var async = require('./async_extend.js'); +var async = require('../utils/async_extend.js'); var ServicesMonitor = function(options) { this.events = options.events; diff --git a/lib/dashboard/console.js b/lib/dashboard/console.js index c510f204..51ed85f8 100644 --- a/lib/dashboard/console.js +++ b/lib/dashboard/console.js @@ -1,4 +1,4 @@ -var utils = require('../core/utils.js'); +var utils = require('../utils/utils.js'); var RunCode = require('../core/runCode.js'); var Console = function(options) { diff --git a/lib/index.js b/lib/index.js index 6a71a03f..fc0c360e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,6 @@ /*jshint esversion: 6 */ var async = require('async'); -//require("./core/debug_util.js")(__filename, async); +//require("./utils/debug_util.js")(__filename, async); var colors = require('colors'); diff --git a/lib/core/async_extend.js b/lib/utils/async_extend.js similarity index 100% rename from lib/core/async_extend.js rename to lib/utils/async_extend.js diff --git a/lib/core/debug_util.js b/lib/utils/debug_util.js similarity index 100% rename from lib/core/debug_util.js rename to lib/utils/debug_util.js diff --git a/lib/core/utils.js b/lib/utils/utils.js similarity index 100% rename from lib/core/utils.js rename to lib/utils/utils.js From 2dec088a9b66f595620e3ed167264c257c492613 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 11:17:52 -0500 Subject: [PATCH 20/45] add service check error --- lib/core/services_monitor.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index 4049e320..2177d5f2 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -35,7 +35,12 @@ ServicesMonitor.prototype.startMonitor = function() { check.fn.call(check.fn, function(obj) { self.events.emit('check:' + checkName, obj); }); + callback(); }, function(err) { + if (err) { + self.logger.error("error running service check"); + self.logger.error(err.message); + } }); }; From 5cf287a747ed5b22ceb7ac09af9b8c9a54de025e Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 11:23:42 -0500 Subject: [PATCH 21/45] implement stopCheck --- lib/core/services_monitor.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index 2177d5f2..04bdddea 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -10,10 +10,16 @@ var ServicesMonitor = function(options) { ServicesMonitor.prototype.addCheck = function(name, checkFn, time) { this.logger.info('add check: ' + name); - // TODO: check if a service with the same name already exists this.checkList[name] = {fn: checkFn, interval: time || 5000}; }; +ServicesMonitor.prototype.stopCheck = function(name) { + clearInterval(this.checkTimers[name]); + delete this.checkTimers[name]; + delete this.checkList[name]; + delete this.checkState[name]; +}; + ServicesMonitor.prototype.startMonitor = function() { this.logger.info('startMonitor'); var self = this; From 8d8ff671f7727d69a2bfa8150595f44f56046a50 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 11:48:12 -0500 Subject: [PATCH 22/45] if monitor is already running then init service --- lib/core/services_monitor.js | 56 +++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index 04bdddea..04febab6 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -6,11 +6,41 @@ var ServicesMonitor = function(options) { this.checkList = {}; this.checkTimers = {}; this.checkState = {}; + this.working = false; }; -ServicesMonitor.prototype.addCheck = function(name, checkFn, time) { - this.logger.info('add check: ' + name); - this.checkList[name] = {fn: checkFn, interval: time || 5000}; +ServicesMonitor.prototype.initCheck = function(checkName) { + var self = this; + var check = this.checkList[checkName]; + + if (!check) { return false; } + + self.events.on('check:' + checkName, function(obj) { + self.checkState[checkName] = obj.name[obj.status]; + self.events.emit("servicesState", self.checkState); + }); + + if (check.interval !== 0) { + self.checkTimers[checkName] = setInterval(function() { + check.fn.call(check.fn, function(obj) { + self.events.emit('check:' + checkName, obj); + }); + }, check.interval); + } + + check.fn.call(check.fn, function(obj) { + self.events.emit('check:' + checkName, obj); + }); +}; + +ServicesMonitor.prototype.addCheck = function(checkName, checkFn, time) { + var self = this; + this.logger.trace('add check: ' + checkName); + this.checkList[checkName] = {fn: checkFn, interval: time || 5000}; + + if (this.working) { + this.initCheck(checkName); + } }; ServicesMonitor.prototype.stopCheck = function(name) { @@ -21,26 +51,12 @@ ServicesMonitor.prototype.stopCheck = function(name) { }; ServicesMonitor.prototype.startMonitor = function() { - this.logger.info('startMonitor'); var self = this; + this.working = true; + this.logger.trace('startMonitor'); async.eachObject(this.checkList, function(checkName, check, callback) { - self.events.on('check:' + checkName, function(obj) { - self.checkState[checkName] = obj.name[obj.status]; - self.events.emit("servicesState", self.checkState); - }); - - if (check.interval !== 0) { - self.checkTimers[checkName] = setInterval(function() { - check.fn.call(check.fn, function(obj) { - self.events.emit('check:' + checkName, obj); - }); - }, check.interval); - } - - check.fn.call(check.fn, function(obj) { - self.events.emit('check:' + checkName, obj); - }); + self.initCheck(checkName); callback(); }, function(err) { if (err) { From 54420b327e2ef5b5c5d3e26445e6446032f822f7 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 12:27:10 -0500 Subject: [PATCH 23/45] detect when ethereum node comes back online and redeploy --- lib/core/services_monitor.js | 11 +++++++++++ lib/index.js | 10 +++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index 04febab6..2685c5c2 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -1,5 +1,8 @@ var async = require('../utils/async_extend.js'); +// TODO: need to separate colors from states +// i.e use status: /on|off|warn/ not /red|green/ +// it's up to the logger or console to determine the color var ServicesMonitor = function(options) { this.events = options.events; this.logger = options.logger; @@ -16,7 +19,15 @@ ServicesMonitor.prototype.initCheck = function(checkName) { if (!check) { return false; } self.events.on('check:' + checkName, function(obj) { + // TODO: see todo above + if (check && check.status === 'red' && obj.status === 'green') { + self.events.emit('check:backOnline:' + checkName); + } + if (check && check.status === 'green' && obj.status === 'red') { + self.events.emit('check:wentOffline:' + checkName); + } self.checkState[checkName] = obj.name[obj.status]; + check.status = obj.status; self.events.emit("servicesState", self.checkState); }); diff --git a/lib/index.js b/lib/index.js index fc0c360e..4ed7ad38 100644 --- a/lib/index.js +++ b/lib/index.js @@ -103,12 +103,21 @@ var Embark = { engine.logger.info("loaded plugins: " + pluginList.join(", ")); } + engine.startMonitor(); engine.startService("web3"); engine.startService("pipeline"); engine.startService("abi"); engine.startService("deployment"); engine.startService("ipfs"); + engine.events.on('check:backOnline:Ethereum', function() { + engine.logger.info('Ethereum node detected..'); + engine.config.reloadConfig(); + engine.deployManager.deployContracts(function() { + engine.logger.info('Deployment Done'); + }); + }); + engine.deployManager.deployContracts(function() { engine.startService("fileWatcher"); if (options.runWebserver) { @@ -117,7 +126,6 @@ var Embark = { port: options.serverPort }); } - engine.startMonitor(); callback(); }); } From 9eb4887d37a292557e1225d2b193cd354d56cf13 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 21:49:12 -0500 Subject: [PATCH 24/45] add backwards compatbility for tests with a deprecation message --- lib/index.js | 12 ++++++++++++ test_app/test/another_storage_spec.js | 3 ++- test_app/test/simple_storage_spec.js | 3 ++- test_app/test/token_spec.js | 3 ++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 3f700731..efb8ba70 100644 --- a/lib/index.js +++ b/lib/index.js @@ -198,5 +198,17 @@ var Embark = function () { }; +Embark.initTests = function() { + console.error("=============================".green); + console.error("deprecated: Starting with Embark 2.5.0 the Embark object needs to be initialized".red); + console.log("replace:"); + console.log("var Embark = require('embark');"); + console.log("with:"); + console.log("var Embark = require('embark')();"); + console.error("=============================".green); + var embark = Embark(); + return embark.initTests(); +}; + module.exports = Embark; diff --git a/test_app/test/another_storage_spec.js b/test_app/test/another_storage_spec.js index e06e11bb..ad12778d 100644 --- a/test_app/test/another_storage_spec.js +++ b/test_app/test/another_storage_spec.js @@ -1,10 +1,11 @@ var assert = require('assert'); -var Embark = require('embark'); +var Embark = require('embark')(); var EmbarkSpec = Embark.initTests(); var web3 = EmbarkSpec.web3; describe("AnotherStorage", function() { before(function(done) { + this.timeout(0); var contractsConfig = { "SimpleStorage": { args: [100] diff --git a/test_app/test/simple_storage_spec.js b/test_app/test/simple_storage_spec.js index 83ad1610..55dfe63f 100644 --- a/test_app/test/simple_storage_spec.js +++ b/test_app/test/simple_storage_spec.js @@ -1,10 +1,11 @@ var assert = require('assert'); -var Embark = require('embark'); +var Embark = require('embark')(); var EmbarkSpec = Embark.initTests(); var web3 = EmbarkSpec.web3; describe("SimpleStorage", function() { before(function(done) { + this.timeout(0); var contractsConfig = { "SimpleStorage": { args: [100] diff --git a/test_app/test/token_spec.js b/test_app/test/token_spec.js index 3ed82f67..8bbb4d2b 100644 --- a/test_app/test/token_spec.js +++ b/test_app/test/token_spec.js @@ -1,10 +1,11 @@ var assert = require('assert'); -var Embark = require('embark'); +var Embark = require('embark')(); var EmbarkSpec = Embark.initTests(); var web3 = EmbarkSpec.web3; describe("Token", function() { before(function(done) { + this.timeout(0); var contractsConfig = { "SimpleStorage": { args: [100] From 0b023e387715e32698d4fe2cc97ad5aefb9b7c6d Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 21:55:34 -0500 Subject: [PATCH 25/45] make demo and boilerplate tests compatible --- boilerplate/test/contract_spec.js | 2 +- demo/test/simple_storage_spec.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/boilerplate/test/contract_spec.js b/boilerplate/test/contract_spec.js index e732155d..0e39bdf0 100644 --- a/boilerplate/test/contract_spec.js +++ b/boilerplate/test/contract_spec.js @@ -1,5 +1,5 @@ var assert = require('assert'); -var Embark = require('embark'); +var Embark = require('embark')(); var EmbarkSpec = Embark.initTests(); var web3 = EmbarkSpec.web3; diff --git a/demo/test/simple_storage_spec.js b/demo/test/simple_storage_spec.js index b2d3cb17..55dfe63f 100644 --- a/demo/test/simple_storage_spec.js +++ b/demo/test/simple_storage_spec.js @@ -1,5 +1,5 @@ var assert = require('assert'); -var Embark = require('embark'); +var Embark = require('embark')(); var EmbarkSpec = Embark.initTests(); var web3 = EmbarkSpec.web3; From f64474b861ff0d5ff3d00fc36d26ff3f0a86dc3e Mon Sep 17 00:00:00 2001 From: Todd Baur Date: Sun, 12 Mar 2017 11:59:13 +0900 Subject: [PATCH 26/45] dont require () at end of requires --- lib/cmd.js | 2 +- lib/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cmd.js b/lib/cmd.js index da226271..4c774b03 100644 --- a/lib/cmd.js +++ b/lib/cmd.js @@ -1,7 +1,7 @@ var program = require('commander'); var colors = require('colors'); var shelljs = require('shelljs'); -var Embark = require('../lib/index')(); +var Embark = require('../lib/index'); var Cmd = function() { program.version(Embark.version); diff --git a/lib/index.js b/lib/index.js index efb8ba70..e60b2681 100644 --- a/lib/index.js +++ b/lib/index.js @@ -196,7 +196,7 @@ var Embark = function () { upload: upload }; -}; +}(); Embark.initTests = function() { console.error("=============================".green); From 3a50dbdfdd3a120c87c7a3c1ed498826e795503c Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 22:16:24 -0500 Subject: [PATCH 27/45] remove deprecation warning; put tests back to previous method; fix eslint error --- boilerplate/test/contract_spec.js | 2 +- demo/test/simple_storage_spec.js | 2 +- lib/index.js | 16 ++-------------- test_app/test/another_storage_spec.js | 2 +- test_app/test/simple_storage_spec.js | 2 +- test_app/test/token_spec.js | 2 +- 6 files changed, 7 insertions(+), 19 deletions(-) diff --git a/boilerplate/test/contract_spec.js b/boilerplate/test/contract_spec.js index 0e39bdf0..e732155d 100644 --- a/boilerplate/test/contract_spec.js +++ b/boilerplate/test/contract_spec.js @@ -1,5 +1,5 @@ var assert = require('assert'); -var Embark = require('embark')(); +var Embark = require('embark'); var EmbarkSpec = Embark.initTests(); var web3 = EmbarkSpec.web3; diff --git a/demo/test/simple_storage_spec.js b/demo/test/simple_storage_spec.js index 55dfe63f..b2d3cb17 100644 --- a/demo/test/simple_storage_spec.js +++ b/demo/test/simple_storage_spec.js @@ -1,5 +1,5 @@ var assert = require('assert'); -var Embark = require('embark')(); +var Embark = require('embark'); var EmbarkSpec = Embark.initTests(); var web3 = EmbarkSpec.web3; diff --git a/lib/index.js b/lib/index.js index e60b2681..46271baf 100644 --- a/lib/index.js +++ b/lib/index.js @@ -13,7 +13,7 @@ var Swarm = require('./upload/swarm.js'); var version = require('../package.json').version; -var Embark = function () { +var Embark = (function () { function initConfig (env, options) { var Events = require('./core/events.js'); var Logger = require('./core/logger.js'); @@ -196,19 +196,7 @@ var Embark = function () { upload: upload }; -}(); - -Embark.initTests = function() { - console.error("=============================".green); - console.error("deprecated: Starting with Embark 2.5.0 the Embark object needs to be initialized".red); - console.log("replace:"); - console.log("var Embark = require('embark');"); - console.log("with:"); - console.log("var Embark = require('embark')();"); - console.error("=============================".green); - var embark = Embark(); - return embark.initTests(); -}; +})(); module.exports = Embark; diff --git a/test_app/test/another_storage_spec.js b/test_app/test/another_storage_spec.js index ad12778d..d0e90c60 100644 --- a/test_app/test/another_storage_spec.js +++ b/test_app/test/another_storage_spec.js @@ -1,5 +1,5 @@ var assert = require('assert'); -var Embark = require('embark')(); +var Embark = require('embark'); var EmbarkSpec = Embark.initTests(); var web3 = EmbarkSpec.web3; diff --git a/test_app/test/simple_storage_spec.js b/test_app/test/simple_storage_spec.js index 55dfe63f..b2d3cb17 100644 --- a/test_app/test/simple_storage_spec.js +++ b/test_app/test/simple_storage_spec.js @@ -1,5 +1,5 @@ var assert = require('assert'); -var Embark = require('embark')(); +var Embark = require('embark'); var EmbarkSpec = Embark.initTests(); var web3 = EmbarkSpec.web3; diff --git a/test_app/test/token_spec.js b/test_app/test/token_spec.js index 8bbb4d2b..362ce058 100644 --- a/test_app/test/token_spec.js +++ b/test_app/test/token_spec.js @@ -1,5 +1,5 @@ var assert = require('assert'); -var Embark = require('embark')(); +var Embark = require('embark'); var EmbarkSpec = Embark.initTests(); var web3 = EmbarkSpec.web3; From 383b3c31a519059c753994139ee1070f364c639b Mon Sep 17 00:00:00 2001 From: Todd Baur Date: Sun, 12 Mar 2017 12:23:30 +0900 Subject: [PATCH 28/45] speed up test class - unstable --- lib/core/test.js | 105 ++++++++++++++++---------- lib/index.js | 21 +----- test/cmd.js | 13 +++- test_app/test/another_storage_spec.js | 5 +- test_app/test/simple_storage_spec.js | 4 +- test_app/test/token_spec.js | 4 +- 6 files changed, 89 insertions(+), 63 deletions(-) diff --git a/lib/core/test.js b/lib/core/test.js index fe3b0406..3c0a186c 100644 --- a/lib/core/test.js +++ b/lib/core/test.js @@ -1,14 +1,9 @@ var async = require('async'); var Web3 = require('web3'); -var Engine = require('./engine.js'); -var RunCode = require('./runCode.js'); -var TestLogger = require('./test_logger.js'); - var getSimulator = function() { try { - var sim = require('ethereumjs-testrpc'); - return sim; + return require('ethereumjs-testrpc'); } catch (e) { if (e.code === 'MODULE_NOT_FOUND') { console.log('Simulator not found; Please install it with "npm install ethereumjs-testrpc --save"'); @@ -25,32 +20,58 @@ var getSimulator = function() { } }; + var Test = function(options) { - this.options = options || {}; - var simOptions = this.options.simulatorOptions || {}; + var opts = options === undefined ? {} : options; + opts.logLevel = opts.hasOwnProperty('logLevel') ? opts.logLevel : 'debug'; + opts.simulatorOptions = opts.hasOwnProperty('simulatorOptions') ? opts.simulatorOptions : {}; - this.engine = new Engine({ - env: this.options.env || 'test', - // TODO: confi will need to detect if this is a obj - embarkConfig: this.options.embarkConfig || 'embark.json', - interceptLogs: false - }); + function newWebThree () { + try { + var Web3 = require('web3'); + var web3 = new Web3(); + web3.setProvider(getSimulator()); + } catch (e) { + if (e.code === 'MODULE_NOT_FOUND') { + console.log('Simulator not found; Please install it with "npm install ethereumjs-testrpc --save"'); + console.log('IMPORTANT: if you using a NodeJS version older than 6.9.1 then you need to install an older version of testrpc "npm install ethereumjs-testrpc@2.0 --save"'); + console.log('For more information see https://github.com/ethereumjs/testrpc'); + // TODO: should throw exception instead + return process.exit(); + } + console.log("=============="); + console.log("Tried to load testrpc but an error occurred. This is a problem with testrpc"); + console.log('IMPORTANT: if you using a NodeJS version older than 6.9.1 then you need to install an older version of testrpc "npm install ethereumjs-testrpc@2.0 --save". Alternatively install node 6.9.1 and the testrpc 3.0'); + console.log("=============="); + throw new Error(e); + } + } - this.engine.init({ - logger: new TestLogger({logLevel: this.options.logLevel || 'debug'}) - }); + function deployAll (contractsConfig, cb) { + var RunCode = require('./runCode.js'); + var self = this; - this.sim = getSimulator(); - this.web3 = new Web3(); - this.web3.setProvider(this.sim.provider(simOptions)); -}; + function newEngine () { + var Engine = require('./engine.js'); + return new Engine({ + env: opts.env || 'test', + // TODO: confi will need to detect if this is a obj + embarkConfig: opts.embarkConfig || 'embark.json', + interceptLogs: false + }).init(); + } -Test.prototype.deployAll = function(contractsConfig, cb) { - var self = this; + function newLogger() { + var TestLogger = require('./test_logger.js'); + new TestLogger({logLevel: opts.logLevel}) + } - async.waterfall([ + this.engine = newEngine(); + this.web3 = newWebThree(); + + async.waterfall([ function getConfig(callback) { - self.engine.config.contractsConfig = {contracts: contractsConfig}; + contractsConfig = { contracts: contractsConfig }; callback(); }, function startServices(callback) { @@ -73,22 +94,28 @@ Test.prototype.deployAll = function(contractsConfig, cb) { } }); } - ], function(err, result) { - if (err) { - console.log("got error"); - process.exit(); - } - // this should be part of the waterfall and not just something done at the - // end - self.web3.eth.getAccounts(function(err, accounts) { + ], function(err, result) { if (err) { - throw new Error(err); + console.log("got error"); + process.exit(); } - self.web3.eth.defaultAccount = accounts[0]; - RunCode.doEval(result, self.web3); // jshint ignore:line - cb(); + // this should be part of the waterfall and not just something done at the + // end + self.web3.eth.getAccounts(function(err, accounts) { + if (err) { + throw new Error(err); + } + self.web3.eth.defaultAccount = accounts[0]; + RunCode.doEval(result, self.web3); // jshint ignore:line + cb(); + }); }); - }); -}; + } + + + return { + deployAll: deployAll + } +}(); module.exports = Test; diff --git a/lib/index.js b/lib/index.js index e60b2681..468a4987 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,19 +1,17 @@ /*jshint esversion: 6 */ var async = require('async'); -//require("./utils/debug_util.js")(__filename, async); +//require("./core/debug_util.js")(__filename, async); var colors = require('colors'); var Engine = require('./core/engine.js'); -var Test = require('./core/test.js'); - var IPFS = require('./upload/ipfs.js'); var Swarm = require('./upload/swarm.js'); var version = require('../package.json').version; -var Embark = function () { +var Embark = (function () { function initConfig (env, options) { var Events = require('./core/events.js'); var Logger = require('./core/logger.js'); @@ -167,6 +165,7 @@ var Embark = function () { } function initTests (options) { + var Test = require('./core/test.js'); return new Test(options); } @@ -196,19 +195,7 @@ var Embark = function () { upload: upload }; -}(); - -Embark.initTests = function() { - console.error("=============================".green); - console.error("deprecated: Starting with Embark 2.5.0 the Embark object needs to be initialized".red); - console.log("replace:"); - console.log("var Embark = require('embark');"); - console.log("with:"); - console.log("var Embark = require('embark')();"); - console.error("=============================".green); - var embark = Embark(); - return embark.initTests(); -}; +})(); module.exports = Embark; diff --git a/test/cmd.js b/test/cmd.js index 8b64a97d..8565ab3d 100644 --- a/test/cmd.js +++ b/test/cmd.js @@ -2,8 +2,8 @@ var Embark = require('../lib/index'); var Cmd = require('../lib/cmd'); describe('embark.Cmd', function () { + this.timeout(0); var cmd = new Cmd(Embark); - describe('#new', function () { it('it should not create an app without a name', function (done) { cmd.newApp(undefined, function (output) { @@ -27,4 +27,15 @@ describe('embark.Cmd', function () { done(); }); }); + + // describe("#help", function () { + // it('it should spit out helpful text if no arguments are supplied', function (done) { + // cmd.process([], function (output) { + // var lines = output.split('\n'); + // assert.equal(lines[0], '\n'); + // assert.equal(lines[1], 'Usage:'); + // done(); + // }); + // }) + // }) }); \ No newline at end of file diff --git a/test_app/test/another_storage_spec.js b/test_app/test/another_storage_spec.js index ad12778d..bb5f8c91 100644 --- a/test_app/test/another_storage_spec.js +++ b/test_app/test/another_storage_spec.js @@ -1,6 +1,7 @@ var assert = require('assert'); -var Embark = require('embark')(); -var EmbarkSpec = Embark.initTests(); +var Embark = require('embark'); +//TODO: this path is temporary to handle the scope of Embark within an app +var EmbarkSpec = require('../node_modules/embark/lib/core/test.js'); var web3 = EmbarkSpec.web3; describe("AnotherStorage", function() { diff --git a/test_app/test/simple_storage_spec.js b/test_app/test/simple_storage_spec.js index 55dfe63f..9a24bc69 100644 --- a/test_app/test/simple_storage_spec.js +++ b/test_app/test/simple_storage_spec.js @@ -1,6 +1,6 @@ var assert = require('assert'); -var Embark = require('embark')(); -var EmbarkSpec = Embark.initTests(); +var Embark = require('embark'); +var EmbarkSpec = require('../node_modules/embark/lib/core/test.js'); var web3 = EmbarkSpec.web3; describe("SimpleStorage", function() { diff --git a/test_app/test/token_spec.js b/test_app/test/token_spec.js index 8bbb4d2b..6dfdff78 100644 --- a/test_app/test/token_spec.js +++ b/test_app/test/token_spec.js @@ -1,6 +1,6 @@ var assert = require('assert'); -var Embark = require('embark')(); -var EmbarkSpec = Embark.initTests(); +var Embark = require('embark'); +var EmbarkSpec = require('../node_modules/embark/lib/core/test.js'); var web3 = EmbarkSpec.web3; describe("Token", function() { From 124177c8fc4a8288f923508ea1a0774e0d4ffc7c Mon Sep 17 00:00:00 2001 From: Todd Baur Date: Mon, 13 Mar 2017 00:21:19 +0900 Subject: [PATCH 29/45] speed up testing --- lib/core/test.js | 56 ++++++++++----------------- lib/index.js | 2 +- test_app/package.json | 2 +- test_app/test/another_storage_spec.js | 5 +-- test_app/test/simple_storage_spec.js | 4 +- test_app/test/token_spec.js | 6 +-- 6 files changed, 26 insertions(+), 49 deletions(-) diff --git a/lib/core/test.js b/lib/core/test.js index 3c0a186c..d4b216e4 100644 --- a/lib/core/test.js +++ b/lib/core/test.js @@ -1,6 +1,3 @@ -var async = require('async'); -var Web3 = require('web3'); - var getSimulator = function() { try { return require('ethereumjs-testrpc'); @@ -20,34 +17,26 @@ var getSimulator = function() { } }; - -var Test = function(options) { +var Test; +Test = (function (options) { + var async = require('async'); var opts = options === undefined ? {} : options; opts.logLevel = opts.hasOwnProperty('logLevel') ? opts.logLevel : 'debug'; opts.simulatorOptions = opts.hasOwnProperty('simulatorOptions') ? opts.simulatorOptions : {}; + var sim = getSimulator(); - function newWebThree () { + function newWebThree() { try { var Web3 = require('web3'); var web3 = new Web3(); - web3.setProvider(getSimulator()); + web3.setProvider(sim.provider(opts.simulatorOptions)); + return web3; } catch (e) { - if (e.code === 'MODULE_NOT_FOUND') { - console.log('Simulator not found; Please install it with "npm install ethereumjs-testrpc --save"'); - console.log('IMPORTANT: if you using a NodeJS version older than 6.9.1 then you need to install an older version of testrpc "npm install ethereumjs-testrpc@2.0 --save"'); - console.log('For more information see https://github.com/ethereumjs/testrpc'); - // TODO: should throw exception instead - return process.exit(); - } - console.log("=============="); - console.log("Tried to load testrpc but an error occurred. This is a problem with testrpc"); - console.log('IMPORTANT: if you using a NodeJS version older than 6.9.1 then you need to install an older version of testrpc "npm install ethereumjs-testrpc@2.0 --save". Alternatively install node 6.9.1 and the testrpc 3.0'); - console.log("=============="); throw new Error(e); } } - function deployAll (contractsConfig, cb) { + function deployAll(contractsConfig, cb) { var RunCode = require('./runCode.js'); var self = this; @@ -58,20 +47,16 @@ var Test = function(options) { // TODO: confi will need to detect if this is a obj embarkConfig: opts.embarkConfig || 'embark.json', interceptLogs: false - }).init(); + }); } - function newLogger() { - var TestLogger = require('./test_logger.js'); - new TestLogger({logLevel: opts.logLevel}) - } - - this.engine = newEngine(); - this.web3 = newWebThree(); + self.web3 = newWebThree(); + self.engine = newEngine(); + self.engine.init(); async.waterfall([ function getConfig(callback) { - contractsConfig = { contracts: contractsConfig }; + self.engine.config.contractsConfig = {contracts: contractsConfig}; callback(); }, function startServices(callback) { @@ -84,24 +69,24 @@ var Test = function(options) { callback(); }, function deploy(callback) { - self.engine.events.on('abi-contracts-vanila', function(vanillaABI) { + self.engine.events.on('abi-contracts-vanila', function (vanillaABI) { callback(null, vanillaABI); }); - self.engine.deployManager.deployContracts(function(err, result) { + self.engine.deployManager.deployContracts(function (err, result) { if (err) { console.log(err); callback(err); } }); } - ], function(err, result) { + ], function (err, result) { if (err) { console.log("got error"); process.exit(); } // this should be part of the waterfall and not just something done at the // end - self.web3.eth.getAccounts(function(err, accounts) { + self.web3.eth.getAccounts(function (err, accounts) { if (err) { throw new Error(err); } @@ -114,8 +99,9 @@ var Test = function(options) { return { - deployAll: deployAll - } -}(); + deployAll: deployAll, + sim: sim + }; +}()); module.exports = Test; diff --git a/lib/index.js b/lib/index.js index 468a4987..7037f224 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,6 @@ /*jshint esversion: 6 */ var async = require('async'); -//require("./core/debug_util.js")(__filename, async); +// require("./utils/debug_util.js")(__filename, async); var colors = require('colors'); diff --git a/test_app/package.json b/test_app/package.json index 29cd34ff..042f454f 100644 --- a/test_app/package.json +++ b/test_app/package.json @@ -10,7 +10,7 @@ "license": "ISC", "homepage": "", "devDependencies": { - "embark": "../", + "embark": "file:../", "mocha": "^2.2.5" }, "dependencies": { diff --git a/test_app/test/another_storage_spec.js b/test_app/test/another_storage_spec.js index bb5f8c91..b60297b5 100644 --- a/test_app/test/another_storage_spec.js +++ b/test_app/test/another_storage_spec.js @@ -1,8 +1,5 @@ var assert = require('assert'); -var Embark = require('embark'); -//TODO: this path is temporary to handle the scope of Embark within an app -var EmbarkSpec = require('../node_modules/embark/lib/core/test.js'); -var web3 = EmbarkSpec.web3; +var EmbarkSpec = require('embark/lib/core/test.js'); describe("AnotherStorage", function() { before(function(done) { diff --git a/test_app/test/simple_storage_spec.js b/test_app/test/simple_storage_spec.js index 9a24bc69..b330ced6 100644 --- a/test_app/test/simple_storage_spec.js +++ b/test_app/test/simple_storage_spec.js @@ -1,7 +1,5 @@ var assert = require('assert'); -var Embark = require('embark'); -var EmbarkSpec = require('../node_modules/embark/lib/core/test.js'); -var web3 = EmbarkSpec.web3; +var EmbarkSpec = require('embark/lib/core/test.js'); describe("SimpleStorage", function() { before(function(done) { diff --git a/test_app/test/token_spec.js b/test_app/test/token_spec.js index a043f84a..93eb28b5 100644 --- a/test_app/test/token_spec.js +++ b/test_app/test/token_spec.js @@ -1,9 +1,5 @@ var assert = require('assert'); -var Embark = require('embark'); -var EmbarkSpec = require('../node_modules/embark/lib/core/test.js'); -var Embark = require('embark'); -var EmbarkSpec = Embark.initTests(); -var web3 = EmbarkSpec.web3; +var EmbarkSpec = require('embark/lib/core/test.js'); describe("Token", function() { before(function(done) { From f1b8438cbb515d78f2c473cb559636516d5df201 Mon Sep 17 00:00:00 2001 From: Andy Nogueira Date: Sun, 12 Mar 2017 17:59:21 -0400 Subject: [PATCH 30/45] Fixing some logic on Embark.Storage.setProvider --- js/build/embark.bundle.js | 962 ++++++++++++++++++++------------------ js/embark.js | 14 +- 2 files changed, 503 insertions(+), 473 deletions(-) diff --git a/js/build/embark.bundle.js b/js/build/embark.bundle.js index eed9d3d4..92fd5011 100644 --- a/js/build/embark.bundle.js +++ b/js/build/embark.bundle.js @@ -2,496 +2,524 @@ var EmbarkJS = /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; - +/******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { - +/******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports; - +/******/ /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { -/******/ exports: {}, -/******/ id: moduleId, -/******/ loaded: false +/******/ i: moduleId, +/******/ l: false, +/******/ exports: {} /******/ }; - +/******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); - +/******/ /******/ // Flag the module as loaded -/******/ module.loaded = true; - +/******/ module.l = true; +/******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } - - +/******/ +/******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; - +/******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; - +/******/ +/******/ // identity function for calling harmony imports with the correct context +/******/ __webpack_require__.i = function(value) { return value; }; +/******/ +/******/ // define getter function for harmony exports +/******/ __webpack_require__.d = function(exports, name, getter) { +/******/ if(!__webpack_require__.o(exports, name)) { +/******/ Object.defineProperty(exports, name, { +/******/ configurable: false, +/******/ enumerable: true, +/******/ get: getter +/******/ }); +/******/ } +/******/ }; +/******/ +/******/ // getDefaultExport function for compatibility with non-harmony modules +/******/ __webpack_require__.n = function(module) { +/******/ var getter = module && module.__esModule ? +/******/ function getDefault() { return module['default']; } : +/******/ function getModuleExports() { return module; }; +/******/ __webpack_require__.d(getter, 'a', getter); +/******/ return getter; +/******/ }; +/******/ +/******/ // Object.prototype.hasOwnProperty.call +/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; +/******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; - +/******/ /******/ // Load entry module and return exports -/******/ return __webpack_require__(0); +/******/ return __webpack_require__(__webpack_require__.s = 0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ -/***/ function(module, exports) { - - /*jshint esversion: 6 */ - //var Ipfs = require('./ipfs.js'); - - var EmbarkJS = { - }; - - EmbarkJS.Contract = function(options) { - var self = this; - var i, abiElement; - - this.abi = options.abi; - this.address = options.address; - this.code = '0x' + options.code; - this.web3 = options.web3 || web3; - - var ContractClass = this.web3.eth.contract(this.abi); - - this.eventList = []; - - if (this.abi) { - for (i = 0; i < this.abi.length; i++) { - abiElement = this.abi[i]; - if (abiElement.type === 'event') { - this.eventList.push(abiElement.name); - } - } - } - - var messageEvents = function() { - this.cb = function() {}; - }; - - messageEvents.prototype.then = function(cb) { - this.cb = cb; - }; - - messageEvents.prototype.error = function(err) { - return err; - }; - - this._originalContractObject = ContractClass.at(this.address); - this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function (p) { - // TODO: check for forbidden properties - if (self.eventList.indexOf(p) >= 0) { - - self[p] = function() { - var promise = new messageEvents(); - var args = Array.prototype.slice.call(arguments); - args.push(function(err, result) { - if (err) { - promise.error(err); - } else { - promise.cb(result); - } - }); - - self._originalContractObject[p].apply(self._originalContractObject[p], args); - return promise; - }; - return true; - } else if (typeof self._originalContractObject[p] === 'function') { - self[p] = function(_args) { - var args = Array.prototype.slice.call(arguments); - var fn = self._originalContractObject[p]; - var props = self.abi.find((x) => x.name == p); - - var promise = new Promise(function(resolve, reject) { - args.push(function(err, transaction) { - promise.tx = transaction; - if (err) { - return reject(err); - } - - var getConfirmation = function() { - self.web3.eth.getTransactionReceipt(transaction, function(err, receipt) { - if (err) { - return reject(err); - } - - if (receipt !== null) { - return resolve(receipt); - } - - setTimeout(getConfirmation, 1000); - }); - }; - - if (typeof(transaction) !== "string" || props.constant) { - resolve(transaction); - } else { - getConfirmation(); - } - }); - - fn.apply(fn, args); - }); - - return promise; - }; - return true; - } - return false; - }); - }; - - EmbarkJS.Contract.prototype.deploy = function(args, _options) { - var self = this; - var contractParams; - var options = _options || {}; - - contractParams = args || []; - - contractParams.push({ - from: this.web3.eth.accounts[0], - data: this.code, - gas: options.gas || 800000 - }); - - var contractObject = this.web3.eth.contract(this.abi); - - var promise = new Promise(function(resolve, reject) { - contractParams.push(function(err, transaction) { - if (err) { - reject(err); - } else if (transaction.address !== undefined) { - resolve(new EmbarkJS.Contract({abi: self.abi, code: self.code, address: transaction.address})); - } - }); - - // returns promise - // deploys contract - // wraps it around EmbarkJS.Contract - contractObject["new"].apply(contractObject, contractParams); - }); - - - return promise; - }; - - EmbarkJS.IPFS = 'ipfs'; - - EmbarkJS.Storage = { - }; - - EmbarkJS.Storage.setProvider = function(provider, options) { - if (provider === 'ipfs') { - this.currentStorage = EmbarkJS.Storage.IPFS; - if (options === undefined) { - this.ipfsConnection = IpfsApi('localhost', '5001'); - } else { - this.ipfsConnection = IpfsApi(options.server, options.port); - } - } else { - throw Error('unknown provider'); - } - }; - - EmbarkJS.Storage.saveText = function(text) { - var self = this; - if (!this.ipfsConnection) { - this.setProvider('ipfs'); - } - var promise = new Promise(function(resolve, reject) { - self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) { - if (err) { - reject(err); - } else { - resolve(result[0].path); - } - }); - }); - - return promise; - }; - - EmbarkJS.Storage.uploadFile = function(inputSelector) { - var self = this; - var file = inputSelector[0].files[0]; - - if (file === undefined) { - throw new Error('no file found'); - } - - if (!this.ipfsConnection) { - this.setProvider('ipfs'); - } - - var promise = new Promise(function(resolve, reject) { - var reader = new FileReader(); - reader.onloadend = function() { - var fileContent = reader.result; - var buffer = self.ipfsConnection.Buffer.from(fileContent); - self.ipfsConnection.add(buffer, function(err, result) { - if (err) { - reject(err); - } else { - resolve(result[0].path); - } - }); - }; - reader.readAsArrayBuffer(file); - }); - - return promise; - }; - - EmbarkJS.Storage.get = function(hash) { - var self = this; - // TODO: detect type, then convert if needed - //var ipfsHash = web3.toAscii(hash); - if (!this.ipfsConnection) { - this.setProvider('ipfs'); - } - - var promise = new Promise(function(resolve, reject) { - self.ipfsConnection.object.get([hash]).then(function(node) { - resolve(node.data); - }); - }); - - return promise; - }; - - EmbarkJS.Storage.getUrl = function(hash) { - //var ipfsHash = web3.toAscii(hash); - - return 'http://localhost:8080/ipfs/' + hash; - }; - - EmbarkJS.Messages = { - }; - - EmbarkJS.Messages.setProvider = function(provider, options) { - var self = this; - var ipfs; - if (provider === 'whisper') { - this.currentMessages = EmbarkJS.Messages.Whisper; - if (typeof variable === 'undefined') { - if (options === undefined) { - web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); - } else { - web3 = new Web3(new Web3.providers.HttpProvider("http://" + options.server + ':' + options.port)); - } - } - web3.version.getWhisper(function(err, res) { - if (err) { - console.log("whisper not available"); - } else { - self.currentMessages.identity = web3.shh.newIdentity(); - } - }); - } else if (provider === 'orbit') { - this.currentMessages = EmbarkJS.Messages.Orbit; - if (options === undefined) { - ipfs = HaadIpfsApi('localhost', '5001'); - } else { - ipfs = HaadIpfsApi(options.server, options.port); - } - this.currentMessages.orbit = new Orbit(ipfs); - this.currentMessages.orbit.connect(web3.eth.accounts[0]); - } else { - throw Error('unknown provider'); - } - }; - - EmbarkJS.Messages.sendMessage = function(options) { - return this.currentMessages.sendMessage(options); - }; - - EmbarkJS.Messages.listenTo = function(options) { - return this.currentMessages.listenTo(options); - }; - - EmbarkJS.Messages.Whisper = { - }; - - EmbarkJS.Messages.Whisper.sendMessage = function(options) { - var topics = options.topic || options.topics; - var data = options.data || options.payload; - var identity = options.identity || this.identity || web3.shh.newIdentity(); - var ttl = options.ttl || 100; - var priority = options.priority || 1000; - var _topics; - - if (topics === undefined) { - throw new Error("missing option: topic"); - } - - if (data === undefined) { - throw new Error("missing option: data"); - } - - // do fromAscii to each topics unless it's already a string - if (typeof topics === 'string') { - _topics = [web3.fromAscii(topics)]; - } else { - // TODO: replace with es6 + babel; - for (var i = 0; i < topics.length; i++) { - _topics.push(web3.fromAscii(topics[i])); - } - } - topics = _topics; - - var payload = JSON.stringify(data); - - var message = { - from: identity, - topics: topics, - payload: web3.fromAscii(payload), - ttl: ttl, - priority: priority - }; - - return web3.shh.post(message, function() {}); - }; - - EmbarkJS.Messages.Whisper.listenTo = function(options) { - var topics = options.topic || options.topics; - var _topics = []; - - if (typeof topics === 'string') { - _topics = [topics]; - } else { - // TODO: replace with es6 + babel; - for (var i = 0; i < topics.length; i++) { - _topics.push(topics[i]); - } - } - topics = _topics; - - var filterOptions = { - topics: topics - }; - - var messageEvents = function() { - this.cb = function() {}; - }; - - messageEvents.prototype.then = function(cb) { - this.cb = cb; - }; - - messageEvents.prototype.error = function(err) { - return err; - }; - - messageEvents.prototype.stop = function() { - this.filter.stopWatching(); - }; - - var promise = new messageEvents(); - - var filter = web3.shh.filter(filterOptions, function(err, result) { - var payload = JSON.parse(web3.toAscii(result.payload)); - var data; - if (err) { - promise.error(err); - } else { - data = { - topic: topics, - data: payload, - from: result.from, - time: (new Date(result.sent * 1000)) - }; - promise.cb(payload, data, result); - } - }); - - promise.filter = filter; - - return promise; - }; - - EmbarkJS.Messages.Orbit = { - }; - - EmbarkJS.Messages.Orbit.sendMessage = function(options) { - var topics = options.topic || options.topics; - var data = options.data || options.payload; - - if (topics === undefined) { - throw new Error("missing option: topic"); - } - - if (data === undefined) { - throw new Error("missing option: data"); - } - - if (typeof topics === 'string') { - topics = topics; - } else { - // TODO: better to just send to different channels instead - topics = topics.join(','); - } - - this.orbit.join(topics); - - var payload = JSON.stringify(data); - - this.orbit.send(topics, data); - }; - - EmbarkJS.Messages.Orbit.listenTo = function(options) { - var self = this; - var topics = options.topic || options.topics; - - if (typeof topics === 'string') { - topics = topics; - } else { - topics = topics.join(','); - } - - this.orbit.join(topics); - - var messageEvents = function() { - this.cb = function() {}; - }; - - messageEvents.prototype.then = function(cb) { - this.cb = cb; - }; - - messageEvents.prototype.error = function(err) { - return err; - }; - - var promise = new messageEvents(); - - this.orbit.events.on('message', (channel, message) => { - // TODO: looks like sometimes it's receving messages from all topics - if (topics !== channel) return; - self.orbit.getPost(message.payload.value, true).then((post) => { - var data = { - topic: channel, - data: post.content, - from: post.meta.from.name, - time: (new Date(post.meta.ts)) - }; - promise.cb(post.content, data, post); - }); - }); - - return promise; - }; - - module.exports = EmbarkJS; - - -/***/ } +/***/ (function(module, exports) { + +/*jshint esversion: 6 */ +//var Ipfs = require('./ipfs.js'); + +var EmbarkJS = { +}; + +EmbarkJS.Contract = function(options) { + var self = this; + var i, abiElement; + + this.abi = options.abi; + this.address = options.address; + this.code = '0x' + options.code; + this.web3 = options.web3 || web3; + + var ContractClass = this.web3.eth.contract(this.abi); + + this.eventList = []; + + if (this.abi) { + for (i = 0; i < this.abi.length; i++) { + abiElement = this.abi[i]; + if (abiElement.type === 'event') { + this.eventList.push(abiElement.name); + } + } + } + + var messageEvents = function() { + this.cb = function() {}; + }; + + messageEvents.prototype.then = function(cb) { + this.cb = cb; + }; + + messageEvents.prototype.error = function(err) { + return err; + }; + + this._originalContractObject = ContractClass.at(this.address); + this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function (p) { + // TODO: check for forbidden properties + if (self.eventList.indexOf(p) >= 0) { + + self[p] = function() { + var promise = new messageEvents(); + var args = Array.prototype.slice.call(arguments); + args.push(function(err, result) { + if (err) { + promise.error(err); + } else { + promise.cb(result); + } + }); + + self._originalContractObject[p].apply(self._originalContractObject[p], args); + return promise; + }; + return true; + } else if (typeof self._originalContractObject[p] === 'function') { + self[p] = function(_args) { + var args = Array.prototype.slice.call(arguments); + var fn = self._originalContractObject[p]; + var props = self.abi.find((x) => x.name == p); + + var promise = new Promise(function(resolve, reject) { + args.push(function(err, transaction) { + promise.tx = transaction; + if (err) { + return reject(err); + } + + var getConfirmation = function() { + self.web3.eth.getTransactionReceipt(transaction, function(err, receipt) { + if (err) { + return reject(err); + } + + if (receipt !== null) { + return resolve(receipt); + } + + setTimeout(getConfirmation, 1000); + }); + }; + + if (typeof(transaction) !== "string" || props.constant) { + resolve(transaction); + } else { + getConfirmation(); + } + }); + + fn.apply(fn, args); + }); + + return promise; + }; + return true; + } + return false; + }); +}; + +EmbarkJS.Contract.prototype.deploy = function(args, _options) { + var self = this; + var contractParams; + var options = _options || {}; + + contractParams = args || []; + + contractParams.push({ + from: this.web3.eth.accounts[0], + data: this.code, + gas: options.gas || 800000 + }); + + var contractObject = this.web3.eth.contract(this.abi); + + var promise = new Promise(function(resolve, reject) { + contractParams.push(function(err, transaction) { + if (err) { + reject(err); + } else if (transaction.address !== undefined) { + resolve(new EmbarkJS.Contract({abi: self.abi, code: self.code, address: transaction.address})); + } + }); + + // returns promise + // deploys contract + // wraps it around EmbarkJS.Contract + contractObject["new"].apply(contractObject, contractParams); + }); + + + return promise; +}; + +EmbarkJS.Storage = { + IPFS : 'ipfs' +}; + +EmbarkJS.Storage.setProvider = function(provider, options) { + if (provider.toLowerCase() === EmbarkJS.Storage.IPFS) { + //I don't think currentStorage is used anywhere, this might not be needed + //for now until additional storage providers are supported. But keeping it + //anyways + this.currentStorage = EmbarkJS.Storage.IPFS; + if (options === undefined) { + this.ipfsConnection = IpfsApi('localhost', '5001'); + } else { + this.ipfsConnection = IpfsApi(options.server, options.port); + } + } else { + throw Error('Unknown storage provider'); + } +}; + +EmbarkJS.Storage.saveText = function(text) { + var self = this; + if (!this.ipfsConnection) { + this.setProvider('ipfs'); + } + var promise = new Promise(function(resolve, reject) { + self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) { + if (err) { + reject(err); + } else { + resolve(result[0].path); + } + }); + }); + + return promise; +}; + +EmbarkJS.Storage.uploadFile = function(inputSelector) { + var self = this; + var file = inputSelector[0].files[0]; + + if (file === undefined) { + throw new Error('no file found'); + } + + if (!this.ipfsConnection) { + this.setProvider('ipfs'); + } + + var promise = new Promise(function(resolve, reject) { + var reader = new FileReader(); + reader.onloadend = function() { + var fileContent = reader.result; + var buffer = self.ipfsConnection.Buffer.from(fileContent); + self.ipfsConnection.add(buffer, function(err, result) { + if (err) { + reject(err); + } else { + resolve(result[0].path); + } + }); + }; + reader.readAsArrayBuffer(file); + }); + + return promise; +}; + +EmbarkJS.Storage.get = function(hash) { + var self = this; + // TODO: detect type, then convert if needed + //var ipfsHash = web3.toAscii(hash); + if (!this.ipfsConnection) { + this.setProvider('ipfs'); + } + + var promise = new Promise(function(resolve, reject) { + self.ipfsConnection.object.get([hash]).then(function(node) { + resolve(node.data); + }); + }); + + return promise; +}; + +EmbarkJS.Storage.getUrl = function(hash) { + //var ipfsHash = web3.toAscii(hash); + + return 'http://localhost:8080/ipfs/' + hash; +}; + +EmbarkJS.Messages = { +}; + +EmbarkJS.Messages.setProvider = function(provider, options) { + var self = this; + var ipfs; + if (provider === 'whisper') { + this.currentMessages = EmbarkJS.Messages.Whisper; + if (typeof variable === 'undefined') { + if (options === undefined) { + web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); + } else { + web3 = new Web3(new Web3.providers.HttpProvider("http://" + options.server + ':' + options.port)); + } + } + web3.version.getWhisper(function(err, res) { + if (err) { + console.log("whisper not available"); + } else { + self.currentMessages.identity = web3.shh.newIdentity(); + } + }); + } else if (provider === 'orbit') { + this.currentMessages = EmbarkJS.Messages.Orbit; + if (options === undefined) { + ipfs = HaadIpfsApi('localhost', '5001'); + } else { + ipfs = HaadIpfsApi(options.server, options.port); + } + this.currentMessages.orbit = new Orbit(ipfs); + this.currentMessages.orbit.connect(web3.eth.accounts[0]); + } else { + throw Error('unknown provider'); + } +}; + +EmbarkJS.Messages.sendMessage = function(options) { + return this.currentMessages.sendMessage(options); +}; + +EmbarkJS.Messages.listenTo = function(options) { + return this.currentMessages.listenTo(options); +}; + +EmbarkJS.Messages.Whisper = { +}; + +EmbarkJS.Messages.Whisper.sendMessage = function(options) { + var topics = options.topic || options.topics; + var data = options.data || options.payload; + var identity = options.identity || this.identity || web3.shh.newIdentity(); + var ttl = options.ttl || 100; + var priority = options.priority || 1000; + var _topics; + + if (topics === undefined) { + throw new Error("missing option: topic"); + } + + if (data === undefined) { + throw new Error("missing option: data"); + } + + // do fromAscii to each topics unless it's already a string + if (typeof topics === 'string') { + _topics = [web3.fromAscii(topics)]; + } else { + // TODO: replace with es6 + babel; + for (var i = 0; i < topics.length; i++) { + _topics.push(web3.fromAscii(topics[i])); + } + } + topics = _topics; + + var payload = JSON.stringify(data); + + var message = { + from: identity, + topics: topics, + payload: web3.fromAscii(payload), + ttl: ttl, + priority: priority + }; + + return web3.shh.post(message, function() {}); +}; + +EmbarkJS.Messages.Whisper.listenTo = function(options) { + var topics = options.topic || options.topics; + var _topics = []; + + if (typeof topics === 'string') { + _topics = [topics]; + } else { + // TODO: replace with es6 + babel; + for (var i = 0; i < topics.length; i++) { + _topics.push(topics[i]); + } + } + topics = _topics; + + var filterOptions = { + topics: topics + }; + + var messageEvents = function() { + this.cb = function() {}; + }; + + messageEvents.prototype.then = function(cb) { + this.cb = cb; + }; + + messageEvents.prototype.error = function(err) { + return err; + }; + + messageEvents.prototype.stop = function() { + this.filter.stopWatching(); + }; + + var promise = new messageEvents(); + + var filter = web3.shh.filter(filterOptions, function(err, result) { + var payload = JSON.parse(web3.toAscii(result.payload)); + var data; + if (err) { + promise.error(err); + } else { + data = { + topic: topics, + data: payload, + from: result.from, + time: (new Date(result.sent * 1000)) + }; + promise.cb(payload, data, result); + } + }); + + promise.filter = filter; + + return promise; +}; + +EmbarkJS.Messages.Orbit = { +}; + +EmbarkJS.Messages.Orbit.sendMessage = function(options) { + var topics = options.topic || options.topics; + var data = options.data || options.payload; + + if (topics === undefined) { + throw new Error("missing option: topic"); + } + + if (data === undefined) { + throw new Error("missing option: data"); + } + + if (typeof topics === 'string') { + topics = topics; + } else { + // TODO: better to just send to different channels instead + topics = topics.join(','); + } + + this.orbit.join(topics); + + var payload = JSON.stringify(data); + + this.orbit.send(topics, data); +}; + +EmbarkJS.Messages.Orbit.listenTo = function(options) { + var self = this; + var topics = options.topic || options.topics; + + if (typeof topics === 'string') { + topics = topics; + } else { + topics = topics.join(','); + } + + this.orbit.join(topics); + + var messageEvents = function() { + this.cb = function() {}; + }; + + messageEvents.prototype.then = function(cb) { + this.cb = cb; + }; + + messageEvents.prototype.error = function(err) { + return err; + }; + + var promise = new messageEvents(); + + this.orbit.events.on('message', (channel, message) => { + // TODO: looks like sometimes it's receving messages from all topics + if (topics !== channel) return; + self.orbit.getPost(message.payload.value, true).then((post) => { + var data = { + topic: channel, + data: post.content, + from: post.meta.from.name, + time: (new Date(post.meta.ts)) + }; + promise.cb(post.content, data, post); + }); + }); + + return promise; +}; + +module.exports = EmbarkJS; + + +/***/ }) /******/ ]); \ No newline at end of file diff --git a/js/embark.js b/js/embark.js index 93b7e39f..a5295fc3 100644 --- a/js/embark.js +++ b/js/embark.js @@ -133,17 +133,19 @@ EmbarkJS.Contract.prototype.deploy = function(args, _options) { contractObject["new"].apply(contractObject, contractParams); }); - + return promise; }; -EmbarkJS.IPFS = 'ipfs'; - EmbarkJS.Storage = { + IPFS : 'ipfs' }; EmbarkJS.Storage.setProvider = function(provider, options) { - if (provider === 'ipfs') { + if (provider.toLowerCase() === EmbarkJS.Storage.IPFS) { + //I don't think currentStorage is used anywhere, this might not be needed + //for now until additional storage providers are supported. But keeping it + //anyways this.currentStorage = EmbarkJS.Storage.IPFS; if (options === undefined) { this.ipfsConnection = IpfsApi('localhost', '5001'); @@ -151,7 +153,7 @@ EmbarkJS.Storage.setProvider = function(provider, options) { this.ipfsConnection = IpfsApi(options.server, options.port); } } else { - throw Error('unknown provider'); + throw Error('Unknown storage provider'); } }; @@ -187,7 +189,7 @@ EmbarkJS.Storage.uploadFile = function(inputSelector) { var promise = new Promise(function(resolve, reject) { var reader = new FileReader(); - reader.onloadend = function() { + reader.onloadend = function() { var fileContent = reader.result; var buffer = self.ipfsConnection.Buffer.from(fileContent); self.ipfsConnection.add(buffer, function(err, result) { From 74343ceb60e0e36e89869110739d05704508f6c5 Mon Sep 17 00:00:00 2001 From: Andy Nogueira Date: Sun, 12 Mar 2017 18:18:21 -0400 Subject: [PATCH 31/45] Added logic to show error message on console if fails to save and retrieve text from IPFS --- demo/app/js/index.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/demo/app/js/index.js b/demo/app/js/index.js index 8b3f1bb3..fe7ac8cf 100644 --- a/demo/app/js/index.js +++ b/demo/app/js/index.js @@ -44,23 +44,33 @@ $(document).ready(function() { $("#status-storage").addClass('status-offline'); $("#storage-controls").hide(); } - }); + }); $("#storage button.setIpfsText").click(function() { var value = $("#storage input.ipfsText").val(); EmbarkJS.Storage.saveText(value).then(function(hash) { $("span.textHash").html(hash); $("input.textHash").val(hash); + addToLog("#storage", "EmbarkJS.Storage.saveText('" + value + "').then(function(hash) { })"); + }) + .catch(function(err) { + if(err){ + console.log("IPFS saveText Error => " + err.message); + } }); - addToLog("#storage", "EmbarkJS.Storage.saveText('" + value + "').then(function(hash) { })"); }); $("#storage button.loadIpfsHash").click(function() { var value = $("#storage input.textHash").val(); EmbarkJS.Storage.get(value).then(function(content) { $("span.ipfsText").html(content); + addToLog("#storage", "EmbarkJS.Storage.get('" + value + "').then(function(content) { })"); + }) + .catch(function(err) { + if(err){ + console.log("IPFS get Error => " + err.message); + } }); - addToLog("#storage", "EmbarkJS.Storage.get('" + value + "').then(function(content) { })"); }); $("#storage button.uploadFile").click(function() { @@ -68,8 +78,13 @@ $(document).ready(function() { EmbarkJS.Storage.uploadFile(input).then(function(hash) { $("span.fileIpfsHash").html(hash); $("input.fileIpfsHash").val(hash); + addToLog("#storage", "EmbarkJS.Storage.uploadFile($('input[type=file]')).then(function(hash) { })"); + }) + .catch(function(err) { + if(err){ + console.log("IPFS uploadFile Error => " + err.message); + } }); - addToLog("#storage", "EmbarkJS.Storage.uploadFile($('input[type=file]')).then(function(hash) { })"); }); $("#storage button.loadIpfsFile").click(function() { From c9a4a3156da552e9df25b4824d2540a266fbbf83 Mon Sep 17 00:00:00 2001 From: Andy Nogueira Date: Sun, 12 Mar 2017 18:19:56 -0400 Subject: [PATCH 32/45] Added logic to return error in case Storage get method fails to retrieve text from IPFS --- js/build/embark.bundle.js | 2 ++ js/embark.js | 2 ++ 2 files changed, 4 insertions(+) diff --git a/js/build/embark.bundle.js b/js/build/embark.bundle.js index 92fd5011..a4ba41df 100644 --- a/js/build/embark.bundle.js +++ b/js/build/embark.bundle.js @@ -290,6 +290,8 @@ EmbarkJS.Storage.get = function(hash) { var promise = new Promise(function(resolve, reject) { self.ipfsConnection.object.get([hash]).then(function(node) { resolve(node.data); + }).catch(function (err){ + reject(err); }); }); diff --git a/js/embark.js b/js/embark.js index a5295fc3..646dc55f 100644 --- a/js/embark.js +++ b/js/embark.js @@ -217,6 +217,8 @@ EmbarkJS.Storage.get = function(hash) { var promise = new Promise(function(resolve, reject) { self.ipfsConnection.object.get([hash]).then(function(node) { resolve(node.data); + }).catch(function (err){ + reject(err); }); }); From 1e2b612d4e340ce534c7543867966de1bdc50936 Mon Sep 17 00:00:00 2001 From: Andy Nogueira Date: Sun, 12 Mar 2017 18:23:21 -0400 Subject: [PATCH 33/45] Updated doc for EmbarkJS Storage showing how to catch errors --- docs/embarkjs-storage.rst | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/docs/embarkjs-storage.rst b/docs/embarkjs-storage.rst index a4641792..c8b9f2a4 100644 --- a/docs/embarkjs-storage.rst +++ b/docs/embarkjs-storage.rst @@ -13,13 +13,25 @@ The current available storage is IPFS. it can be initialized as .. code:: javascript - EmbarkJS.Storage.saveText("hello world").then(function(hash) {}); + EmbarkJS.Storage.saveText("hello world") + .then(function(hash) {}) + .catch(function(err) { + if(err){ + console.log("IPFS saveText Error => " + err.message); + } + }); **Retrieving Data/Text** .. code:: javascript - EmbarkJS.Storage.get(hash).then(function(content) {}); + EmbarkJS.Storage.get(hash) + .then(function(content) {}) + .catch(function(err) { + if(err){ + console.log("IPFS get Error => " + err.message); + } + }); **Uploading a file** @@ -30,7 +42,13 @@ The current available storage is IPFS. it can be initialized as .. code:: javascript var input = $("input[type=file"]); - EmbarkJS.Storage.uploadFile(input).then(function(hash) {}); + EmbarkJS.Storage.uploadFile(input) + .then(function(hash) {}) + .catch(function(err) { + if(err){ + console.log("IPFS uploadFile Error => " + err.message); + } + }); **Generate URL to file** From cd645ab2d64493baa853d47e9e190b261f08daa2 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 12 Mar 2017 18:58:22 -0400 Subject: [PATCH 34/45] fix contracts config example in README and Documentation --- README.md | 6 +++--- docs/using-contracts.rst | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3fa98e19..39e95e8a 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Table of Contents * [Dashboard](#dashboard) * [Creating a new DApp](#creating-a-new-dapp) * [Libraries and APIs available](#libraries-and-languages-available) -* [Using and Configuring Contracts](#dapp-structure) +* [Using and Configuring Contracts](#using-contracts) * [EmbarkJS](#embarkjs) * [EmbarkJS - Storage (IPFS)](#embarkjs---storage) * [EmbarkJS - Communication (Whisper/Orbit)](#embarkjs---communication) @@ -227,7 +227,7 @@ If you are using multiple contracts, you can pass a reference to another contrac "SimpleStorage": { "args": [ 100, - $MyStorage + "$MyStorage" ] }, "MyStorage": { @@ -237,7 +237,7 @@ If you are using multiple contracts, you can pass a reference to another contrac }, "MyMainContract": { "args": [ - $SimpleStorage + "$SimpleStorage" ] } } diff --git a/docs/using-contracts.rst b/docs/using-contracts.rst index 7c68a287..d3cbe86a 100644 --- a/docs/using-contracts.rst +++ b/docs/using-contracts.rst @@ -64,7 +64,7 @@ with the correct address for the contract. "SimpleStorage": { "args": [ 100, - $MyStorage + "$MyStorage" ] }, "MyStorage": { @@ -74,7 +74,7 @@ with the correct address for the contract. }, "MyMainContract": { "args": [ - $SimpleStorage + "$SimpleStorage" ] } } From e8c8a857134e066faa6830cb1e40c77d3228bd34 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 12 Mar 2017 20:47:07 -0400 Subject: [PATCH 35/45] fix broken js generation on asset change: (temporarily) revert change to not deploy on asset change and force redeploy of everything --- lib/core/engine.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/core/engine.js b/lib/core/engine.js index 6122bf8b..856593d5 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -79,13 +79,15 @@ Engine.prototype.pipelineService = function(options) { pipeline.build(abi); self.events.emit('outputDone'); }); - this.events.on('file-event', function(fileType, path) { - if (fileType === 'asset') { - self.config.reloadConfig(); - pipeline.build(self.abi, path); - self.events.emit('outputDone'); - } - }); + // TODO: still need to redeploy contracts because the original contracts + // config is being corrupted + //this.events.on('file-event', function(fileType, path) { + // if (fileType === 'asset') { + // self.config.reloadConfig(); + // pipeline.build(self.abi, path); + // self.events.emit('outputDone'); + // } + //}); }; Engine.prototype.abiService = function(options) { @@ -122,10 +124,12 @@ Engine.prototype.deploymentService = function(options) { }); this.events.on('file-event', function(fileType, path) { - if (fileType === 'contract' || fileType === 'config') { + // TODO: for now need to deploy on asset chanes as well + // because the contractsManager config is corrupted after a deploy + //if (fileType === 'contract' || fileType === 'config') { self.config.reloadConfig(); self.deployManager.deployContracts(function() {}); - } + //} }); }; From 5689a6a981c75000906878282788c16ee68d8c97 Mon Sep 17 00:00:00 2001 From: Todd Baur Date: Mon, 13 Mar 2017 11:02:03 +0900 Subject: [PATCH 36/45] update boilerplate test after PR#246 --- boilerplate/test/contract_spec.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/boilerplate/test/contract_spec.js b/boilerplate/test/contract_spec.js index e732155d..e0a6a9dc 100644 --- a/boilerplate/test/contract_spec.js +++ b/boilerplate/test/contract_spec.js @@ -1,9 +1,8 @@ var assert = require('assert'); -var Embark = require('embark'); -var EmbarkSpec = Embark.initTests(); -var web3 = EmbarkSpec.web3; +var EmbarkSpec = require('embark/lib/core/test.js'); -//describe("SimpleStorage", function() { + +// describe("SimpleStorage", function() { // before(function(done) { // this.timeout(0); // var contractsConfig = { @@ -30,4 +29,4 @@ var web3 = EmbarkSpec.web3; // }); // }); // -//}); +// }); \ No newline at end of file From 494e776f05301982f409498eede88250b8542cf3 Mon Sep 17 00:00:00 2001 From: Andy Nogueira Date: Mon, 13 Mar 2017 09:38:30 -0400 Subject: [PATCH 37/45] Initial refactoring of EmbarkJS to support multiple storage providers --- js/build/embark.bundle.js | 47 +++++++++++++++++++++++++++++++++++---- js/embark.js | 47 +++++++++++++++++++++++++++++++++++---- 2 files changed, 86 insertions(+), 8 deletions(-) diff --git a/js/build/embark.bundle.js b/js/build/embark.bundle.js index a4ba41df..e576997e 100644 --- a/js/build/embark.bundle.js +++ b/js/build/embark.bundle.js @@ -211,11 +211,40 @@ EmbarkJS.Contract.prototype.deploy = function(args, _options) { }; EmbarkJS.Storage = { - IPFS : 'ipfs' +}; + +EmbarkJS.Storage.Providers = { + IPFS : 'ipfs', + SWARM: 'swarm' +}; + +EmbarkJS.Storage.IPFS = { +}; + +EmbarkJS.Storage.IPFS.saveText = function(text) { + var self = this; + if (!this.ipfsConnection) { + this.setProvider('ipfs'); + } + var promise = new Promise(function(resolve, reject) { + self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) { + if (err) { + reject(err); + } else { + resolve(result[0].path); + } + }); + }); + + return promise; +}; + +EmbarkJS.Storage.saveText = function(text) { + return this.currentStorage.sendMessage(text); }; EmbarkJS.Storage.setProvider = function(provider, options) { - if (provider.toLowerCase() === EmbarkJS.Storage.IPFS) { + if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) { //I don't think currentStorage is used anywhere, this might not be needed //for now until additional storage providers are supported. But keeping it //anyways @@ -225,7 +254,17 @@ EmbarkJS.Storage.setProvider = function(provider, options) { } else { this.ipfsConnection = IpfsApi(options.server, options.port); } - } else { + } + else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM){ + throw Error('Swarm not implemented'); + this.currentStorage = EmbarkJS.Storage.SWARM; + if (options === undefined) { + //Connect to default Swarm node + } else { + //Connect using options + } + } + else { throw Error('Unknown storage provider'); } }; @@ -336,7 +375,7 @@ EmbarkJS.Messages.setProvider = function(provider, options) { this.currentMessages.orbit = new Orbit(ipfs); this.currentMessages.orbit.connect(web3.eth.accounts[0]); } else { - throw Error('unknown provider'); + throw Error('Unknown message provider'); } }; diff --git a/js/embark.js b/js/embark.js index 646dc55f..76818970 100644 --- a/js/embark.js +++ b/js/embark.js @@ -138,11 +138,40 @@ EmbarkJS.Contract.prototype.deploy = function(args, _options) { }; EmbarkJS.Storage = { - IPFS : 'ipfs' +}; + +EmbarkJS.Storage.Providers = { + IPFS : 'ipfs', + SWARM: 'swarm' +}; + +EmbarkJS.Storage.IPFS = { +}; + +EmbarkJS.Storage.IPFS.saveText = function(text) { + var self = this; + if (!this.ipfsConnection) { + this.setProvider('ipfs'); + } + var promise = new Promise(function(resolve, reject) { + self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) { + if (err) { + reject(err); + } else { + resolve(result[0].path); + } + }); + }); + + return promise; +}; + +EmbarkJS.Storage.saveText = function(text) { + return this.currentStorage.sendMessage(text); }; EmbarkJS.Storage.setProvider = function(provider, options) { - if (provider.toLowerCase() === EmbarkJS.Storage.IPFS) { + if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) { //I don't think currentStorage is used anywhere, this might not be needed //for now until additional storage providers are supported. But keeping it //anyways @@ -152,7 +181,17 @@ EmbarkJS.Storage.setProvider = function(provider, options) { } else { this.ipfsConnection = IpfsApi(options.server, options.port); } - } else { + } + else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM){ + throw Error('Swarm not implemented'); + this.currentStorage = EmbarkJS.Storage.SWARM; + if (options === undefined) { + //Connect to default Swarm node + } else { + //Connect using options + } + } + else { throw Error('Unknown storage provider'); } }; @@ -263,7 +302,7 @@ EmbarkJS.Messages.setProvider = function(provider, options) { this.currentMessages.orbit = new Orbit(ipfs); this.currentMessages.orbit.connect(web3.eth.accounts[0]); } else { - throw Error('unknown provider'); + throw Error('Unknown message provider'); } }; From fdf0129e5dc918bd6271d032c500c444600afc72 Mon Sep 17 00:00:00 2001 From: Andy Nogueira Date: Tue, 14 Mar 2017 11:39:09 -0400 Subject: [PATCH 38/45] Refactored EmbarkJS Storage logic to allow multiple storage providers --- js/build/embark.bundle.js | 129 ++++++++++++++++++++++++-------------- js/embark.js | 129 ++++++++++++++++++++++++-------------- 2 files changed, 166 insertions(+), 92 deletions(-) diff --git a/js/build/embark.bundle.js b/js/build/embark.bundle.js index e576997e..f3a09a2c 100644 --- a/js/build/embark.bundle.js +++ b/js/build/embark.bundle.js @@ -74,6 +74,10 @@ var EmbarkJS = /*jshint esversion: 6 */ //var Ipfs = require('./ipfs.js'); +//========================================================= +// Embark Smart Contracts +//========================================================= + var EmbarkJS = { }; @@ -210,6 +214,10 @@ EmbarkJS.Contract.prototype.deploy = function(args, _options) { return promise; }; +//========================================================= +// Embark Storage +//========================================================= + EmbarkJS.Storage = { }; @@ -221,39 +229,53 @@ EmbarkJS.Storage.Providers = { EmbarkJS.Storage.IPFS = { }; -EmbarkJS.Storage.IPFS.saveText = function(text) { - var self = this; - if (!this.ipfsConnection) { - this.setProvider('ipfs'); - } - var promise = new Promise(function(resolve, reject) { - self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) { - if (err) { - reject(err); - } else { - resolve(result[0].path); - } - }); - }); +EmbarkJS.Storage.connect = function(provider){ + var self = this; + if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) { + var promise = new Promise(function(resolve, reject) { + resolve(self.currentStorage.ipfsConnection); + }); + } + else { + reject('Storage provider not supported'); + } - return promise; + return promise; }; EmbarkJS.Storage.saveText = function(text) { - return this.currentStorage.sendMessage(text); + return this.currentStorage.saveText(text); }; +EmbarkJS.Storage.get = function(hash) { + return this.currentStorage.get(hash); +}; + +EmbarkJS.Storage.uploadFile = function(inputSelector){ + return this.currentStorage.uploadFile(inputSelector); +} + +EmbarkJS.Storage.getUrl = function(hash){ + return this.currentStorage.getUrl(hash); +} + EmbarkJS.Storage.setProvider = function(provider, options) { if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) { //I don't think currentStorage is used anywhere, this might not be needed //for now until additional storage providers are supported. But keeping it //anyways this.currentStorage = EmbarkJS.Storage.IPFS; - if (options === undefined) { - this.ipfsConnection = IpfsApi('localhost', '5001'); - } else { - this.ipfsConnection = IpfsApi(options.server, options.port); + try{ + if (options === undefined) { + this.currentStorage.ipfsConnection = IpfsApi('localhost', '5001'); + } else { + this.currentStorage.ipfsConnection = IpfsApi(options.server, options.port); + } } + catch(err){ + this.currentStorage.ipfsConnection = null; + } + } else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM){ throw Error('Swarm not implemented'); @@ -269,12 +291,16 @@ EmbarkJS.Storage.setProvider = function(provider, options) { } }; -EmbarkJS.Storage.saveText = function(text) { +EmbarkJS.Storage.IPFS.saveText = function(text) { var self = this; - if (!this.ipfsConnection) { - this.setProvider('ipfs'); + if (!self.ipfsConnection) { + EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS); } var promise = new Promise(function(resolve, reject) { + if (!self.ipfsConnection){ + var connectionError = new Error('No IPFS connection'); + reject(connectionError); + } self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) { if (err) { reject(err); @@ -287,7 +313,29 @@ EmbarkJS.Storage.saveText = function(text) { return promise; }; -EmbarkJS.Storage.uploadFile = function(inputSelector) { +EmbarkJS.Storage.IPFS.get = function(hash) { + var self = this; + // TODO: detect type, then convert if needed + //var ipfsHash = web3.toAscii(hash); + if (!self.ipfsConnection) { + EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS); + } + var promise = new Promise(function(resolve, reject) { + if (!self.ipfsConnection){ + var connectionError = new Error('No IPFS connection'); + reject(connectionError); + } + self.ipfsConnection.object.get([hash]).then(function(node) { + resolve(node.data); + }).catch(function (err){ + reject(err); + }); + }); + + return promise; +}; + +EmbarkJS.Storage.IPFS.uploadFile = function(inputSelector) { var self = this; var file = inputSelector[0].files[0]; @@ -295,11 +343,15 @@ EmbarkJS.Storage.uploadFile = function(inputSelector) { throw new Error('no file found'); } - if (!this.ipfsConnection) { - this.setProvider('ipfs'); + if (!self.ipfsConnection) { + EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS); } var promise = new Promise(function(resolve, reject) { + if (!self.ipfsConnection){ + var connectionError = new Error('No IPFS connection'); + reject(connectionError); + } var reader = new FileReader(); reader.onloadend = function() { var fileContent = reader.result; @@ -318,31 +370,16 @@ EmbarkJS.Storage.uploadFile = function(inputSelector) { return promise; }; -EmbarkJS.Storage.get = function(hash) { - var self = this; - // TODO: detect type, then convert if needed - //var ipfsHash = web3.toAscii(hash); - if (!this.ipfsConnection) { - this.setProvider('ipfs'); - } - - var promise = new Promise(function(resolve, reject) { - self.ipfsConnection.object.get([hash]).then(function(node) { - resolve(node.data); - }).catch(function (err){ - reject(err); - }); - }); - - return promise; -}; - -EmbarkJS.Storage.getUrl = function(hash) { +EmbarkJS.Storage.IPFS.getUrl = function(hash) { //var ipfsHash = web3.toAscii(hash); return 'http://localhost:8080/ipfs/' + hash; }; +//========================================================= +// Embark Messaging +//========================================================= + EmbarkJS.Messages = { }; diff --git a/js/embark.js b/js/embark.js index 76818970..b874b04a 100644 --- a/js/embark.js +++ b/js/embark.js @@ -1,6 +1,10 @@ /*jshint esversion: 6 */ //var Ipfs = require('./ipfs.js'); +//========================================================= +// Embark Smart Contracts +//========================================================= + var EmbarkJS = { }; @@ -137,6 +141,10 @@ EmbarkJS.Contract.prototype.deploy = function(args, _options) { return promise; }; +//========================================================= +// Embark Storage +//========================================================= + EmbarkJS.Storage = { }; @@ -148,39 +156,53 @@ EmbarkJS.Storage.Providers = { EmbarkJS.Storage.IPFS = { }; -EmbarkJS.Storage.IPFS.saveText = function(text) { - var self = this; - if (!this.ipfsConnection) { - this.setProvider('ipfs'); - } - var promise = new Promise(function(resolve, reject) { - self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) { - if (err) { - reject(err); - } else { - resolve(result[0].path); - } - }); - }); +EmbarkJS.Storage.connect = function(provider){ + var self = this; + if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) { + var promise = new Promise(function(resolve, reject) { + resolve(self.currentStorage.ipfsConnection); + }); + } + else { + reject('Storage provider not supported'); + } - return promise; + return promise; }; EmbarkJS.Storage.saveText = function(text) { - return this.currentStorage.sendMessage(text); + return this.currentStorage.saveText(text); }; +EmbarkJS.Storage.get = function(hash) { + return this.currentStorage.get(hash); +}; + +EmbarkJS.Storage.uploadFile = function(inputSelector){ + return this.currentStorage.uploadFile(inputSelector); +} + +EmbarkJS.Storage.getUrl = function(hash){ + return this.currentStorage.getUrl(hash); +} + EmbarkJS.Storage.setProvider = function(provider, options) { if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) { //I don't think currentStorage is used anywhere, this might not be needed //for now until additional storage providers are supported. But keeping it //anyways this.currentStorage = EmbarkJS.Storage.IPFS; - if (options === undefined) { - this.ipfsConnection = IpfsApi('localhost', '5001'); - } else { - this.ipfsConnection = IpfsApi(options.server, options.port); + try{ + if (options === undefined) { + this.currentStorage.ipfsConnection = IpfsApi('localhost', '5001'); + } else { + this.currentStorage.ipfsConnection = IpfsApi(options.server, options.port); + } } + catch(err){ + this.currentStorage.ipfsConnection = null; + } + } else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM){ throw Error('Swarm not implemented'); @@ -196,12 +218,16 @@ EmbarkJS.Storage.setProvider = function(provider, options) { } }; -EmbarkJS.Storage.saveText = function(text) { +EmbarkJS.Storage.IPFS.saveText = function(text) { var self = this; - if (!this.ipfsConnection) { - this.setProvider('ipfs'); + if (!self.ipfsConnection) { + EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS); } var promise = new Promise(function(resolve, reject) { + if (!self.ipfsConnection){ + var connectionError = new Error('No IPFS connection'); + reject(connectionError); + } self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) { if (err) { reject(err); @@ -214,7 +240,29 @@ EmbarkJS.Storage.saveText = function(text) { return promise; }; -EmbarkJS.Storage.uploadFile = function(inputSelector) { +EmbarkJS.Storage.IPFS.get = function(hash) { + var self = this; + // TODO: detect type, then convert if needed + //var ipfsHash = web3.toAscii(hash); + if (!self.ipfsConnection) { + EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS); + } + var promise = new Promise(function(resolve, reject) { + if (!self.ipfsConnection){ + var connectionError = new Error('No IPFS connection'); + reject(connectionError); + } + self.ipfsConnection.object.get([hash]).then(function(node) { + resolve(node.data); + }).catch(function (err){ + reject(err); + }); + }); + + return promise; +}; + +EmbarkJS.Storage.IPFS.uploadFile = function(inputSelector) { var self = this; var file = inputSelector[0].files[0]; @@ -222,11 +270,15 @@ EmbarkJS.Storage.uploadFile = function(inputSelector) { throw new Error('no file found'); } - if (!this.ipfsConnection) { - this.setProvider('ipfs'); + if (!self.ipfsConnection) { + EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS); } var promise = new Promise(function(resolve, reject) { + if (!self.ipfsConnection){ + var connectionError = new Error('No IPFS connection'); + reject(connectionError); + } var reader = new FileReader(); reader.onloadend = function() { var fileContent = reader.result; @@ -245,31 +297,16 @@ EmbarkJS.Storage.uploadFile = function(inputSelector) { return promise; }; -EmbarkJS.Storage.get = function(hash) { - var self = this; - // TODO: detect type, then convert if needed - //var ipfsHash = web3.toAscii(hash); - if (!this.ipfsConnection) { - this.setProvider('ipfs'); - } - - var promise = new Promise(function(resolve, reject) { - self.ipfsConnection.object.get([hash]).then(function(node) { - resolve(node.data); - }).catch(function (err){ - reject(err); - }); - }); - - return promise; -}; - -EmbarkJS.Storage.getUrl = function(hash) { +EmbarkJS.Storage.IPFS.getUrl = function(hash) { //var ipfsHash = web3.toAscii(hash); return 'http://localhost:8080/ipfs/' + hash; }; +//========================================================= +// Embark Messaging +//========================================================= + EmbarkJS.Messages = { }; From a0af3ff5d9cf6dfb2418e90fbc51144af321619d Mon Sep 17 00:00:00 2001 From: Andy Nogueira Date: Tue, 14 Mar 2017 12:37:58 -0400 Subject: [PATCH 39/45] Fixing logic to return reject in promise --- js/build/embark.bundle.js | 17 +++++++++-------- js/embark.js | 17 +++++++++-------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/js/build/embark.bundle.js b/js/build/embark.bundle.js index f3a09a2c..62b08ae5 100644 --- a/js/build/embark.bundle.js +++ b/js/build/embark.bundle.js @@ -231,14 +231,15 @@ EmbarkJS.Storage.IPFS = { EmbarkJS.Storage.connect = function(provider){ var self = this; - if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) { - var promise = new Promise(function(resolve, reject) { - resolve(self.currentStorage.ipfsConnection); - }); - } - else { - reject('Storage provider not supported'); - } + var promise = new Promise(function(resolve, reject) { + if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) { + resolve(self.currentStorage.ipfsConnection); + } + else { + var error = new Error(provider + ' storage provider not supported'); + reject(error); + }; + }); return promise; }; diff --git a/js/embark.js b/js/embark.js index b874b04a..c01e58cc 100644 --- a/js/embark.js +++ b/js/embark.js @@ -158,14 +158,15 @@ EmbarkJS.Storage.IPFS = { EmbarkJS.Storage.connect = function(provider){ var self = this; - if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) { - var promise = new Promise(function(resolve, reject) { - resolve(self.currentStorage.ipfsConnection); - }); - } - else { - reject('Storage provider not supported'); - } + var promise = new Promise(function(resolve, reject) { + if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) { + resolve(self.currentStorage.ipfsConnection); + } + else { + var error = new Error(provider + ' storage provider not supported'); + reject(error); + }; + }); return promise; }; From 447f96706123cea2ebe71473d34e2cc4cc18a568 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 16 Mar 2017 07:31:52 -0400 Subject: [PATCH 40/45] add service check functionality to plugin api --- lib/core/engine.js | 9 +++++++++ lib/core/plugin.js | 6 ++++++ test_app/embark.json | 3 ++- test_app/extensions/embark-service/index.js | 5 +++++ test_app/extensions/embark-service/package.json | 11 +++++++++++ test_app/package.json | 1 + 6 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test_app/extensions/embark-service/index.js create mode 100644 test_app/extensions/embark-service/package.json diff --git a/lib/core/engine.js b/lib/core/engine.js index 856593d5..9f7a8736 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -37,6 +37,15 @@ Engine.prototype.init = function(_options) { }; Engine.prototype.startMonitor = function() { + var self = this; + if (this.plugins) { + var servicePlugins = this.plugins.getPluginsFor('serviceChecks'); + servicePlugins.forEach(function(plugin) { + plugin.serviceChecks.forEach(function(pluginCheck) { + self.servicesMonitor.addCheck(pluginCheck.checkName, pluginCheck.checkFn, pluginCheck.time); + }); + }); + } this.servicesMonitor.startMonitor(); }; diff --git a/lib/core/plugin.js b/lib/core/plugin.js index 9d6ff37a..45a498f0 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -17,6 +17,7 @@ var Plugin = function(options) { this.contractsConfigs = []; this.contractsFiles = []; this.compilers = []; + this.serviceChecks = []; this.pluginTypes = []; this.logger = options.logger; this.events = options.events; @@ -98,6 +99,11 @@ Plugin.prototype.registerConsoleCommand = function(cb) { this.pluginTypes.push('console'); }; +Plugin.prototype.registerServiceCheck = function(checkName, checkFn, time) { + this.serviceChecks.push({checkName: checkName, checkFn: checkFn, time: time}); + this.pluginTypes.push('serviceChecks'); +}; + Plugin.prototype.has = function(pluginType) { return this.pluginTypes.indexOf(pluginType) >= 0; }; diff --git a/test_app/embark.json b/test_app/embark.json index 5cdaae97..0dd46dd1 100644 --- a/test_app/embark.json +++ b/test_app/embark.json @@ -14,6 +14,7 @@ "buildDir": "dist/", "config": "config/", "plugins": { - "embark-babel": {"files": ["**/*.js", "**/*.jsx", "!**/_vendor/*.js"]} + "embark-babel": {"files": ["**/*.js", "**/*.jsx", "!**/_vendor/*.js"]}, + "embark-service": {} } } diff --git a/test_app/extensions/embark-service/index.js b/test_app/extensions/embark-service/index.js new file mode 100644 index 00000000..27f58c84 --- /dev/null +++ b/test_app/extensions/embark-service/index.js @@ -0,0 +1,5 @@ +module.exports = function(embark) { + embark.registerServiceCheck('PluginService', function(cb) { + cb({name: "ServiceName", status: "green"}); + }); +}; diff --git a/test_app/extensions/embark-service/package.json b/test_app/extensions/embark-service/package.json new file mode 100644 index 00000000..b20033a1 --- /dev/null +++ b/test_app/extensions/embark-service/package.json @@ -0,0 +1,11 @@ +{ + "name": "embark-service", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC" +} diff --git a/test_app/package.json b/test_app/package.json index 042f454f..d1ea282a 100644 --- a/test_app/package.json +++ b/test_app/package.json @@ -15,6 +15,7 @@ }, "dependencies": { "embark-babel": "^1.0.0", + "embark-service": "./extensions/embark-service", "ethereumjs-testrpc": "^3.0.3" } } From 3bb81c6a795cff3eacc7a06fc276ddb3140d13cd Mon Sep 17 00:00:00 2001 From: Andy Nogueira Date: Thu, 16 Mar 2017 21:46:43 -0400 Subject: [PATCH 41/45] Updating embark npm version for demo --- demo/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/package.json b/demo/package.json index 2d381594..d955d134 100644 --- a/demo/package.json +++ b/demo/package.json @@ -10,7 +10,7 @@ "license": "ISC", "homepage": "", "devDependencies": { - "embark": "^2.4.0", + "embark": "^2.4.1", "mocha": "^2.2.5" } } From e39853daa0486c862d91c1ff7c5b2019e64ab706 Mon Sep 17 00:00:00 2001 From: Todd Baur Date: Fri, 17 Mar 2017 12:01:40 +0900 Subject: [PATCH 42/45] update test in readme --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 39e95e8a..361aa454 100644 --- a/README.md +++ b/README.md @@ -438,12 +438,11 @@ Embark includes a testing lib to fastly run & test your contracts in a EVM. # test/simple_storage_spec.js var assert = require('assert'); -var Embark = require('embark'); -var EmbarkSpec = Embark.initTests(); -var web3 = EmbarkSpec.web3; +var EmbarkSpec = require('embark/lib/core/test.js'); describe("SimpleStorage", function() { before(function(done) { + this.timeout(0); var contractsConfig = { "SimpleStorage": { args: [100] @@ -469,6 +468,7 @@ describe("SimpleStorage", function() { }); }); + ``` Embark uses [Mocha](http://mochajs.org/) by default, but you can use any testing framework you want. From c1f72ae10e96daafe9a1d565c059ef94fff205eb Mon Sep 17 00:00:00 2001 From: Andy Nogueira Date: Fri, 17 Mar 2017 10:02:26 -0400 Subject: [PATCH 43/45] Refactoring Storage logic to allow multiple storages and keep backwards compatibility --- js/build/embark.bundle.js | 788 ++++++++++++++++++-------------------- js/embark.js | 788 ++++++++++++++++++-------------------- 2 files changed, 762 insertions(+), 814 deletions(-) diff --git a/js/build/embark.bundle.js b/js/build/embark.bundle.js index 62b08ae5..7a237acb 100644 --- a/js/build/embark.bundle.js +++ b/js/build/embark.bundle.js @@ -78,523 +78,497 @@ var EmbarkJS = // Embark Smart Contracts //========================================================= -var EmbarkJS = { -}; +var EmbarkJS = {}; EmbarkJS.Contract = function(options) { - var self = this; - var i, abiElement; + var self = this; + var i, abiElement; - this.abi = options.abi; - this.address = options.address; - this.code = '0x' + options.code; - this.web3 = options.web3 || web3; + this.abi = options.abi; + this.address = options.address; + this.code = '0x' + options.code; + this.web3 = options.web3 || web3; - var ContractClass = this.web3.eth.contract(this.abi); + var ContractClass = this.web3.eth.contract(this.abi); - this.eventList = []; + this.eventList = []; - if (this.abi) { - for (i = 0; i < this.abi.length; i++) { - abiElement = this.abi[i]; - if (abiElement.type === 'event') { - this.eventList.push(abiElement.name); - } - } - } - - var messageEvents = function() { - this.cb = function() {}; - }; - - messageEvents.prototype.then = function(cb) { - this.cb = cb; - }; - - messageEvents.prototype.error = function(err) { - return err; - }; - - this._originalContractObject = ContractClass.at(this.address); - this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function (p) { - // TODO: check for forbidden properties - if (self.eventList.indexOf(p) >= 0) { - - self[p] = function() { - var promise = new messageEvents(); - var args = Array.prototype.slice.call(arguments); - args.push(function(err, result) { - if (err) { - promise.error(err); - } else { - promise.cb(result); - } - }); - - self._originalContractObject[p].apply(self._originalContractObject[p], args); - return promise; - }; - return true; - } else if (typeof self._originalContractObject[p] === 'function') { - self[p] = function(_args) { - var args = Array.prototype.slice.call(arguments); - var fn = self._originalContractObject[p]; - var props = self.abi.find((x) => x.name == p); - - var promise = new Promise(function(resolve, reject) { - args.push(function(err, transaction) { - promise.tx = transaction; - if (err) { - return reject(err); + if (this.abi) { + for (i = 0; i < this.abi.length; i++) { + abiElement = this.abi[i]; + if (abiElement.type === 'event') { + this.eventList.push(abiElement.name); } + } + } - var getConfirmation = function() { - self.web3.eth.getTransactionReceipt(transaction, function(err, receipt) { - if (err) { - return reject(err); - } + var messageEvents = function() { + this.cb = function() {}; + }; - if (receipt !== null) { - return resolve(receipt); - } + messageEvents.prototype.then = function(cb) { + this.cb = cb; + }; - setTimeout(getConfirmation, 1000); - }); + messageEvents.prototype.error = function(err) { + return err; + }; + + this._originalContractObject = ContractClass.at(this.address); + this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function(p) { + // TODO: check for forbidden properties + if (self.eventList.indexOf(p) >= 0) { + + self[p] = function() { + var promise = new messageEvents(); + var args = Array.prototype.slice.call(arguments); + args.push(function(err, result) { + if (err) { + promise.error(err); + } else { + promise.cb(result); + } + }); + + self._originalContractObject[p].apply(self._originalContractObject[p], args); + return promise; }; + return true; + } else if (typeof self._originalContractObject[p] === 'function') { + self[p] = function(_args) { + var args = Array.prototype.slice.call(arguments); + var fn = self._originalContractObject[p]; + var props = self.abi.find((x) => x.name == p); - if (typeof(transaction) !== "string" || props.constant) { - resolve(transaction); - } else { - getConfirmation(); - } - }); + var promise = new Promise(function(resolve, reject) { + args.push(function(err, transaction) { + promise.tx = transaction; + if (err) { + return reject(err); + } - fn.apply(fn, args); - }); + var getConfirmation = function() { + self.web3.eth.getTransactionReceipt(transaction, function(err, receipt) { + if (err) { + return reject(err); + } - return promise; - }; - return true; - } - return false; - }); + if (receipt !== null) { + return resolve(receipt); + } + + setTimeout(getConfirmation, 1000); + }); + }; + + if (typeof(transaction) !== "string" || props.constant) { + resolve(transaction); + } else { + getConfirmation(); + } + }); + + fn.apply(fn, args); + }); + + return promise; + }; + return true; + } + return false; + }); }; EmbarkJS.Contract.prototype.deploy = function(args, _options) { - var self = this; - var contractParams; - var options = _options || {}; + var self = this; + var contractParams; + var options = _options || {}; - contractParams = args || []; + contractParams = args || []; - contractParams.push({ - from: this.web3.eth.accounts[0], - data: this.code, - gas: options.gas || 800000 - }); - - var contractObject = this.web3.eth.contract(this.abi); - - var promise = new Promise(function(resolve, reject) { - contractParams.push(function(err, transaction) { - if (err) { - reject(err); - } else if (transaction.address !== undefined) { - resolve(new EmbarkJS.Contract({abi: self.abi, code: self.code, address: transaction.address})); - } + contractParams.push({ + from: this.web3.eth.accounts[0], + data: this.code, + gas: options.gas || 800000 }); - // returns promise - // deploys contract - // wraps it around EmbarkJS.Contract - contractObject["new"].apply(contractObject, contractParams); - }); + var contractObject = this.web3.eth.contract(this.abi); + + var promise = new Promise(function(resolve, reject) { + contractParams.push(function(err, transaction) { + if (err) { + reject(err); + } else if (transaction.address !== undefined) { + resolve(new EmbarkJS.Contract({ + abi: self.abi, + code: self.code, + address: transaction.address + })); + } + }); + + // returns promise + // deploys contract + // wraps it around EmbarkJS.Contract + contractObject["new"].apply(contractObject, contractParams); + }); - return promise; + return promise; }; //========================================================= // Embark Storage //========================================================= -EmbarkJS.Storage = { -}; +EmbarkJS.Storage = {}; EmbarkJS.Storage.Providers = { - IPFS : 'ipfs', - SWARM: 'swarm' + IPFS: 'ipfs', + SWARM: 'swarm' }; -EmbarkJS.Storage.IPFS = { +EmbarkJS.Storage.IPFS = {}; + +EmbarkJS.Storage.saveText = function(text) { + return this.currentStorage.saveText(text); }; -EmbarkJS.Storage.connect = function(provider){ +EmbarkJS.Storage.get = function(hash) { + return this.currentStorage.get(hash); +}; + +EmbarkJS.Storage.uploadFile = function(inputSelector) { + return this.currentStorage.uploadFile(inputSelector); +} + +EmbarkJS.Storage.getUrl = function(hash) { + return this.currentStorage.getUrl(hash); +} + +EmbarkJS.Storage.setProvider = function(provider, options) { var self = this; var promise = new Promise(function(resolve, reject) { - if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) { - resolve(self.currentStorage.ipfsConnection); - } - else { - var error = new Error(provider + ' storage provider not supported'); - reject(error); - }; + if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) { + //I don't think currentStorage is used anywhere, this might not be needed + //for now until additional storage providers are supported. But keeping it + //anyways + self.currentStorage = EmbarkJS.Storage.IPFS; + + try { + if (options === undefined) { + self.ipfsConnection = IpfsApi('localhost', '5001'); + } else { + self.ipfsConnection = IpfsApi(options.server, options.port); + } + resolve(self); + } catch (err) { + self.ipfsConnection = null; + reject(new Error('Failed to connect to IPFS')); + } + } else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM) { + reject('Swarm not implemented'); + // TODO Implement Swarm + // this.currentStorage = EmbarkJS.Storage.SWARM; + // if (options === undefined) { + // //Connect to default Swarm node + // } else { + // //Connect using options + // } + } else { + reject('Unknown storage provider'); + } + }); + return promise; +}; + +EmbarkJS.Storage.IPFS.saveText = function(text) { + var promise = new Promise(function(resolve, reject) { + if (!EmbarkJS.Storage.ipfsConnection) { + var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()'); + reject(connectionError); + } + EmbarkJS.Storage.ipfsConnection.add((new EmbarkJS.Storage.ipfsConnection.Buffer(text)), function(err, result) { + if (err) { + reject(err); + } else { + resolve(result[0].path); + } + }); }); return promise; }; -EmbarkJS.Storage.saveText = function(text) { - return this.currentStorage.saveText(text); -}; - -EmbarkJS.Storage.get = function(hash) { - return this.currentStorage.get(hash); -}; - -EmbarkJS.Storage.uploadFile = function(inputSelector){ - return this.currentStorage.uploadFile(inputSelector); -} - -EmbarkJS.Storage.getUrl = function(hash){ - return this.currentStorage.getUrl(hash); -} - -EmbarkJS.Storage.setProvider = function(provider, options) { - if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) { - //I don't think currentStorage is used anywhere, this might not be needed - //for now until additional storage providers are supported. But keeping it - //anyways - this.currentStorage = EmbarkJS.Storage.IPFS; - try{ - if (options === undefined) { - this.currentStorage.ipfsConnection = IpfsApi('localhost', '5001'); - } else { - this.currentStorage.ipfsConnection = IpfsApi(options.server, options.port); - } - } - catch(err){ - this.currentStorage.ipfsConnection = null; - } - - } - else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM){ - throw Error('Swarm not implemented'); - this.currentStorage = EmbarkJS.Storage.SWARM; - if (options === undefined) { - //Connect to default Swarm node - } else { - //Connect using options - } - } - else { - throw Error('Unknown storage provider'); - } -}; - -EmbarkJS.Storage.IPFS.saveText = function(text) { - var self = this; - if (!self.ipfsConnection) { - EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS); - } - var promise = new Promise(function(resolve, reject) { - if (!self.ipfsConnection){ - var connectionError = new Error('No IPFS connection'); - reject(connectionError); - } - self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) { - if (err) { - reject(err); - } else { - resolve(result[0].path); - } - }); - }); - - return promise; -}; - EmbarkJS.Storage.IPFS.get = function(hash) { - var self = this; - // TODO: detect type, then convert if needed - //var ipfsHash = web3.toAscii(hash); - if (!self.ipfsConnection) { - EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS); - } - var promise = new Promise(function(resolve, reject) { - if (!self.ipfsConnection){ - var connectionError = new Error('No IPFS connection'); - reject(connectionError); - } - self.ipfsConnection.object.get([hash]).then(function(node) { - resolve(node.data); - }).catch(function (err){ - reject(err); + // TODO: detect type, then convert if needed + //var ipfsHash = web3.toAscii(hash); + var promise = new Promise(function(resolve, reject) { + if (!EmbarkJS.Storage.ipfsConnection) { + var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()'); + reject(connectionError); + } + EmbarkJS.Storage.ipfsConnection.object.get([hash]).then(function(node) { + resolve(node.data); + }).catch(function(err) { + reject(err); + }); }); - }); - return promise; + return promise; }; EmbarkJS.Storage.IPFS.uploadFile = function(inputSelector) { - var self = this; - var file = inputSelector[0].files[0]; + var file = inputSelector[0].files[0]; - if (file === undefined) { - throw new Error('no file found'); - } - - if (!self.ipfsConnection) { - EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS); - } - - var promise = new Promise(function(resolve, reject) { - if (!self.ipfsConnection){ - var connectionError = new Error('No IPFS connection'); - reject(connectionError); + if (file === undefined) { + throw new Error('no file found'); } - var reader = new FileReader(); - reader.onloadend = function() { - var fileContent = reader.result; - var buffer = self.ipfsConnection.Buffer.from(fileContent); - self.ipfsConnection.add(buffer, function(err, result) { - if (err) { - reject(err); - } else { - resolve(result[0].path); - } - }); - }; - reader.readAsArrayBuffer(file); - }); - return promise; + var promise = new Promise(function(resolve, reject) { + if (!EmbarkJS.Storage.ipfsConnection) { + var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()'); + reject(connectionError); + } + var reader = new FileReader(); + reader.onloadend = function() { + var fileContent = reader.result; + var buffer = EmbarkJS.Storage.ipfsConnection.Buffer.from(fileContent); + EmbarkJS.Storage.ipfsConnection.add(buffer, function(err, result) { + if (err) { + reject(err); + } else { + resolve(result[0].path); + } + }); + }; + reader.readAsArrayBuffer(file); + }); + + return promise; }; EmbarkJS.Storage.IPFS.getUrl = function(hash) { - //var ipfsHash = web3.toAscii(hash); + //var ipfsHash = web3.toAscii(hash); - return 'http://localhost:8080/ipfs/' + hash; + return 'http://localhost:8080/ipfs/' + hash; }; //========================================================= // Embark Messaging //========================================================= -EmbarkJS.Messages = { -}; +EmbarkJS.Messages = {}; EmbarkJS.Messages.setProvider = function(provider, options) { - var self = this; - var ipfs; - if (provider === 'whisper') { - this.currentMessages = EmbarkJS.Messages.Whisper; - if (typeof variable === 'undefined') { - if (options === undefined) { - web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); - } else { - web3 = new Web3(new Web3.providers.HttpProvider("http://" + options.server + ':' + options.port)); - } - } - web3.version.getWhisper(function(err, res) { - if (err) { - console.log("whisper not available"); - } else { - self.currentMessages.identity = web3.shh.newIdentity(); - } - }); - } else if (provider === 'orbit') { - this.currentMessages = EmbarkJS.Messages.Orbit; - if (options === undefined) { - ipfs = HaadIpfsApi('localhost', '5001'); + var self = this; + var ipfs; + if (provider === 'whisper') { + this.currentMessages = EmbarkJS.Messages.Whisper; + if (typeof variable === 'undefined') { + if (options === undefined) { + web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); + } else { + web3 = new Web3(new Web3.providers.HttpProvider("http://" + options.server + ':' + options.port)); + } + } + web3.version.getWhisper(function(err, res) { + if (err) { + console.log("whisper not available"); + } else { + self.currentMessages.identity = web3.shh.newIdentity(); + } + }); + } else if (provider === 'orbit') { + this.currentMessages = EmbarkJS.Messages.Orbit; + if (options === undefined) { + ipfs = HaadIpfsApi('localhost', '5001'); + } else { + ipfs = HaadIpfsApi(options.server, options.port); + } + this.currentMessages.orbit = new Orbit(ipfs); + this.currentMessages.orbit.connect(web3.eth.accounts[0]); } else { - ipfs = HaadIpfsApi(options.server, options.port); + throw Error('Unknown message provider'); } - this.currentMessages.orbit = new Orbit(ipfs); - this.currentMessages.orbit.connect(web3.eth.accounts[0]); - } else { - throw Error('Unknown message provider'); - } }; EmbarkJS.Messages.sendMessage = function(options) { - return this.currentMessages.sendMessage(options); + return this.currentMessages.sendMessage(options); }; EmbarkJS.Messages.listenTo = function(options) { - return this.currentMessages.listenTo(options); + return this.currentMessages.listenTo(options); }; -EmbarkJS.Messages.Whisper = { -}; +EmbarkJS.Messages.Whisper = {}; EmbarkJS.Messages.Whisper.sendMessage = function(options) { - var topics = options.topic || options.topics; - var data = options.data || options.payload; - var identity = options.identity || this.identity || web3.shh.newIdentity(); - var ttl = options.ttl || 100; - var priority = options.priority || 1000; - var _topics; + var topics = options.topic || options.topics; + var data = options.data || options.payload; + var identity = options.identity || this.identity || web3.shh.newIdentity(); + var ttl = options.ttl || 100; + var priority = options.priority || 1000; + var _topics; - if (topics === undefined) { - throw new Error("missing option: topic"); - } - - if (data === undefined) { - throw new Error("missing option: data"); - } - - // do fromAscii to each topics unless it's already a string - if (typeof topics === 'string') { - _topics = [web3.fromAscii(topics)]; - } else { - // TODO: replace with es6 + babel; - for (var i = 0; i < topics.length; i++) { - _topics.push(web3.fromAscii(topics[i])); + if (topics === undefined) { + throw new Error("missing option: topic"); } - } - topics = _topics; - var payload = JSON.stringify(data); + if (data === undefined) { + throw new Error("missing option: data"); + } - var message = { - from: identity, - topics: topics, - payload: web3.fromAscii(payload), - ttl: ttl, - priority: priority - }; + // do fromAscii to each topics unless it's already a string + if (typeof topics === 'string') { + _topics = [web3.fromAscii(topics)]; + } else { + // TODO: replace with es6 + babel; + for (var i = 0; i < topics.length; i++) { + _topics.push(web3.fromAscii(topics[i])); + } + } + topics = _topics; - return web3.shh.post(message, function() {}); + var payload = JSON.stringify(data); + + var message = { + from: identity, + topics: topics, + payload: web3.fromAscii(payload), + ttl: ttl, + priority: priority + }; + + return web3.shh.post(message, function() {}); }; EmbarkJS.Messages.Whisper.listenTo = function(options) { - var topics = options.topic || options.topics; - var _topics = []; + var topics = options.topic || options.topics; + var _topics = []; - if (typeof topics === 'string') { - _topics = [topics]; - } else { - // TODO: replace with es6 + babel; - for (var i = 0; i < topics.length; i++) { - _topics.push(topics[i]); - } - } - topics = _topics; - - var filterOptions = { - topics: topics - }; - - var messageEvents = function() { - this.cb = function() {}; - }; - - messageEvents.prototype.then = function(cb) { - this.cb = cb; - }; - - messageEvents.prototype.error = function(err) { - return err; - }; - - messageEvents.prototype.stop = function() { - this.filter.stopWatching(); - }; - - var promise = new messageEvents(); - - var filter = web3.shh.filter(filterOptions, function(err, result) { - var payload = JSON.parse(web3.toAscii(result.payload)); - var data; - if (err) { - promise.error(err); + if (typeof topics === 'string') { + _topics = [topics]; } else { - data = { - topic: topics, - data: payload, - from: result.from, - time: (new Date(result.sent * 1000)) - }; - promise.cb(payload, data, result); + // TODO: replace with es6 + babel; + for (var i = 0; i < topics.length; i++) { + _topics.push(topics[i]); + } } - }); + topics = _topics; - promise.filter = filter; + var filterOptions = { + topics: topics + }; - return promise; + var messageEvents = function() { + this.cb = function() {}; + }; + + messageEvents.prototype.then = function(cb) { + this.cb = cb; + }; + + messageEvents.prototype.error = function(err) { + return err; + }; + + messageEvents.prototype.stop = function() { + this.filter.stopWatching(); + }; + + var promise = new messageEvents(); + + var filter = web3.shh.filter(filterOptions, function(err, result) { + var payload = JSON.parse(web3.toAscii(result.payload)); + var data; + if (err) { + promise.error(err); + } else { + data = { + topic: topics, + data: payload, + from: result.from, + time: (new Date(result.sent * 1000)) + }; + promise.cb(payload, data, result); + } + }); + + promise.filter = filter; + + return promise; }; -EmbarkJS.Messages.Orbit = { -}; +EmbarkJS.Messages.Orbit = {}; EmbarkJS.Messages.Orbit.sendMessage = function(options) { - var topics = options.topic || options.topics; - var data = options.data || options.payload; + var topics = options.topic || options.topics; + var data = options.data || options.payload; - if (topics === undefined) { - throw new Error("missing option: topic"); - } + if (topics === undefined) { + throw new Error("missing option: topic"); + } - if (data === undefined) { - throw new Error("missing option: data"); - } + if (data === undefined) { + throw new Error("missing option: data"); + } - if (typeof topics === 'string') { - topics = topics; - } else { - // TODO: better to just send to different channels instead - topics = topics.join(','); - } + if (typeof topics === 'string') { + topics = topics; + } else { + // TODO: better to just send to different channels instead + topics = topics.join(','); + } - this.orbit.join(topics); + this.orbit.join(topics); - var payload = JSON.stringify(data); + var payload = JSON.stringify(data); - this.orbit.send(topics, data); + this.orbit.send(topics, data); }; EmbarkJS.Messages.Orbit.listenTo = function(options) { - var self = this; - var topics = options.topic || options.topics; + var self = this; + var topics = options.topic || options.topics; - if (typeof topics === 'string') { - topics = topics; - } else { - topics = topics.join(','); - } + if (typeof topics === 'string') { + topics = topics; + } else { + topics = topics.join(','); + } - this.orbit.join(topics); + this.orbit.join(topics); - var messageEvents = function() { - this.cb = function() {}; - }; + var messageEvents = function() { + this.cb = function() {}; + }; - messageEvents.prototype.then = function(cb) { - this.cb = cb; - }; + messageEvents.prototype.then = function(cb) { + this.cb = cb; + }; - messageEvents.prototype.error = function(err) { - return err; - }; + messageEvents.prototype.error = function(err) { + return err; + }; - var promise = new messageEvents(); + var promise = new messageEvents(); - this.orbit.events.on('message', (channel, message) => { - // TODO: looks like sometimes it's receving messages from all topics - if (topics !== channel) return; - self.orbit.getPost(message.payload.value, true).then((post) => { - var data = { - topic: channel, - data: post.content, - from: post.meta.from.name, - time: (new Date(post.meta.ts)) - }; - promise.cb(post.content, data, post); + this.orbit.events.on('message', (channel, message) => { + // TODO: looks like sometimes it's receving messages from all topics + if (topics !== channel) return; + self.orbit.getPost(message.payload.value, true).then((post) => { + var data = { + topic: channel, + data: post.content, + from: post.meta.from.name, + time: (new Date(post.meta.ts)) + }; + promise.cb(post.content, data, post); + }); }); - }); - return promise; + return promise; }; module.exports = EmbarkJS; diff --git a/js/embark.js b/js/embark.js index c01e58cc..216efcad 100644 --- a/js/embark.js +++ b/js/embark.js @@ -5,523 +5,497 @@ // Embark Smart Contracts //========================================================= -var EmbarkJS = { -}; +var EmbarkJS = {}; EmbarkJS.Contract = function(options) { - var self = this; - var i, abiElement; + var self = this; + var i, abiElement; - this.abi = options.abi; - this.address = options.address; - this.code = '0x' + options.code; - this.web3 = options.web3 || web3; + this.abi = options.abi; + this.address = options.address; + this.code = '0x' + options.code; + this.web3 = options.web3 || web3; - var ContractClass = this.web3.eth.contract(this.abi); + var ContractClass = this.web3.eth.contract(this.abi); - this.eventList = []; + this.eventList = []; - if (this.abi) { - for (i = 0; i < this.abi.length; i++) { - abiElement = this.abi[i]; - if (abiElement.type === 'event') { - this.eventList.push(abiElement.name); - } - } - } - - var messageEvents = function() { - this.cb = function() {}; - }; - - messageEvents.prototype.then = function(cb) { - this.cb = cb; - }; - - messageEvents.prototype.error = function(err) { - return err; - }; - - this._originalContractObject = ContractClass.at(this.address); - this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function (p) { - // TODO: check for forbidden properties - if (self.eventList.indexOf(p) >= 0) { - - self[p] = function() { - var promise = new messageEvents(); - var args = Array.prototype.slice.call(arguments); - args.push(function(err, result) { - if (err) { - promise.error(err); - } else { - promise.cb(result); - } - }); - - self._originalContractObject[p].apply(self._originalContractObject[p], args); - return promise; - }; - return true; - } else if (typeof self._originalContractObject[p] === 'function') { - self[p] = function(_args) { - var args = Array.prototype.slice.call(arguments); - var fn = self._originalContractObject[p]; - var props = self.abi.find((x) => x.name == p); - - var promise = new Promise(function(resolve, reject) { - args.push(function(err, transaction) { - promise.tx = transaction; - if (err) { - return reject(err); + if (this.abi) { + for (i = 0; i < this.abi.length; i++) { + abiElement = this.abi[i]; + if (abiElement.type === 'event') { + this.eventList.push(abiElement.name); } + } + } - var getConfirmation = function() { - self.web3.eth.getTransactionReceipt(transaction, function(err, receipt) { - if (err) { - return reject(err); - } + var messageEvents = function() { + this.cb = function() {}; + }; - if (receipt !== null) { - return resolve(receipt); - } + messageEvents.prototype.then = function(cb) { + this.cb = cb; + }; - setTimeout(getConfirmation, 1000); - }); + messageEvents.prototype.error = function(err) { + return err; + }; + + this._originalContractObject = ContractClass.at(this.address); + this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function(p) { + // TODO: check for forbidden properties + if (self.eventList.indexOf(p) >= 0) { + + self[p] = function() { + var promise = new messageEvents(); + var args = Array.prototype.slice.call(arguments); + args.push(function(err, result) { + if (err) { + promise.error(err); + } else { + promise.cb(result); + } + }); + + self._originalContractObject[p].apply(self._originalContractObject[p], args); + return promise; }; + return true; + } else if (typeof self._originalContractObject[p] === 'function') { + self[p] = function(_args) { + var args = Array.prototype.slice.call(arguments); + var fn = self._originalContractObject[p]; + var props = self.abi.find((x) => x.name == p); - if (typeof(transaction) !== "string" || props.constant) { - resolve(transaction); - } else { - getConfirmation(); - } - }); + var promise = new Promise(function(resolve, reject) { + args.push(function(err, transaction) { + promise.tx = transaction; + if (err) { + return reject(err); + } - fn.apply(fn, args); - }); + var getConfirmation = function() { + self.web3.eth.getTransactionReceipt(transaction, function(err, receipt) { + if (err) { + return reject(err); + } - return promise; - }; - return true; - } - return false; - }); + if (receipt !== null) { + return resolve(receipt); + } + + setTimeout(getConfirmation, 1000); + }); + }; + + if (typeof(transaction) !== "string" || props.constant) { + resolve(transaction); + } else { + getConfirmation(); + } + }); + + fn.apply(fn, args); + }); + + return promise; + }; + return true; + } + return false; + }); }; EmbarkJS.Contract.prototype.deploy = function(args, _options) { - var self = this; - var contractParams; - var options = _options || {}; + var self = this; + var contractParams; + var options = _options || {}; - contractParams = args || []; + contractParams = args || []; - contractParams.push({ - from: this.web3.eth.accounts[0], - data: this.code, - gas: options.gas || 800000 - }); - - var contractObject = this.web3.eth.contract(this.abi); - - var promise = new Promise(function(resolve, reject) { - contractParams.push(function(err, transaction) { - if (err) { - reject(err); - } else if (transaction.address !== undefined) { - resolve(new EmbarkJS.Contract({abi: self.abi, code: self.code, address: transaction.address})); - } + contractParams.push({ + from: this.web3.eth.accounts[0], + data: this.code, + gas: options.gas || 800000 }); - // returns promise - // deploys contract - // wraps it around EmbarkJS.Contract - contractObject["new"].apply(contractObject, contractParams); - }); + var contractObject = this.web3.eth.contract(this.abi); + + var promise = new Promise(function(resolve, reject) { + contractParams.push(function(err, transaction) { + if (err) { + reject(err); + } else if (transaction.address !== undefined) { + resolve(new EmbarkJS.Contract({ + abi: self.abi, + code: self.code, + address: transaction.address + })); + } + }); + + // returns promise + // deploys contract + // wraps it around EmbarkJS.Contract + contractObject["new"].apply(contractObject, contractParams); + }); - return promise; + return promise; }; //========================================================= // Embark Storage //========================================================= -EmbarkJS.Storage = { -}; +EmbarkJS.Storage = {}; EmbarkJS.Storage.Providers = { - IPFS : 'ipfs', - SWARM: 'swarm' + IPFS: 'ipfs', + SWARM: 'swarm' }; -EmbarkJS.Storage.IPFS = { +EmbarkJS.Storage.IPFS = {}; + +EmbarkJS.Storage.saveText = function(text) { + return this.currentStorage.saveText(text); }; -EmbarkJS.Storage.connect = function(provider){ +EmbarkJS.Storage.get = function(hash) { + return this.currentStorage.get(hash); +}; + +EmbarkJS.Storage.uploadFile = function(inputSelector) { + return this.currentStorage.uploadFile(inputSelector); +} + +EmbarkJS.Storage.getUrl = function(hash) { + return this.currentStorage.getUrl(hash); +} + +EmbarkJS.Storage.setProvider = function(provider, options) { var self = this; var promise = new Promise(function(resolve, reject) { - if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) { - resolve(self.currentStorage.ipfsConnection); - } - else { - var error = new Error(provider + ' storage provider not supported'); - reject(error); - }; + if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) { + //I don't think currentStorage is used anywhere, this might not be needed + //for now until additional storage providers are supported. But keeping it + //anyways + self.currentStorage = EmbarkJS.Storage.IPFS; + + try { + if (options === undefined) { + self.ipfsConnection = IpfsApi('localhost', '5001'); + } else { + self.ipfsConnection = IpfsApi(options.server, options.port); + } + resolve(self); + } catch (err) { + self.ipfsConnection = null; + reject(new Error('Failed to connect to IPFS')); + } + } else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM) { + reject('Swarm not implemented'); + // TODO Implement Swarm + // this.currentStorage = EmbarkJS.Storage.SWARM; + // if (options === undefined) { + // //Connect to default Swarm node + // } else { + // //Connect using options + // } + } else { + reject('Unknown storage provider'); + } + }); + return promise; +}; + +EmbarkJS.Storage.IPFS.saveText = function(text) { + var promise = new Promise(function(resolve, reject) { + if (!EmbarkJS.Storage.ipfsConnection) { + var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()'); + reject(connectionError); + } + EmbarkJS.Storage.ipfsConnection.add((new EmbarkJS.Storage.ipfsConnection.Buffer(text)), function(err, result) { + if (err) { + reject(err); + } else { + resolve(result[0].path); + } + }); }); return promise; }; -EmbarkJS.Storage.saveText = function(text) { - return this.currentStorage.saveText(text); -}; - -EmbarkJS.Storage.get = function(hash) { - return this.currentStorage.get(hash); -}; - -EmbarkJS.Storage.uploadFile = function(inputSelector){ - return this.currentStorage.uploadFile(inputSelector); -} - -EmbarkJS.Storage.getUrl = function(hash){ - return this.currentStorage.getUrl(hash); -} - -EmbarkJS.Storage.setProvider = function(provider, options) { - if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) { - //I don't think currentStorage is used anywhere, this might not be needed - //for now until additional storage providers are supported. But keeping it - //anyways - this.currentStorage = EmbarkJS.Storage.IPFS; - try{ - if (options === undefined) { - this.currentStorage.ipfsConnection = IpfsApi('localhost', '5001'); - } else { - this.currentStorage.ipfsConnection = IpfsApi(options.server, options.port); - } - } - catch(err){ - this.currentStorage.ipfsConnection = null; - } - - } - else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM){ - throw Error('Swarm not implemented'); - this.currentStorage = EmbarkJS.Storage.SWARM; - if (options === undefined) { - //Connect to default Swarm node - } else { - //Connect using options - } - } - else { - throw Error('Unknown storage provider'); - } -}; - -EmbarkJS.Storage.IPFS.saveText = function(text) { - var self = this; - if (!self.ipfsConnection) { - EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS); - } - var promise = new Promise(function(resolve, reject) { - if (!self.ipfsConnection){ - var connectionError = new Error('No IPFS connection'); - reject(connectionError); - } - self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) { - if (err) { - reject(err); - } else { - resolve(result[0].path); - } - }); - }); - - return promise; -}; - EmbarkJS.Storage.IPFS.get = function(hash) { - var self = this; - // TODO: detect type, then convert if needed - //var ipfsHash = web3.toAscii(hash); - if (!self.ipfsConnection) { - EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS); - } - var promise = new Promise(function(resolve, reject) { - if (!self.ipfsConnection){ - var connectionError = new Error('No IPFS connection'); - reject(connectionError); - } - self.ipfsConnection.object.get([hash]).then(function(node) { - resolve(node.data); - }).catch(function (err){ - reject(err); + // TODO: detect type, then convert if needed + //var ipfsHash = web3.toAscii(hash); + var promise = new Promise(function(resolve, reject) { + if (!EmbarkJS.Storage.ipfsConnection) { + var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()'); + reject(connectionError); + } + EmbarkJS.Storage.ipfsConnection.object.get([hash]).then(function(node) { + resolve(node.data); + }).catch(function(err) { + reject(err); + }); }); - }); - return promise; + return promise; }; EmbarkJS.Storage.IPFS.uploadFile = function(inputSelector) { - var self = this; - var file = inputSelector[0].files[0]; + var file = inputSelector[0].files[0]; - if (file === undefined) { - throw new Error('no file found'); - } - - if (!self.ipfsConnection) { - EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS); - } - - var promise = new Promise(function(resolve, reject) { - if (!self.ipfsConnection){ - var connectionError = new Error('No IPFS connection'); - reject(connectionError); + if (file === undefined) { + throw new Error('no file found'); } - var reader = new FileReader(); - reader.onloadend = function() { - var fileContent = reader.result; - var buffer = self.ipfsConnection.Buffer.from(fileContent); - self.ipfsConnection.add(buffer, function(err, result) { - if (err) { - reject(err); - } else { - resolve(result[0].path); - } - }); - }; - reader.readAsArrayBuffer(file); - }); - return promise; + var promise = new Promise(function(resolve, reject) { + if (!EmbarkJS.Storage.ipfsConnection) { + var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()'); + reject(connectionError); + } + var reader = new FileReader(); + reader.onloadend = function() { + var fileContent = reader.result; + var buffer = EmbarkJS.Storage.ipfsConnection.Buffer.from(fileContent); + EmbarkJS.Storage.ipfsConnection.add(buffer, function(err, result) { + if (err) { + reject(err); + } else { + resolve(result[0].path); + } + }); + }; + reader.readAsArrayBuffer(file); + }); + + return promise; }; EmbarkJS.Storage.IPFS.getUrl = function(hash) { - //var ipfsHash = web3.toAscii(hash); + //var ipfsHash = web3.toAscii(hash); - return 'http://localhost:8080/ipfs/' + hash; + return 'http://localhost:8080/ipfs/' + hash; }; //========================================================= // Embark Messaging //========================================================= -EmbarkJS.Messages = { -}; +EmbarkJS.Messages = {}; EmbarkJS.Messages.setProvider = function(provider, options) { - var self = this; - var ipfs; - if (provider === 'whisper') { - this.currentMessages = EmbarkJS.Messages.Whisper; - if (typeof variable === 'undefined') { - if (options === undefined) { - web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); - } else { - web3 = new Web3(new Web3.providers.HttpProvider("http://" + options.server + ':' + options.port)); - } - } - web3.version.getWhisper(function(err, res) { - if (err) { - console.log("whisper not available"); - } else { - self.currentMessages.identity = web3.shh.newIdentity(); - } - }); - } else if (provider === 'orbit') { - this.currentMessages = EmbarkJS.Messages.Orbit; - if (options === undefined) { - ipfs = HaadIpfsApi('localhost', '5001'); + var self = this; + var ipfs; + if (provider === 'whisper') { + this.currentMessages = EmbarkJS.Messages.Whisper; + if (typeof variable === 'undefined') { + if (options === undefined) { + web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); + } else { + web3 = new Web3(new Web3.providers.HttpProvider("http://" + options.server + ':' + options.port)); + } + } + web3.version.getWhisper(function(err, res) { + if (err) { + console.log("whisper not available"); + } else { + self.currentMessages.identity = web3.shh.newIdentity(); + } + }); + } else if (provider === 'orbit') { + this.currentMessages = EmbarkJS.Messages.Orbit; + if (options === undefined) { + ipfs = HaadIpfsApi('localhost', '5001'); + } else { + ipfs = HaadIpfsApi(options.server, options.port); + } + this.currentMessages.orbit = new Orbit(ipfs); + this.currentMessages.orbit.connect(web3.eth.accounts[0]); } else { - ipfs = HaadIpfsApi(options.server, options.port); + throw Error('Unknown message provider'); } - this.currentMessages.orbit = new Orbit(ipfs); - this.currentMessages.orbit.connect(web3.eth.accounts[0]); - } else { - throw Error('Unknown message provider'); - } }; EmbarkJS.Messages.sendMessage = function(options) { - return this.currentMessages.sendMessage(options); + return this.currentMessages.sendMessage(options); }; EmbarkJS.Messages.listenTo = function(options) { - return this.currentMessages.listenTo(options); + return this.currentMessages.listenTo(options); }; -EmbarkJS.Messages.Whisper = { -}; +EmbarkJS.Messages.Whisper = {}; EmbarkJS.Messages.Whisper.sendMessage = function(options) { - var topics = options.topic || options.topics; - var data = options.data || options.payload; - var identity = options.identity || this.identity || web3.shh.newIdentity(); - var ttl = options.ttl || 100; - var priority = options.priority || 1000; - var _topics; + var topics = options.topic || options.topics; + var data = options.data || options.payload; + var identity = options.identity || this.identity || web3.shh.newIdentity(); + var ttl = options.ttl || 100; + var priority = options.priority || 1000; + var _topics; - if (topics === undefined) { - throw new Error("missing option: topic"); - } - - if (data === undefined) { - throw new Error("missing option: data"); - } - - // do fromAscii to each topics unless it's already a string - if (typeof topics === 'string') { - _topics = [web3.fromAscii(topics)]; - } else { - // TODO: replace with es6 + babel; - for (var i = 0; i < topics.length; i++) { - _topics.push(web3.fromAscii(topics[i])); + if (topics === undefined) { + throw new Error("missing option: topic"); } - } - topics = _topics; - var payload = JSON.stringify(data); + if (data === undefined) { + throw new Error("missing option: data"); + } - var message = { - from: identity, - topics: topics, - payload: web3.fromAscii(payload), - ttl: ttl, - priority: priority - }; + // do fromAscii to each topics unless it's already a string + if (typeof topics === 'string') { + _topics = [web3.fromAscii(topics)]; + } else { + // TODO: replace with es6 + babel; + for (var i = 0; i < topics.length; i++) { + _topics.push(web3.fromAscii(topics[i])); + } + } + topics = _topics; - return web3.shh.post(message, function() {}); + var payload = JSON.stringify(data); + + var message = { + from: identity, + topics: topics, + payload: web3.fromAscii(payload), + ttl: ttl, + priority: priority + }; + + return web3.shh.post(message, function() {}); }; EmbarkJS.Messages.Whisper.listenTo = function(options) { - var topics = options.topic || options.topics; - var _topics = []; + var topics = options.topic || options.topics; + var _topics = []; - if (typeof topics === 'string') { - _topics = [topics]; - } else { - // TODO: replace with es6 + babel; - for (var i = 0; i < topics.length; i++) { - _topics.push(topics[i]); - } - } - topics = _topics; - - var filterOptions = { - topics: topics - }; - - var messageEvents = function() { - this.cb = function() {}; - }; - - messageEvents.prototype.then = function(cb) { - this.cb = cb; - }; - - messageEvents.prototype.error = function(err) { - return err; - }; - - messageEvents.prototype.stop = function() { - this.filter.stopWatching(); - }; - - var promise = new messageEvents(); - - var filter = web3.shh.filter(filterOptions, function(err, result) { - var payload = JSON.parse(web3.toAscii(result.payload)); - var data; - if (err) { - promise.error(err); + if (typeof topics === 'string') { + _topics = [topics]; } else { - data = { - topic: topics, - data: payload, - from: result.from, - time: (new Date(result.sent * 1000)) - }; - promise.cb(payload, data, result); + // TODO: replace with es6 + babel; + for (var i = 0; i < topics.length; i++) { + _topics.push(topics[i]); + } } - }); + topics = _topics; - promise.filter = filter; + var filterOptions = { + topics: topics + }; - return promise; + var messageEvents = function() { + this.cb = function() {}; + }; + + messageEvents.prototype.then = function(cb) { + this.cb = cb; + }; + + messageEvents.prototype.error = function(err) { + return err; + }; + + messageEvents.prototype.stop = function() { + this.filter.stopWatching(); + }; + + var promise = new messageEvents(); + + var filter = web3.shh.filter(filterOptions, function(err, result) { + var payload = JSON.parse(web3.toAscii(result.payload)); + var data; + if (err) { + promise.error(err); + } else { + data = { + topic: topics, + data: payload, + from: result.from, + time: (new Date(result.sent * 1000)) + }; + promise.cb(payload, data, result); + } + }); + + promise.filter = filter; + + return promise; }; -EmbarkJS.Messages.Orbit = { -}; +EmbarkJS.Messages.Orbit = {}; EmbarkJS.Messages.Orbit.sendMessage = function(options) { - var topics = options.topic || options.topics; - var data = options.data || options.payload; + var topics = options.topic || options.topics; + var data = options.data || options.payload; - if (topics === undefined) { - throw new Error("missing option: topic"); - } + if (topics === undefined) { + throw new Error("missing option: topic"); + } - if (data === undefined) { - throw new Error("missing option: data"); - } + if (data === undefined) { + throw new Error("missing option: data"); + } - if (typeof topics === 'string') { - topics = topics; - } else { - // TODO: better to just send to different channels instead - topics = topics.join(','); - } + if (typeof topics === 'string') { + topics = topics; + } else { + // TODO: better to just send to different channels instead + topics = topics.join(','); + } - this.orbit.join(topics); + this.orbit.join(topics); - var payload = JSON.stringify(data); + var payload = JSON.stringify(data); - this.orbit.send(topics, data); + this.orbit.send(topics, data); }; EmbarkJS.Messages.Orbit.listenTo = function(options) { - var self = this; - var topics = options.topic || options.topics; + var self = this; + var topics = options.topic || options.topics; - if (typeof topics === 'string') { - topics = topics; - } else { - topics = topics.join(','); - } + if (typeof topics === 'string') { + topics = topics; + } else { + topics = topics.join(','); + } - this.orbit.join(topics); + this.orbit.join(topics); - var messageEvents = function() { - this.cb = function() {}; - }; + var messageEvents = function() { + this.cb = function() {}; + }; - messageEvents.prototype.then = function(cb) { - this.cb = cb; - }; + messageEvents.prototype.then = function(cb) { + this.cb = cb; + }; - messageEvents.prototype.error = function(err) { - return err; - }; + messageEvents.prototype.error = function(err) { + return err; + }; - var promise = new messageEvents(); + var promise = new messageEvents(); - this.orbit.events.on('message', (channel, message) => { - // TODO: looks like sometimes it's receving messages from all topics - if (topics !== channel) return; - self.orbit.getPost(message.payload.value, true).then((post) => { - var data = { - topic: channel, - data: post.content, - from: post.meta.from.name, - time: (new Date(post.meta.ts)) - }; - promise.cb(post.content, data, post); + this.orbit.events.on('message', (channel, message) => { + // TODO: looks like sometimes it's receving messages from all topics + if (topics !== channel) return; + self.orbit.getPost(message.payload.value, true).then((post) => { + var data = { + topic: channel, + data: post.content, + from: post.meta.from.name, + time: (new Date(post.meta.ts)) + }; + promise.cb(post.content, data, post); + }); }); - }); - return promise; + return promise; }; module.exports = EmbarkJS; From 4245d63bbba5fe3e0978ad8430f18b42420b3ef3 Mon Sep 17 00:00:00 2001 From: Andy Nogueira Date: Fri, 17 Mar 2017 10:10:05 -0400 Subject: [PATCH 44/45] Improved demo app code to better handle the IPFS storage connection based on the new EmbarkJS refactoring. The old code is stillcompatible with the refactored EmbarkJS code --- demo/app/js/index.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/demo/app/js/index.js b/demo/app/js/index.js index fe7ac8cf..2db14830 100644 --- a/demo/app/js/index.js +++ b/demo/app/js/index.js @@ -32,18 +32,28 @@ $(document).ready(function() { //EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'}); $("#storage .error").hide(); - EmbarkJS.Storage.ipfsConnection.ping() + EmbarkJS.Storage.setProvider('ipfs') .then(function(){ - $("#status-storage").addClass('status-online'); - $("#storage-controls").show(); + console.log('Provider set to IPFS'); + EmbarkJS.Storage.ipfsConnection.ping() + .then(function(){ + $("#status-storage").addClass('status-online'); + $("#storage-controls").show(); + }) + .catch(function(err) { + if(err){ + console.log("IPFS Connection Error => " + err.message); + $("#storage .error").show(); + $("#status-storage").addClass('status-offline'); + $("#storage-controls").hide(); + } + }); }) - .catch(function(err) { - if(err){ - console.log("IPFS Connection Error => " + err.message); - $("#storage .error").show(); - $("#status-storage").addClass('status-offline'); - $("#storage-controls").hide(); - } + .catch(function(err){ + console.log('Failed to set IPFS as Provider:', err.message); + $("#storage .error").show(); + $("#status-storage").addClass('status-offline'); + $("#storage-controls").hide(); }); $("#storage button.setIpfsText").click(function() { From 47aaa5bb1934180095b431ec249d7f0a543e5a59 Mon Sep 17 00:00:00 2001 From: Andy Nogueira Date: Sat, 18 Mar 2017 10:14:19 -0400 Subject: [PATCH 45/45] Added missing semi-colons --- js/embark.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/embark.js b/js/embark.js index 216efcad..8f31e9ac 100644 --- a/js/embark.js +++ b/js/embark.js @@ -167,11 +167,11 @@ EmbarkJS.Storage.get = function(hash) { EmbarkJS.Storage.uploadFile = function(inputSelector) { return this.currentStorage.uploadFile(inputSelector); -} +}; EmbarkJS.Storage.getUrl = function(hash) { return this.currentStorage.getUrl(hash); -} +}; EmbarkJS.Storage.setProvider = function(provider, options) { var self = this;