Browse Source

Merge pull request #278 from isocolsky/ref/history_pagination

Remote pagination of tx history
activeAddress
Matias Alejo Garcia 10 years ago
parent
commit
348eb0e48a
  1. 17
      lib/server.js
  2. 28
      test/integration/server.js

17
lib/server.js

@ -1470,17 +1470,6 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
}); });
}; };
function paginate(txs) {
var skip = opts.skip || 0;
var limited = _.isNumber(opts.limit) && opts.limit != -1;
var sliced = _.slice(_.sortBy(txs, function(tx) {
return -tx.time;
}), skip);
return limited ? _.take(sliced, opts.limit) : sliced;
};
// Get addresses for this wallet // Get addresses for this wallet
self.storage.fetchAddresses(self.walletId, function(err, addresses) { self.storage.fetchAddresses(self.walletId, function(err, addresses) {
if (err) return cb(err); if (err) return cb(err);
@ -1499,7 +1488,9 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
}); });
}, },
function(next) { function(next) {
bc.getTransactions(addressStrs, null, null, function(err, txs) { var from = opts.skip || 0;
var to = from + (_.isUndefined(opts.limit) ? 100 : opts.limit);
bc.getTransactions(addressStrs, from, to, function(err, txs) {
if (err) { if (err) {
log.error('Could not fetch transactions', err); log.error('Could not fetch transactions', err);
return next(new ClientError('BLOCKCHAINERROR', 'Could not fetch transactions')); return next(new ClientError('BLOCKCHAINERROR', 'Could not fetch transactions'));
@ -1513,7 +1504,7 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
var proposals = res[0]; var proposals = res[0];
var txs = res[1]; var txs = res[1];
txs = paginate(decorate(txs, addresses, proposals)); txs = decorate(txs, addresses, proposals);
return cb(null, txs); return cb(null, txs);
}); });

28
test/integration/server.js

@ -173,7 +173,28 @@ helpers.stubBroadcastFail = function() {
}; };
helpers.stubHistory = function(txs) { helpers.stubHistory = function(txs) {
blockchainExplorer.getTransactions = sinon.stub().callsArgWith(3, null, txs); blockchainExplorer.getTransactions = function(addresses, from, to, cb) {
var MAX_BATCH_SIZE = 100;
var nbTxs = txs.length;
if (_.isUndefined(from) && _.isUndefined(to)) {
from = 0;
to = MAX_BATCH_SIZE;
}
if (!_.isUndefined(from) && _.isUndefined(to))
to = from + MAX_BATCH_SIZE;
if (!_.isUndefined(from) && !_.isUndefined(to) && to - from > MAX_BATCH_SIZE)
to = from + MAX_BATCH_SIZE;
if (from < 0) from = 0;
if (to < 0) to = 0;
if (from > nbTxs) from = nbTxs;
if (to > nbTxs) to = nbTxs;
var page = txs.slice(from, to);
return cb(null, page);
};
}; };
helpers.stubAddressActivity = function(activeAddresses) { helpers.stubAddressActivity = function(activeAddresses) {
@ -227,8 +248,7 @@ helpers.createProposalOpts = function(type, outputs, message, signingKey, feePer
opts.amount = outputs[0].amount; opts.amount = outputs[0].amount;
hash = WalletUtils.getProposalHash(opts.toAddress, opts.amount, hash = WalletUtils.getProposalHash(opts.toAddress, opts.amount,
opts.message, opts.payProUrl); opts.message, opts.payProUrl);
} } else if (type == Model.TxProposal.Types.MULTIPLEOUTPUTS) {
else if (type == Model.TxProposal.Types.MULTIPLEOUTPUTS) {
opts.outputs = outputs; opts.outputs = outputs;
var header = { var header = {
outputs: outputs, outputs: outputs,
@ -3536,7 +3556,7 @@ describe('Wallet service', function() {
}]; }];
server._normalizeTxHistory = sinon.stub().returnsArg(0); server._normalizeTxHistory = sinon.stub().returnsArg(0);
var timestamps = [10, 50, 30, 40, 20]; var timestamps = [50, 40, 30, 20, 10];
var txs = _.map(timestamps, function(ts, idx) { var txs = _.map(timestamps, function(ts, idx) {
return { return {
txid: (idx + 1).toString(), txid: (idx + 1).toString(),

Loading…
Cancel
Save