mirror of https://github.com/embarklabs/embark.git
feat: call action before starting the blockchain node
This commit is contained in:
parent
1bb25b9082
commit
c54b8d9e44
|
@ -3,6 +3,7 @@ module.exports = {
|
||||||
default: {
|
default: {
|
||||||
// order of connections the dapp should connect to
|
// order of connections the dapp should connect to
|
||||||
dappConnection: [
|
dappConnection: [
|
||||||
|
"$EMBARK",
|
||||||
"$WEB3", // uses pre existing web3 object if available (e.g in Mist)
|
"$WEB3", // uses pre existing web3 object if available (e.g in Mist)
|
||||||
"ws://localhost:8546",
|
"ws://localhost:8546",
|
||||||
"http://localhost:8545"
|
"http://localhost:8545"
|
||||||
|
|
|
@ -6,7 +6,7 @@ const Web3RequestManager = require('web3-core-requestmanager');
|
||||||
import BlockchainAPI from "./api";
|
import BlockchainAPI from "./api";
|
||||||
class Blockchain {
|
class Blockchain {
|
||||||
|
|
||||||
constructor(embark) {
|
constructor(embark, options) {
|
||||||
this.embarkConfig = embark.config.embarkConfig;
|
this.embarkConfig = embark.config.embarkConfig;
|
||||||
this.logger = embark.logger;
|
this.logger = embark.logger;
|
||||||
this.events = embark.events;
|
this.events = embark.events;
|
||||||
|
@ -14,6 +14,7 @@ class Blockchain {
|
||||||
this.contractConfig = embark.config.contractConfig;
|
this.contractConfig = embark.config.contractConfig;
|
||||||
this.blockchainApi = new BlockchainAPI(embark);
|
this.blockchainApi = new BlockchainAPI(embark);
|
||||||
this.startedClient = null;
|
this.startedClient = null;
|
||||||
|
this.plugins = options.plugins;
|
||||||
|
|
||||||
embark.registerActionForEvent("pipeline:generateAll:before", this.addArtifactFile.bind(this));
|
embark.registerActionForEvent("pipeline:generateAll:before", this.addArtifactFile.bind(this));
|
||||||
|
|
||||||
|
@ -22,47 +23,54 @@ class Blockchain {
|
||||||
this.blockchainNodes[clientName] = startCb;
|
this.blockchainNodes[clientName] = startCb;
|
||||||
});
|
});
|
||||||
|
|
||||||
this.events.setCommandHandler("blockchain:node:start", async (blockchainConfig, cb) => {
|
this.events.setCommandHandler("blockchain:node:start", async (initialBlockchainConfig, cb) => {
|
||||||
const self = this;
|
this.plugins.emitAndRunActionsForEvent("blockchain:config:modify", initialBlockchainConfig, (err, blockchainConfig) => {
|
||||||
const clientName = blockchainConfig.client;
|
if (err) {
|
||||||
function started() {
|
this.logger.error(__('Error getting modified blockchain config: %s', err.message || err));
|
||||||
self.startedClient = clientName;
|
blockchainConfig = initialBlockchainConfig;
|
||||||
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);
|
const self = this;
|
||||||
};
|
const clientName = blockchainConfig.client;
|
||||||
requestManager.send({method: 'eth_accounts'}, (err, _accounts) => {
|
function started() {
|
||||||
console.error = ogConsoleError;
|
self.startedClient = clientName;
|
||||||
if (!err) {
|
self.events.emit("blockchain:started", clientName);
|
||||||
// Node is already started
|
}
|
||||||
|
if (clientName === constants.blockchain.vm) {
|
||||||
started();
|
started();
|
||||||
return cb(null, true);
|
return cb();
|
||||||
}
|
|
||||||
const clientFunctions = this.blockchainNodes[clientName];
|
|
||||||
if (!clientFunctions) {
|
|
||||||
return cb(__("Client %s not found", clientName));
|
|
||||||
}
|
}
|
||||||
|
const requestManager = new Web3RequestManager.Manager(blockchainConfig.endpoint);
|
||||||
|
|
||||||
let onStart = () => {
|
const ogConsoleError = console.error;
|
||||||
started();
|
// TODO remove this once we update to web3 2.0
|
||||||
cb();
|
// 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);
|
||||||
|
}
|
||||||
|
const clientFunctions = this.blockchainNodes[clientName];
|
||||||
|
if (!clientFunctions) {
|
||||||
|
return cb(__("Client %s not found", clientName));
|
||||||
|
}
|
||||||
|
|
||||||
this.startedClient = clientName;
|
let onStart = () => {
|
||||||
clientFunctions.launchFn.apply(clientFunctions, [onStart]);
|
started();
|
||||||
|
cb();
|
||||||
|
};
|
||||||
|
|
||||||
|
this.startedClient = clientName;
|
||||||
|
clientFunctions.launchFn.apply(clientFunctions, [onStart]);
|
||||||
|
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,8 @@ class EthereumBlockchainClient {
|
||||||
const web3 = await this.web3;
|
const web3 = await this.web3;
|
||||||
const [account] = await web3.eth.getAccounts();
|
const [account] = await web3.eth.getAccounts();
|
||||||
const contractObj = new web3.eth.Contract(contract.abiDefinition, contract.address);
|
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) {
|
if (contract.gas === 'auto' || !contract.gas) {
|
||||||
const gasValue = await contractObject.estimateGas();
|
const gasValue = await contractObject.estimateGas();
|
||||||
|
|
|
@ -59,7 +59,8 @@ var Blockchain = function(userConfig, clientClass, communicationConfig) {
|
||||||
vmdebug: this.userConfig.vmdebug || false,
|
vmdebug: this.userConfig.vmdebug || false,
|
||||||
targetGasLimit: this.userConfig.targetGasLimit || false,
|
targetGasLimit: this.userConfig.targetGasLimit || false,
|
||||||
syncMode: this.userConfig.syncMode || this.userConfig.syncmode,
|
syncMode: this.userConfig.syncMode || this.userConfig.syncmode,
|
||||||
verbosity: this.userConfig.verbosity
|
verbosity: this.userConfig.verbosity,
|
||||||
|
customOptions: this.userConfig.customOptions
|
||||||
};
|
};
|
||||||
|
|
||||||
this.devFunds = null;
|
this.devFunds = null;
|
||||||
|
|
|
@ -381,6 +381,16 @@ class GethClient {
|
||||||
return callback(null, '--dev');
|
return callback(null, '--dev');
|
||||||
}
|
}
|
||||||
callback(null, '');
|
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) {
|
], function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|
|
@ -61,7 +61,8 @@ var Blockchain = function (userConfig, clientClass) {
|
||||||
targetGasLimit: this.userConfig.targetGasLimit || false,
|
targetGasLimit: this.userConfig.targetGasLimit || false,
|
||||||
syncMode: this.userConfig.syncMode || this.userConfig.syncmode,
|
syncMode: this.userConfig.syncMode || this.userConfig.syncmode,
|
||||||
verbosity: this.userConfig.verbosity,
|
verbosity: this.userConfig.verbosity,
|
||||||
proxy: this.userConfig.proxy
|
proxy: this.userConfig.proxy,
|
||||||
|
customOptions: this.userConfig.customOptions
|
||||||
};
|
};
|
||||||
|
|
||||||
this.devFunds = null;
|
this.devFunds = null;
|
||||||
|
|
|
@ -394,6 +394,16 @@ class ParityClient {
|
||||||
// Default Parity gas limit is 4700000: let's set to the geth default
|
// Default Parity gas limit is 4700000: let's set to the geth default
|
||||||
args.push("--gas-floor-target=" + DEFAULTS.TARGET_GAS_LIMIT);
|
args.push("--gas-floor-target=" + DEFAULTS.TARGET_GAS_LIMIT);
|
||||||
return callback(null, "--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) {
|
], function (err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|
|
@ -44,7 +44,9 @@ class EmbarkWeb3 {
|
||||||
const web3 = new Web3(provider);
|
const web3 = new Web3(provider);
|
||||||
await this.events.request2("runcode:register", 'web3', web3);
|
await this.events.request2("runcode:register", 'web3', web3);
|
||||||
const accounts = await web3.eth.getAccounts();
|
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', {
|
await this.events.request2('console:register:helpCmd', {
|
||||||
cmdName: "web3",
|
cmdName: "web3",
|
||||||
|
|
|
@ -75,7 +75,7 @@ export class Proxy {
|
||||||
// Send the possibly modified request to the Node
|
// Send the possibly modified request to the Node
|
||||||
requestManager.send(resp.reqData, (err, result) => {
|
requestManager.send(resp.reqData, (err, result) => {
|
||||||
if (err) {
|
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) => {
|
this.emitActionsForResponse(resp.reqData, {jsonrpc: "2.0", id: resp.reqData.id, result}, (_err, resp) => {
|
||||||
// Send back to the caller (web3)
|
// Send back to the caller (web3)
|
||||||
|
|
Loading…
Reference in New Issue