diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index e7adc4c9..90231e8c 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -28,8 +28,6 @@ class CodeGenerator { this.communicationConfig = options.communicationConfig || {}; this.namesystemConfig = options.namesystemConfig || {}; this.env = options.env || 'development'; - // TODO: this should also be removed and use events instead - this.contractsManager = options.contractsManager; this.plugins = options.plugins; this.events = options.events; } @@ -43,71 +41,47 @@ class CodeGenerator { cb(providerCode); }); - // deprecated events; to remove in embark 2.7.0 - this.events.setCommandHandlerOnce('abi-vanila', function(cb) { - let vanillaABI = self.generateABI({useEmbarkJS: false}); - let contractsJSON = self.generateContractsJSON(); - - cb(vanillaABI, contractsJSON); - }); - - this.events.setCommandHandlerOnce('abi', function(cb) { - let embarkJSABI = self.generateABI({useEmbarkJS: true}); - let contractsJSON = self.generateContractsJSON(); - - cb(embarkJSABI, contractsJSON); - }); - - this.events.setCommandHandlerOnce('abi-contracts-vanila', function(cb) { - let vanillaContractsABI = self.generateContracts(false, true, false); - let contractsJSON = self.generateContractsJSON(); - - cb(vanillaContractsABI, contractsJSON); - }); - - this.events.setCommandHandlerOnce('abi-vanila-deployment', function(cb) { - let vanillaABI = self.generateABI({useEmbarkJS: false, deployment: true}); - let contractsJSON = self.generateContractsJSON(); - - cb(vanillaABI, contractsJSON); - }); - // new events this.events.setCommandHandlerOnce('code-vanila', function(cb) { - let vanillaABI = self.generateABI({useEmbarkJS: false}); - let contractsJSON = self.generateContractsJSON(); - - cb(vanillaABI, contractsJSON); + self.events.request("contracts:list", (contractsList) => { + let vanillaABI = self.generateABI(contractsList, {useEmbarkJS: false}); + let contractsJSON = self.generateContractsJSON(contractsList); + cb(vanillaABI, contractsJSON); + }); }); this.events.setCommandHandlerOnce('code', function(cb) { - let embarkJSABI = self.generateABI({useEmbarkJS: true}); - let contractsJSON = self.generateContractsJSON(); - - cb(embarkJSABI, contractsJSON); + self.events.request("contracts:list", (contractsList) => { + let embarkJSABI = self.generateABI(contractsList, {useEmbarkJS: true}); + let contractsJSON = self.generateContractsJSON(contractsList); + cb(embarkJSABI, contractsJSON); + }); }); this.events.setCommandHandlerOnce('code-contracts-vanila', function(cb) { - let vanillaContractsABI = self.generateContracts(false, true, false); - let contractsJSON = self.generateContractsJSON(); - - cb(vanillaContractsABI, contractsJSON); + self.events.request("contracts:list", (contractsList) => { + let vanillaContractsABI = self.generateContracts(contractsList, false, true, false); + let contractsJSON = self.generateContractsJSON(contractsList); + cb(vanillaContractsABI, contractsJSON); + }); }); this.events.setCommandHandlerOnce('code-vanila-deployment', function(cb) { - let vanillaABI = self.generateABI({useEmbarkJS: false, deployment: true}); - let contractsJSON = self.generateContractsJSON(); - - cb(vanillaABI, contractsJSON); + self.events.request("contracts:list", (contractsList) => { + let vanillaABI = self.generateABI(contractsList, {useEmbarkJS: false, deployment: true}); + let contractsJSON = self.generateContractsJSON(contractsList); + cb(vanillaABI, contractsJSON); + }); }); this.events.setCommandHandlerOnce('code-generator:web3js', function(cb) { self.buildWeb3JS(cb); }); - this.events.setCommandHandler('code-generator:contract', (contractName, cb) => { - let contract = self.contractsManager.contracts[contractName]; - self.buildContractJS(contractName, self.generateContractJSON(contractName, contract), cb); + self.events.setCommandHandler('code-generator:contract', (contractName, cb) => { + self.events.request('contracts:contract', contractName, (contract) => { + self.buildContractJS(contractName, self.generateContractJSON(contract, contract), cb); + }); }); self.events.setCommandHandler('code-generator:contract:vanilla', (contract, gasLimit, cb) => { @@ -115,7 +89,7 @@ class CodeGenerator { }); this.events.setCommandHandler('embark-building-placeholder', (cb) => { - this.buildPlaceholderPage(cb); + self.buildPlaceholderPage(cb); }); } @@ -173,16 +147,15 @@ class CodeGenerator { return result; } - generateContracts(useEmbarkJS, isDeployment, useLoader) { + generateContracts(contractsList, useEmbarkJS, isDeployment, useLoader) { let self = this; let result = "\n"; let contractsPlugins; if (useLoader === false) { - for (let className in this.contractsManager.contracts) { - let contract = this.contractsManager.contracts[className]; + for (let contract of contractsList) { let abi = JSON.stringify(contract.abiDefinition); - result += Templates.vanilla_contract({className: className, abi: abi, contract: contract, gasLimit: 6000000}); + result += Templates.vanilla_contract({className: contract.className, abi: abi, contract: contract, gasLimit: 6000000}); } return result; } @@ -197,12 +170,10 @@ class CodeGenerator { if (this.plugins && contractsPlugins.length > 0) { contractsPlugins.forEach(function (plugin) { - result += plugin.generateContracts({contracts: self.contractsManager.contracts}); + result += plugin.generateContracts({contracts: contractsList}); }); } else { - for (let className in this.contractsManager.contracts) { - let contract = this.contractsManager.contracts[className]; - + for (let contract of contractsList) { let abi = JSON.stringify(contract.abiDefinition); let gasEstimates = JSON.stringify(contract.gasEstimates); @@ -210,9 +181,9 @@ class CodeGenerator { if (useEmbarkJS) { let contractAddress = contract.deployedAddress ? ("'" + contract.deployedAddress + "'") : "undefined"; - block += Templates.embarkjs_contract({className: className, abi: abi, contract: contract, contractAddress: contractAddress, gasEstimates: gasEstimates}); + block += Templates.embarkjs_contract({className: contract.className, abi: abi, contract: contract, contractAddress: contractAddress, gasEstimates: gasEstimates}); } else { - block += Templates.vanilla_contract({className: className, abi: abi, contract: contract, gasLimit: (isDeployment ? 6000000 : false)}); + block += Templates.vanilla_contract({className: contract.className, abi: abi, contract: contract, gasLimit: (isDeployment ? 6000000 : false)}); } result += Templates.exec_when_ready({block: block}); @@ -276,11 +247,11 @@ class CodeGenerator { return result; } - generateABI(options) { + generateABI(contractsList, options) { let result = ""; result += this.generateProvider(options.deployment); - result += this.generateContracts(options.useEmbarkJS, options.deployment, true); + result += this.generateContracts(contractsList, options.useEmbarkJS, options.deployment, true); result += this.generateStorageInitialization(options.useEmbarkJS); result += this.generateCommunicationInitialization(options.useEmbarkJS); result += this.generateNamesInitialization(options.useEmbarkJS); @@ -304,12 +275,11 @@ class CodeGenerator { return contractJSON; } - generateContractsJSON() { + generateContractsJSON(contractsList) { let contracts = {}; - for (let className in this.contractsManager.contracts) { - let contract = this.contractsManager.contracts[className]; - contracts[className] = this.generateContractJSON(className, contract); + for (let contract of contractsList) { + contracts[contract.className] = this.generateContractJSON(contract.className, contract); } return contracts; diff --git a/lib/contracts/deploy.js b/lib/contracts/contract_deployer.js similarity index 67% rename from lib/contracts/deploy.js rename to lib/contracts/contract_deployer.js index 462d7a81..23476954 100644 --- a/lib/contracts/deploy.js +++ b/lib/contracts/contract_deployer.js @@ -2,13 +2,19 @@ let async = require('async'); //require("../utils/debug_util.js")(__filename, async); let utils = require('../utils/utils.js'); -class Deploy { +class ContractDeployer { constructor(options) { + const self = this; + this.blockchain = options.blockchain; this.logger = options.logger; this.events = options.events; this.plugins = options.plugins; this.gasLimit = options.gasLimit; + + self.events.setCommandHandler('deploy:contract', (contract, cb) => { + self.checkAndDeployContract(contract, null, cb); + }); } // TODO: determining the arguments could also be in a module since it's not @@ -30,40 +36,30 @@ class Deploy { } } - let realArgs = []; - async.eachLimit(args, 1, (arg, nextEachCb) => { + async.map(args, (arg, nextEachCb) => { if (arg[0] === "$") { let contractName = arg.substr(1); self.events.request('contracts:contract', contractName, (referedContract) => { - realArgs.push(referedContract.deployedAddress); - nextEachCb(); + nextEachCb(null, referedContract.deployedAddress); }); } else if (Array.isArray(arg)) { - let subRealArgs = []; - - async.eachLimit(arg, 1, (sub_arg, nextSubEachCb) => { + async.map(arg, (sub_arg, nextSubEachCb) => { if (sub_arg[0] === "$") { let contractName = sub_arg.substr(1); self.events.request('contracts:contract', contractName, (referedContract) => { - subRealArgs.push(referedContract.deployedAddress); - nextSubEachCb(); + nextSubEachCb(null, referedContract.deployedAddress); }); } else { - subRealArgs.push(sub_arg); - nextSubEachCb(); + nextSubEachCb(null, sub_arg); } - }, () => { - realArgs.push(subRealArgs); - nextEachCb(); + }, (err, subRealArgs) => { + nextEachCb(null, subRealArgs); }); } else { - realArgs.push(arg); - nextEachCb(); + nextEachCb(null, arg); } - }, () => { - callback(realArgs); - }); + }, callback); } checkAndDeployContract(contract, params, callback) { @@ -77,7 +73,10 @@ class Deploy { async.waterfall([ function _determineArguments(next) { - self.determineArguments(params || contract.args, contract, (realArgs) => { + self.determineArguments(params || contract.args, contract, (err, realArgs) => { + if (err) { + return next(err); + } contract.realArgs = realArgs; next(); }); @@ -102,14 +101,14 @@ class Deploy { // TODO: this should be a plugin API instead, if not existing, it should by default deploy the contract self.events.request("deploy:contract:shouldDeploy", contract, function(trackedContract) { if (!trackedContract) { - return self.contractToDeploy(contract, params, next); + return self.deployContract(contract, next); } self.blockchain.getCode(trackedContract.address, function(_getCodeErr, codeInChain) { if (codeInChain !== "0x") { self.contractAlreadyDeployed(contract, trackedContract, next); } else { - self.contractToDeploy(contract, params, next); + self.deployContract(contract, next); } }); }); @@ -125,49 +124,16 @@ class Deploy { // TODO: can be moved into a afterDeploy event // just need to figure out the gasLimit coupling issue - self.events.request('code-generator:contract:vanilla', contract, self.gasLimit, (contractCode) => { + self.events.request('code-generator:contract:vanilla', contract, contract._gasLimit, (contractCode) => { self.events.request('runcode:eval', contractCode); return callback(); }); } - contractToDeploy(contract, params, callback) { - const self = this; - - // TODO: refactor to async - self.determineArguments(params || contract.args, contract, (realArgs) => { - contract.realArgs = realArgs; - - this.deployContract(contract, contract.realArgs, function (err, address) { - if (err) { - self.events.emit("deploy:contract:error", contract); - return callback(new Error(err)); - } - contract.address = address; - self.events.emit("deploy:contract:deployed", contract); - - // TODO: can be moved into a afterDeploy event - // just need to figure out the gasLimit coupling issue - self.events.request('code-generator:contract:vanilla', contract, self.gasLimit, (contractCode) => { - self.events.request('runcode:eval', contractCode); - - let onDeployPlugins = self.plugins.getPluginsProperty('onDeployActions', 'onDeployActions'); - - async.eachLimit(onDeployPlugins, 1, function(plugin, nextEach) { - plugin.call(plugin, contract, nextEach); - }, () => { - callback(); - }); - }); - - }); - }); - } - - deployContract(contract, params, callback) { + deployContract(contract, callback) { let self = this; let accounts = []; - let contractParams = (params || contract.args).slice(); + let contractParams = (contract.realArgs || contract.args).slice(); let contractCode = contract.code; let deploymentAccount = self.blockchain.defaultAccount(); let deployObject; @@ -227,34 +193,7 @@ class Deploy { }); }, function applyBeforeDeploy(next) { - let beforeDeployPlugins = self.plugins.getPluginsFor('beforeDeploy'); - - //self.logger.info("applying beforeDeploy plugins...", beforeDeployPlugins.length); - async.eachSeries(beforeDeployPlugins, (plugin, eachPluginCb) => { - self.logger.info(__("running beforeDeploy plugin %s .", plugin.name)); - - // calling each beforeDeploy handler declared by the plugin - async.eachSeries(plugin.beforeDeploy, (beforeDeployFn, eachCb) => { - function beforeDeployCb(resObj){ - contract.code = resObj.contractCode; - eachCb(); - } - beforeDeployFn({ - embarkDeploy: self, - pluginConfig: plugin.pluginConfig, - deploymentAccount: deploymentAccount, - contract: contract, - callback: beforeDeployCb - }, beforeDeployCb); - }, () => { - //self.logger.info('All beforeDeploy handlers of the plugin has processed.'); - eachPluginCb(); - }); - }, () => { - //self.logger.info('All beforeDeploy plugins has been processed.'); - contractCode = contract.code; - next(); - }); + self.plugins.emitAndRunActionsForEvent('deploy:contract:beforeDeploy', {contract: contract}, next); }, function createDeployObject(next) { let contractObject = self.blockchain.ContractObject({abi: contract.abiDefinition}); @@ -289,46 +228,29 @@ class Deploy { gasPrice: contract.gasPrice }, function(error, receipt) { if (error) { + contract.error = error.message; + self.events.emit("deploy:contract:error", contract); return next(new Error("error deploying =" + contract.className + "= due to error: " + error.message)); } self.logger.info(contract.className.bold.cyan + " " + __("deployed at").green + " " + receipt.contractAddress.bold.cyan); contract.deployedAddress = receipt.contractAddress; contract.transactionHash = receipt.transactionHash; - return next(null, receipt.contractAddress); + self.events.emit("deploy:contract:receipt", receipt); + self.events.emit("deploy:contract:deployed", contract); + + // TODO: can be moved into a afterDeploy event + // just need to figure out the gasLimit coupling issue + self.events.request('code-generator:contract:vanilla', contract, contract._gasLimit, (contractCode) => { + self.events.request('runcode:eval', contractCode); + self.plugins.runActionsForEvent('deploy:contract:deployed', {contract: contract}, () => { + return next(null, receipt); + }); + }); }); } ], callback); } - deployAll(done) { - let self = this; - this.logger.info(__("deploying contracts")); - this.events.emit("deploy:beforeAll"); - - self.events.request('contracts:list', (contracts) => { - async.eachOfSeries(contracts, - function (contract, key, callback) { - self.logger.trace(arguments); - self.checkAndDeployContract(contract, null, callback); - }, - function (err, _results) { - if (err) { - self.logger.error(__("error deploying contracts")); - self.logger.error(err.message); - self.logger.debug(err.stack); - } - if (contracts.length === 0) { - self.logger.info(__("no contracts found")); - return done(); - } - self.logger.info(__("finished deploying contracts")); - self.logger.trace(arguments); - done(err); - } - ); - }); - - } } -module.exports = Deploy; +module.exports = ContractDeployer; diff --git a/lib/contracts/contracts.js b/lib/contracts/contracts.js index 6f9fbd81..b09945b6 100644 --- a/lib/contracts/contracts.js +++ b/lib/contracts/contracts.js @@ -34,6 +34,13 @@ class ContractsManager { cb(self.getContract(contractName)); }); + self.events.setCommandHandler("contracts:build", (configOnly, cb) => { + self.deployOnlyOnConfig = configOnly; // temporary, should refactor + self.build(() => { + cb(); + }); + }); + self.events.on("deploy:contract:error", (_contract) => { self.events.emit('contractsState', self.contractsState()); }); @@ -45,6 +52,7 @@ class ContractsManager { self.events.on("deploy:contract:undeployed", (_contract) => { self.events.emit('contractsState', self.contractsState()); }); + } build(done) { diff --git a/lib/contracts/deploy_manager.js b/lib/contracts/deploy_manager.js index e5954138..ff87adf4 100644 --- a/lib/contracts/deploy_manager.js +++ b/lib/contracts/deploy_manager.js @@ -1,9 +1,8 @@ let async = require('async'); -//require("../utils/debug_util.js")(__filename, async); -let Deploy = require('./deploy.js'); class DeployManager { constructor(options) { + const self = this; this.config = options.config; this.logger = options.logger; this.blockchainConfig = this.config.blockchainConfig; @@ -11,12 +10,44 @@ class DeployManager { this.events = options.events; this.plugins = options.plugins; this.blockchain = options.blockchain; - this.chainConfig = (options.trackContracts !== false) ? this.config.chainTracker : false; - this.contractsManager = options.contractsManager; this.gasLimit = false; this.fatalErrors = false; this.deployOnlyOnConfig = false; this.onlyCompile = options.onlyCompile !== undefined ? options.onlyCompile : false; + + this.events.setCommandHandler('deploy:contracts', (cb) => { + self.deployContracts(cb); + }); + } + + deployAll(done) { + let self = this; + this.logger.info(__("deploying contracts")); + this.events.emit("deploy:beforeAll"); + + self.events.request('contracts:list', (contracts) => { + async.eachOfSeries(contracts, + function (contract, key, callback) { + contract._gasLimit = self.gasLimit; + self.events.request('deploy:contract', contract, () => { + callback(); + }); + }, + function (err, _results) { + if (err) { + self.logger.error(__("error deploying contracts")); + self.logger.error(err.message); + self.logger.debug(err.stack); + } + if (contracts.length === 0) { + self.logger.info(__("no contracts found")); + return done(); + } + self.logger.info(__("finished deploying contracts")); + done(err); + } + ); + }); } deployContracts(done) { @@ -30,8 +61,7 @@ class DeployManager { async.waterfall([ function buildContracts(callback) { - self.contractsManager.deployOnlyOnConfig = self.deployOnlyOnConfig; // temporary, should refactor - self.contractsManager.build(() => { + self.events.request("contracts:build", self.deployOnlyOnConfig, () => { callback(); }); }, @@ -39,7 +69,7 @@ class DeployManager { // TODO: shouldn't be necessary function checkCompileOnly(callback){ if(self.onlyCompile){ - self.events.emit('contractsDeployed', self.contractsManager); + self.events.emit('contractsDeployed'); return done(); } return callback(); @@ -62,20 +92,9 @@ class DeployManager { }, function deployAllContracts(callback) { - let deploy = new Deploy({ - blockchain: self.blockchain, - contractsManager: self.contractsManager, - logger: self.logger, - events: self.events, - chainConfig: self.chainConfig, - env: self.config.env, - plugins: self.plugins, - gasLimit: self.gasLimit - }); - - deploy.deployAll(function (err) { + self.deployAll(function (err) { if (!err) { - self.events.emit('contractsDeployed', self.contractsManager); + self.events.emit('contractsDeployed'); } if (err && self.fatalErrors) { return callback(err); @@ -84,13 +103,7 @@ class DeployManager { }); }, function runAfterDeploy(callback) { - let afterDeployPlugins = self.plugins.getPluginsProperty('afterContractsDeployActions', 'afterContractsDeployActions'); - - async.eachLimit(afterDeployPlugins, 1, function(plugin, nextEach) { - plugin.call(plugin, nextEach); - }, () => { - callback(); - }); + self.plugins.emitAndRunActionsForEvent('contracts:deploy:afterAll', callback); } ], function (err, _result) { done(err); diff --git a/lib/core/engine.js b/lib/core/engine.js index 440dc695..cf7336e2 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -6,6 +6,7 @@ const Config = require('./config.js'); const Blockchain = require('../contracts/blockchain.js'); const Compiler = require('../contracts/compiler.js'); const ContractsManager = require('../contracts/contracts.js'); +const ContractDeployer = require('../contracts/contract_deployer.js'); const DeployManager = require('../contracts/deploy_manager.js'); const CodeGenerator = require('../contracts/code_generator.js'); const ServicesMonitor = require('./services_monitor.js'); @@ -92,7 +93,7 @@ class Engine { } registerModule(moduleName, options) { - this.plugins.loadInternalPlugin(moduleName, options); + this.plugins.loadInternalPlugin(moduleName, options || {}); } startService(serviceName, _options) { @@ -148,12 +149,7 @@ class Engine { } namingSystem(_options) { - this.registerModule('ens', { - logger: this.logger, - events: this.events, - web3: this.blockchain.web3, - namesConfig: this.config.namesystemConfig - }); + this.registerModule('ens'); } codeRunnerService(_options) { @@ -170,7 +166,6 @@ class Engine { this.codeGenerator = new CodeGenerator({ blockchainConfig: self.config.blockchainConfig, contractsConfig: self.config.contractsConfig, - contractsManager: this.contractsManager, plugins: self.plugins, storageConfig: self.config.storageConfig, namesystemConfig: self.config.namesystemConfig, @@ -185,12 +180,12 @@ class Engine { self.events.emit('code-generator-ready'); }); }; - const cargo = async.cargo((tasks, callback) => { - generateCode(tasks[tasks.length - 1].contractsManager); + const cargo = async.cargo((_tasks, callback) => { + generateCode(); self.events.once('outputDone', callback); }); - const addToCargo = function (contractsManager) { - cargo.push({contractsManager}); + const addToCargo = function () { + cargo.push({}); }; this.events.on('contractsDeployed', addToCargo); @@ -206,22 +201,11 @@ class Engine { compiler.compile_contracts(contractFiles, cb); }); - this.registerModule('solidity', { - contractDirectories: self.config.contractDirectories - }); - this.registerModule('vyper', { - contractDirectories: self.config.contractDirectories - }); - this.registerModule('profiler', { - events: this.events, - logger: this.logger - }); - - this.registerModule('deploytracker', { - }); - - this.registerModule('specialconfigs', { - }); + this.registerModule('solidity'); + this.registerModule('vyper'); + this.registerModule('profiler'); + this.registerModule('deploytracker'); + this.registerModule('specialconfigs'); this.contractsManager = new ContractsManager({ contractFiles: this.config.contractsFiles, @@ -234,15 +218,20 @@ class Engine { this.deployManager = new DeployManager({ blockchain: this.blockchain, - trackContracts: options.trackContracts, config: this.config, logger: this.logger, plugins: this.plugins, events: this.events, - contractsManager: this.contractsManager, onlyCompile: options.onlyCompile }); + this.contractDeployer = new ContractDeployer({ + blockchain: this.blockchain, + logger: this.logger, + events: this.events, + plugins: this.plugins + }); + this.events.on('file-event', function (fileType) { clearTimeout(self.fileTimeout); self.fileTimeout = setTimeout(() => { @@ -256,8 +245,8 @@ class Engine { // because the contractsManager config is corrupted after a deploy if (fileType === 'contract' || fileType === 'config') { self.config.reloadConfig(); - self.deployManager.deployContracts(function () { - }); + + self.events.request('deploy:contracts', () => {}); } }, 50); }); @@ -278,7 +267,6 @@ class Engine { ipfsService(_options) { this.registerModule('ipfs', { addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), - storageConfig: this.config.storageConfig, host: _options.host, port: _options.port }); @@ -287,7 +275,6 @@ class Engine { swarmService(_options) { this.registerModule('swarm', { addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), - storageConfig: this.config.storageConfig, // TODO: this should not be needed and should be deducted from the config instead // the eth provider is not necessary the same as the swarm one bzz: this.blockchain.web3.bzz @@ -308,7 +295,6 @@ class Engine { this.registerModule('whisper', { addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), - communicationConfig: this.config.communicationConfig, // TODO: this should not be needed and should be deducted from the config instead // the eth provider is not necessary the same as the whisper one web3: this.blockchain.web3 diff --git a/lib/core/events.js b/lib/core/events.js index 2111e221..f68d736a 100644 --- a/lib/core/events.js +++ b/lib/core/events.js @@ -1,7 +1,7 @@ var EventEmitter = require('events'); function warnIfLegacy(eventName) { - const legacyEvents = ['abi-vanila', 'abi', 'abi-contracts-vanila', 'abi-vanila-deployment']; + const legacyEvents = []; if (legacyEvents.indexOf(eventName) >= 0) { console.info(__("this event is deprecated and will be removed in future versions %s", eventName)); } diff --git a/lib/core/plugin.js b/lib/core/plugin.js index 17feb99a..fa8c8650 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -1,6 +1,7 @@ const fs = require('./fs.js'); const utils = require('../utils/utils.js'); const constants = require('../constants'); +const _ = require('underscore'); // TODO: pass other params like blockchainConfig, contract files, etc.. var Plugin = function(options) { @@ -27,6 +28,7 @@ var Plugin = function(options) { this.embarkjs_init_code = {}; this.afterContractsDeployActions = []; this.onDeployActions = []; + this.eventActions = {}; this.logger = options.logger; this.events = options.events; this.config = options.config; @@ -119,43 +121,45 @@ Plugin.prototype.interceptLogs = function(context) { Plugin.prototype.registerClientWeb3Provider = function(cb) { this.clientWeb3Providers.push(cb); this.pluginTypes.push('clientWeb3Provider'); -}; - -Plugin.prototype.registerBeforeDeploy = function(cb) { - this.beforeDeploy.push(cb); - this.pluginTypes.push('beforeDeploy'); + this.pluginTypes = _.uniq(this.pluginTypes); }; Plugin.prototype.registerContractsGeneration = function(cb) { this.contractsGenerators.push(cb); this.pluginTypes.push('contractGeneration'); + this.pluginTypes = _.uniq(this.pluginTypes); }; Plugin.prototype.registerPipeline = function(matcthingFiles, cb) { // TODO: generate error for more than one pipeline per plugin this.pipeline.push({matcthingFiles: matcthingFiles, cb: cb}); this.pluginTypes.push('pipeline'); + this.pluginTypes = _.uniq(this.pluginTypes); }; Plugin.prototype.addFileToPipeline = function(file, intendedPath, options) { this.pipelineFiles.push({file: file, intendedPath: intendedPath, options: options}); this.pluginTypes.push('pipelineFiles'); + this.pluginTypes = _.uniq(this.pluginTypes); }; Plugin.prototype.addContractFile = function(file) { this.contractsFiles.push(file); this.pluginTypes.push('contractFiles'); + this.pluginTypes = _.uniq(this.pluginTypes); }; Plugin.prototype.registerConsoleCommand = function(cb) { this.console.push(cb); this.pluginTypes.push('console'); + this.pluginTypes = _.uniq(this.pluginTypes); }; // TODO: this only works for services done on startup Plugin.prototype.registerServiceCheck = function(checkName, checkFn, time) { this.serviceChecks.push({checkName: checkName, checkFn: checkFn, time: time}); this.pluginTypes.push('serviceChecks'); + this.pluginTypes = _.uniq(this.pluginTypes); }; Plugin.prototype.has = function(pluginType) { @@ -177,42 +181,47 @@ Plugin.prototype.generateContracts = function(args) { Plugin.prototype.registerContractConfiguration = function(config) { this.contractsConfigs.push(config); this.pluginTypes.push('contractsConfig'); + this.pluginTypes = _.uniq(this.pluginTypes); }; Plugin.prototype.registerCompiler = function(extension, cb) { this.compilers.push({extension: extension, cb: cb}); this.pluginTypes.push('compilers'); + this.pluginTypes = _.uniq(this.pluginTypes); }; Plugin.prototype.registerUploadCommand = function(cmd, cb) { this.uploadCmds.push({cmd: cmd, cb: cb}); this.pluginTypes.push('uploadCmds'); + this.pluginTypes = _.uniq(this.pluginTypes); }; Plugin.prototype.addCodeToEmbarkJS = function(code) { this.embarkjs_code.push(code); this.pluginTypes.push('embarkjsCode'); + this.pluginTypes = _.uniq(this.pluginTypes); }; Plugin.prototype.addProviderInit = function(providerType, code, initCondition) { this.embarkjs_init_code[providerType] = this.embarkjs_init_code[providerType] || []; this.embarkjs_init_code[providerType].push([code, initCondition]); this.pluginTypes.push('initCode'); + this.pluginTypes = _.uniq(this.pluginTypes); }; Plugin.prototype.registerImportFile = function(importName, importLocation) { this.imports.push([importName, importLocation]); this.pluginTypes.push('imports'); + this.pluginTypes = _.uniq(this.pluginTypes); }; -Plugin.prototype.registerAfterAllContractsDeploy = function(cb) { - this.afterContractsDeployActions.push(cb); - this.pluginTypes.push('afterContractsDeployActions'); -}; - -Plugin.prototype.registerOnDeployContracts = function(cb) { - this.onDeployActions.push(cb); - this.pluginTypes.push('onDeployActions'); +Plugin.prototype.registerActionForEvent = function(eventName, cb) { + if (!this.eventActions[eventName]) { + this.eventActions[eventName] = []; + } + this.eventActions[eventName].push(cb); + this.pluginTypes.push('eventActions'); + this.pluginTypes = _.uniq(this.pluginTypes); }; Plugin.prototype.runFilePipeline = function() { diff --git a/lib/core/plugins.js b/lib/core/plugins.js index 78a21975..096ce43f 100644 --- a/lib/core/plugins.js +++ b/lib/core/plugins.js @@ -1,3 +1,4 @@ +const async = require('async'); var Plugin = require('./plugin.js'); var utils = require('../utils/utils.js'); @@ -58,7 +59,7 @@ Plugins.prototype.loadInternalPlugin = function(pluginName, pluginConfig) { var pluginWrapper = new Plugin({ name: pluginName, pluginModule: plugin, - pluginConfig: pluginConfig, + pluginConfig: pluginConfig || {}, logger: this.logger, pluginPath: pluginPath, interceptLogs: this.interceptLogs, @@ -98,18 +99,48 @@ Plugins.prototype.getPluginsFor = function(pluginType) { }); }; -Plugins.prototype.getPluginsProperty = function(pluginType, property) { +Plugins.prototype.getPluginsProperty = function(pluginType, property, sub_property) { let matchingPlugins = this.plugins.filter(function(plugin) { return plugin.has(pluginType); }); let matchingProperties = matchingPlugins.map((plugin) => { + if (sub_property) { + return plugin[property][sub_property]; + } return plugin[property]; }); + matchingProperties = matchingProperties.filter((property) => property); + //return flattened list if (matchingProperties.length === 0) return []; - return matchingProperties.reduce((a,b) => { return a.concat(b); }); + return matchingProperties.reduce((a,b) => { return a.concat(b); }) || []; +}; + +Plugins.prototype.runActionsForEvent = function(eventName, args, cb) { + if (typeof (args) === 'function') { + cb = args; + } + let actionPlugins = this.getPluginsProperty('eventActions', 'eventActions', eventName); + + if (actionPlugins.length === 0) { + return cb(); + } + + async.eachLimit(actionPlugins, 1, function(plugin, nextEach) { + if (typeof (args) === 'function') { + plugin.call(plugin, nextEach); + } else { + //plugin.call(plugin, ...args, nextEach); + plugin.call(plugin, args, nextEach); + } + }, cb); +}; + +Plugins.prototype.emitAndRunActionsForEvent = function(eventName, args, cb) { + this.events.emit(eventName); + return this.runActionsForEvent(eventName, args, cb); }; module.exports = Plugins; diff --git a/lib/i18n/locales/en.json b/lib/i18n/locales/en.json index 2ec0df1b..4b25e6ba 100644 --- a/lib/i18n/locales/en.json +++ b/lib/i18n/locales/en.json @@ -143,4 +143,4 @@ "Starting ipfs process": "Starting ipfs process", "ipfs process started": "ipfs process started", "IPFS node detected": "IPFS node detected" -} \ No newline at end of file +} diff --git a/lib/index.js b/lib/index.js index 82802615..f8a0f645 100644 --- a/lib/index.js +++ b/lib/index.js @@ -167,7 +167,7 @@ class Embark { engine.events.on('check:backOnline:Ethereum', function () { engine.logger.info(__('Ethereum node detected') + '..'); engine.config.reloadConfig(); - engine.deployManager.deployContracts(function () { + engine.events.request('deploy:contracts', function() { engine.logger.info(__('Deployment Done')); }); }); @@ -191,6 +191,7 @@ class Embark { engine.logger.info(__("Ready").underline); engine.events.emit("status", __("Ready").green); }); + if (options.runWebserver) { engine.startService("webServer", { host: options.serverHost, @@ -255,7 +256,7 @@ class Embark { callback(); }, function deploy(callback) { - engine.deployManager.deployContracts(function (err) { + engine.events.request('deploy:contracts', function(err) { callback(err); }); } @@ -306,7 +307,7 @@ class Embark { engine.startService("deployment", {onlyCompile: true}); engine.startService("codeGenerator"); - engine.deployManager.deployContracts(function (err) { + engine.events.request('deploy:contracts', function(err) { callback(err); }); } @@ -418,7 +419,6 @@ class Embark { callback(); }, function deploy(callback) { - // 2. upload to storage (outputDone event triggered after webpack finished) engine.events.on('outputDone', function () { cmdPlugin.uploadCmds[0].cb() .then((success) => { @@ -427,8 +427,7 @@ class Embark { .catch(callback); }); - // 1. build the contracts and dapp webpack - engine.deployManager.deployContracts(function (err) { + engine.events.request('deploy:contracts', function(err) { engine.logger.info(__("finished deploying").underline); if(err){ callback(err); diff --git a/lib/modules/ens/index.js b/lib/modules/ens/index.js index e6ba7295..0b0c6656 100644 --- a/lib/modules/ens/index.js +++ b/lib/modules/ens/index.js @@ -2,11 +2,10 @@ const fs = require('../../core/fs.js'); const utils = require('../../utils/utils.js'); class ENS { - constructor(embark, options) { + constructor(embark, _options) { this.logger = embark.logger; this.events = embark.events; - this.web3 = options.web3; - this.namesConfig = options.namesConfig; + this.namesConfig = embark.config.namesystemConfig; this.embark = embark; this.addENSToEmbarkJS(); diff --git a/lib/modules/ipfs/index.js b/lib/modules/ipfs/index.js index 1b25411e..146a96ac 100644 --- a/lib/modules/ipfs/index.js +++ b/lib/modules/ipfs/index.js @@ -9,7 +9,7 @@ class IPFS { this.logger = embark.logger; this.events = embark.events; this.buildDir = options.buildDir; - this.storageConfig = options.storageConfig; + this.storageConfig = embark.config.storageConfig; this.host = options.host || this.storageConfig.host; this.port = options.port || this.storageConfig.port; this.addCheck = options.addCheck; diff --git a/lib/modules/profiler/index.js b/lib/modules/profiler/index.js index e78c2dcf..f86ad995 100644 --- a/lib/modules/profiler/index.js +++ b/lib/modules/profiler/index.js @@ -5,7 +5,6 @@ class Profiler { this.embark = embark; this.logger = embark.logger; this.events = embark.events; - this.plugins = embark.plugins; this.registerConsoleCommand(); } diff --git a/lib/modules/solidity/index.js b/lib/modules/solidity/index.js index b28fb039..c0d77188 100644 --- a/lib/modules/solidity/index.js +++ b/lib/modules/solidity/index.js @@ -3,10 +3,10 @@ let SolcW = require('./solcW.js'); class Solidity { - constructor(embark, options) { + constructor(embark, _options) { this.logger = embark.logger; this.events = embark.events; - this.contractDirectories = options.contractDirectories; + this.contractDirectories = embark.config.contractDirectories; this.solcAlreadyLoaded = false; this.solcW = null; diff --git a/lib/modules/specialconfigs/index.js b/lib/modules/specialconfigs/index.js index 94b8328e..25b299df 100644 --- a/lib/modules/specialconfigs/index.js +++ b/lib/modules/specialconfigs/index.js @@ -48,7 +48,7 @@ class SpecialConfigs { registerAfterDeployAction() { const self = this; - this.embark.registerAfterAllContractsDeploy((cb) => { + this.embark.registerActionForEvent("contracts:deploy:afterAll", (cb) => { let afterDeployCmds = self.contractsConfig.afterDeploy || []; async.mapLimit(afterDeployCmds, 1, (cmd, nextMapCb) => { @@ -79,7 +79,9 @@ class SpecialConfigs { registerOnDeployAction() { const self = this; - this.embark.registerOnDeployContracts((contract, cb) => { + this.embark.registerActionForEvent("deploy:contract:deployed", (params, cb) => { + let contract = params.contract; + if (!contract.onDeploy) { return cb(); } diff --git a/lib/modules/swarm/index.js b/lib/modules/swarm/index.js index a8901cc1..b6479b94 100644 --- a/lib/modules/swarm/index.js +++ b/lib/modules/swarm/index.js @@ -8,7 +8,7 @@ class Swarm { this.logger = embark.logger; this.events = embark.events; this.buildDir = options.buildDir; - this.storageConfig = options.storageConfig; + this.storageConfig = embark.config.storageConfig; this.host = options.host || this.storageConfig.host; this.port = options.port || this.storageConfig.port; this.addCheck = options.addCheck; diff --git a/lib/modules/vyper/index.js b/lib/modules/vyper/index.js index 10a603de..6e11d0b3 100644 --- a/lib/modules/vyper/index.js +++ b/lib/modules/vyper/index.js @@ -4,10 +4,10 @@ const path = require('path'); class Vyper { - constructor(embark, options) { + constructor(embark, _options) { this.logger = embark.logger; this.events = embark.events; - this.contractDirectories = options.contractDirectories; + this.contractDirectories = embark.config.contractDirectories; // FIXME: Use array of extensions instead of duplicatiing embark.registerCompiler(".py", this.compile_vyper.bind(this)); diff --git a/lib/modules/whisper/index.js b/lib/modules/whisper/index.js index 624fb63e..f2d961f1 100644 --- a/lib/modules/whisper/index.js +++ b/lib/modules/whisper/index.js @@ -6,7 +6,7 @@ class Whisper { constructor(embark, options) { this.logger = embark.logger; this.events = embark.events; - this.communicationConfig = options.communicationConfig; + this.communicationConfig = embark.config.communicationConfig; this.addCheck = options.addCheck; // TODO: this should not be needed and should be deducted from the config instead this.web3 = options.web3; diff --git a/lib/tests/test.js b/lib/tests/test.js index 956f4ac2..a17a5d03 100644 --- a/lib/tests/test.js +++ b/lib/tests/test.js @@ -88,7 +88,7 @@ Test.prototype.deployAll = function(contractsConfig, cb) { self.engine.contractsManager.gasLimit = 6000000; self.engine.deployManager.fatalErrors = true; self.engine.deployManager.deployOnlyOnConfig = true; - self.engine.deployManager.deployContracts(function(err, _result) { + self.engine.events.request('deploy:contracts', function(err) { if (err) { callback(err); } diff --git a/test/code_generator.js b/test/code_generator.js index c74149c2..b8d7c6dd 100644 --- a/test/code_generator.js +++ b/test/code_generator.js @@ -12,29 +12,31 @@ describe('embark.CodeGenerator', function() { this.timeout(0); describe('#generateContracts', function() { - let generator = new CodeGenerator({blockchainConfig: {}, contractsManager: { - contracts: { - SimpleStorage: { - abiDefinition: [{"constant":true,"inputs":[],"name":"storedData","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"initialValue","type":"uint256"}],"type":"constructor"}], - gasEstimates: 12000, - deployedAddress: "0x123", - code: '12345' - }, - Foo: { - abiDefinition: [{"constant":true,"inputs":[],"name":"storedData","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"initialValue","type":"uint256"}],"type":"constructor"}], - gasEstimates: 12000, - deployedAddress: "0x124", - code: '123456' - } + let contracts = [ + { + className: 'SimpleStorage', + abiDefinition: [{"constant":true,"inputs":[],"name":"storedData","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"initialValue","type":"uint256"}],"type":"constructor"}], + gasEstimates: 12000, + deployedAddress: "0x123", + code: '12345' + }, + { + className: 'Foo', + abiDefinition: [{"constant":true,"inputs":[],"name":"storedData","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"initialValue","type":"uint256"}],"type":"constructor"}], + gasEstimates: 12000, + deployedAddress: "0x124", + code: '123456' } - }}); + ] + + let generator = new CodeGenerator({blockchainConfig: {}}); describe('with EmbarkJS', function() { let withEmbarkJS = true; it('should generate contract code', function() { var contractCode = "\n__mainContext.__loadManagerInstance.execWhenReady(function() {\n __mainContext.SimpleStorage = new EmbarkJS.Contract({abi: [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}], address: '0x123', code: '12345', gasEstimates: 12000});\n\n});\n__mainContext.__loadManagerInstance.execWhenReady(function() {\n __mainContext.Foo = new EmbarkJS.Contract({abi: [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}], address: '0x124', code: '123456', gasEstimates: 12000});\n\n});\n"; - assert.strictEqual(replaceCRLF(generator.generateContracts(withEmbarkJS)), contractCode); + assert.strictEqual(replaceCRLF(generator.generateContracts(contracts, withEmbarkJS)), contractCode); }); }); @@ -43,7 +45,7 @@ describe('embark.CodeGenerator', function() { it('should generate contract code', function() { var contractCode = "\n__mainContext.__loadManagerInstance.execWhenReady(function() {\n SimpleStorageAbi = [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}];\nSimpleStorage = new web3.eth.Contract(SimpleStorageAbi);\nSimpleStorage.options.address = '0x123';\nSimpleStorage.address = '0x123';\nSimpleStorage.options.from = web3.eth.defaultAccount;\n\n\n});\n__mainContext.__loadManagerInstance.execWhenReady(function() {\n FooAbi = [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}];\nFoo = new web3.eth.Contract(FooAbi);\nFoo.options.address = '0x124';\nFoo.address = '0x124';\nFoo.options.from = web3.eth.defaultAccount;\n\n\n});\n"; - assert.strictEqual(replaceCRLF(generator.generateContracts(withEmbarkJS)), contractCode); + assert.strictEqual(replaceCRLF(generator.generateContracts(contracts, withEmbarkJS)), contractCode); }); }); diff --git a/test/compiler.js b/test/compiler.js index 1fffdc55..e79fcd52 100644 --- a/test/compiler.js +++ b/test/compiler.js @@ -19,20 +19,22 @@ var TestEvents = { var apiObject = { registerCompiler: function() {}, logger: new TestLogger({}), - events: TestEvents + events: TestEvents, + config: { + contractDirectories: ['app/contracts/'] + } }; describe('embark.Compiler', function() { //let compiler = new Compiler({logger: new TestLogger({})}); - let compiler = new SolidityCompiler(apiObject, {contractDirectories: ['app/contracts/']}); + let compiler = new SolidityCompiler(apiObject); describe('#compile_solidity', function() { this.timeout(0); let expectedObject = {}; - //expectedObject["SimpleStorage"] = {"code":"608060405234801561001057600080fd5b50604051602080610114833981016040525160005560e1806100336000396000f30060806040526004361060525763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416632a1afcd98114605757806360fe47b114607b5780636d4ce63c146092575b600080fd5b348015606257600080fd5b50606960a4565b60408051918252519081900360200190f35b348015608657600080fd5b50609060043560aa565b005b348015609d57600080fd5b50606960af565b60005481565b600055565b600054905600a165627a7a72305820194e9895d54b0b5d8b469e794926ef9115b42631ab9a06ce726f9fea067633850029","runtimeBytecode":"60806040526004361060525763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416632a1afcd98114605757806360fe47b114607b5780636d4ce63c146092575b600080fd5b348015606257600080fd5b50606960a4565b60408051918252519081900360200190f35b348015608657600080fd5b50609060043560aa565b005b348015609d57600080fd5b50606960af565b60005481565b600055565b600054905600a165627a7a72305820194e9895d54b0b5d8b469e794926ef9115b42631ab9a06ce726f9fea067633850029","realRuntimeBytecode":"60806040526004361060525763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416632a1afcd98114605757806360fe47b114607b5780636d4ce63c146092575b600080fd5b348015606257600080fd5b50606960a4565b60408051918252519081900360200190f35b348015608657600080fd5b50609060043560aa565b005b348015609d57600080fd5b50606960af565b60005481565b600055565b600054905600a165627a7a72305820","swarmHash":"194e9895d54b0b5d8b469e794926ef9115b42631ab9a06ce726f9fea06763385","gasEstimates":{"creation":{"codeDepositCost":"45000","executionCost":"20141","totalCost":"65141"},"external":{"get()":"428","set(uint256)":"20161","storedData()":"384"}},"functionHashes":{"get()":"6d4ce63c","set(uint256)":"60fe47b1","storedData()":"2a1afcd9"},"abiDefinition":[{"constant":true,"inputs":[],"name":"storedData","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"initialValue","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}],"filename":"test/contracts/simple_storage.sol"}; - expectedObject["SimpleStorage"] = expectedObject["SimpleStorage"] = {"code":"608060405234801561001057600080fd5b50604051602080610114833981016040525160005560e1806100336000396000f30060806040526004361060525763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416632a1afcd98114605757806360fe47b114607b5780636d4ce63c146092575b600080fd5b348015606257600080fd5b50606960a4565b60408051918252519081900360200190f35b348015608657600080fd5b50609060043560aa565b005b348015609d57600080fd5b50606960af565b60005481565b600055565b600054905600a165627a7a72305820194e9895d54b0b5d8b469e794926ef9115b42631ab9a06ce726f9fea067633850029","runtimeBytecode":"60806040526004361060525763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416632a1afcd98114605757806360fe47b114607b5780636d4ce63c146092575b600080fd5b348015606257600080fd5b50606960a4565b60408051918252519081900360200190f35b348015608657600080fd5b50609060043560aa565b005b348015609d57600080fd5b50606960af565b60005481565b600055565b600054905600a165627a7a72305820194e9895d54b0b5d8b469e794926ef9115b42631ab9a06ce726f9fea067633850029","realRuntimeBytecode":"60806040526004361060525763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416632a1afcd98114605757806360fe47b114607b5780636d4ce63c146092575b600080fd5b348015606257600080fd5b50606960a4565b60408051918252519081900360200190f35b348015608657600080fd5b50609060043560aa565b005b348015609d57600080fd5b50606960af565b60005481565b600055565b600054905600a165627a7a72305820","swarmHash":"194e9895d54b0b5d8b469e794926ef9115b42631ab9a06ce726f9fea06763385","gasEstimates":{"creation":{"codeDepositCost":"45000","executionCost":"20141","totalCost":"65141"},"external":{"get()":"428","set(uint256)":"20161","storedData()":"384"}},"functionHashes":{"get()":"6d4ce63c","set(uint256)":"60fe47b1","storedData()":"2a1afcd9"},"abiDefinition":[{"constant":true,"inputs":[],"name":"storedData","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"initialValue","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}],"filename":"test/contracts/simple_storage.sol"}; + expectedObject["SimpleStorage"] = {"code":"608060405234801561001057600080fd5b50604051602080610114833981016040525160005560e1806100336000396000f30060806040526004361060525763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416632a1afcd98114605757806360fe47b114607b5780636d4ce63c146092575b600080fd5b348015606257600080fd5b50606960a4565b60408051918252519081900360200190f35b348015608657600080fd5b50609060043560aa565b005b348015609d57600080fd5b50606960af565b60005481565b600055565b600054905600a165627a7a72305820194e9895d54b0b5d8b469e794926ef9115b42631ab9a06ce726f9fea067633850029","runtimeBytecode":"60806040526004361060525763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416632a1afcd98114605757806360fe47b114607b5780636d4ce63c146092575b600080fd5b348015606257600080fd5b50606960a4565b60408051918252519081900360200190f35b348015608657600080fd5b50609060043560aa565b005b348015609d57600080fd5b50606960af565b60005481565b600055565b600054905600a165627a7a72305820194e9895d54b0b5d8b469e794926ef9115b42631ab9a06ce726f9fea067633850029","realRuntimeBytecode":"60806040526004361060525763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416632a1afcd98114605757806360fe47b114607b5780636d4ce63c146092575b600080fd5b348015606257600080fd5b50606960a4565b60408051918252519081900360200190f35b348015608657600080fd5b50609060043560aa565b005b348015609d57600080fd5b50606960af565b60005481565b600055565b600054905600a165627a7a72305820","swarmHash":"194e9895d54b0b5d8b469e794926ef9115b42631ab9a06ce726f9fea06763385","gasEstimates":{"creation":{"codeDepositCost":"45000","executionCost":"20141","totalCost":"65141"},"external":{"get()":"428","set(uint256)":"20161","storedData()":"384"}},"functionHashes":{"get()":"6d4ce63c","set(uint256)":"60fe47b1","storedData()":"2a1afcd9"},"abiDefinition":[{"constant":true,"inputs":[],"name":"storedData","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"initialValue","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}],"filename":"test/contracts/simple_storage.sol"}; expectedObject["Token"] = {"code":"608060405234801561001057600080fd5b5060405160208061047e8339810160409081529051600160a060020a033316600090815260208190529190912081905560025561042c806100526000396000f3006080604052600436106100775763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663095ea7b3811461007c57806318160ddd146100b457806323b872dd146100db57806370a0823114610105578063a9059cbb14610126578063dd62ed3e1461014a575b600080fd5b34801561008857600080fd5b506100a0600160a060020a0360043516602435610171565b604080519115158252519081900360200190f35b3480156100c057600080fd5b506100c96101db565b60408051918252519081900360200190f35b3480156100e757600080fd5b506100a0600160a060020a03600435811690602435166044356101e1565b34801561011157600080fd5b506100c9600160a060020a03600435166102f2565b34801561013257600080fd5b506100a0600160a060020a036004351660243561030d565b34801561015657600080fd5b506100c9600160a060020a03600435811690602435166103ce565b600160a060020a03338116600081815260016020908152604080832094871680845294825280832086905580518681529051929493927f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925929181900390910190a350600192915050565b60025490565b600160a060020a03831660009081526020819052604081205482111561020657600080fd5b600160a060020a038085166000908152600160209081526040808320339094168352929052205482111561023957600080fd5b600160a060020a03831660009081526020819052604090205461025c90836103f9565b151561026757600080fd5b600160a060020a038085166000818152600160209081526040808320338616845282528083208054889003905583835282825280832080548890039055938716808352918490208054870190558351868152935191937fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929081900390910190a35060019392505050565b600160a060020a031660009081526020819052604090205490565b600160a060020a03331660009081526020819052604081205482111561033257600080fd5b600160a060020a03831660009081526020819052604090205461035590836103f9565b151561036057600080fd5b600160a060020a0333811660008181526020818152604080832080548890039055938716808352918490208054870190558351868152935191937fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929081900390910190a350600192915050565b600160a060020a03918216600090815260016020908152604080832093909416825291909152205490565b81011015905600a165627a7a72305820c34209c42f57b08f62d29e65db6744b4c8991eee33c4c83e0d81674550945da80029","runtimeBytecode":"6080604052600436106100775763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663095ea7b3811461007c57806318160ddd146100b457806323b872dd146100db57806370a0823114610105578063a9059cbb14610126578063dd62ed3e1461014a575b600080fd5b34801561008857600080fd5b506100a0600160a060020a0360043516602435610171565b604080519115158252519081900360200190f35b3480156100c057600080fd5b506100c96101db565b60408051918252519081900360200190f35b3480156100e757600080fd5b506100a0600160a060020a03600435811690602435166044356101e1565b34801561011157600080fd5b506100c9600160a060020a03600435166102f2565b34801561013257600080fd5b506100a0600160a060020a036004351660243561030d565b34801561015657600080fd5b506100c9600160a060020a03600435811690602435166103ce565b600160a060020a03338116600081815260016020908152604080832094871680845294825280832086905580518681529051929493927f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925929181900390910190a350600192915050565b60025490565b600160a060020a03831660009081526020819052604081205482111561020657600080fd5b600160a060020a038085166000908152600160209081526040808320339094168352929052205482111561023957600080fd5b600160a060020a03831660009081526020819052604090205461025c90836103f9565b151561026757600080fd5b600160a060020a038085166000818152600160209081526040808320338616845282528083208054889003905583835282825280832080548890039055938716808352918490208054870190558351868152935191937fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929081900390910190a35060019392505050565b600160a060020a031660009081526020819052604090205490565b600160a060020a03331660009081526020819052604081205482111561033257600080fd5b600160a060020a03831660009081526020819052604090205461035590836103f9565b151561036057600080fd5b600160a060020a0333811660008181526020818152604080832080548890039055938716808352918490208054870190558351868152935191937fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929081900390910190a350600192915050565b600160a060020a03918216600090815260016020908152604080832093909416825291909152205490565b81011015905600a165627a7a72305820c34209c42f57b08f62d29e65db6744b4c8991eee33c4c83e0d81674550945da80029","realRuntimeBytecode":"6080604052600436106100775763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663095ea7b3811461007c57806318160ddd146100b457806323b872dd146100db57806370a0823114610105578063a9059cbb14610126578063dd62ed3e1461014a575b600080fd5b34801561008857600080fd5b506100a0600160a060020a0360043516602435610171565b604080519115158252519081900360200190f35b3480156100c057600080fd5b506100c96101db565b60408051918252519081900360200190f35b3480156100e757600080fd5b506100a0600160a060020a03600435811690602435166044356101e1565b34801561011157600080fd5b506100c9600160a060020a03600435166102f2565b34801561013257600080fd5b506100a0600160a060020a036004351660243561030d565b34801561015657600080fd5b506100c9600160a060020a03600435811690602435166103ce565b600160a060020a03338116600081815260016020908152604080832094871680845294825280832086905580518681529051929493927f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925929181900390910190a350600192915050565b60025490565b600160a060020a03831660009081526020819052604081205482111561020657600080fd5b600160a060020a038085166000908152600160209081526040808320339094168352929052205482111561023957600080fd5b600160a060020a03831660009081526020819052604090205461025c90836103f9565b151561026757600080fd5b600160a060020a038085166000818152600160209081526040808320338616845282528083208054889003905583835282825280832080548890039055938716808352918490208054870190558351868152935191937fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929081900390910190a35060019392505050565b600160a060020a031660009081526020819052604090205490565b600160a060020a03331660009081526020819052604081205482111561033257600080fd5b600160a060020a03831660009081526020819052604090205461035590836103f9565b151561036057600080fd5b600160a060020a0333811660008181526020818152604080832080548890039055938716808352918490208054870190558351868152935191937fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929081900390910190a350600192915050565b600160a060020a03918216600090815260016020908152604080832093909416825291909152205490565b81011015905600a165627a7a72305820","swarmHash":"c34209c42f57b08f62d29e65db6744b4c8991eee33c4c83e0d81674550945da8","gasEstimates":{"creation":{"codeDepositCost":"213600","executionCost":"40466","totalCost":"254066"},"external":{"allowance(address,address)":"818","approve(address,uint256)":"22344","balanceOf(address)":"675","totalSupply()":"406","transfer(address,uint256)":"43625","transferFrom(address,address,uint256)":"64402"},"internal":{"safeToAdd(uint256,uint256)":"24"}},"functionHashes":{"allowance(address,address)":"dd62ed3e","approve(address,uint256)":"095ea7b3","balanceOf(address)":"70a08231","totalSupply()":"18160ddd","transfer(address,uint256)":"a9059cbb","transferFrom(address,address,uint256)":"23b872dd"},"abiDefinition":[{"constant":false,"inputs":[{"name":"spender","type":"address"},{"name":"value","type":"uint256"}],"name":"approve","outputs":[{"name":"ok","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"supply","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"from","type":"address"},{"name":"to","type":"address"},{"name":"value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"ok","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"who","type":"address"}],"name":"balanceOf","outputs":[{"name":"value","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"value","type":"uint256"}],"name":"transfer","outputs":[{"name":"ok","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"owner","type":"address"},{"name":"spender","type":"address"}],"name":"allowance","outputs":[{"name":"_allowance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"initial_balance","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"}],"filename":"test/contracts/token.sol"}; diff --git a/test/contracts.js b/test/contracts.js index bb695dc7..b8452b80 100644 --- a/test/contracts.js +++ b/test/contracts.js @@ -26,9 +26,12 @@ describe('embark.Contracts', function() { describe('simple', function() { let plugins = new Plugins({ logger: new TestLogger({}), - events: TestEvents + events: TestEvents, + config: { + contractDirectories: ['app/contracts/'] + } }); - plugins.loadInternalPlugin('solidity', {solcVersion: '0.4.17', contractDirectories: ['app/contracts/']}); + plugins.loadInternalPlugin('solidity'); let compiler = new Compiler({plugins: plugins, logger: plugins.logger}); let events = new Events(); @@ -115,9 +118,12 @@ describe('embark.Contracts', function() { describe('config with contract instances', function() { let plugins = new Plugins({ logger: new TestLogger({}), - events: TestEvents + events: TestEvents, + config: { + contractDirectories: ['app/contracts/'] + } }); - plugins.loadInternalPlugin('solidity', {solcVersion: '0.4.17', contractDirectories: ['app/contracts/']}); + plugins.loadInternalPlugin('solidity'); let compiler = new Compiler({plugins: plugins, logger: plugins.logger}); let events = new Events(); diff --git a/test_apps/test_app/extensions/embark-service/index.js b/test_apps/test_app/extensions/embark-service/index.js index 6783352a..7717c760 100644 --- a/test_apps/test_app/extensions/embark-service/index.js +++ b/test_apps/test_app/extensions/embark-service/index.js @@ -23,9 +23,12 @@ module.exports = function (embark) { embark.addFileToPipeline('./fileInPipeline.js'); embark.addFileToPipeline('./fileInPipeline.js', 'js/fileInPipeline.js'); - embark.registerBeforeDeploy(function (options, callback) { - // Just calling register to prove it works. We don't actually want to change the contracts - callback({contractCode: options.contract.code}); + embark.registerActionForEvent("deploy:contract:beforeDeploy", (params, cb) => { + embark.logger.info("applying beforeDeploy plugin..."); + //console.dir(params); + //console.dir(cb); + //console.dir('------------------'); + cb(); }); // NOTE: uncommenting this will make dappConnection stop working diff --git a/test_apps/test_app/package-lock.json b/test_apps/test_app/package-lock.json index 6a0c2c66..3dba6607 100644 --- a/test_apps/test_app/package-lock.json +++ b/test_apps/test_app/package-lock.json @@ -89,7 +89,7 @@ "dom-helpers": { "version": "3.3.1", "resolved": "https://registry.npmjs.org/dom-helpers/-/dom-helpers-3.3.1.tgz", - "integrity": "sha1-/BpOFf/fYN3eA6SAqcD+zoId1KY=" + "integrity": "sha512-2Sm+JaYn74OiTM2wHvxJOo3roiq/h25Yi69Fqk269cNUwIXsCvATB6CRSFC9Am/20G2b28hGv/+7NiWydIrPvg==" }, "dotenv": { "version": "4.0.0", @@ -229,7 +229,7 @@ "node-fetch": { "version": "1.7.3", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-1.7.3.tgz", - "integrity": "sha1-mA9vcthSEaU0fGsrwYxbhMPrR+8=", + "integrity": "sha512-NhZ4CsKx7cYm2vSrBAr2PvFOe6sWDf0UYLRqA6svUYg7+/TSfVAu49jYC4BvQ4Sms9SZgdqGBgroqfDhJdTyKQ==", "requires": { "encoding": "0.1.12", "is-stream": "1.1.0" @@ -252,7 +252,7 @@ "promise": { "version": "7.3.1", "resolved": "https://registry.npmjs.org/promise/-/promise-7.3.1.tgz", - "integrity": "sha1-BktyYCsY+Q8pGSuLG8QY/9Hr078=", + "integrity": "sha512-nolQXZ/4L+bP/UGlkfaIujX9BKxGwmQ9OT4mOt5yvy8iK1h3wqTEJCijzGANTCCl9nWjY41juyAn2K3Q1hLLTg==", "requires": { "asap": "2.0.6" } @@ -289,7 +289,7 @@ "react-bootstrap": { "version": "0.32.1", "resolved": "https://registry.npmjs.org/react-bootstrap/-/react-bootstrap-0.32.1.tgz", - "integrity": "sha1-YGJMG0ijnXc+9szmQhpPM+zBZrs=", + "integrity": "sha512-RbfzKUbsukWsToWqGHfCCyMFq9QQI0TznutdyxyJw6dih2NvIne25Mrssg8LZsprqtPpyQi8bN0L0Fx3fUsL8Q==", "requires": { "babel-runtime": "6.26.0", "classnames": "2.2.5", @@ -319,7 +319,7 @@ "react-overlays": { "version": "0.8.3", "resolved": "https://registry.npmjs.org/react-overlays/-/react-overlays-0.8.3.tgz", - "integrity": "sha1-+tZe6lskMBzKGSoWn13dsLINOsU=", + "integrity": "sha512-h6GT3jgy90PgctleP39Yu3eK1v9vaJAW73GOA/UbN9dJ7aAN4BTZD6793eI1D5U+ukMk17qiqN/wl3diK1Z5LA==", "requires": { "classnames": "2.2.5", "dom-helpers": "3.3.1", @@ -353,7 +353,7 @@ "regenerator-runtime": { "version": "0.11.1", "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz", - "integrity": "sha1-vgWtf5v30i4Fb5cmzuUBf78Z4uk=" + "integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==" }, "setimmediate": { "version": "1.0.5", @@ -412,7 +412,7 @@ "zeppelin-solidity": { "version": "1.8.0", "resolved": "https://registry.npmjs.org/zeppelin-solidity/-/zeppelin-solidity-1.8.0.tgz", - "integrity": "sha1-BJ/N59rqn8hSEPjG25+M0auKhTo=", + "integrity": "sha512-7Mxq6Y7EES0PSLrRF6v0EVYqBVRRo8hFrr7m3jEs69VbbQ5kpANzizeEdbP1/PWKSOmBOg208qP2vSA0FlzFLA==", "requires": { "dotenv": "4.0.0", "ethjs-abi": "0.2.1"