From 880f98e66914ede724593fb86572698f0319eafc Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Tue, 17 Oct 2017 17:46:11 -0400 Subject: [PATCH] Fixed pagination for getAddressHistory. --- lib/services/address/index.js | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/services/address/index.js b/lib/services/address/index.js index 57883317..c628a529 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -32,6 +32,7 @@ var AddressService = function(options) { this._network = 'testnet'; } + }; inherits(AddressService, BaseService); @@ -71,11 +72,12 @@ AddressService.prototype.getAddressHistory = function(addresses, options, callba return callback(err); } - txList = utils.dedupByTxid(txList); + // TODO: these should already be ordered correctly, please check + txList = utils.dedupByTxid(txList); txList = utils.orderByConfirmations(txList); var results = { - totalCount: txList.length, + totalCount: options.txCount, items: txList }; @@ -110,7 +112,6 @@ AddressService.prototype.getAddressSummary = function(address, options, callback unconfirmedBalanceSat: 0, unconfirmedTxApperances: 0, txApperances: 0, - transactions: [] }; self.getAddressHistory(address, options, function(err, results) { @@ -174,6 +175,9 @@ AddressService.prototype._getAddressSummaryResult = function(txs, address, resul } if (!options.noTxList) { + if (!result.transactions) { + result.transactions = []; + } result.transactions.push(tx.txid()); } @@ -338,7 +342,8 @@ AddressService.prototype._getTxidStream = function(address, options) { var criteria = { gte: start, - lte: end + lte: end, + reverse: true // txids stream from low confirmations to high confirmations }; // txid stream @@ -357,6 +362,13 @@ AddressService.prototype._transformTxForAddressHistory = function(opts, chunk, e var txid = _.isString(chunk) ? chunk : self._encoding.decodeAddressIndexKey(chunk).txid; + opts.txCount++; + + // no need to look up the tx if the tx is outside the range of the query. + if (opts.txCount >= (opts.to + 1) || opts.txCount < (opts.from + 1)) { + return callback(); + } + self._tx.getTransaction(txid, opts, function(err, tx) { if (err) { @@ -407,6 +419,7 @@ AddressService.prototype._getAddressHistory = function(address, options, callbac options.endHeightBuf = new Buffer(4); options.endHeightBuf.writeUInt32BE(options.end); options.results = []; + options.txCount = 0; // this tracks the number of txs in the record set for pagination if (_.isUndefined(options.queryMempool)) { options.queryMempool = true; @@ -430,19 +443,26 @@ AddressService.prototype._getAddressHistory = function(address, options, callbac if (mempoolTxs.length <= 0) { return next(); } - async.mapSeries(mempoolTxs, function(tx, next) { + async.mapLimit(mempoolTxs, 4, function(tx, next) { self._transaction.setTxMetaInfo(tx, options, next); }, function(err, txs) { if (err) { return next(err); } - options.results = txs; + // what tx range are we looking for? + options.results = txs.slice(options.from, options.to); next(); }); }, // stream the rest of the confirmed txids out of the address index function(next) { + if (options.results.length >= (options.to - options.from)) { + return callback(null, options.results); + } + + options.from += options.results.length; + var txStream = self._getTxStream(address, options); txStream.on('end', function() { @@ -453,6 +473,7 @@ AddressService.prototype._getAddressHistory = function(address, options, callbac log.error('Address Service: txstream err: ' + err); txStream.unpipe(); }); + var txidStream = self._getTxidStream(address, options); txidStream.pipe(txStream); }