bitcoin: address history by height range

This commit is contained in:
Braydon Fuller 2016-04-12 18:05:26 -04:00
parent 1d358a6994
commit c2eda9b3c2
3 changed files with 68 additions and 36 deletions

View File

@ -707,6 +707,21 @@ Bitcoin.prototype._getTxidsFromMempool = function(deltas) {
return mempoolTxids; 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 * Will get the txids for an address or multiple addresses
* @param {String|Address|Array} addressArg - An address string, bitcore address, or array of 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) { Bitcoin.prototype.getAddressTxids = function(addressArg, options, callback) {
var self = this; var self = this;
var queryMempool = _.isUndefined(options.queryMempool) ? true : options.queryMempool; 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 addresses = self._normalizeAddressArg(addressArg);
var cacheKey = addresses.join(''); var cacheKey = addresses.join('');
var mempoolTxids = []; var mempoolTxids = [];
var txids = self.txidsCache.get(cacheKey); var txids = self.txidsCache.get(cacheKey);
function finish() { function finish() {
if (txids) { if (txids && !rangeQuery) {
var allTxids = mempoolTxids.reverse().concat(txids); var allTxids = mempoolTxids.reverse().concat(txids);
return setImmediate(function() { return setImmediate(function() {
callback(null, allTxids); callback(null, allTxids);
}); });
} else { } 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) { if (err) {
return callback(self._wrapRPCError(err)); return callback(self._wrapRPCError(err));
} }
response.result.reverse(); response.result.reverse();
self.txidsCache.set(cacheKey, response.result); if (!rangeQuery) {
self.txidsCache.set(cacheKey, response.result);
}
var allTxids = mempoolTxids.reverse().concat(response.result); var allTxids = mempoolTxids.reverse().concat(response.result);
return callback(null, allTxids); return callback(null, allTxids);
}); });
@ -895,7 +927,7 @@ Bitcoin.prototype.getAddressHistory = function(addressArg, options, callback) {
var queryMempool = _.isUndefined(options.queryMempool) ? true : options.queryMempool; var queryMempool = _.isUndefined(options.queryMempool) ? true : options.queryMempool;
var addressStrings = this._getAddressStrings(addresses); var addressStrings = this._getAddressStrings(addresses);
self.getAddressTxids(addresses, {}, function(err, txids) { self.getAddressTxids(addresses, options, function(err, txids) {
if (err) { if (err) {
return callback(err); return callback(err);
} }

View File

@ -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 = [ var addresses = [
address2, address2,
address3, address3,
@ -503,39 +530,12 @@ describe('Node Functionality', function() {
var history = results.items; var history = results.items;
history.length.should.equal(2); history.length.should.equal(2);
history[0].height.should.equal(157); history[0].height.should.equal(157);
history[0].confirmations.should.equal(1);
history[1].height.should.equal(156); history[1].height.should.equal(156);
should.exist(history[1].addresses[address4]);
done(); done();
}); });
}); });
it.skip('five addresses (limited by height 155 to 154)', function(done) { it('five addresses (paginated by index)', 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) {
var addresses = [ var addresses = [
address2, address2,
address3, address3,
@ -554,9 +554,9 @@ describe('Node Functionality', function() {
results.totalCount.should.equal(4); results.totalCount.should.equal(4);
var history = results.items; var history = results.items;
history.length.should.equal(3); history.length.should.equal(3);
history[0].height.should.equal(157); history[0].height.should.equal(159);
history[0].confirmations.should.equal(1); history[0].confirmations.should.equal(1);
history[1].height.should.equal(156); history[1].height.should.equal(158);
should.exist(history[1].addresses[address4]); should.exist(history[1].addresses[address4]);
done(); done();
}); });

View File

@ -5,7 +5,7 @@ platform=`uname -a | awk '{print tolower($1)}'`
arch=`uname -m` arch=`uname -m`
version="0.12.0" version="0.12.0"
url="https://github.com/braydonf/bitcoin/releases/download" url="https://github.com/braydonf/bitcoin/releases/download"
tag="v0.12.0-bitcore-beta2" tag="v0.12.0-bitcore-beta3"
cd "${root_dir}/bin" cd "${root_dir}/bin"