From a2a27bb0c2d8f539c3e9c58a8904038424df13f2 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 24 Feb 2017 08:20:03 -0500 Subject: [PATCH] start refactoring deployment steps --- lib/contracts/abi.js | 6 +- lib/contracts/contracts.js | 8 +-- lib/contracts/deploy_manager.js | 83 ++++++++++++++++++++++++ lib/index.js | 110 ++++++-------------------------- 4 files changed, 112 insertions(+), 95 deletions(-) create mode 100644 lib/contracts/deploy_manager.js diff --git a/lib/contracts/abi.js b/lib/contracts/abi.js index 86480995..7246de69 100644 --- a/lib/contracts/abi.js +++ b/lib/contracts/abi.js @@ -108,8 +108,10 @@ ABIGenerator.prototype.generateABI = function(options) { result += this.generateProvider(); result += this.generateContracts(options.useEmbarkJS); - result += this.generateStorageInitialization(options.useEmbarkJS); - result += this.generateCommunicationInitialization(options.useEmbarkJS); + + // TODO: disable this for now until refactor is over + //result += this.generateStorageInitialization(options.useEmbarkJS); + //result += this.generateCommunicationInitialization(options.useEmbarkJS); return result; }; diff --git a/lib/contracts/contracts.js b/lib/contracts/contracts.js index 9bd20e05..ef7df60e 100644 --- a/lib/contracts/contracts.js +++ b/lib/contracts/contracts.js @@ -41,14 +41,14 @@ ContractsManager.prototype.build = function(done) { function compileContracts(callback) { var compiler = new Compiler({plugins: self.plugins}); // TODO: check if try is still needed - try { + //try { compiler.compile_contracts(self.contractFiles, function(compiledObject) { self.compiledContracts = compiledObject; callback(); }); - } catch(err) { - return callback(new Error(err.message)); - } + //} catch(err) { + // return callback(new Error(err.message)); + //} }, function prepareContractsFromConfig(callback) { var className, contract; diff --git a/lib/contracts/deploy_manager.js b/lib/contracts/deploy_manager.js new file mode 100644 index 00000000..08c535bc --- /dev/null +++ b/lib/contracts/deploy_manager.js @@ -0,0 +1,83 @@ +var async = require('async'); +var Web3 = require('web3'); + +var Deploy = require('./deploy.js'); +var ContractsManager = require('./contracts.js'); +var ABIGenerator = require('./abi.js'); + +var DeployManager = function(options) { + this.config = options.config; + this.logger = options.logger; + this.plugins = options.plugins; + this.events = options.events; +}; + +DeployManager.prototype.deployContracts = function(done) { + var self = this; + async.waterfall([ + function buildContracts(callback) { + var contractsManager = new ContractsManager({ + contractFiles: self.config.contractsFiles, + contractsConfig: self.config.contractsConfig, + logger: self.logger, + plugins: self.plugins + }); + contractsManager.build(callback); + }, + function deployContracts(contractsManager, callback) { + + //TODO: figure out where to put this since the web3 can be passed along if needed + // perhaps it should go into the deploy object itself + // TODO: should come from the config object + var web3 = new Web3(); + var web3Endpoint = 'http://' + self.config.blockchainConfig.rpcHost + ':' + self.config.blockchainConfig.rpcPort; + web3.setProvider(new web3.providers.HttpProvider(web3Endpoint)); + + if (!web3.isConnected()) { + console.log(("Couldn't connect to " + web3Endpoint.underline + " are you sure it's on?").red); + console.log("make sure you have an ethereum node or simulator running. e.g 'embark blockchain'".magenta); + // =================================== + // TODO: should throw exception instead + // =================================== + process.exit(); + } + + web3.eth.getAccounts(function(err, accounts) { + if (err) { + return callback(new Error(err)); + } + web3.eth.defaultAccount = accounts[0]; + + var deploy = new Deploy({ + web3: web3, + contractsManager: contractsManager, + logger: self.logger, + chainConfig: self.config.chainTracker, + env: self.config.env + }); + deploy.deployAll(function() { + callback(null, contractsManager); + }); + }); + }, + function generateABI(contractsManager, callback) { + var abiGenerator = new ABIGenerator({blockchainConfig: self.config.blockchainConfig, contractsManager: contractsManager, plugins: self.plugins, storageConfig: self.config.storageConfig}); + var embarkJSABI = abiGenerator.generateABI({useEmbarkJS: true}); + var vanillaABI = abiGenerator.generateABI({useEmbarkJS: false}); + + self.events.emit('abi-vanila', vanillaABI); + self.events.emit('abi', embarkJSABI); + + callback(null, embarkJSABI); + } + ], function(err, result) { + if (err) { + done(err, null); + } else { + done(null, result); + } + }); +}; + +module.exports = DeployManager; + diff --git a/lib/index.js b/lib/index.js index 8f2bf337..1ffed4ba 100644 --- a/lib/index.js +++ b/lib/index.js @@ -7,9 +7,7 @@ var Blockchain = require('./cmds/blockchain/blockchain.js'); var Simulator = require('./cmds/simulator.js'); var TemplateGenerator = require('./cmds/template_generator.js'); -var Deploy = require('./contracts/deploy.js'); -var ContractsManager = require('./contracts/contracts.js'); -var ABIGenerator = require('./contracts/abi.js'); +var DeployManager = require('./contracts/deploy_manager.js'); var Test = require('./core/test.js'); var Logger = require('./core/logger.js'); @@ -138,7 +136,20 @@ var Embark = { Embark.servicesMonitor.startMonitor(); callback(); }, - self.buildDeployGenerate.bind(self), + function (callback) { + var deployManager = new DeployManager({ + config: Embark.config, + logger: Embark.logger, + plugins: self.plugins, + events: self.events + }); + deployManager.deployContracts(function(abi) { + callback(null, abi); + }); + //if (Embark.dashboard) { + // Embark.dashboard.console.runCode(consoleABI); + //} + }, function buildPipeline(abi, callback) { self.logger.setStatus("Building Assets"); var pipeline = new Pipeline({ @@ -148,6 +159,7 @@ var Embark = { logger: self.logger, plugins: self.plugins }); + // TODO: do this with event instead pipeline.build(abi); callback(); }, @@ -158,7 +170,7 @@ var Embark = { self.events.on('file-event', function(fileType, path) { if (fileType === 'contract' || fileType === 'config') { self.logger.info("received redeploy event"); - Embark.redeploy(); + //Embark.redeploy(); } }); self.events.on('rebuildAssets', function(fileType, path) { @@ -166,7 +178,7 @@ var Embark = { self.logger.info("received rebuildAssets event"); // TODO: can just rebuild pipeline, no need to deploy contracts // again - Embark.redeploy(); + //Embark.redeploy(); } }); callback(); @@ -211,7 +223,9 @@ var Embark = { logger: self.logger, plugins: self.plugins }); - pipeline.build(abi); + Embark.events.on('abi', function(abi) { + pipeline.build(abi); + }); callback(); } ], function(err, result) { @@ -223,64 +237,6 @@ var Embark = { }); }, - buildAndDeploy: function(done) { - var self = this; - async.waterfall([ - function buildContracts(callback) { - var contractsManager = new ContractsManager({ - contractFiles: self.config.contractsFiles, - contractsConfig: self.config.contractsConfig, - logger: Embark.logger, - plugins: self.plugins - }); - contractsManager.build(callback); - }, - function deployContracts(contractsManager, callback) { - - //TODO: figure out where to put this since the web3 can be passed along if needed - // perhaps it should go into the deploy object itself - // TODO: should come from the config object - var web3 = new Web3(); - var web3Endpoint = 'http://' + self.config.blockchainConfig.rpcHost + ':' + self.config.blockchainConfig.rpcPort; - web3.setProvider(new web3.providers.HttpProvider(web3Endpoint)); - - if (!web3.isConnected()) { - console.log(("Couldn't connect to " + web3Endpoint.underline + " are you sure it's on?").red); - console.log("make sure you have an ethereum node or simulator running. e.g 'embark blockchain'".magenta); - // =================================== - // TODO: should throw exception instead - // =================================== - process.exit(); - } - - web3.eth.getAccounts(function(err, accounts) { - if (err) { - return callback(new Error(err)); - } - web3.eth.defaultAccount = accounts[0]; - - var deploy = new Deploy({ - web3: web3, - contractsManager: contractsManager, - logger: Embark.logger, - chainConfig: self.config.chainTracker, - env: self.config.env - }); - deploy.deployAll(function() { - callback(null, contractsManager); - }); - }); - - } - ], function(err, result) { - if (err) { - done(err, null); - } else { - done(null, result); - } - }); - }, - deploy: function(done) { var self = this; async.waterfall([ @@ -290,8 +246,6 @@ var Embark = { }); }, function generateABI(contractsManager, callback) { - var abiGenerator = new ABIGenerator({blockchainConfig: self.config.blockchainConfig, contractsManager: contractsManager, plugins: self.plugins, storageConfig: self.config.storageConfig}); - callback(null, abiGenerator.generateABI({useEmbarkJS: true})); } ], function(err, result) { if (err) { @@ -301,38 +255,16 @@ var Embark = { }); }, - buildDeployGenerate: function(done) { var self = this; - if (self.config.blockchainConfig.enabled === false) { - self.logger.info('== blockchain is disabled in this environment in config/blockchain.json'.underline); - return done(null, ""); - } - self.logger.setStatus("Deploying...".magenta.underline); async.waterfall([ - function deployAndBuildContractsManager(callback) { - Embark.buildAndDeploy(function(err, contractsManager) { - if (err) { - return callback(err); - } - return callback(null, contractsManager); - }); - }, function generateConsoleABI(contractsManager, callback) { - var abiGenerator = new ABIGenerator({blockchainConfig: self.config.blockchainConfig, contractsManager: contractsManager, storageConfig: self.config.storageConfig, communicationConfig: self.config.communicationConfig}); - var consoleABI = abiGenerator.generateABI({useEmbarkJS: false}); - // not good, better generate events when deployment is done and do this // through a listener - if (Embark.dashboard) { - Embark.dashboard.console.runCode(consoleABI); - } callback(null, contractsManager); }, function generateABI(contractsManager, callback) { - var abiGenerator = new ABIGenerator({blockchainConfig: self.config.blockchainConfig, contractsManager: contractsManager, plugins: self.plugins, storageConfig: self.config.storageConfig, communicationConfig: self.config.communicationConfig}); - callback(null, abiGenerator.generateABI({useEmbarkJS: true})); } ], function(err, result) { if (err) {