added pagination to addrs/txs

This commit is contained in:
Ivan Socolsky 2014-11-17 16:24:48 -03:00
parent 275dba3749
commit 6dfaefdcc5
2 changed files with 48 additions and 14 deletions

View File

@ -94,8 +94,23 @@ exports.multiutxo = function(req, res, next) {
exports.multitxs = function(req, res, next) {
function processTxs(txs, cb) {
txs = _.uniq(_.flatten(txs));
function processTxs(txs, from, to, cb) {
txs = _.uniq(_.flatten(txs), 'txid');
var nbTxs = txs.length;
var paginated = !_.isUndefined(from) || !_.isUndefined(to);
if (paginated) {
txs.sort(function(a, b) {
return (b.ts || b.ts) - (a.ts || a.ts);
});
var start = Math.max(from || 0, 0);
var end = Math.min(to || txs.length, txs.length);
txs = txs.slice(start, end);
}
txs = _.pluck(txs, 'txid');
var transactions = [];
async.each(txs, function (tx, callback) {
tDb.fromIdWithInfo(tx, function(err, tx) {
@ -107,10 +122,22 @@ exports.multitxs = function(req, res, next) {
});
}, function (err) {
if (err) return cb(err);
if (paginated) {
transactions = {
nbItems: nbTxs,
from: +from,
to: +to,
data: transactions,
};
}
return cb(null, transactions);
});
};
var from = req.query.from;
var to = req.query.to;
var as = getAddrs(req, res, next);
if (as) {
var txs = [];
@ -119,10 +146,10 @@ exports.multitxs = function(req, res, next) {
if (err) callback(err);
txs = txs.concat(a.transactions);
callback();
}, {ignoreCache: req.param('noCache')});
}, {ignoreCache: req.param('noCache'), includeTxInfo: true});
}, function(err) { // finished callback
if (err) return common.handleErrors(err, res);
processTxs(txs, function (err, transactions) {
processTxs(txs, from, to, function (err, transactions) {
if (err) return common.handleErrors(err, res);
res.jsonp(transactions);
});

View File

@ -93,25 +93,31 @@ Address.prototype.getObj = function() {
};
};
Address.prototype._addTxItem = function(txItem, txList) {
Address.prototype._addTxItem = function(txItem, txList, includeInfo) {
function addTx(data) {
if (!txList) return;
if (includeInfo) {
txList.push(data);
} else {
txList.push(data.txid);
}
};
var add=0, addSpend=0;
var v = txItem.value_sat;
var seen = this.seen;
// Founding tx
if ( !seen[txItem.txid] ) {
seen[txItem.txid]=1;
add=1;
if (!seen[txItem.txid]) {
seen[txItem.txid] = 1;
add = 1;
if (txList)
txList.push(txItem.txid);
addTx({ txid: txItem.txid, ts: txItem.ts });
}
// Spent tx
if (txItem.spentTxId && !seen[txItem.spentTxId] ) {
if (txList) {
txList.push(txItem.spentTxId);
}
addTx({ txid: txItem.spentTxId, ts: txItem.spentTs });
seen[txItem.spentTxId]=1;
addSpend=1;
}
@ -143,6 +149,7 @@ Address.prototype._addTxItem = function(txItem, txList) {
// opts are
// .onlyUnspent
// .txLimit (=0 -> no txs, => -1 no limit)
// .includeTxInfo
//
Address.prototype.update = function(next, opts) {
var self = this;
@ -188,7 +195,7 @@ Address.prototype.update = function(next, opts) {
}
else {
txOut.forEach(function(txItem){
self._addTxItem(txItem, txList);
self._addTxItem(txItem, txList, opts.includeTxInfo);
});
if (txList)
self.transactions = txList;