diff --git a/lib/services/address/index.js b/lib/services/address/index.js index b494a819..81e18930 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -189,9 +189,10 @@ AddressService.prototype.getAddressHistory = function(addresses, options, stream options.queryMempool = true; } + var old_support = false; //Quick support for `from` and `to` options (DEPRECATED! Not recommeded to use) if( !_.isUndefined(options.from) || !_.isUndefined(options.to)) { - options.old_support = true; + old_support = true; options.from = options.from || 0; options.to = options.to || 0xffffffff; //Max value of to will actually be MAX_TX_QUERY_LIMIT } @@ -207,30 +208,31 @@ AddressService.prototype.getAddressHistory = function(addresses, options, stream async.eachLimit(addresses, 4, function(address, next) { - self._streamAddressSummary(address, options, function(err, tx){ + var addr_options = Object.assign({}, options), addr_count = 0; + + self._streamAddressSummary(address, addr_options, function(err, tx){ if(err) return log.error(err); - if(!options.txNotNeeded) { - results.totalCount++; - + addr_count++; + if(!results.items.some(x => x.txid() === tx.txid())) //push only if tx not already in array results.items.unshift(tx); //using unshift, so that recent tx (low) are at front if(results.items.length > MAX_TX_QUERY_LIMIT) { //remove items from array when overflown results.items.sort((a, b) => (b.__height || 0xffffffff) - (a.__height || 0xffffffff) || b.txid().localeCompare(a.txid())); let del_count = results.items.length - MAX_TX_QUERY_LIMIT; - let start_index = options.old_support ? MAX_TX_QUERY_LIMIT : 0; + let start_index = old_support ? MAX_TX_QUERY_LIMIT : 0; results.items.splice(start_index, del_count); - if(!options.old_support) - options.flag_stop = true; //limit has reached, stop quering db for more tx + results.incomplete = true; + if(!old_support && addr_count >= MAX_TX_QUERY_LIMIT) + addr_options.flag_stop = true; //limit has reached, stop quering db for more tx + } - } - streamer(null, tx); }, next); @@ -243,10 +245,10 @@ AddressService.prototype.getAddressHistory = function(addresses, options, stream //sort items in desc block-height, then asc txid (if same height) results.items.sort((a, b) => (b.__height || 0xffffffff) - (a.__height || 0xffffffff) || b.txid().localeCompare(a.txid())); - results.totalCount = parseInt(results.totalCount.toFixed()); + results.totalCount = results.items.length ; //Quick support for `from` and `to` options (DEPRECATED! Not recommeded to use) - if(options.old_support) { + if(old_support) { results.items = results.items.slice(options.from, options.to); } @@ -308,7 +310,6 @@ AddressService.prototype.getAddressSummary = function(address, options, streamer options = options || {}; //options.from = options.from || 0; //Deprecated //options.to = options.to || 0xffffffff; //Deprecated - options.txNotNeeded = true; //no need to store tx details in result if (_.isUndefined(options.queryMempool)) { options.queryMempool = true;