chaindb: strict arg checking.

This commit is contained in:
Christopher Jeffrey 2016-08-18 00:31:54 -07:00
parent 0208f9383c
commit 096957ccb9
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -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
*/