Streamed multiutxos.

This commit is contained in:
Chris Kleeschulte 2017-10-20 10:40:57 -04:00
parent f862a7696b
commit 9e216a818a

View File

@ -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) {