diff --git a/docs/services/db.md b/docs/services/db.md index aafb7e63..5c7e31c0 100644 --- a/docs/services/db.md +++ b/docs/services/db.md @@ -36,10 +36,10 @@ node.getBlock(blockHash, function(err, block) { Get Block Hashes by Timestamp Range ```js -var time1 = 1441911000; // Notice time is in seconds not milliseconds -var time2 = 1441914000; +var newest = 1441914000; // Notice time is in seconds not milliseconds +var oldest = 1441911000; -node.getBlockHashesByTimestamp(time1, time2, function(err, hashes) { +node.getBlockHashesByTimestamp(newest, oldest, function(err, hashes) { //... }); ``` diff --git a/lib/services/db.js b/lib/services/db.js index 7e90b870..6e4f8b93 100644 --- a/lib/services/db.js +++ b/lib/services/db.js @@ -201,17 +201,18 @@ DB.prototype.getBlock = function(hash, callback) { /** * get block hashes between two timestamps - * @param {Number} start - first timestamp, in seconds, inclusive - * @param {Number} end - second timestamp, in seconds, inclusive + * @param {Number} high - high timestamp, in seconds, inclusive + * @param {Number} low - low timestamp, in seconds, inclusive * @param {Function} callback */ -DB.prototype.getBlockHashesByTimestamp = function(start, end, callback) { +DB.prototype.getBlockHashesByTimestamp = function(high, low, callback) { var self = this; var hashes = []; var stream = this.store.createReadStream({ - gte: this._encodeBlockIndexKey(start), - lte: this._encodeBlockIndexKey(end), + gte: this._encodeBlockIndexKey(low), + lte: this._encodeBlockIndexKey(high), + reverse: true, valueEncoding: 'binary', keyEncoding: 'binary' }); diff --git a/test/services/db.unit.js b/test/services/db.unit.js index aa122fb9..82148b5d 100644 --- a/test/services/db.unit.js +++ b/test/services/db.unit.js @@ -387,22 +387,22 @@ describe('DB Service', function() { timestamp: 1441913112 }; - db.getBlockHashesByTimestamp(1441911000, 1441914000, function(err, hashes) { + db.getBlockHashesByTimestamp(1441914000, 1441911000, function(err, hashes) { should.not.exist(err); - hashes.should.deep.equal([block1.hash, block2.hash]); + hashes.should.deep.equal([block2.hash, block1.hash]); done(); }); - readStream.emit('data', { - key: db._encodeBlockIndexKey(block1.timestamp), - value: db._encodeBlockIndexValue(block1.hash) - }); - readStream.emit('data', { key: db._encodeBlockIndexKey(block2.timestamp), value: db._encodeBlockIndexValue(block2.hash) }); + readStream.emit('data', { + key: db._encodeBlockIndexKey(block1.timestamp), + value: db._encodeBlockIndexValue(block1.hash) + }); + readStream.emit('close'); });