Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions lib/services/address/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -427,7 +417,6 @@ AddressService.prototype._getAddressTxHistory = function(options, callback) {
};

AddressService.prototype._getAddressTxidHistory = function(address, options, callback) {

var self = this;

options = options || {};
Expand Down
101 changes: 100 additions & 1 deletion test/services/address/index.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down Expand Up @@ -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, []);
Expand All @@ -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() {
Expand Down