From 2ec8d1f44789b5c016e7bfc262db29324f3ee0f2 Mon Sep 17 00:00:00 2001 From: Patrick Nagurny Date: Thu, 17 Sep 2015 10:41:35 -0400 Subject: [PATCH] use faster getAddressSummary() --- lib/addresses.js | 86 ++++++++++++++++------------------------------- test/addresses.js | 17 ++++++++-- 2 files changed, 44 insertions(+), 59 deletions(-) diff --git a/lib/addresses.js b/lib/addresses.js index 0d5e965..9c33e72 100644 --- a/lib/addresses.js +++ b/lib/addresses.js @@ -10,7 +10,11 @@ function AddressController(node) { } AddressController.prototype.show = function(req, res) { - this.getAddressHistory(req.addr, function(err, data) { + var options = { + noTxList: parseInt(req.query.noTxList) + }; + + this.getAddressSummary(req.addr, options, function(err, data) { if(err) { return common.handleErrors(err, res); } @@ -20,23 +24,23 @@ AddressController.prototype.show = function(req, res) { }; AddressController.prototype.balance = function(req, res) { - this.addressHistorySubQuery(req, res, 'balanceSat'); + this.addressSummarySubQuery(req, res, 'balanceSat'); }; AddressController.prototype.totalReceived = function(req, res) { - this.addressHistorySubQuery(req, res, 'totalReceivedSat'); + this.addressSummarySubQuery(req, res, 'totalReceivedSat'); }; AddressController.prototype.totalSent = function(req, res) { - this.addressHistorySubQuery(req, res, 'totalSentSat'); + this.addressSummarySubQuery(req, res, 'totalSentSat'); }; AddressController.prototype.unconfirmedBalance = function(req, res) { - this.addressHistorySubQuery(req, res, 'unconfirmedBalanceSat'); + this.addressSummarySubQuery(req, res, 'unconfirmedBalanceSat'); }; -AddressController.prototype.addressHistorySubQuery = function(req, res, param) { - this.getAddressHistory(req.addr, function(err, data) { +AddressController.prototype.addressSummarySubQuery = function(req, res, param) { + this.getAddressSummary(req.addr, {}, function(err, data) { if(err) { return common.handleErrors(err, res); } @@ -45,15 +49,30 @@ AddressController.prototype.addressHistorySubQuery = function(req, res, param) { }); }; -AddressController.prototype.getAddressHistory = function(address, callback) { +AddressController.prototype.getAddressSummary = function(address, options, callback) { var self = this; - this.node.getAddressHistory(address, {}, function(err, result) { + this.node.getAddressSummary(address, options, function(err, summary) { if(err) { return callback(err); } - callback(null, self.transformAddressHistory(result.items, address)); + var transformed = { + addrStr: address, + balance: summary.balance / 1e8, + balanceSat: summary.balance, + totalReceived: summary.totalReceived / 1e8, + totalReceivedSat: summary.totalReceived, + totalSent: summary.totalSpent / 1e8, + totalSentSat: summary.totalSpent, + unconfirmedBalance: summary.unconfirmedBalance / 1e8, + unconfirmedBalanceSat: summary.unconfirmedBalance, + unconfirmedTxApperances: summary.unconfirmedAppearances, // misspelling - ew + txApperances: summary.appearances, // yuck + transactions: summary.txids + }; + + callback(null, transformed); }); }; @@ -94,53 +113,6 @@ AddressController.prototype.check = function(req, res, next, addresses) { next(); }; -AddressController.prototype.transformAddressHistory = function(txinfos, address) { - var transactions = txinfos.map(function(info) { - return info.tx.hash; - }).filter(function(value, index, self) { - return self.indexOf(value) === index; - }); - - var balance = 0; - var appearances = 0; - var totalReceived = 0; - var totalSent = 0; - var unconfirmedBalance = 0; - var unconfirmedAppearances = 0; - - for(var i = 0; i < txinfos.length; i++) { - if(txinfos[i].satoshis > 0) { - totalReceived += txinfos[i].satoshis; - } else { - totalSent += -txinfos[i].satoshis; - } - - if(txinfos[i].confirmations) { - balance += txinfos[i].satoshis; - unconfirmedBalance += txinfos[i].satoshis; - appearances++; - } else { - unconfirmedBalance += txinfos[i].satoshis; - unconfirmedAppearances++; - } - } - - return { - addrStr: address, - balance: balance / 1e8, - balanceSat: balance, - totalReceived: totalReceived / 1e8, - totalReceivedSat: totalReceived, - totalSent: totalSent / 1e8, - totalSentSat: totalSent, - unconfirmedBalance: unconfirmedBalance / 1e8, - unconfirmedBalanceSat: unconfirmedBalance, - unconfirmedTxApperances: unconfirmedAppearances, // misspelling - ew - txApperances: appearances, // yuck - transactions: transactions - }; -}; - AddressController.prototype.utxo = function(req, res) { var self = this; diff --git a/test/addresses.js b/test/addresses.js index 0be2c0c..8bfa00b 100644 --- a/test/addresses.js +++ b/test/addresses.js @@ -171,14 +171,27 @@ var utxos = [ ]; describe('Addresses', function() { + var summary = { + balance: 0, + totalReceived: 2782729129, + totalSpent: 2782729129, + unconfirmedBalance: 0, + appearances: 2, + unconfirmedAppearances: 0, + txids: [ + 'bb0ec3b96209fac9529570ea6f83a86af2cceedde4aaf2bfcc4796680d23f1c7', + '01f700df84c466f2a389440e5eeacdc47d04f380c39e5d19dce2ce91a11ecba3' + ] + }; describe('/addr/:addr', function() { var node = { - getAddressHistory: sinon.stub().callsArgWith(2, null, txinfos) + getAddressSummary: sinon.stub().callsArgWith(2, null, summary) }; var addresses = new AddressController(node); var req = { - addr: 'mkPvAKZ2rar6qeG3KjBtJHHMSP1wFZH7Er' + addr: 'mkPvAKZ2rar6qeG3KjBtJHHMSP1wFZH7Er', + query: {} }; it('should have correct data', function(done) {