diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index 1754bea4..672c6e85 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -437,8 +437,7 @@ ChainDB.prototype.addCache = function addCache(entry) { */ ChainDB.prototype.hasCache = function hasCache(hash) { - if (hash == null || hash < 0) - return false; + checkHash(hash); if (typeof hash === 'number') return this.cacheHeight.has(hash); @@ -454,8 +453,7 @@ ChainDB.prototype.hasCache = function hasCache(hash) { */ ChainDB.prototype.getCache = function getCache(hash) { - if (hash == null || hash < 0) - return; + checkHash(hash); if (typeof hash === 'number') return this.cacheHeight.get(hash); @@ -472,10 +470,7 @@ ChainDB.prototype.getCache = function getCache(hash) { ChainDB.prototype.getHeight = function getHeight(hash, callback) { var entry; - if (hash == null || hash < 0) { - callback = utils.asyncify(callback); - return callback(null, -1); - } + checkHash(hash); if (typeof hash === 'number') { callback = utils.asyncify(callback); @@ -518,10 +513,7 @@ ChainDB.prototype.getHeight = function getHeight(hash, callback) { ChainDB.prototype.getHash = function getHash(height, callback) { var entry; - if (height == null || height < 0) { - callback = utils.asyncify(callback); - return callback(null, null); - } + checkHash(height); if (typeof height === 'string') { callback = utils.asyncify(callback); @@ -567,8 +559,7 @@ ChainDB.prototype.getChainHeight = function getChainHeight(callback) { ChainDB.prototype.getBoth = function getBoth(block, callback) { var hash, height; - if (block == null || block < 0) - return utils.asyncify(callback)(null, null, -1); + checkHash(block); if (typeof block === 'string') hash = block; @@ -608,8 +599,7 @@ ChainDB.prototype.getEntry = function getEntry(hash, callback) { var self = this; var entry; - if (hash == null || hash < 0) - return utils.nextTick(callback); + checkHash(hash); this.getHash(hash, function(err, hash) { if (err) @@ -941,8 +931,7 @@ ChainDB.prototype.reset = function reset(block, callback) { */ ChainDB.prototype.has = function has(height, callback) { - if (height == null || height < 0) - return utils.asyncify(callback)(null, false); + checkHash(height); this.getBoth(height, function(err, hash, height) { if (err) @@ -1880,6 +1869,11 @@ function getSize(value) { return 80 + value.length; } +function checkHash(hash) { + assert(typeof hash === 'string' || typeof hash === 'number', + 'Must pass in height or hash.'); +} + /* * Expose */