From c2eda9b3c29d3dd69cbb9c4db118a63d26da1428 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Tue, 12 Apr 2016 18:05:26 -0400 Subject: [PATCH] bitcoin: address history by height range --- lib/services/bitcoind.js | 40 +++++++++++++++++++++++--- regtest/node.js | 62 ++++++++++++++++++++-------------------- scripts/install | 2 +- 3 files changed, 68 insertions(+), 36 deletions(-) diff --git a/lib/services/bitcoind.js b/lib/services/bitcoind.js index be30eb02..fdc2a612 100644 --- a/lib/services/bitcoind.js +++ b/lib/services/bitcoind.js @@ -707,6 +707,21 @@ Bitcoin.prototype._getTxidsFromMempool = function(deltas) { return mempoolTxids; }; +Bitcoin.prototype._getHeightRangeQuery = function(options, clone) { + if (options.start >= 0 && options.end >=0) { + if (options.end > options.start) { + throw new TypeError('"end" is expected to be less than or equal to "start"'); + } + if (clone) { + // reverse start and end as the order in bitcore is most recent to less recent + clone.start = options.end; + clone.end = options.start; + } + return true; + } + return false; +}; + /** * Will get the txids for an address or multiple addresses * @param {String|Address|Array} addressArg - An address string, bitcore address, or array of addresses @@ -716,24 +731,41 @@ Bitcoin.prototype._getTxidsFromMempool = function(deltas) { Bitcoin.prototype.getAddressTxids = function(addressArg, options, callback) { var self = this; var queryMempool = _.isUndefined(options.queryMempool) ? true : options.queryMempool; + var rangeQuery = false; + try { + rangeQuery = self._getHeightRangeQuery(options); + } catch(err) { + return callback(err); + } + if (rangeQuery) { + queryMempool = false; + } var addresses = self._normalizeAddressArg(addressArg); var cacheKey = addresses.join(''); var mempoolTxids = []; var txids = self.txidsCache.get(cacheKey); function finish() { - if (txids) { + if (txids && !rangeQuery) { var allTxids = mempoolTxids.reverse().concat(txids); return setImmediate(function() { callback(null, allTxids); }); } else { - self.client.getAddressTxids({addresses: addresses}, function(err, response) { + var txidOpts = { + addresses: addresses + }; + if (rangeQuery) { + self._getHeightRangeQuery(options, txidOpts); + } + self.client.getAddressTxids(txidOpts, function(err, response) { if (err) { return callback(self._wrapRPCError(err)); } response.result.reverse(); - self.txidsCache.set(cacheKey, response.result); + if (!rangeQuery) { + self.txidsCache.set(cacheKey, response.result); + } var allTxids = mempoolTxids.reverse().concat(response.result); return callback(null, allTxids); }); @@ -895,7 +927,7 @@ Bitcoin.prototype.getAddressHistory = function(addressArg, options, callback) { var queryMempool = _.isUndefined(options.queryMempool) ? true : options.queryMempool; var addressStrings = this._getAddressStrings(addresses); - self.getAddressTxids(addresses, {}, function(err, txids) { + self.getAddressTxids(addresses, options, function(err, txids) { if (err) { return callback(err); } diff --git a/regtest/node.js b/regtest/node.js index ebb58cab..9e9e281f 100644 --- a/regtest/node.js +++ b/regtest/node.js @@ -483,7 +483,34 @@ describe('Node Functionality', function() { }); }); - it.skip('five addresses (limited by height)', function(done) { + it('five addresses (limited by height)', function(done) { + var addresses = [ + address2, + address3, + address4, + address5, + address6 + ]; + var options = { + start: 158, + end: 157 + }; + node.getAddressHistory(addresses, options, function(err, results) { + if (err) { + throw err; + } + results.totalCount.should.equal(2); + var history = results.items; + history.length.should.equal(2); + history[0].height.should.equal(158); + history[0].confirmations.should.equal(2); + history[1].height.should.equal(157); + should.exist(history[1].addresses[address3]); + done(); + }); + }); + + it('five addresses (limited by height 155 to 154)', function(done) { var addresses = [ address2, address3, @@ -503,39 +530,12 @@ describe('Node Functionality', function() { var history = results.items; history.length.should.equal(2); history[0].height.should.equal(157); - history[0].confirmations.should.equal(1); history[1].height.should.equal(156); - should.exist(history[1].addresses[address4]); done(); }); }); - it.skip('five addresses (limited by height 155 to 154)', function(done) { - var addresses = [ - address2, - address3, - address4, - address5, - address6 - ]; - var options = { - start: 155, - end: 154 - }; - node.getAddressHistory(addresses, options, function(err, results) { - if (err) { - throw err; - } - results.totalCount.should.equal(2); - var history = results.items; - history.length.should.equal(2); - history[0].height.should.equal(155); - history[1].height.should.equal(154); - done(); - }); - }); - - it.skip('five addresses (paginated by index)', function(done) { + it('five addresses (paginated by index)', function(done) { var addresses = [ address2, address3, @@ -554,9 +554,9 @@ describe('Node Functionality', function() { results.totalCount.should.equal(4); var history = results.items; history.length.should.equal(3); - history[0].height.should.equal(157); + history[0].height.should.equal(159); history[0].confirmations.should.equal(1); - history[1].height.should.equal(156); + history[1].height.should.equal(158); should.exist(history[1].addresses[address4]); done(); }); diff --git a/scripts/install b/scripts/install index c0e18cb4..f1f7bd9d 100755 --- a/scripts/install +++ b/scripts/install @@ -5,7 +5,7 @@ platform=`uname -a | awk '{print tolower($1)}'` arch=`uname -m` version="0.12.0" url="https://github.com/braydonf/bitcoin/releases/download" -tag="v0.12.0-bitcore-beta2" +tag="v0.12.0-bitcore-beta3" cd "${root_dir}/bin"