feat: call action before starting the blockchain node

This commit is contained in:
Jonathan Rainville 2019-09-24 13:35:19 -04:00
parent 1bb25b9082
commit c54b8d9e44
9 changed files with 75 additions and 41 deletions

View File

@ -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"

View File

@ -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,7 +23,12 @@ 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) => {
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 self = this; const self = this;
const clientName = blockchainConfig.client; const clientName = blockchainConfig.client;
function started() { function started() {
@ -63,6 +69,8 @@ class Blockchain {
this.startedClient = clientName; this.startedClient = clientName;
clientFunctions.launchFn.apply(clientFunctions, [onStart]); clientFunctions.launchFn.apply(clientFunctions, [onStart]);
});
}); });
}); });

View File

@ -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();

View File

@ -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;

View File

@ -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) {

View File

@ -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;

View File

@ -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) {

View File

@ -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();
if (accounts.length) {
await this.events.request2('runcode:eval', `web3.eth.defaultAccount = '${accounts[0]}'`); 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",

View File

@ -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)