move blockchain connection code to its own module

This commit is contained in:
Iuri Matias 2018-07-27 16:54:33 -04:00
parent bb4aeac899
commit a3d828af05
5 changed files with 103 additions and 103 deletions

View File

@ -219,7 +219,7 @@ class Engine {
this.registerModule('blockchain_connector', {
isDev: this.isDev,
web3: options.web3
})); /*eslint no-new: "off"*/
});
this.registerModule('whisper');
}

View File

@ -2,8 +2,6 @@ const Web3 = require('web3');
const async = require('async');
const Provider = require('./provider.js');
const utils = require('../../utils/utils');
const constants = require('../../constants');
const embarkJsUtils = require('embarkjs').Utils;
const WEB3_READY = 'web3Ready';
@ -20,6 +18,7 @@ class BlockchainConnector {
this.isDev = options.isDev;
this.web3Endpoint = '';
this.isWeb3Ready = false;
this.web3StartedInProcess = false;
self.events.setCommandHandler("blockchain:web3:isReady", (cb) => {
cb(self.isWeb3Ready);
@ -38,19 +37,9 @@ class BlockchainConnector {
this.registerRequests();
this.registerWeb3Object();
this.registerEvents();
this.subscribeToPendingTransactions();
}
//initWeb3() {
initWeb3(cb) {
if (!cb) {
cb = function(){};
}
if (this.isWeb3Ready) {
this.events.emit(WEB3_READY);
return cb();
}
initWeb3() {
const self = this;
this.web3 = new Web3();
@ -76,18 +65,6 @@ class BlockchainConnector {
self.events.request("processes:launch", "blockchain", () => {
self.provider.startWeb3Provider(() => {
this.web3.eth.net.getId()
.then(id => {
let networkId = self.blockchainConfig.networkId;
if (!networkId && constants.blockchain.networkIds[self.blockchainConfig.networkType]) {
networkId = constants.blockchain.networkIds[self.blockchainConfig.networkType];
}
if (id.toString() !== networkId.toString()) {
self.logger.warn(__('Connected to a blockchain node on network {{realId}} while your config specifies {{configId}}', {realId: id, configId: networkId}));
self.logger.warn(__('Make sure you started the right blockchain node'));
}
})
.catch(console.error);
self.provider.fundAccounts(() => {
self.isWeb3Ready = true;
self.events.emit(WEB3_READY);
@ -232,14 +209,60 @@ class BlockchainConnector {
}
deployContractFromObject(deployContractObject, params, cb) {
embarkJsUtils.secureSend(this.web3, deployContractObject, {
const self = this;
let hash;
let calledBacked = false;
function callback(err, receipt) {
if (calledBacked) {
return;
}
if (!err && !receipt.contractAddress) {
return; // Not deployed yet. Need to wait
}
if (interval) {
clearInterval(interval);
}
calledBacked = true;
cb(err, receipt);
}
// This interval is there to compensate for the event that sometimes doesn't get triggered when using WebSocket
// FIXME The issue somehow only happens when the blockchain node is started in the same terminal
const interval = setInterval(() => {
if (!hash) {
return; // Wait until we receive the hash
}
self.web3.eth.getTransactionReceipt(hash, (err, receipt) => {
if (!err && !receipt) {
return; // Transaction is not yet complete
}
callback(err, receipt);
});
}, 500);
deployContractObject.send({
from: params.from, gas: params.gas, gasPrice: params.gasPrice
}, true, cb);
}, function (err, transactionHash) {
if (err) {
return callback(err);
}
hash = transactionHash;
}).on('receipt', function (receipt) {
if (receipt.contractAddress !== undefined) {
callback(null, receipt);
}
}).then(function (_contract) {
if (!hash) {
return; // Somehow we didn't get the receipt yet... Interval will catch it
}
self.web3.eth.getTransactionReceipt(hash, callback);
}).catch(callback);
}
determineDefaultAccount(cb) {
const self = this;
self.getAccounts(function(err, accounts) {
self.getAccounts(function (err, accounts) {
if (err) {
self.logger.error(err);
return cb(new Error(err));
@ -256,20 +279,6 @@ class BlockchainConnector {
// can just be a command without a callback
this.events.emit("runcode:register", "web3", this.web3);
}
subscribeToPendingTransactions() {
const self = this;
this.onReady(() => {
if (self.logsSubscription) {
self.logsSubscription.unsubscribe();
}
self.logsSubscription = self.web3.eth
.subscribe('newBlockHeaders', () => {})
.on("data", function (blockHeader) {
self.events.emit('block:header', blockHeader);
});
});
}
}
module.exports = BlockchainConnector;

View File

@ -1,24 +1,25 @@
const Web3 = require('web3');
const async = require('async');
const Provider = require('./provider.js');
const utils = require('../utils/utils');
const utils = require('../../utils/utils');
const constants = require('../../constants');
const embarkJsUtils = require('embarkjs').Utils;
const WEB3_READY = 'web3Ready';
// TODO: consider another name, this is the blockchain connector
class BlockchainConnector {
constructor(options) {
constructor(embark, options) {
const self = this;
this.plugins = options.plugins;
this.logger = options.logger;
this.events = options.events;
this.contractsConfig = options.contractsConfig;
this.blockchainConfig = options.blockchainConfig;
this.logger = embark.logger;
this.events = embark.events;
this.contractsConfig = embark.config.contractsConfig;
this.blockchainConfig = embark.config.blockchainConfig;
this.web3 = options.web3;
this.isDev = options.isDev;
this.web3Endpoint = '';
this.isWeb3Ready = false;
this.web3StartedInProcess = false;
self.events.setCommandHandler("blockchain:web3:isReady", (cb) => {
cb(self.isWeb3Ready);
@ -37,9 +38,19 @@ class BlockchainConnector {
this.registerRequests();
this.registerWeb3Object();
this.registerEvents();
this.subscribeToPendingTransactions();
}
initWeb3() {
//initWeb3() {
initWeb3(cb) {
if (!cb) {
cb = function(){};
}
if (this.isWeb3Ready) {
this.events.emit(WEB3_READY);
return cb();
}
const self = this;
this.web3 = new Web3();
@ -65,6 +76,18 @@ class BlockchainConnector {
self.events.request("processes:launch", "blockchain", () => {
self.provider.startWeb3Provider(() => {
this.web3.eth.net.getId()
.then(id => {
let networkId = self.blockchainConfig.networkId;
if (!networkId && constants.blockchain.networkIds[self.blockchainConfig.networkType]) {
networkId = constants.blockchain.networkIds[self.blockchainConfig.networkType];
}
if (id.toString() !== networkId.toString()) {
self.logger.warn(__('Connected to a blockchain node on network {{realId}} while your config specifies {{configId}}', {realId: id, configId: networkId}));
self.logger.warn(__('Make sure you started the right blockchain node'));
}
})
.catch(console.error);
self.provider.fundAccounts(() => {
self.isWeb3Ready = true;
self.events.emit(WEB3_READY);
@ -209,60 +232,14 @@ class BlockchainConnector {
}
deployContractFromObject(deployContractObject, params, cb) {
const self = this;
let hash;
let calledBacked = false;
function callback(err, receipt) {
if (calledBacked) {
return;
}
if (!err && !receipt.contractAddress) {
return; // Not deployed yet. Need to wait
}
if (interval) {
clearInterval(interval);
}
calledBacked = true;
cb(err, receipt);
}
// This interval is there to compensate for the event that sometimes doesn't get triggered when using WebSocket
// FIXME The issue somehow only happens when the blockchain node is started in the same terminal
const interval = setInterval(() => {
if (!hash) {
return; // Wait until we receive the hash
}
self.web3.eth.getTransactionReceipt(hash, (err, receipt) => {
if (!err && !receipt) {
return; // Transaction is not yet complete
}
callback(err, receipt);
});
}, 500);
deployContractObject.send({
embarkJsUtils.secureSend(this.web3, deployContractObject, {
from: params.from, gas: params.gas, gasPrice: params.gasPrice
}, function (err, transactionHash) {
if (err) {
return callback(err);
}
hash = transactionHash;
}).on('receipt', function (receipt) {
if (receipt.contractAddress !== undefined) {
callback(null, receipt);
}
}).then(function (_contract) {
if (!hash) {
return; // Somehow we didn't get the receipt yet... Interval will catch it
}
self.web3.eth.getTransactionReceipt(hash, callback);
}).catch(callback);
}, true, cb);
}
determineDefaultAccount(cb) {
const self = this;
self.getAccounts(function (err, accounts) {
self.getAccounts(function(err, accounts) {
if (err) {
self.logger.error(err);
return cb(new Error(err));
@ -279,6 +256,20 @@ class BlockchainConnector {
// can just be a command without a callback
this.events.emit("runcode:register", "web3", this.web3);
}
subscribeToPendingTransactions() {
const self = this;
this.onReady(() => {
if (self.logsSubscription) {
self.logsSubscription.unsubscribe();
}
self.logsSubscription = self.web3.eth
.subscribe('newBlockHeaders', () => {})
.on("data", function (blockHeader) {
self.events.emit('block:header', blockHeader);
});
});
}
}
module.exports = BlockchainConnector;

View File

@ -73,7 +73,7 @@ class Provider {
if (!self.isDev) {
return callback();
}
async.eachLimit(self.accounts, 1, (account, eachCb) => {
async.each(self.accounts, (account, eachCb) => {
fundAccount(self.web3, account.address, account.hexBalance, eachCb);
}, callback);
}

View File

@ -1,5 +1,5 @@
const async = require('async');
const AccountParser = require('../utils/accountParser');
const AccountParser = require('../../utils/accountParser');
const fundAccount = require('./fundAccount');
class Provider {
@ -73,7 +73,7 @@ class Provider {
if (!self.isDev) {
return callback();
}
async.each(self.accounts, (account, eachCb) => {
async.eachLimit(self.accounts, 1, (account, eachCb) => {
fundAccount(self.web3, account.address, account.hexBalance, eachCb);
}, callback);
}