diff --git a/integration/regtest-node.js b/integration/regtest-node.js index 0c41524e..fedb0f84 100644 --- a/integration/regtest-node.js +++ b/integration/regtest-node.js @@ -518,6 +518,20 @@ describe('Node Functionality', function() { }); }); + it('total transaction count (sending and receiving)', function(done) { + var addresses = [ + address + ]; + var options = {}; + node.services.address.getAddressHistoryCount(addresses, options, function(err, count) { + if (err) { + throw err; + } + count.should.equal(6); + done(); + }); + }); + describe('Pagination', function() { it('from 0 to 1', function(done) { var options = { diff --git a/lib/services/address/history.js b/lib/services/address/history.js index c5e91c5a..18c97afd 100644 --- a/lib/services/address/history.js +++ b/lib/services/address/history.js @@ -27,6 +27,25 @@ function AddressHistory(args) { AddressHistory.MAX_ADDRESS_QUERIES = 20; +AddressHistory.prototype.getCount = function(callback) { + var self = this; + + async.eachLimit( + self.addresses, + AddressHistory.MAX_ADDRESS_QUERIES, + function(address, next) { + self.getTransactionInfo(address, next); + }, + function(err) { + if (err) { + return callback(err); + } + self.combineTransactionInfo(); + callback(null, self.combinedArray.length); + } + ); +}; + AddressHistory.prototype.get = function(callback) { var self = this; diff --git a/lib/services/address/index.js b/lib/services/address/index.js index 42cd6617..af54d3dc 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -731,6 +731,25 @@ AddressService.prototype.isSpent = function(output, queryMempool, callback) { }); }; +/** + * This will give the total count of transactions for a single or several addresses + * limited by a range of block heights. + * @param {Array} addresses - An array of addresses + * @param {Object} options - The options to limit the query + * @param {Number} [options.start] - The beginning block height (e.g. 1500 the most recent block height). + * @param {Number} [options.end] - The ending block height (e.g. 0 the older block height, results are inclusive). + * @param {Boolean} [options.queryMempool] - Include the mempool in the query + * @param {Function} callback + */ +AddressService.prototype.getAddressHistoryCount = function(addresses, options, callback) { + var history = new AddressHistory({ + node: this.node, + options: options, + addresses: addresses + }); + history.getCount(callback); +}; + /** * This will give the history for many addresses limited by a range of block heights (to limit * the database lookup times) and/or paginated to limit the results length. diff --git a/test/services/address/history.unit.js b/test/services/address/history.unit.js index 9c24e05a..c0ce648a 100644 --- a/test/services/address/history.unit.js +++ b/test/services/address/history.unit.js @@ -37,6 +37,43 @@ describe('Address Service History', function() { }); }); + describe('#getCount', function() { + it('will complete the async each limit series', function(done) { + var addresses = [address]; + var history = new AddressHistory({ + node: {}, + options: {}, + addresses: addresses + }); + history.getTransactionInfo = sinon.stub().callsArg(1); + history.combineTransactionInfo = sinon.stub(); + history.get(function(err, results) { + if (err) { + throw err; + } + history.getTransactionInfo.callCount.should.equal(1); + history.combineTransactionInfo.callCount.should.equal(1); + done(); + }); + }); + it('handle an error from getTransactionInfo', function(done) { + var addresses = [address]; + var history = new AddressHistory({ + node: {}, + options: {}, + addresses: addresses + }); + var expected = [{}]; + history.sortedArray = expected; + history.transactionInfo = [{}]; + history.getTransactionInfo = sinon.stub().callsArgWith(1, new Error('test')); + history.get(function(err) { + err.message.should.equal('test'); + done(); + }); + }); + }); + describe('#get', function() { it('will complete the async each limit series', function(done) { var addresses = [address]; diff --git a/test/services/address/index.unit.js b/test/services/address/index.unit.js index 4aac31b5..6d611b60 100644 --- a/test/services/address/index.unit.js +++ b/test/services/address/index.unit.js @@ -857,6 +857,25 @@ describe('Address Service', function() { }); }); + describe('#getAddressHistoryCount', function() { + it('will call getCount on address history instance', function(done) { + function TestAddressHistory(args) { + args.node.should.equal(mocknode); + args.addresses.should.deep.equal([]); + args.options.should.deep.equal({}); + } + TestAddressHistory.prototype.getCount = sinon.stub().callsArg(0); + var TestAddressService = proxyquire('../../../lib/services/address', { + './history': TestAddressHistory + }); + var am = new TestAddressService({node: mocknode}); + am.getAddressHistoryCount([], {}, function(err, history) { + TestAddressHistory.prototype.getCount.callCount.should.equal(1); + done(); + }); + }); + }); + describe('#getAddressHistory', function() { it('will call get on address history instance', function(done) { function TestAddressHistory(args) {