2018-05-18 13:25:44 +00:00
|
|
|
const async = require('async');
|
2018-07-27 20:54:33 +00:00
|
|
|
const AccountParser = require('../../utils/accountParser');
|
2018-05-18 13:25:44 +00:00
|
|
|
const fundAccount = require('./fundAccount');
|
|
|
|
|
2018-07-03 20:39:17 +00:00
|
|
|
class Provider {
|
2018-05-10 18:52:51 +00:00
|
|
|
constructor(options) {
|
|
|
|
this.web3 = options.web3;
|
|
|
|
this.accountsConfig = options.accountsConfig;
|
2018-06-15 18:35:50 +00:00
|
|
|
this.blockchainConfig = options.blockchainConfig;
|
|
|
|
this.type = options.type;
|
2018-05-18 13:25:44 +00:00
|
|
|
this.web3Endpoint = options.web3Endpoint;
|
2018-05-10 18:52:51 +00:00
|
|
|
this.logger = options.logger;
|
2018-05-18 13:46:39 +00:00
|
|
|
this.isDev = options.isDev;
|
2018-07-27 20:54:33 +00:00
|
|
|
this.engine = new ProviderEngine();
|
|
|
|
this.asyncMethods = {};
|
2018-05-18 13:25:44 +00:00
|
|
|
}
|
2018-05-10 18:52:30 +00:00
|
|
|
|
2018-05-18 17:30:12 +00:00
|
|
|
startWeb3Provider(callback) {
|
2018-05-18 13:25:44 +00:00
|
|
|
const self = this;
|
2018-06-15 18:35:50 +00:00
|
|
|
|
|
|
|
if (this.type === 'rpc') {
|
2018-07-27 20:54:33 +00:00
|
|
|
self.engine.addProvider(new RpcSubprovider({
|
|
|
|
rpcUrl: self.web3Endpoint
|
|
|
|
}));
|
2018-06-15 18:35:50 +00:00
|
|
|
} else if (this.type === 'ws') {
|
2018-10-06 16:05:37 +00:00
|
|
|
// Note: don't pass to the provider things like {headers: {Origin: "embark"}}. Origin header is for browser to fill
|
|
|
|
// to protect user, it has no meaning if it is used server-side. See here for more details: https://github.com/ethereum/go-ethereum/issues/16608
|
|
|
|
// Moreover, Parity reject origins that are not urls so if you try to connect with Origin: "embark" it gives the followin error:
|
|
|
|
// << Blocked connection to WebSockets server from untrusted origin: Some("embark") >>
|
|
|
|
// The best choice is to use void origin, BUT Geth rejects void origin, so to keep both clients happy we can use http://embark
|
|
|
|
self.provider = new this.web3.providers.WebsocketProvider(self.web3Endpoint, {headers: {Origin: "http://embark"}});
|
|
|
|
|
2018-10-17 10:27:12 +00:00
|
|
|
self.provider.on('error', () => self.logger.error('Websocket Error'));
|
|
|
|
self.provider.on('end', () => self.logger.error('Websocket connection ended'));
|
2018-06-15 18:35:50 +00:00
|
|
|
} else {
|
|
|
|
return callback(__("contracts config error: unknown deployment type %s", this.type));
|
|
|
|
}
|
|
|
|
|
2018-07-27 20:54:33 +00:00
|
|
|
// network connectivity error
|
|
|
|
self.engine.on('error', (err) => {
|
|
|
|
// report connectivity errors
|
|
|
|
self.logger.error(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
self.engine.start();
|
|
|
|
//self.on('error', (err) => {
|
|
|
|
// console.log('ERR', JSON.stringify(err));
|
|
|
|
// // report connectivity errors as trace due to polling
|
|
|
|
// self.logger.trace('web3 provider error: ', err);
|
|
|
|
// self.logger.trace('stopping web3 provider due to error');
|
|
|
|
|
|
|
|
// // prevent continuous polling errors
|
|
|
|
// self.stop();
|
|
|
|
//});
|
|
|
|
|
|
|
|
//self.web3.setProvider(self);
|
|
|
|
//self.start();
|
2018-05-18 13:25:44 +00:00
|
|
|
|
2018-10-04 19:18:00 +00:00
|
|
|
self.web3.eth.getAccounts((err, accounts) => {
|
|
|
|
if (err) {
|
|
|
|
self.logger.warn('Error while getting the node\'s accounts.', err.message || err);
|
|
|
|
}
|
2018-07-06 18:43:01 +00:00
|
|
|
|
2018-10-04 19:18:00 +00:00
|
|
|
self.accounts = AccountParser.parseAccountsConfig(self.accountsConfig, self.web3, self.logger, accounts);
|
|
|
|
self.addresses = [];
|
|
|
|
|
|
|
|
if (!self.accounts.length) {
|
|
|
|
return callback();
|
2018-07-06 18:43:01 +00:00
|
|
|
}
|
2018-07-03 20:39:17 +00:00
|
|
|
|
2018-10-04 19:18:00 +00:00
|
|
|
self.accounts.forEach(account => {
|
|
|
|
self.addresses.push(account.address);
|
|
|
|
if (account.privateKey) {
|
|
|
|
self.web3.eth.accounts.wallet.add(account);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
self.web3.eth.defaultAccount = self.addresses[0];
|
|
|
|
|
|
|
|
const realSend = self.provider.send.bind(self.provider);
|
|
|
|
self.provider.send = function (payload, cb) {
|
|
|
|
if (payload.method === 'eth_accounts') {
|
|
|
|
return realSend(payload, function (err, result) {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
result.result = self.addresses; // Send our addresses
|
|
|
|
cb(null, result);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
realSend(payload, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
callback();
|
|
|
|
});
|
2018-05-10 18:52:30 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 10:27:12 +00:00
|
|
|
connected() {
|
|
|
|
if (this.type === 'rpc') {
|
|
|
|
return !!this.provider;
|
|
|
|
} else if (this.type === 'ws') {
|
|
|
|
return this.provider && this.provider.connected;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-06 18:43:01 +00:00
|
|
|
stop() {
|
|
|
|
if (this.provider && this.provider.removeAllListeners) {
|
|
|
|
this.provider.removeAllListeners('connect');
|
|
|
|
this.provider.removeAllListeners('error');
|
|
|
|
this.provider.removeAllListeners('end');
|
|
|
|
this.provider.removeAllListeners('data');
|
|
|
|
this.provider.responseCallbacks = {};
|
|
|
|
}
|
2018-10-17 10:27:12 +00:00
|
|
|
this.provider = null;
|
|
|
|
this.web3.setProvider(null);
|
2018-07-06 18:43:01 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 16:55:07 +00:00
|
|
|
fundAccounts(callback) {
|
|
|
|
const self = this;
|
|
|
|
if (!self.accounts.length) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
if (!self.isDev) {
|
|
|
|
return callback();
|
|
|
|
}
|
2018-07-27 20:54:33 +00:00
|
|
|
async.each(self.accounts, (account, eachCb) => {
|
2018-06-07 17:06:09 +00:00
|
|
|
fundAccount(self.web3, account.address, account.hexBalance, eachCb);
|
2018-06-07 16:55:07 +00:00
|
|
|
}, callback);
|
|
|
|
}
|
2018-07-27 20:54:33 +00:00
|
|
|
|
|
|
|
stop() {
|
|
|
|
this.engine.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
eth_accounts(payload, cb) {
|
|
|
|
return cb(null, this.addresses);
|
|
|
|
}
|
|
|
|
|
|
|
|
sendAsync(payload, callback) {
|
|
|
|
let method = this.asyncMethods[payload.method];
|
|
|
|
if (method) {
|
|
|
|
return method.call(method, payload, (err, result) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
let response = {'id': payload.id, 'jsonrpc': '2.0', 'result': result};
|
|
|
|
callback(null, response);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.engine.sendAsync.apply(this.engine, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
send() {
|
|
|
|
return this.engine.send.apply(this.engine, arguments);
|
|
|
|
}
|
2018-05-10 18:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Provider;
|