From 4d30c9428a84e302d3d00482bd603ba8606f80aa Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 1 Apr 2016 01:12:22 -0700 Subject: [PATCH] fix binary index. --- lib/bcoin/mempool.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/bcoin/mempool.js b/lib/bcoin/mempool.js index f3a11d34..44de457f 100644 --- a/lib/bcoin/mempool.js +++ b/lib/bcoin/mempool.js @@ -1429,7 +1429,12 @@ BinaryIndex.prototype.insert = function insert(tx) { }; BinaryIndex.prototype.remove = function remove(tx) { - var index = binarySearch(this.data, tx.hash()); + var ps = new Buffer(4); + var index; + + utils.writeU32BE(ps, tx.ps, 0); + + index = binarySearch(this.index, ps); if (index !== -1) { this.index.splice(index, 1); @@ -1439,12 +1444,12 @@ BinaryIndex.prototype.remove = function remove(tx) { BinaryIndex.prototype.range = function range(start, end) { var hashes = []; - var s = new Buffer(4); + var ts = new Buffer(4); var i, ps; - utils.writeU32BE(s, start, 0); + utils.writeU32BE(ts, start, 0); - i = binarySearch(this.index, s, true); + i = binarySearch(this.index, ts, true); for (; i < this.index.length; i++) { ps = utils.readU32BE(this.index[i], 0); @@ -1460,14 +1465,17 @@ BinaryIndex.prototype.range = function range(start, end) { * Helpers */ -function binarySearch(items, key, insert) { +function binarySearch(items, key, insert, compare) { var start = 0; var end = items.length - 1; var pos, cmp; + if (!compare) + compare = utils.cmp; + while (start <= end) { pos = (start + end) >>> 1; - cmp = utils.cmp(items[pos], key); + cmp = compare(items[pos], key); if (cmp === 0) return pos;