use iterators for getHeight.

This commit is contained in:
Christopher Jeffrey 2016-02-24 01:46:12 -08:00
parent f9577cdc04
commit 96bb12568e

View File

@ -955,29 +955,39 @@ BlockDB.prototype.isSpent = function isSpent(hash, index, callback) {
BlockDB.prototype.getHeight = function getHeight(callback) {
var self = this;
var maxHeight = -1;
var stream;
var iter;
callback = utils.asyncify(callback);
stream = this.index.createKeyStream({
start: 'b/h',
end: 'b/h~'
iter = this.index.db.iterator({
gte: 'b/h',
lte: 'b/h~',
keys: true,
values: false,
fillCache: false,
keyAsBuffer: false
});
stream.on('data', function(key) {
var parts = key.split('/').slice(2);
var height = +parts[0];
if (height > maxHeight)
maxHeight = height;
});
(function next() {
iter.next(function(err, key, value) {
var parts, height;
stream.on('error', function(err) {
return callback(err);
});
if (err) {
return iter.end(function() {
return callback(err);
});
}
stream.on('end', function() {
return callback(null, maxHeight);
});
// iter.end() here?
if (!key)
return callback(null, maxHeight);
parts = key.split('/').slice(2);
height = +parts[0];
if (height > maxHeight)
maxHeight = height;
next();
});
})();
};
BlockDB.prototype.resetHeight = function resetHeight(height, callback, emit) {