From 9e216a818a4f3a44fa1ed1cfa92f2af555a9af3a Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Fri, 20 Oct 2017 10:40:57 -0400 Subject: [PATCH] Streamed multiutxos. --- lib/addresses.js | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/addresses.js b/lib/addresses.js index 70566a4..64b302e 100644 --- a/lib/addresses.js +++ b/lib/addresses.js @@ -152,10 +152,13 @@ AddressController.prototype.utxo = 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.multiutxo = function(req, res) { var self = this; - var finalUtxos = []; + var sep = ','; var addresses; if (_.isArray(req.addrs)) { @@ -164,6 +167,9 @@ AddressController.prototype.multiutxo = function(req, res) { addresses = req.addrs.split(','); } + var addressesLeft = addresses.length; + res.write('['); + async.eachLimit(addresses, 4, function(addr, next) { self._address.getAddressUnspentOutputs(addr, {}, function(err, utxos) { @@ -172,20 +178,28 @@ AddressController.prototype.multiutxo = function(req, res) { return next(err); } - finalUtxos = finalUtxos.concat(utxos); + for(var i = 0; i < utxos.length; i++) { + + if (--addressesLeft <= 0 && utxos[i] === utxos[utxos.length - 1] ) { + sep = ''; + } + + res.write(JSON.stringify(self.transformUtxo(utxos[i])) + sep); + } + next(); }); }, function(err) { - if (err) { - return self.common.handleErrors(err, res); - } - - var finalRes = finalUtxos.map(self.transformUtxo.bind(self)); - res.jsonp(finalRes); + if (err) { + return self.common.handleErrors(err, res); + } + res.write(']'); + res.end(); }); + }; AddressController.prototype.transformUtxo = function(utxoArg) {