From 9485398879ddab1931dff1d05e5190625720eb82 Mon Sep 17 00:00:00 2001 From: Patrick Nagurny Date: Mon, 14 Sep 2015 18:00:32 -0400 Subject: [PATCH] address history pagination --- lib/addresses.js | 30 ++++---- lib/transactions.js | 19 +++-- test/addresses.js | 175 ++++++++++++++++++++++--------------------- test/transactions.js | 7 +- 4 files changed, 122 insertions(+), 109 deletions(-) diff --git a/lib/addresses.js b/lib/addresses.js index 2b1442a..0d5e965 100644 --- a/lib/addresses.js +++ b/lib/addresses.js @@ -48,12 +48,12 @@ AddressController.prototype.addressHistorySubQuery = function(req, res, param) { AddressController.prototype.getAddressHistory = function(address, callback) { var self = this; - this.node.getAddressHistory(address, true, function(err, txinfos) { + this.node.getAddressHistory(address, {}, function(err, result) { if(err) { return callback(err); } - callback(null, self.transformAddressHistory(txinfos, address)); + callback(null, self.transformAddressHistory(result.items, address)); }); }; @@ -184,12 +184,23 @@ AddressController.prototype.transformUtxo = function(utxo) { AddressController.prototype.multitxs = function(req, res, next) { var self = this; - this.node.getAddressHistory(req.addrs, true, function(err, txinfos) { + var options = { + from: req.query.from || req.body.from || 0 + }; + + options.to = req.query.to || req.body.to || options.from + 10; + + self.node.getAddressHistory(req.addrs, options, function(err, result) { if(err) { return common.handleErrors(err, res); } - res.jsonp(self.transformAddressHistoryForMultiTxs(txinfos)); + res.jsonp({ + totalItems: result.totalCount, + from: options.from, + to: Math.min(options.to, result.totalCount), + items: self.transformAddressHistoryForMultiTxs(result.items) + }); }); }; @@ -202,16 +213,9 @@ AddressController.prototype.transformAddressHistoryForMultiTxs = function(txinfo return self.indexOf(value) === index; }).map(function(tx) { return self.txController.transformTransaction(tx); - }).reverse(); + }); - var transformed = { - totalItems: items.length, - from: 0, - to: items.length, - items: items - }; - - return transformed; + return items; }; diff --git a/lib/transactions.js b/lib/transactions.js index 0c15a01..fb1ed05 100644 --- a/lib/transactions.js +++ b/lib/transactions.js @@ -190,7 +190,7 @@ TxController.prototype.list = function(req, res) { var blockHash = req.query.block; var address = req.query.address; - var page = req.query.pageNum; + var page = parseInt(req.query.pageNum) || 0; var pageLength = 10; var pagesTotal = 1; @@ -235,28 +235,27 @@ TxController.prototype.list = function(req, res) { }); }); } else if(address) { - self.node.getAddressHistory(address, true, function(err, txinfos) { + var options = { + from: page * pageLength, + to: (page + 1) * pageLength + }; + + self.node.getAddressHistory(address, options, function(err, result) { if(err) { return common.handleErrors(err, res); } - var txs = txinfos.map(function(info) { + var txs = result.items.map(function(info) { return info.tx; }).filter(function(value, index, self) { return self.indexOf(value) === index; }); - var totalTxs = txs.length; - - if(page) { - txs = txs.splice(page * pageLength, pageLength); - pagesTotal = Math.ceil(totalTxs / pageLength); - } var transformed = txs.map(self.transformTransaction.bind(self)); res.jsonp({ - pagesTotal: pagesTotal, + pagesTotal: Math.ceil(result.totalCount / pageLength), txs: transformed }); }); diff --git a/test/addresses.js b/test/addresses.js index 79ac3d6..0be2c0c 100644 --- a/test/addresses.js +++ b/test/addresses.js @@ -5,88 +5,91 @@ var AddressController = require('../lib/addresses'); var _ = require('lodash'); var bitcore = require('bitcore'); -var txinfos = [ - { - "address": "mkPvAKZ2rar6qeG3KjBtJHHMSP1wFZH7Er", - "satoshis": 2782729129, - "height": 534105, - "confirmations": 123, - "timestamp": 1441068774, - "fees": 35436, - "outputIndexes": [ - 0 - ], - "inputIndexes": [], - "tx": { - "hash": "bb0ec3b96209fac9529570ea6f83a86af2cceedde4aaf2bfcc4796680d23f1c7", - "version": 1, - "inputs": [ - { - "prevTxId": "ea5e5bafbf29cdf6f6097ab344128477e67889d4d6203cb43594836daa6cc425", - "outputIndex": 1, - "sequenceNumber": 4294967294, - "script": "483045022100f4d169783bef70e3943d2a617cce55d9fe4e33fc6f9880b8277265e2f619a97002201238648abcdf52960500664e969046d41755f7fc371971ebc78002fc418465a6012103acdcd31d51272403ce0829447e59e2ac9e08ed0bf92011cbf7420addf24534e6", - "scriptString": "72 0x3045022100f4d169783bef70e3943d2a617cce55d9fe4e33fc6f9880b8277265e2f619a97002201238648abcdf52960500664e969046d41755f7fc371971ebc78002fc418465a601 33 0x03acdcd31d51272403ce0829447e59e2ac9e08ed0bf92011cbf7420addf24534e6", - "output": { - "satoshis": 2796764565, - "script": "76a91488b1fe8aec5ae4358a11447a2f22b2781faedb9b88ac" +var txinfos = { + totalCount: 2, + items: [ + { + "address": "mkPvAKZ2rar6qeG3KjBtJHHMSP1wFZH7Er", + "satoshis": 2782729129, + "height": 534105, + "confirmations": 123, + "timestamp": 1441068774, + "fees": 35436, + "outputIndexes": [ + 0 + ], + "inputIndexes": [], + "tx": { + "hash": "bb0ec3b96209fac9529570ea6f83a86af2cceedde4aaf2bfcc4796680d23f1c7", + "version": 1, + "inputs": [ + { + "prevTxId": "ea5e5bafbf29cdf6f6097ab344128477e67889d4d6203cb43594836daa6cc425", + "outputIndex": 1, + "sequenceNumber": 4294967294, + "script": "483045022100f4d169783bef70e3943d2a617cce55d9fe4e33fc6f9880b8277265e2f619a97002201238648abcdf52960500664e969046d41755f7fc371971ebc78002fc418465a6012103acdcd31d51272403ce0829447e59e2ac9e08ed0bf92011cbf7420addf24534e6", + "scriptString": "72 0x3045022100f4d169783bef70e3943d2a617cce55d9fe4e33fc6f9880b8277265e2f619a97002201238648abcdf52960500664e969046d41755f7fc371971ebc78002fc418465a601 33 0x03acdcd31d51272403ce0829447e59e2ac9e08ed0bf92011cbf7420addf24534e6", + "output": { + "satoshis": 2796764565, + "script": "76a91488b1fe8aec5ae4358a11447a2f22b2781faedb9b88ac" + } } - } - ], - "outputs": [ - { - "satoshis": 2782729129, - "script": "76a9143583efb5e64a4668c6c54bb5fcc30af4417b4f2d88ac" - }, - { - "satoshis": 14000000, - "script": "76a9149713201957f42379e574d7c70d506ee49c2c8ad688ac" - } - ], - "nLockTime": 534089 - } - }, - { - "address": "mkPvAKZ2rar6qeG3KjBtJHHMSP1wFZH7Er", - "satoshis": -2782729129, - "height": 534110, - "confirmations": 118, - "timestamp": 1441072817, - "fees": 35437, - "outputIndexes": [], - "inputIndexes": [ - "0" - ], - "tx": { - "hash": "01f700df84c466f2a389440e5eeacdc47d04f380c39e5d19dce2ce91a11ecba3", - "version": 1, - "inputs": [ - { - "prevTxId": "bb0ec3b96209fac9529570ea6f83a86af2cceedde4aaf2bfcc4796680d23f1c7", - "outputIndex": 0, - "sequenceNumber": 4294967294, - "script": "47304402201ee69281db6b95bb1aa3074059b67581635b719e8f64e4c2694db6ec56ad9447022011e91528996ea459b1fb2c0b59363fecbefe4bc2ca90f7b2382bdaa358f2d5640121034cc057b12a68ee79df998004b9a1341bbb18b17ea4939bebaa3bac001e940f24", - "scriptString": "71 0x304402201ee69281db6b95bb1aa3074059b67581635b719e8f64e4c2694db6ec56ad9447022011e91528996ea459b1fb2c0b59363fecbefe4bc2ca90f7b2382bdaa358f2d56401 33 0x034cc057b12a68ee79df998004b9a1341bbb18b17ea4939bebaa3bac001e940f24", - "output": { + ], + "outputs": [ + { "satoshis": 2782729129, "script": "76a9143583efb5e64a4668c6c54bb5fcc30af4417b4f2d88ac" + }, + { + "satoshis": 14000000, + "script": "76a9149713201957f42379e574d7c70d506ee49c2c8ad688ac" } - } + ], + "nLockTime": 534089 + } + }, + { + "address": "mkPvAKZ2rar6qeG3KjBtJHHMSP1wFZH7Er", + "satoshis": -2782729129, + "height": 534110, + "confirmations": 118, + "timestamp": 1441072817, + "fees": 35437, + "outputIndexes": [], + "inputIndexes": [ + "0" ], - "outputs": [ - { - "satoshis": 2764693692, - "script": "76a91456e446bc3489543d8324c6d0271524c0bd0506dd88ac" - }, - { - "satoshis": 18000000, - "script": "76a914011d2963b619186a318f768dddfd98cd553912a088ac" - } - ], - "nLockTime": 534099 + "tx": { + "hash": "01f700df84c466f2a389440e5eeacdc47d04f380c39e5d19dce2ce91a11ecba3", + "version": 1, + "inputs": [ + { + "prevTxId": "bb0ec3b96209fac9529570ea6f83a86af2cceedde4aaf2bfcc4796680d23f1c7", + "outputIndex": 0, + "sequenceNumber": 4294967294, + "script": "47304402201ee69281db6b95bb1aa3074059b67581635b719e8f64e4c2694db6ec56ad9447022011e91528996ea459b1fb2c0b59363fecbefe4bc2ca90f7b2382bdaa358f2d5640121034cc057b12a68ee79df998004b9a1341bbb18b17ea4939bebaa3bac001e940f24", + "scriptString": "71 0x304402201ee69281db6b95bb1aa3074059b67581635b719e8f64e4c2694db6ec56ad9447022011e91528996ea459b1fb2c0b59363fecbefe4bc2ca90f7b2382bdaa358f2d56401 33 0x034cc057b12a68ee79df998004b9a1341bbb18b17ea4939bebaa3bac001e940f24", + "output": { + "satoshis": 2782729129, + "script": "76a9143583efb5e64a4668c6c54bb5fcc30af4417b4f2d88ac" + } + } + ], + "outputs": [ + { + "satoshis": 2764693692, + "script": "76a91456e446bc3489543d8324c6d0271524c0bd0506dd88ac" + }, + { + "satoshis": 18000000, + "script": "76a914011d2963b619186a318f768dddfd98cd553912a088ac" + } + ], + "nLockTime": 534099 + } } - } -]; + ] +}; var tx = bitcore.Transaction().fromObject({ "hash": "63b68becb0e514b32317f4b29a5cf0627d4087e54ac17f686fcb1d9a27680f73", @@ -135,14 +138,14 @@ var tx = bitcore.Transaction().fromObject({ tx.__height = 534181; tx.__timestamp = 1441116143; tx.__blockHash = '0000000000000041ddc94ecf4f86a456a83b2e320c36c6f0c13ff92c7e75f013'; -var txinfos2 = [ - { - tx: tx - }, - { - tx: tx - } -]; +var txinfos2 = { + totalCount: 1, + items: [ + { + tx: tx + } + ] +}; var utxos = [ { @@ -512,7 +515,9 @@ describe('Addresses', function() { var addresses = new AddressController(node); var req = { - addrs: 'mzkD4nmQ8ixqxySdBgsXTpgvAMK5iRZpNK,moZY18rGNmh4YCPeugtGW46AkkWMQttBUD' + addrs: 'mzkD4nmQ8ixqxySdBgsXTpgvAMK5iRZpNK,moZY18rGNmh4YCPeugtGW46AkkWMQttBUD', + query: {}, + body: {} }; var res = { diff --git a/test/transactions.js b/test/transactions.js index 2e27ca9..ef6abe3 100644 --- a/test/transactions.js +++ b/test/transactions.js @@ -613,6 +613,11 @@ describe('Transactions', function() { } ]; + var historyResult = { + totalCount: txinfos.length, + items: txinfos + }; + txinfos[0].tx.__blockHash = '00000000000001001aba15de213648f370607fb048288dd27b96f7e833a73520'; txinfos[0].tx.__timestamp = 1441068774; txinfos[0].tx.__height = 534105; @@ -622,7 +627,7 @@ describe('Transactions', function() { txinfos[1].tx.__height = 534110; var node = { - getAddressHistory: sinon.stub().callsArgWith(2, null, txinfos), + getAddressHistory: sinon.stub().callsArgWith(2, null, historyResult), services: { db: { tip: {