Skip to content

Commit 11612e0

Browse files
Merge pull request #547 from bitpay/bug/sort-by-hash
fix history sorting when paging results
2 parents 9db5f2b + 0c24271 commit 11612e0

File tree

2 files changed

+104
-16
lines changed

2 files changed

+104
-16
lines changed

lib/services/address/index.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ AddressService.prototype.getAddressHistory = function(addresses, options, callba
6666
}
6767

6868
async.eachLimit(addresses, 4, function(address, next) {
69-
7069
self._getAddressTxidHistory(address, options, next);
7170

7271
}, function(err) {
@@ -75,21 +74,12 @@ AddressService.prototype.getAddressHistory = function(addresses, options, callba
7574
return callback(err);
7675
}
7776

78-
var unique = {};
79-
var list = [];
80-
81-
for (let i = 0; i < options.txIdList.length; i++) {
82-
unique[options.txIdList[i].txid + options.txIdList[i].height] = options.txIdList[i];
83-
}
84-
85-
for (var prop in unique) {
86-
list.push(unique[prop]);
87-
}
88-
89-
options.txIdList = list.sort(function(a, b) {
90-
return b.height - a.height;
77+
var list = lodash.uniqBy(options.txIdList, function(x) {
78+
return x.txid + x.height;
9179
});
9280

81+
82+
options.txIdList = lodash.orderBy(list,['height','txid'], ['desc','asc']);
9383
self._getAddressTxHistory(options, function(err, txList) {
9484

9585
if (err) {
@@ -427,7 +417,6 @@ AddressService.prototype._getAddressTxHistory = function(options, callback) {
427417
};
428418

429419
AddressService.prototype._getAddressTxidHistory = function(address, options, callback) {
430-
431420
var self = this;
432421

433422
options = options || {};

test/services/address/index.unit.js

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var Encoding = require('../../../lib/services/address/encoding');
88
var Readable = require('stream').Readable;
99
var EventEmitter = require('events').EventEmitter;
1010
var bcoin = require('bcoin');
11+
var lodash = require('lodash');
1112

1213
describe('Address Service', function() {
1314

@@ -54,7 +55,7 @@ describe('Address Service', function() {
5455

5556
describe('#getAddressHistory', function() {
5657

57-
it('should get the address history', function(done) {
58+
it('should get the address history (null case)', function(done) {
5859

5960
sandbox.stub(addressService, '_getAddressTxidHistory').callsArgWith(2, null, null);
6061
sandbox.stub(addressService, '_getAddressTxHistory').callsArgWith(1, null, []);
@@ -74,6 +75,104 @@ describe('Address Service', function() {
7475
});
7576
});
7677

78+
it('should get the sorted address history', function(done) {
79+
80+
var old_getAddressTxidHistory = addressService._getAddressTxidHistory;
81+
addressService._getAddressTxidHistory = function(addr, options, cb) {
82+
options.txIdList = [
83+
{
84+
txid: "d",
85+
height: 10,
86+
},
87+
{
88+
txid: "c",
89+
height: 10,
90+
},
91+
{
92+
txid: "a",
93+
height: 101,
94+
},
95+
{
96+
txid: "b",
97+
height: 100,
98+
},
99+
];
100+
return cb();
101+
};
102+
103+
104+
var old_getAddressTxHistory = addressService._getAddressTxHistory;
105+
addressService._getAddressTxHistory = function(options, cb) {
106+
return cb(null, options.txIdList);
107+
};
108+
109+
addressService.getAddressHistory(['a', 'b', 'c'], { from: 12, to: 14 }, function(err, res) {
110+
111+
if (err) {
112+
return done(err);
113+
}
114+
115+
expect(res.totalCount).equal(4);
116+
expect(lodash.map(res.items,'txid')).to.be.deep.equal(['a','b','c','d']);
117+
118+
addressService._getAddressTxidHistory = old_getAddressTxHistory;
119+
addressService._getAddressTxHistory = old_getAddressTxHistory;
120+
done();
121+
});
122+
});
123+
124+
it('should remove duplicated items in history', function(done) {
125+
126+
var old_getAddressTxidHistory = addressService._getAddressTxidHistory;
127+
addressService._getAddressTxidHistory = function(addr, options, cb) {
128+
options.txIdList = [
129+
{
130+
txid: "b",
131+
height: 10,
132+
},
133+
{
134+
txid: "b",
135+
height: 10,
136+
},
137+
{
138+
txid: "d",
139+
height: 101,
140+
},
141+
{
142+
txid: "c",
143+
height: 100,
144+
},
145+
{
146+
txid: "d",
147+
height: 101,
148+
},
149+
];
150+
return cb();
151+
};
152+
153+
154+
var old_getAddressTxHistory = addressService._getAddressTxHistory;
155+
addressService._getAddressTxHistory = function(options, cb) {
156+
return cb(null, options.txIdList);
157+
};
158+
159+
addressService.getAddressHistory(['a', 'b', 'c'], { from: 12, to: 14 }, function(err, res) {
160+
161+
if (err) {
162+
return done(err);
163+
}
164+
165+
expect(res.totalCount).equal(3);
166+
expect(lodash.map(res.items,'txid')).to.be.deep.equal(['d','c','b']);
167+
168+
addressService._getAddressTxidHistory = old_getAddressTxHistory;
169+
addressService._getAddressTxHistory = old_getAddressTxHistory;
170+
done();
171+
});
172+
});
173+
174+
175+
77176
});
78177

79178
describe('#_getAddressTxidHistory', function() {

0 commit comments

Comments
 (0)