Fixed pagination for getAddressHistory.

This commit is contained in:
Chris Kleeschulte 2017-10-17 17:46:11 -04:00
parent 0dc69d87af
commit 880f98e669
No known key found for this signature in database
GPG Key ID: 33195D27EF6BDB7F

View File

@ -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);
}