Merge pull request #1849 from isocolsky/history_pagination

using paginated version of insight-api addrs/txs
This commit is contained in:
Matias Alejo Garcia 2014-11-20 18:22:27 -03:00
commit 2ad7535467
5 changed files with 60 additions and 34 deletions

View file

@ -269,11 +269,14 @@ Insight.prototype.getTransaction = function(txid, cb) {
}); });
}; };
Insight.prototype.getTransactions = function (addresses, cb) { Insight.prototype.getTransactions = function (addresses, from, to, cb) {
preconditions.shouldBeArray(addresses); preconditions.shouldBeArray(addresses);
preconditions.shouldBeFunction(cb); preconditions.shouldBeFunction(cb);
this.requestPost('/api/addrs/txs', { var qs = '?from=' + (from || 0);
if (to) qs += '&to=' + to;
this.requestPost('/api/addrs/txs' + qs, {
addrs: addresses.join(',') addrs: addresses.join(',')
}, function (err, res, txs) { }, function (err, res, txs) {
if (err || res.statusCode != 200) return cb(err || res); if (err || res.statusCode != 200) return cb(err || res);

View file

@ -3029,38 +3029,39 @@ Wallet.prototype.getTransactionHistory = function(opts, cb) {
} }
}; };
function paginate(list, currentPage, itemsPerPage) { function paginate(res, currentPage, itemsPerPage) {
var res = { if (!res) {
itemsPerPage: itemsPerPage || list.length, res = {
currentPage: currentPage || 1, totalItems: 0,
nbItems: list.length, items: [],
};
}; };
res.nbPages = res.itemsPerPage != 0 ? Math.ceil(res.nbItems / res.itemsPerPage) : 1;
var from = (res.currentPage - 1) * res.itemsPerPage; var r = {
var to = Math.min(from + res.itemsPerPage, res.nbItems); itemsPerPage: itemsPerPage || res.totalItems,
res.items = list.slice(from, to); currentPage: currentPage || 1,
nbItems: res.totalItems,
return res; items: res.items,
};
r.nbPages = r.itemsPerPage != 0 ? Math.ceil(r.nbItems / r.itemsPerPage) : 1;
return r;
}; };
if (addresses.length > 0) { if (addresses.length > 0) {
var addressesStr = _.pluck(addresses, 'addressStr'); var addressesStr = _.pluck(addresses, 'addressStr');
self.blockchain.getTransactions(addressesStr, function(err, txs) { var from = (opts.currentPage - 1) * opts.itemsPerPage;
var to = opts.currentPage * opts.itemsPerPage;
self.blockchain.getTransactions(addressesStr, from, to, function(err, res) {
if (err) return cb(err); if (err) return cb(err);
var history = _.map(txs, function(tx) { _.each(res.items, function(tx) {
decorateTx(tx); decorateTx(tx);
return tx;
});
history.sort(function(a, b) {
return (b.sentTs || b.minedTs) - (a.sentTs || a.minedTs);
}); });
return cb(null, paginate(history, opts.currentPage, opts.itemsPerPage)); return cb(null, paginate(res, opts.currentPage, opts.itemsPerPage));
}); });
} else { } else {
return paginate([], opts.currentPage, opts.ItemsPerPage); return cb(null, paginate(null, opts.currentPage, opts.itemsPerPage));
} }
}; };

View file

@ -1979,7 +1979,10 @@ describe('Wallet model', function() {
fees: 0.00000100 fees: 0.00000100
}]; }];
w.blockchain.getTransactions = sinon.stub().yields(null, txs); w.blockchain.getTransactions = sinon.stub().yields(null, {
items: txs,
totalItems: txs.length,
});
w.getAddressesInfo = sinon.stub().returns([{ w.getAddressesInfo = sinon.stub().returns([{
addressStr: 'addr_in_1' addressStr: 'addr_in_1'
}, { }, {
@ -2045,7 +2048,10 @@ describe('Wallet model', function() {
fees: 0.00000100 fees: 0.00000100
}]; }];
w.blockchain.getTransactions = sinon.stub().yields(null, txs); w.blockchain.getTransactions = sinon.stub().yields(null, {
items: txs.slice(2,3),
totalItems: txs.length,
});
w.getAddressesInfo = sinon.stub().returns([{ w.getAddressesInfo = sinon.stub().returns([{
addressStr: 'addr_in_1' addressStr: 'addr_in_1'
}, { }, {
@ -2068,7 +2074,11 @@ describe('Wallet model', function() {
}); });
it('should paginate empty list', function(done) { it('should paginate empty list', function(done) {
var w = cachedCreateW2(); var w = cachedCreateW2();
w.blockchain.getTransactions = sinon.stub().yields(null, []); var txs = [];
w.blockchain.getTransactions = sinon.stub().yields(null, {
items: txs,
totalItems: txs.length,
});
w.getAddressesInfo = sinon.stub().returns([{ w.getAddressesInfo = sinon.stub().returns([{
addressStr: 'addr_in_1' addressStr: 'addr_in_1'
}, { }, {
@ -2111,7 +2121,10 @@ describe('Wallet model', function() {
fees: 0.00000100 fees: 0.00000100
}]; }];
w.blockchain.getTransactions = sinon.stub().yields(null, txs); w.blockchain.getTransactions = sinon.stub().yields(null, {
items: txs,
totalItems: txs.length,
});
w.getAddressesInfo = sinon.stub().returns([{ w.getAddressesInfo = sinon.stub().returns([{
addressStr: 'addr_in_1' addressStr: 'addr_in_1'
}, { }, {
@ -2154,7 +2167,10 @@ describe('Wallet model', function() {
fees: 0.00000100 fees: 0.00000100
}]; }];
w.blockchain.getTransactions = sinon.stub().yields(null, txs); w.blockchain.getTransactions = sinon.stub().yields(null, {
items: txs,
totalItems: txs.length,
});
w.getAddressesInfo = sinon.stub().returns([{ w.getAddressesInfo = sinon.stub().returns([{
addressStr: 'addr_1' addressStr: 'addr_1'
}, { }, {
@ -2197,7 +2213,10 @@ describe('Wallet model', function() {
fees: 0.00000100 fees: 0.00000100
}]; }];
w.blockchain.getTransactions = sinon.stub().yields(null, txs); w.blockchain.getTransactions = sinon.stub().yields(null, {
items: txs,
totalItems: txs.length,
});
w.getAddressesInfo = sinon.stub().returns([{ w.getAddressesInfo = sinon.stub().returns([{
addressStr: 'addr_in_1' addressStr: 'addr_in_1'
}, { }, {
@ -2245,7 +2264,10 @@ describe('Wallet model', function() {
fees: 0.00000100 fees: 0.00000100
}]; }];
w.blockchain.getTransactions = sinon.stub().yields(null, txs); w.blockchain.getTransactions = sinon.stub().yields(null, {
items: txs,
totalItems: txs.length,
});
w.getAddressesInfo = sinon.stub().returns([{ w.getAddressesInfo = sinon.stub().returns([{
addressStr: 'addr_in_1' addressStr: 'addr_in_1'
}, { }, {

View file

@ -230,19 +230,19 @@ describe('Insight model', function() {
var blockchain = new Insight(FAKE_OPTS); var blockchain = new Insight(FAKE_OPTS);
sinon.stub(blockchain, "requestPost", function(url, data, cb) { sinon.stub(blockchain, "requestPost", function(url, data, cb) {
url.should.be.equal('/api/addrs/txs'); url.should.be.equal('/api/addrs/txs?from=0');
data.addrs.should.be.equal('2NATQJnaQe2CUKLyhL1zdNkttJM1dUH9HaM,2NE9hTCffeugo5gQtfB4owq98gyTeWC56yb'); data.addrs.should.be.equal('2NATQJnaQe2CUKLyhL1zdNkttJM1dUH9HaM,2NE9hTCffeugo5gQtfB4owq98gyTeWC56yb');
setTimeout(function() { setTimeout(function() {
var res = {statusCode: 200}; var res = {statusCode: 200};
var body = [1, 2, 3]; var body = { totalItems: 3, items: [1, 2, 3] };
cb(null, res, body); cb(null, res, body);
}, 0); }, 0);
}); });
var addresses = ['2NATQJnaQe2CUKLyhL1zdNkttJM1dUH9HaM', '2NE9hTCffeugo5gQtfB4owq98gyTeWC56yb']; var addresses = ['2NATQJnaQe2CUKLyhL1zdNkttJM1dUH9HaM', '2NE9hTCffeugo5gQtfB4owq98gyTeWC56yb'];
blockchain.getTransactions(addresses, function(err, txs) { blockchain.getTransactions(addresses, null, null, function(err, res) {
chai.expect(err).to.be.null; chai.expect(err).to.be.null;
txs.length.should.be.equal(3); res.items.length.should.be.equal(3);
done(); done();
}); });
}); });

View file

@ -10,8 +10,8 @@ FakeBlockchain.prototype.getTransaction = function(txid, cb) {
return cb(null, {txid: txid}); return cb(null, {txid: txid});
}; };
FakeBlockchain.prototype.getTransactions = function(addresses, cb) { FakeBlockchain.prototype.getTransactions = function(addresses, from, to, cb) {
cb(null, []); cb(null, { totalItems: 0, items: [] });
}; };
FakeBlockchain.prototype.subscribe = function() { FakeBlockchain.prototype.subscribe = function() {