From 92a89b5121ab579c9bf6d53778169ec5e6d95c16 Mon Sep 17 00:00:00 2001 From: winteraz Date: Wed, 24 Jan 2018 05:54:23 +0200 Subject: [PATCH 1/4] multisummary -batch request for summary information --- lib/addresses.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/lib/addresses.js b/lib/addresses.js index 7fb8f796a..26a72900f 100644 --- a/lib/addresses.js +++ b/lib/addresses.js @@ -278,6 +278,70 @@ AddressController.prototype.multitxs = function(req, res) { }); }; +// this call could take a while to run depending on what addresses are used +// considering memory constraints, we will streaming out the results for addresses +// not necessarily in the order we received them + + +AddressController.prototype.multisummary = function(req, res) { + + var self = this; + + var addresses; + + if (_.isArray(req.addrs)) { + addresses = _.uniq(req.addrs); + } else { + addresses = _.compact(req.addrs.split(',')); + } + + var addressesLeft = addresses.length; + var startedWriting = false; + var cache = []; + + res.write('['); + + var sep = ','; + + async.eachLimit(addresses, 4, function(addr, next) { + + self._address.getAddressSummary(addr, {}, function(err, summaries) { + + if (err) { + return next(err); + } + + if (addressesLeft-- > 0 && summaries.length > 0 && startedWriting) { + res.write(sep); + } + + for(var i = 0; i < summaries.length; i++) { + startedWriting = true; + if (summaries.length - 1 === i) { + sep = ''; + } + cache.push(summaries[i]); + res.write(JSON.stringify(summaries[i]) + sep); + } + + sep = ','; + next(); + + }); + + }, function(err) { + + if (err) { + return self.common.handleErrors(err, res); + } + + res.write(']'); + res.end(); + }); + +}; + + AddressController.prototype.transformAddressHistoryForMultiTxs = function(txs, options, callback) { var self = this; From 7b1cf91680662a072c6965dbf7ab63143e50105c Mon Sep 17 00:00:00 2001 From: winteraz Date: Wed, 24 Jan 2018 05:56:09 +0200 Subject: [PATCH 2/4] add summary route --- lib/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/index.js b/lib/index.js index c6519cf9f..6de8b561a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -227,6 +227,8 @@ InsightAPI.prototype.setupRoutes = function(app) { app.get('/addr/:addr/totalReceived', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.totalReceived.bind(addresses)); app.get('/addr/:addr/totalSent', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.totalSent.bind(addresses)); app.get('/addr/:addr/unconfirmedBalance', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.unconfirmedBalance.bind(addresses)); + + app.get('/addrs/:addrs/summary', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.multisummary.bind(addresses)); // Status route var status = new StatusController(this.node); From d155a07e067284f7021ed5d7b3d0b1b89e419a59 Mon Sep 17 00:00:00 2001 From: winteraz Date: Wed, 24 Jan 2018 06:53:20 +0200 Subject: [PATCH 3/4] fix multisummary --- lib/addresses.js | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/lib/addresses.js b/lib/addresses.js index 26a72900f..8cbd333b6 100644 --- a/lib/addresses.js +++ b/lib/addresses.js @@ -305,28 +305,21 @@ AddressController.prototype.multisummary = function(req, res) { async.eachLimit(addresses, 4, function(addr, next) { - self._address.getAddressSummary(addr, {}, function(err, summaries) { + self._address.getAddressSummary(addr, {}, function(err, summary) { if (err) { return next(err); } - if (addressesLeft-- > 0 && summaries.length > 0 && startedWriting) { + if (addressesLeft-- > 0 && startedWriting) { res.write(sep); } - - for(var i = 0; i < summaries.length; i++) { - startedWriting = true; - if (summaries.length - 1 === i) { - sep = ''; - } - cache.push(summaries[i]); - res.write(JSON.stringify(summaries[i]) + sep); - } - + startedWriting = true; + sep = ''; + cache.push(summary); + res.write(JSON.stringify(summary) + sep); sep = ','; next(); - }); }, function(err) { @@ -342,6 +335,7 @@ AddressController.prototype.multisummary = function(req, res) { }; + AddressController.prototype.transformAddressHistoryForMultiTxs = function(txs, options, callback) { var self = this; From 932e1a7274d4f8365cac3e09909d596f919cdc0e Mon Sep 17 00:00:00 2001 From: winteraz Date: Wed, 24 Jan 2018 07:01:44 +0200 Subject: [PATCH 4/4] updated readme with /summary information --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 4d5208ce8..bea57cdc1 100644 --- a/README.md +++ b/README.md @@ -214,6 +214,8 @@ Example response: /insight-api/addr/[:addr]/totalReceived /insight-api/addr/[:addr]/totalSent /insight-api/addr/[:addr]/unconfirmedBalance + + /insight-api/addrs/[:addrs]/summary ``` The response contains the value in Satoshis.