diff --git a/lib/services/address/index.js b/lib/services/address/index.js index 71901e4c7..97144473b 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -66,7 +66,6 @@ AddressService.prototype.getAddressHistory = function(addresses, options, callba } async.eachLimit(addresses, 4, function(address, next) { - self._getAddressTxidHistory(address, options, next); }, function(err) { @@ -75,21 +74,12 @@ AddressService.prototype.getAddressHistory = function(addresses, options, callba return callback(err); } - var unique = {}; - var list = []; - - for (let i = 0; i < options.txIdList.length; i++) { - unique[options.txIdList[i].txid + options.txIdList[i].height] = options.txIdList[i]; - } - - for (var prop in unique) { - list.push(unique[prop]); - } - - options.txIdList = list.sort(function(a, b) { - return b.height - a.height; + var list = lodash.uniqBy(options.txIdList, function(x) { + return x.txid + x.height; }); + + options.txIdList = lodash.orderBy(list,['height','txid'], ['desc','asc']); self._getAddressTxHistory(options, function(err, txList) { if (err) { @@ -427,7 +417,6 @@ AddressService.prototype._getAddressTxHistory = function(options, callback) { }; AddressService.prototype._getAddressTxidHistory = function(address, options, callback) { - var self = this; options = options || {}; diff --git a/test/services/address/index.unit.js b/test/services/address/index.unit.js index 1fd2ddc19..5ded3800b 100644 --- a/test/services/address/index.unit.js +++ b/test/services/address/index.unit.js @@ -8,6 +8,7 @@ var Encoding = require('../../../lib/services/address/encoding'); var Readable = require('stream').Readable; var EventEmitter = require('events').EventEmitter; var bcoin = require('bcoin'); +var lodash = require('lodash'); describe('Address Service', function() { @@ -54,7 +55,7 @@ describe('Address Service', function() { describe('#getAddressHistory', function() { - it('should get the address history', function(done) { + it('should get the address history (null case)', function(done) { sandbox.stub(addressService, '_getAddressTxidHistory').callsArgWith(2, null, null); sandbox.stub(addressService, '_getAddressTxHistory').callsArgWith(1, null, []); @@ -74,6 +75,104 @@ describe('Address Service', function() { }); }); + it('should get the sorted address history', function(done) { + + var old_getAddressTxidHistory = addressService._getAddressTxidHistory; + addressService._getAddressTxidHistory = function(addr, options, cb) { + options.txIdList = [ + { + txid: "d", + height: 10, + }, + { + txid: "c", + height: 10, + }, + { + txid: "a", + height: 101, + }, + { + txid: "b", + height: 100, + }, + ]; + return cb(); + }; + + + var old_getAddressTxHistory = addressService._getAddressTxHistory; + addressService._getAddressTxHistory = function(options, cb) { + return cb(null, options.txIdList); + }; + + addressService.getAddressHistory(['a', 'b', 'c'], { from: 12, to: 14 }, function(err, res) { + + if (err) { + return done(err); + } + + expect(res.totalCount).equal(4); + expect(lodash.map(res.items,'txid')).to.be.deep.equal(['a','b','c','d']); + + addressService._getAddressTxidHistory = old_getAddressTxHistory; + addressService._getAddressTxHistory = old_getAddressTxHistory; + done(); + }); + }); + + it('should remove duplicated items in history', function(done) { + + var old_getAddressTxidHistory = addressService._getAddressTxidHistory; + addressService._getAddressTxidHistory = function(addr, options, cb) { + options.txIdList = [ + { + txid: "b", + height: 10, + }, + { + txid: "b", + height: 10, + }, + { + txid: "d", + height: 101, + }, + { + txid: "c", + height: 100, + }, + { + txid: "d", + height: 101, + }, + ]; + return cb(); + }; + + + var old_getAddressTxHistory = addressService._getAddressTxHistory; + addressService._getAddressTxHistory = function(options, cb) { + return cb(null, options.txIdList); + }; + + addressService.getAddressHistory(['a', 'b', 'c'], { from: 12, to: 14 }, function(err, res) { + + if (err) { + return done(err); + } + + expect(res.totalCount).equal(3); + expect(lodash.map(res.items,'txid')).to.be.deep.equal(['d','c','b']); + + addressService._getAddressTxidHistory = old_getAddressTxHistory; + addressService._getAddressTxHistory = old_getAddressTxHistory; + done(); + }); + }); + + + }); describe('#_getAddressTxidHistory', function() {