From f8712795b2a1d40d158d467d095d42bf4aec8c98 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 13 Jul 2015 17:32:12 -0300 Subject: [PATCH] remote pagination of tx history --- lib/blockchainexplorers/insight.js | 1 + lib/server.js | 17 ++++------------- test/integration/server.js | 30 +++++++++++++++++++++++++----- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/lib/blockchainexplorers/insight.js b/lib/blockchainexplorers/insight.js index f11e901..5db6b5e 100644 --- a/lib/blockchainexplorers/insight.js +++ b/lib/blockchainexplorers/insight.js @@ -84,6 +84,7 @@ Insight.prototype.getTransactions = function(addresses, from, to, cb) { addrs: [].concat(addresses).join(',') }, }; + console.log('*** [insight.js ln87] args:', args); // TODO request(args, function(err, res, txs) { if (err || res.statusCode != 200) return cb(err || res); diff --git a/lib/server.js b/lib/server.js index 95d2a4d..38ba428 100644 --- a/lib/server.js +++ b/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 self.storage.fetchAddresses(self.walletId, function(err, addresses) { if (err) return cb(err); @@ -1499,7 +1488,9 @@ WalletService.prototype.getTxHistory = function(opts, cb) { }); }, 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) { log.error('Could not fetch transactions', err); return next(new ClientError('BLOCKCHAINERROR', 'Could not fetch transactions')); @@ -1513,7 +1504,7 @@ WalletService.prototype.getTxHistory = function(opts, cb) { var proposals = res[0]; var txs = res[1]; - txs = paginate(decorate(txs, addresses, proposals)); + txs = decorate(txs, addresses, proposals); return cb(null, txs); }); diff --git a/test/integration/server.js b/test/integration/server.js index 0fbce2a..4c84fe1 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -173,7 +173,28 @@ helpers.stubBroadcastFail = function() { }; 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) { @@ -227,8 +248,7 @@ helpers.createProposalOpts = function(type, outputs, message, signingKey, feePer opts.amount = outputs[0].amount; hash = WalletUtils.getProposalHash(opts.toAddress, opts.amount, opts.message, opts.payProUrl); - } - else if (type == Model.TxProposal.Types.MULTIPLEOUTPUTS) { + } else if (type == Model.TxProposal.Types.MULTIPLEOUTPUTS) { opts.outputs = outputs; var header = { outputs: outputs, @@ -260,7 +280,7 @@ helpers.createAddresses = function(server, wallet, main, change, cb) { var storage, blockchainExplorer; -var useMongo = false; +var useMongo = true; function initStorage(cb) { function getDb(cb) { @@ -3536,7 +3556,7 @@ describe('Wallet service', function() { }]; 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) { return { txid: (idx + 1).toString(),