diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index 2621d24f..1b2be018 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -280,19 +280,25 @@ ChainDB.prototype.getCache = function getCache(hash) { */ ChainDB.prototype.getHeight = function getHeight(hash, callback) { - callback = utils.asyncify(callback); - - if (hash == null || hash < 0) + if (hash == null || hash < 0) { + callback = utils.asyncify(callback); return callback(null, -1); + } - if (typeof hash === 'number') + if (typeof hash === 'number') { + callback = utils.asyncify(callback); return callback(null, hash); + } - if (hash === constants.NULL_HASH) + if (hash === constants.NULL_HASH) { + callback = utils.asyncify(callback); return callback(null, -1); + } - if (this.cacheHash.has(hash)) + if (this.cacheHash.has(hash)) { + callback = utils.asyncify(callback); return callback(null, this.cacheHash.get(hash).height); + } this.db.fetch('h/' + hash, function(data) { assert(data.length === 4, 'Database corruption.'); @@ -316,16 +322,20 @@ ChainDB.prototype.getHeight = function getHeight(hash, callback) { */ ChainDB.prototype.getHash = function getHash(height, callback) { - callback = utils.asyncify(callback); - - if (height == null || height < 0) + if (height == null || height < 0) { + callback = utils.asyncify(callback); return callback(null, null); + } - if (typeof height === 'string') + if (typeof height === 'string') { + callback = utils.asyncify(callback); return callback(null, height); + } - if (this.cacheHeight.has(height)) + if (this.cacheHeight.has(height)) { + callback = utils.asyncify(callback); return callback(null, this.cacheHeight.get(height).hash); + } this.db.fetch('H/' + pad32(height), function(data) { assert(data.length === 32, 'Database corruption.');