From c54b8d9e44ecf8f111f0cf6e343236d7f3e10262 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Tue, 24 Sep 2019 13:35:19 -0400 Subject: [PATCH] feat: call action before starting the blockchain node --- dapps/templates/demo/config/contracts.js | 1 + .../src/lib/modules/blockchain/index.js | 84 ++++++++++--------- .../ethereum-blockchain-client/index.js | 3 +- .../embark/src/lib/modules/geth/blockchain.js | 3 +- .../embark/src/lib/modules/geth/gethClient.js | 10 +++ .../src/lib/modules/parity/blockchain.js | 3 +- .../src/lib/modules/parity/parityClient.js | 10 +++ packages/plugins/web3/src/index.js | 4 +- packages/stack/proxy/src/proxy.js | 2 +- 9 files changed, 77 insertions(+), 43 deletions(-) diff --git a/dapps/templates/demo/config/contracts.js b/dapps/templates/demo/config/contracts.js index 61cf86b23e..8924b4c90b 100644 --- a/dapps/templates/demo/config/contracts.js +++ b/dapps/templates/demo/config/contracts.js @@ -3,6 +3,7 @@ module.exports = { default: { // order of connections the dapp should connect to dappConnection: [ + "$EMBARK", "$WEB3", // uses pre existing web3 object if available (e.g in Mist) "ws://localhost:8546", "http://localhost:8545" diff --git a/packages/embark/src/lib/modules/blockchain/index.js b/packages/embark/src/lib/modules/blockchain/index.js index 79e25a601f..92fe5a891d 100644 --- a/packages/embark/src/lib/modules/blockchain/index.js +++ b/packages/embark/src/lib/modules/blockchain/index.js @@ -6,7 +6,7 @@ const Web3RequestManager = require('web3-core-requestmanager'); import BlockchainAPI from "./api"; class Blockchain { - constructor(embark) { + constructor(embark, options) { this.embarkConfig = embark.config.embarkConfig; this.logger = embark.logger; this.events = embark.events; @@ -14,6 +14,7 @@ class Blockchain { this.contractConfig = embark.config.contractConfig; this.blockchainApi = new BlockchainAPI(embark); this.startedClient = null; + this.plugins = options.plugins; embark.registerActionForEvent("pipeline:generateAll:before", this.addArtifactFile.bind(this)); @@ -22,47 +23,54 @@ class Blockchain { this.blockchainNodes[clientName] = startCb; }); - this.events.setCommandHandler("blockchain:node:start", async (blockchainConfig, cb) => { - const self = this; - const clientName = blockchainConfig.client; - function started() { - self.startedClient = clientName; - self.events.emit("blockchain:started", clientName); - } - if (clientName === constants.blockchain.vm) { - started(); - return cb(); - } - const requestManager = new Web3RequestManager.Manager(blockchainConfig.endpoint); - - const ogConsoleError = console.error; - // TODO remove this once we update to web3 2.0 - // TODO in web3 1.0, it console.errors "connection not open on send()" even if we catch the error - console.error = (...args) => { - if (args[0].indexOf('connection not open on send()') > -1) { - return; - } - ogConsoleError(...args); - }; - requestManager.send({method: 'eth_accounts'}, (err, _accounts) => { - console.error = ogConsoleError; - if (!err) { - // Node is already started - started(); - return cb(null, true); + this.events.setCommandHandler("blockchain:node:start", async (initialBlockchainConfig, cb) => { + this.plugins.emitAndRunActionsForEvent("blockchain:config:modify", initialBlockchainConfig, (err, blockchainConfig) => { + if (err) { + this.logger.error(__('Error getting modified blockchain config: %s', err.message || err)); + blockchainConfig = initialBlockchainConfig; } - const clientFunctions = this.blockchainNodes[clientName]; - if (!clientFunctions) { - return cb(__("Client %s not found", clientName)); + const self = this; + const clientName = blockchainConfig.client; + function started() { + self.startedClient = clientName; + self.events.emit("blockchain:started", clientName); } - - let onStart = () => { + if (clientName === constants.blockchain.vm) { started(); - cb(); + return cb(); + } + const requestManager = new Web3RequestManager.Manager(blockchainConfig.endpoint); + + const ogConsoleError = console.error; + // TODO remove this once we update to web3 2.0 + // TODO in web3 1.0, it console.errors "connection not open on send()" even if we catch the error + console.error = (...args) => { + if (args[0].indexOf('connection not open on send()') > -1) { + return; + } + ogConsoleError(...args); }; - - this.startedClient = clientName; - clientFunctions.launchFn.apply(clientFunctions, [onStart]); + requestManager.send({method: 'eth_accounts'}, (err, _accounts) => { + console.error = ogConsoleError; + if (!err) { + // Node is already started + started(); + return cb(null, true); + } + const clientFunctions = this.blockchainNodes[clientName]; + if (!clientFunctions) { + return cb(__("Client %s not found", clientName)); + } + + let onStart = () => { + started(); + cb(); + }; + + this.startedClient = clientName; + clientFunctions.launchFn.apply(clientFunctions, [onStart]); + + }); }); }); diff --git a/packages/embark/src/lib/modules/ethereum-blockchain-client/index.js b/packages/embark/src/lib/modules/ethereum-blockchain-client/index.js index 9fddd85b43..d2ffe3210e 100644 --- a/packages/embark/src/lib/modules/ethereum-blockchain-client/index.js +++ b/packages/embark/src/lib/modules/ethereum-blockchain-client/index.js @@ -60,7 +60,8 @@ class EthereumBlockchainClient { const web3 = await this.web3; const [account] = await web3.eth.getAccounts(); const contractObj = new web3.eth.Contract(contract.abiDefinition, contract.address); - const contractObject = contractObj.deploy({arguments: (contract.args || []), data: ("0x" + contract.code)}); + const code = contract.code.substring(0, 2) === '0x' ? contract.code : "0x" + contract.code; + const contractObject = contractObj.deploy({arguments: (contract.args || []), data: code}); if (contract.gas === 'auto' || !contract.gas) { const gasValue = await contractObject.estimateGas(); diff --git a/packages/embark/src/lib/modules/geth/blockchain.js b/packages/embark/src/lib/modules/geth/blockchain.js index 9d6bd3cd10..dff5852cdc 100644 --- a/packages/embark/src/lib/modules/geth/blockchain.js +++ b/packages/embark/src/lib/modules/geth/blockchain.js @@ -59,7 +59,8 @@ var Blockchain = function(userConfig, clientClass, communicationConfig) { vmdebug: this.userConfig.vmdebug || false, targetGasLimit: this.userConfig.targetGasLimit || false, syncMode: this.userConfig.syncMode || this.userConfig.syncmode, - verbosity: this.userConfig.verbosity + verbosity: this.userConfig.verbosity, + customOptions: this.userConfig.customOptions }; this.devFunds = null; diff --git a/packages/embark/src/lib/modules/geth/gethClient.js b/packages/embark/src/lib/modules/geth/gethClient.js index 935b5b2d97..8e5288171a 100644 --- a/packages/embark/src/lib/modules/geth/gethClient.js +++ b/packages/embark/src/lib/modules/geth/gethClient.js @@ -381,6 +381,16 @@ class GethClient { return callback(null, '--dev'); } callback(null, ''); + }, + function customOptions(callback) { + if (config.customOptions) { + if (Array.isArray(config.customOptions)) { + config.customOptions = config.customOptions.join(' '); + } + args.push(config.customOptions); + return callback(null, config.customOptions); + } + callback(null, ''); } ], function(err) { if (err) { diff --git a/packages/embark/src/lib/modules/parity/blockchain.js b/packages/embark/src/lib/modules/parity/blockchain.js index 6158c10de1..840bd9c3e8 100644 --- a/packages/embark/src/lib/modules/parity/blockchain.js +++ b/packages/embark/src/lib/modules/parity/blockchain.js @@ -61,7 +61,8 @@ var Blockchain = function (userConfig, clientClass) { targetGasLimit: this.userConfig.targetGasLimit || false, syncMode: this.userConfig.syncMode || this.userConfig.syncmode, verbosity: this.userConfig.verbosity, - proxy: this.userConfig.proxy + proxy: this.userConfig.proxy, + customOptions: this.userConfig.customOptions }; this.devFunds = null; diff --git a/packages/embark/src/lib/modules/parity/parityClient.js b/packages/embark/src/lib/modules/parity/parityClient.js index 2398a59144..a391fa8038 100644 --- a/packages/embark/src/lib/modules/parity/parityClient.js +++ b/packages/embark/src/lib/modules/parity/parityClient.js @@ -394,6 +394,16 @@ class ParityClient { // Default Parity gas limit is 4700000: let's set to the geth default args.push("--gas-floor-target=" + DEFAULTS.TARGET_GAS_LIMIT); return callback(null, "--gas-floor-target=" + DEFAULTS.TARGET_GAS_LIMIT); + }, + function customOptions(callback) { + if (config.customOptions) { + if (Array.isArray(config.customOptions)) { + config.customOptions = config.customOptions.join(' '); + } + args.push(config.customOptions); + return callback(null, config.customOptions); + } + callback(null, ''); } ], function (err) { if (err) { diff --git a/packages/plugins/web3/src/index.js b/packages/plugins/web3/src/index.js index 9677c9066c..95d68291f8 100644 --- a/packages/plugins/web3/src/index.js +++ b/packages/plugins/web3/src/index.js @@ -44,7 +44,9 @@ class EmbarkWeb3 { const web3 = new Web3(provider); await this.events.request2("runcode:register", 'web3', web3); const accounts = await web3.eth.getAccounts(); - await this.events.request2('runcode:eval', `web3.eth.defaultAccount = '${accounts[0]}'`); + if (accounts.length) { + await this.events.request2('runcode:eval', `web3.eth.defaultAccount = '${accounts[0]}'`); + } await this.events.request2('console:register:helpCmd', { cmdName: "web3", diff --git a/packages/stack/proxy/src/proxy.js b/packages/stack/proxy/src/proxy.js index 91ebda9240..0c347d6e58 100644 --- a/packages/stack/proxy/src/proxy.js +++ b/packages/stack/proxy/src/proxy.js @@ -75,7 +75,7 @@ export class Proxy { // Send the possibly modified request to the Node requestManager.send(resp.reqData, (err, result) => { if (err) { - res.status(500).send(err.message || err); + return res.status(500).send(err.message || err); } this.emitActionsForResponse(resp.reqData, {jsonrpc: "2.0", id: resp.reqData.id, result}, (_err, resp) => { // Send back to the caller (web3)