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: {
|
||||
// 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"
|
||||
|
|
|
@ -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;
|
||||
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;
|
||||
}
|
||||
ogConsoleError(...args);
|
||||
};
|
||||
requestManager.send({method: 'eth_accounts'}, (err, _accounts) => {
|
||||
console.error = ogConsoleError;
|
||||
if (!err) {
|
||||
// Node is already started
|
||||
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(null, true);
|
||||
}
|
||||
const clientFunctions = this.blockchainNodes[clientName];
|
||||
if (!clientFunctions) {
|
||||
return cb(__("Client %s not found", clientName));
|
||||
return cb();
|
||||
}
|
||||
const requestManager = new Web3RequestManager.Manager(blockchainConfig.endpoint);
|
||||
|
||||
let onStart = () => {
|
||||
started();
|
||||
cb();
|
||||
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);
|
||||
}
|
||||
const clientFunctions = this.blockchainNodes[clientName];
|
||||
if (!clientFunctions) {
|
||||
return cb(__("Client %s not found", clientName));
|
||||
}
|
||||
|
||||
this.startedClient = clientName;
|
||||
clientFunctions.launchFn.apply(clientFunctions, [onStart]);
|
||||
let onStart = () => {
|
||||
started();
|
||||
cb();
|
||||
};
|
||||
|
||||
this.startedClient = clientName;
|
||||
clientFunctions.launchFn.apply(clientFunctions, [onStart]);
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue