Skip to content

Commit

Permalink
Merge pull request bitpay#462 from matiu/feat/multiple-insights
Browse files Browse the repository at this point in the history
adds multiple insight server support
  • Loading branch information
isocolsky committed Feb 17, 2016
2 parents 0aca76d + 7eea242 commit b20e0b7
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 34 deletions.
2 changes: 2 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ var config = {
testnet: {
provider: 'insight',
url: 'https://test-insight.bitpay.com:443',
// Multiple servers (in priority order)
// url: ['http://a.b.c', 'https://test-insight.bitpay.com:443'],
},
},
pushNotificationsOpts: {
Expand Down
49 changes: 26 additions & 23 deletions lib/blockchainexplorers/insight.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ var _ = require('lodash');
var $ = require('preconditions').singleton();
var log = require('npmlog');
log.debug = log.verbose;
var request = require('request');
var io = require('socket.io-client');
var requestList = require('./request-list');

function Insight(opts) {
$.checkArgument(opts);
Expand All @@ -14,7 +14,7 @@ function Insight(opts) {

this.apiPrefix = opts.apiPrefix || '/api';
this.network = opts.network || 'livenet';
this.url = opts.url;
this.hosts = opts.url;
};


Expand All @@ -28,23 +28,23 @@ var _parseErr = function(err, res) {
};

Insight.prototype.getConnectionInfo = function() {
return 'Insight (' + this.network + ') @ ' + this.url;
return 'Insight (' + this.network + ') @ ' + this.hosts;
};

/**
* Retrieve a list of unspent outputs associated with an address or set of addresses
*/
Insight.prototype.getUnspentUtxos = function(addresses, cb) {
var url = this.url + this.apiPrefix + '/addrs/utxo';
var args = {
method: 'POST',
url: url,
hosts: this.hosts,
path: this.apiPrefix + '/addrs/utxo',
json: {
addrs: [].concat(addresses).join(',')
},
};

request(args, function(err, res, unspent) {
requestList(args, function(err, res, unspent) {
if (err || res.statusCode !== 200) return cb(_parseErr(err, res));
return cb(null, unspent);
});
Expand All @@ -54,30 +54,30 @@ Insight.prototype.getUnspentUtxos = function(addresses, cb) {
* Broadcast a transaction to the bitcoin network
*/
Insight.prototype.broadcast = function(rawTx, cb) {
var url = this.url + this.apiPrefix + '/tx/send';
var args = {
method: 'POST',
url: url,
hosts: this.hosts,
path: this.apiPrefix + '/tx/send',
json: {
rawtx: rawTx
},
};

request(args, function(err, res, body) {
requestList(args, function(err, res, body) {
if (err || res.statusCode !== 200) return cb(_parseErr(err, res));
return cb(null, body ? body.txid : null);
});
};

Insight.prototype.getTransaction = function(txid, cb) {
var url = this.url + this.apiPrefix + '/tx/' + txid;
var args = {
method: 'GET',
url: url,
hosts: this.hosts,
path: this.apiPrefix + '/tx/' + txid,
json: true,
};

request(args, function(err, res, tx) {
requestList(args, function(err, res, tx) {
if (res && res.statusCode == 404) return cb();
if (err || res.statusCode !== 200)
return cb(_parseErr(err, res));
Expand All @@ -91,16 +91,16 @@ Insight.prototype.getTransactions = function(addresses, from, to, cb) {
if (_.isNumber(from)) qs.push('from=' + from);
if (_.isNumber(to)) qs.push('to=' + to);

var url = this.url + this.apiPrefix + '/addrs/txs' + (qs.length > 0 ? '?' + qs.join('&') : '');
var args = {
method: 'POST',
url: url,
hosts: this.hosts,
path: this.apiPrefix + '/addrs/txs' + (qs.length > 0 ? '?' + qs.join('&') : ''),
json: {
addrs: [].concat(addresses).join(',')
},
};

request(args, function(err, res, txs) {
requestList(args, function(err, res, txs) {
if (err || res.statusCode !== 200) return cb(_parseErr(err, res));

if (_.isObject(txs) && txs.items)
Expand All @@ -116,14 +116,14 @@ Insight.prototype.getTransactions = function(addresses, from, to, cb) {
Insight.prototype.getAddressActivity = function(address, cb) {
var self = this;

var url = self.url + self.apiPrefix + '/addr/' + address;
var args = {
method: 'GET',
url: url,
hosts: this.hosts,
path: self.apiPrefix + '/addr/' + address,
json: true,
};

request(args, function(err, res, result) {
requestList(args, function(err, res, result) {
if (res && res.statusCode == 404) return cb();
if (err || res.statusCode !== 200)
return cb(_parseErr(err, res));
Expand All @@ -134,24 +134,27 @@ Insight.prototype.getAddressActivity = function(address, cb) {
};

Insight.prototype.estimateFee = function(nbBlocks, cb) {
var url = this.url + this.apiPrefix + '/utils/estimatefee';
var path = this.apiPrefix + '/utils/estimatefee';
if (nbBlocks) {
url += '?nbBlocks=' + [].concat(nbBlocks).join(',');
path += '?nbBlocks=' + [].concat(nbBlocks).join(',');
}

var args = {
method: 'GET',
url: url,
hosts: this.hosts,
path: path,
json: true,
};
request(args, function(err, res, body) {
requestList(args, function(err, res, body) {
if (err || res.statusCode !== 200) return cb(_parseErr(err, res));
return cb(null, body);
});
};

Insight.prototype.initSocket = function() {
var socket = io.connect(this.url, {

// sockets always use the first server on the pull
var socket = io.connect(this.hosts[0], {
'reconnection': true,
});
return socket;
Expand Down
57 changes: 57 additions & 0 deletions lib/blockchainexplorers/request-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var _ = require('lodash');
var async = require('async');
var $ = require('preconditions').singleton();

var log = require('npmlog');
log.debug = log.verbose;

/**
* Query a server, using one of the given options
*
* @param {Object} opts
* @param {Array} opts.hosts Array of hosts to query. Until the first success one.
* @param {Array} opts.path Path to request in each server
*/
var requestList = function(args, cb) {
$.checkArgument(args.hosts);
request = args.request || require('request');

if (!_.isArray(args.hosts))
args.hosts = [args.hosts];

var urls = _.map(args.hosts, function(x) {
return (x + args.path);
});
var nextUrl, result, success;

async.whilst(
function() {
nextUrl = urls.shift();
return nextUrl && !success;
},
function(a_cb) {
args.uri = nextUrl;
request(args, function(err, res, body) {
if (err) {
log.warn('REQUEST FAIL: ' + nextUrl + ' ERROR: ' + err);
}

if (res) {
success = !!res.statusCode.toString().match(/^[1234]../);
if (!success) {
log.warn('REQUEST FAIL: ' + nextUrl + ' STATUS CODE: ' + res.statusCode);
}
}

result = [err, res, body];
return a_cb();
});
},
function(err) {
if (err) return cb(err);
return cb(result[0], result[1], result[2]);
}
);
};

module.exports = requestList;
26 changes: 15 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"url": "https://github.com/bitpay/bitcore-wallet-service/issues"
},
"dependencies": {
"async": "^0.9.0",
"async": "^0.9.2",
"bitcore-lib": "^0.13.7",
"body-parser": "^1.11.0",
"coveralls": "^2.11.2",
Expand Down Expand Up @@ -65,14 +65,18 @@
"coveralls": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
},
"bitcoreNode": "./bitcorenode",
"contributors": [{
"name": "Braydon Fuller",
"email": "braydon@bitpay.com"
}, {
"name": "Ivan Socolsky",
"email": "ivan@bitpay.com"
}, {
"name": "Matias Alejo Garcia",
"email": "ematiu@gmail.com"
}]
"contributors": [
{
"name": "Braydon Fuller",
"email": "braydon@bitpay.com"
},
{
"name": "Ivan Socolsky",
"email": "ivan@bitpay.com"
},
{
"name": "Matias Alejo Garcia",
"email": "ematiu@gmail.com"
}
]
}
Loading

0 comments on commit b20e0b7

Please sign in to comment.