Skip to content

Commit

Permalink
feat: call action before starting the blockchain node
Browse files Browse the repository at this point in the history
  • Loading branch information
jrainville committed Oct 4, 2019
1 parent 1bb25b9 commit c54b8d9
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 43 deletions.
1 change: 1 addition & 0 deletions dapps/templates/demo/config/contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
84 changes: 46 additions & 38 deletions packages/embark/src/lib/modules/blockchain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ 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;
this.blockchainConfig = embark.config.blockchainConfig;
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));

Expand All @@ -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]);

});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion packages/embark/src/lib/modules/geth/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions packages/embark/src/lib/modules/geth/gethClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion packages/embark/src/lib/modules/parity/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions packages/embark/src/lib/modules/parity/parityClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion packages/plugins/web3/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/stack/proxy/src/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c54b8d9

Please sign in to comment.