From 56d541fb8d2b3fcbd0706f968e64eee0986809c0 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Mon, 27 Aug 2018 16:22:53 -0400 Subject: [PATCH] fix most rebase issues --- lib/core/processes/processLauncher.js | 14 ++-- lib/modules/blockchain_connector/index.js | 84 +++++++---------------- lib/modules/contracts_manager/index.js | 9 ++- lib/modules/ens/embarkjs.js | 15 ++-- lib/modules/profiler/index.js | 40 ++++++++++- lib/modules/solidity/index.js | 66 +++--------------- lib/modules/webserver/index.js | 3 + lib/modules/webserver/server.js | 8 ++- 8 files changed, 103 insertions(+), 136 deletions(-) diff --git a/lib/core/processes/processLauncher.js b/lib/core/processes/processLauncher.js index 764943e61..6f4915143 100644 --- a/lib/core/processes/processLauncher.js +++ b/lib/core/processes/processLauncher.js @@ -47,8 +47,8 @@ class ProcessLauncher { _subscribeToMessages() { const self = this; this.process.on('message', (msg) => { - if (msg.error) { - self.logger.error(msg.error); + if (msg.error) { + self.logger.error(msg.error); } if (msg.result === constants.process.log) { return self._handleLog(msg); @@ -94,7 +94,13 @@ class ProcessLauncher { _handleLog(msg) { const timestamp = new Date().getTime(); this.events.emit('process-log-' + this.name, msg.type, msg.message, this.name, timestamp); - this.logs.push({msg: msg.message, msg_clear: msg.message.stripColors, logLevel: msg.logLevel, name: this.name, timestamp}); + this.logs.push({ + msg: msg.message, + msg_clear: msg.message.stripColors, + logLevel: msg.logLevel, + name: this.name, + timestamp + }); if (this.silent && msg.type !== 'error') { return; } @@ -204,7 +210,7 @@ class ProcessLauncher { * Unsubscribes from a previously subscribed key-value pair (or key if no value) * @param {String} key Message key to unsubscribe * @param {String} value [Optional] Value of the key to unsubscribe - * If there is no value, unsubscribes from all the values of that key + * If there is no value, unsubscribes from all the values of that key * @return {void} */ unsubscribeTo(key, value) { diff --git a/lib/modules/blockchain_connector/index.js b/lib/modules/blockchain_connector/index.js index cd91ca6d2..42a9387c3 100644 --- a/lib/modules/blockchain_connector/index.js +++ b/lib/modules/blockchain_connector/index.js @@ -205,7 +205,7 @@ class BlockchainConnector { const self = this; const NO_NODE = 'noNode'; - this.events.request("services:register", 'Ethereum', function (cb) { + this.events.request("services:register", 'Ethereum', function(cb) { async.waterfall([ function checkNodeConnection(next) { if (!self.provider || !self.provider.connected()) { @@ -324,7 +324,7 @@ class BlockchainConnector { '/embark-api/blockchain/blocks/:blockNumber', (req, res) => { self.getBlock(req.params.blockNumber, (err, block) => { - if(err){ + if (err) { self.logger.error(err); } res.send(block); @@ -347,7 +347,7 @@ class BlockchainConnector { '/embark-api/blockchain/transactions/:hash', (req, res) => { self.getTransaction(req.params.hash, (err, transaction) => { - if(err){ + if (err) { self.logger.error(err); } res.send(transaction); @@ -395,11 +395,11 @@ class BlockchainConnector { callback(null, account); }); } - ], function(_err, account){ + ], function(_err, account) { accounts.push(account); eachCb(); }); - }, function () { + }, function() { callback(accounts); }); }); @@ -411,7 +411,7 @@ class BlockchainConnector { function(next) { self.getAccountsWithTransactionCount((accounts) => { let account = accounts.find((a) => a.address === address); - if(!account) { + if (!account) { return next("No account found with this address"); } next(null, account); @@ -433,7 +433,7 @@ class BlockchainConnector { next(null, account); }); } - ], function (err, result) { + ], function(err, result) { if (err) { callback(); } @@ -469,7 +469,7 @@ class BlockchainConnector { function(next) { async.times(limit, function(n, eachCb) { self.web3.eth.getBlock(from - n, returnTransactionObjects, function(err, block) { - if (err){ + if (err) { self.logger.error(err); return eachCb(); } @@ -478,7 +478,7 @@ class BlockchainConnector { }); }, next); } - ], function () { + ], function() { callback(blocks); }); } @@ -557,60 +557,14 @@ class BlockchainConnector { } deployContractFromObject(deployContractObject, params, cb) { - const self = this; - let hash; - let calledBacked = false; - - function callback(err, receipt) { - if (calledBacked) { - return; - } - if (!err && !receipt.contractAddress) { - return; // Not deployed yet. Need to wait - } - if (interval) { - clearInterval(interval); - } - calledBacked = true; - cb(err, receipt); - } - - // This interval is there to compensate for the event that sometimes doesn't get triggered when using WebSocket - // FIXME The issue somehow only happens when the blockchain node is started in the same terminal - const interval = setInterval(() => { - if (!hash) { - return; // Wait until we receive the hash - } - self.web3.eth.getTransactionReceipt(hash, (err, receipt) => { - if (!err && !receipt) { - return; // Transaction is not yet complete - } - callback(err, receipt); - }); - }, 500); - - deployContractObject.send({ + embarkJsUtils.secureSend(this.web3, deployContractObject, { from: params.from, gas: params.gas, gasPrice: params.gasPrice - }, function (err, transactionHash) { - if (err) { - return callback(err); - } - hash = transactionHash; - }).on('receipt', function (receipt) { - if (receipt.contractAddress !== undefined) { - callback(null, receipt); - } - }).then(function (_contract) { - if (!hash) { - return; // Somehow we didn't get the receipt yet... Interval will catch it - } - self.web3.eth.getTransactionReceipt(hash, callback); - }).catch(callback); + }, true, cb); } determineDefaultAccount(cb) { const self = this; - self.getAccounts(function (err, accounts) { + self.getAccounts(function(err, accounts) { if (err) { self.logger.error(err); return cb(new Error(err)); @@ -628,6 +582,20 @@ class BlockchainConnector { // can just be a command without a callback this.events.emit("runcode:register", "web3", this.web3, false); } + + subscribeToPendingTransactions() { + const self = this; + this.onReady(() => { + if (self.logsSubscription) { + self.logsSubscription.unsubscribe(); + } + self.logsSubscription = self.web3.eth + .subscribe('newBlockHeaders', () => {}) + .on("data", function (blockHeader) { + self.events.emit('block:header', blockHeader); + }); + }); + } } BlockchainConnector.ACCEPTED_TYPES = ['rpc', 'ws', 'vm']; diff --git a/lib/modules/contracts_manager/index.js b/lib/modules/contracts_manager/index.js index 6b1b25ee3..de2f339df 100644 --- a/lib/modules/contracts_manager/index.js +++ b/lib/modules/contracts_manager/index.js @@ -82,8 +82,7 @@ class ContractsManager { cb(results); }); - let plugin = this.plugins.createPlugin('deployment', {}); - plugin.registerAPICall( + embark.registerAPICall( 'get', '/embark-api/contract/:contractName', (req, res) => { @@ -91,7 +90,7 @@ class ContractsManager { } ); - plugin.registerAPICall( + embark.registerAPICall( 'post', '/embark-api/contract/:contractName/function', (req, res) => { @@ -127,7 +126,7 @@ class ContractsManager { } ); - plugin.registerAPICall( + embark.registerAPICall( 'post', '/embark-api/contract/:contractName/deploy', (req, res) => { @@ -158,7 +157,7 @@ class ContractsManager { } ); - plugin.registerAPICall( + embark.registerAPICall( 'get', '/embark-api/contracts', (req, res) => { diff --git a/lib/modules/ens/embarkjs.js b/lib/modules/ens/embarkjs.js index 93cd2b9df..5bd0c454a 100644 --- a/lib/modules/ens/embarkjs.js +++ b/lib/modules/ens/embarkjs.js @@ -157,18 +157,15 @@ __embarkENS.setProvider = function (config) { const ERROR_MESSAGE = 'ENS is not available in this chain'; self.registration = config.registration; self.env = config.env; - EmbarkJS.onReady(() => { - self.registration = config.registration; - EmbarkJS.onReady(() => { web3.eth.net.getId() .then((id) => { - const registryAddress = self.registryAddresses[id] || config.registryAddress; - self._isAvailable = true; - self.ens = new EmbarkJS.Blockchain.Contract({abi: config.registryAbi, address: registryAddress, web3: web3}); - self.registrar = new EmbarkJS.Blockchain.Contract({abi: config.registrarAbi, address: config.registrarAddress, web3: web3}); - self.resolver = new EmbarkJS.Blockchain.Contract({abi: config.resolverAbi, address: config.resolverAddress, web3: web3}); - }) + const registryAddress = self.registryAddresses[id] || config.registryAddress; + self._isAvailable = true; + self.ens = new EmbarkJS.Blockchain.Contract({abi: config.registryAbi, address: registryAddress, web3: web3}); + self.registrar = new EmbarkJS.Blockchain.Contract({abi: config.registrarAbi, address: config.registrarAddress, web3: web3}); + self.resolver = new EmbarkJS.Blockchain.Contract({abi: config.resolverAbi, address: config.resolverAddress, web3: web3}); + }) .catch(err => { if (err.message.indexOf('Provider not set or invalid') > -1) { console.warn(ERROR_MESSAGE); diff --git a/lib/modules/profiler/index.js b/lib/modules/profiler/index.js index 5794f7794..5e35fb761 100644 --- a/lib/modules/profiler/index.js +++ b/lib/modules/profiler/index.js @@ -13,12 +13,46 @@ class Profiler { this.registerApi(); } - profile(contractName, contract, callback) { + profileJSON(contractName, returnCb) { + const self = this; + + let profileObj = {}; + profileObj.name = contractName; + profileObj.methods = []; + + self.events.request('contracts:contract', contractName, (contract) => { + if (!contract || !contract.deployedAddress) { + return returnCb("-- couldn't profile " + contractName + " - it's not deployed or could be an interface"); + } + self.gasEstimator.estimateGas(contractName, function(_err, gastimates, _name) { + contract.abiDefinition.forEach((abiMethod) => { + let methodName = abiMethod.name; + if (['constructor', 'fallback'].indexOf(abiMethod.type) >= 0) { + methodName = abiMethod.type; + } + + profileObj.methods.push({ + name: methodName, + payable: abiMethod.payable, + mutability: abiMethod.stateMutability, + inputs: abiMethod.inputs || [], + outputs: abiMethod.outputs || [], + gasEstimates: gastimates && gastimates[methodName] + }); + }); + + returnCb(null, profileObj); + }); + }); + } + + profile(contractName, returnCb) { const self = this; this.profileJSON(contractName, (err, profileObj) => { if (err) { - return callback(null, "error found in method: " + name + " error: " + JSON.stringify(err)); + self.logger.error(JSON.stringify(err)); + return returnCb(err); } let table = new asciiTable(contractName); @@ -26,7 +60,7 @@ class Profiler { profileObj.methods.forEach((method) => { table.addRow(method.name, method.payable, method.mutability, self.formatParams(method.inputs), self.formatParams(method.outputs), method.gasEstimates); }); - callback(null, table.toString()); + return returnCb(null, table.toString()); }); } diff --git a/lib/modules/solidity/index.js b/lib/modules/solidity/index.js index 1ff8829f1..9db2f85f7 100644 --- a/lib/modules/solidity/index.js +++ b/lib/modules/solidity/index.js @@ -30,23 +30,26 @@ class Solidity { } _compile(jsonObj, returnAllErrors, callback) { - this.solcW.compile(jsonObj, (err, output) => { - if (err) { + const self = this; + self.solcW.compile(jsonObj, function (err, output) { + self.events.emit('contracts:compile:solc', jsonObj); + + if(err){ return callback(err); } - if (output.errors && returnAllErrors) { - return callback(output.errors); - } + if (output.errors) { - for (let i = 0; i < output.errors.length; i++) { + for (let i=0; i this.logger.info(message)); } setServiceCheck() { diff --git a/lib/modules/webserver/server.js b/lib/modules/webserver/server.js index 94001961b..999c0b04d 100644 --- a/lib/modules/webserver/server.js +++ b/lib/modules/webserver/server.js @@ -23,6 +23,8 @@ class Server { this.opened = false; this.openBrowser = options.openBrowser; this.logging = false; + this.plugins = options.plugins; + this.enableCatchAll = options.enableCatchAll; this.events.once('outputDone', () => { this.logger.info(this._getMessage()); @@ -40,11 +42,12 @@ class Server { } start(callback) { + callback = callback || function() {}; const self = this; if (this.server && this.server.listening) { let message = __("a webserver is already running at") + " " + - ("http://" + canonicalHost(this.hostname) + - ":" + this.port).bold.underline.green; + ("http://" + canonicalHost(this.hostname) + + ":" + this.port).bold.underline.green; return callback(null, message); } @@ -159,6 +162,7 @@ class Server { } stop(callback) { + callback = callback || function () {}; if (!this.server || !this.server.listening) { return callback(null, __("no webserver is currently running")); }