move some chaindb stuff around.
This commit is contained in:
parent
f4be7651cd
commit
361134029f
@ -177,6 +177,49 @@ ChainDB.prototype.destroy = function destroy(callback) {
|
||||
this.db.close(callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Dump the database to a map for debugging.
|
||||
* @param {Function} callback - Returns [Error, Object].
|
||||
*/
|
||||
|
||||
ChainDB.prototype.dump = function dump(callback) {
|
||||
var records = {};
|
||||
|
||||
var iter = this.db.iterator({
|
||||
gte: 'c',
|
||||
lte: 'c~',
|
||||
keys: true,
|
||||
values: true,
|
||||
fillCache: false,
|
||||
keyAsBuffer: false,
|
||||
valueAsBuffer: true
|
||||
});
|
||||
|
||||
callback = utils.ensure(callback);
|
||||
|
||||
(function next() {
|
||||
iter.next(function(err, key, value) {
|
||||
if (err) {
|
||||
return iter.end(function() {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
|
||||
if (key === undefined) {
|
||||
return iter.end(function(err) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
return callback(null, records);
|
||||
});
|
||||
}
|
||||
|
||||
records[key] = value;
|
||||
|
||||
next();
|
||||
});
|
||||
})();
|
||||
};
|
||||
|
||||
/**
|
||||
* Add an entry to the LRU cache.
|
||||
* @param {ChainBlock} entry
|
||||
@ -279,49 +322,6 @@ ChainDB.prototype.getHash = function getHash(height, callback) {
|
||||
}, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Dump the database to a map for debugging.
|
||||
* @param {Function} callback - Returns [Error, Object].
|
||||
*/
|
||||
|
||||
ChainDB.prototype.dump = function dump(callback) {
|
||||
var records = {};
|
||||
|
||||
var iter = this.db.iterator({
|
||||
gte: 'c',
|
||||
lte: 'c~',
|
||||
keys: true,
|
||||
values: true,
|
||||
fillCache: false,
|
||||
keyAsBuffer: false,
|
||||
valueAsBuffer: true
|
||||
});
|
||||
|
||||
callback = utils.ensure(callback);
|
||||
|
||||
(function next() {
|
||||
iter.next(function(err, key, value) {
|
||||
if (err) {
|
||||
return iter.end(function() {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
|
||||
if (key === undefined) {
|
||||
return iter.end(function(err) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
return callback(null, records);
|
||||
});
|
||||
}
|
||||
|
||||
records[key] = value;
|
||||
|
||||
next();
|
||||
});
|
||||
})();
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the current chain height from the tip record.
|
||||
* @param {Function} callback - Returns [Error, Number].
|
||||
@ -1032,6 +1032,68 @@ ChainDB.prototype.fillHistory = function fillHistory(tx, callback) {
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a coin (unspents only).
|
||||
* @param {Hash} hash
|
||||
* @param {Number} index
|
||||
* @param {Function} callback - Returns [Error, {@link Coin}].
|
||||
*/
|
||||
|
||||
ChainDB.prototype.getCoin = function getCoin(hash, index, callback) {
|
||||
var self = this;
|
||||
var key = hash + '/' + index;
|
||||
var coin;
|
||||
|
||||
coin = this.coinCache.get(key);
|
||||
if (coin)
|
||||
return utils.asyncify(callback)(null, coin);
|
||||
|
||||
this.db.fetch('c/' + key, function(data) {
|
||||
var coin = bcoin.coin.fromRaw(data);
|
||||
coin.hash = hash;
|
||||
coin.index = index;
|
||||
self.coinCache.set(key, coin);
|
||||
return coin;
|
||||
}, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve a transaction (not filled with coins).
|
||||
* @param {Hash} hash
|
||||
* @param {Function} callback - Returns [Error, {@link TX}].
|
||||
*/
|
||||
|
||||
ChainDB.prototype.getTX = function getTX(hash, callback) {
|
||||
var self = this;
|
||||
|
||||
if (!this.options.indexTX)
|
||||
return utils.nextTick(callback);
|
||||
|
||||
this.db.fetch('t/' + hash, function(data) {
|
||||
return bcoin.tx.fromExtended(data);
|
||||
}, function(err, tx) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
if (self.options.paranoid)
|
||||
assert(tx.hash('hex') === hash, 'Database is corrupt.');
|
||||
|
||||
return callback(null, tx);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Hash} hash
|
||||
* @param {Function} callback - Returns [Error, Boolean].
|
||||
*/
|
||||
|
||||
ChainDB.prototype.hasTX = function hasTX(hash, callback) {
|
||||
if (!this.options.indexTX)
|
||||
return utils.asyncify(callback)(null, false);
|
||||
|
||||
return this.db.has('t/' + hash, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get all coins pertinent to an address.
|
||||
* @param {Base58Address|Base58Address[]} addresses
|
||||
@ -1071,31 +1133,6 @@ ChainDB.prototype.getCoinsByAddress = function getCoinsByAddress(addresses, call
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a coin (unspents only).
|
||||
* @param {Hash} hash
|
||||
* @param {Number} index
|
||||
* @param {Function} callback - Returns [Error, {@link Coin}].
|
||||
*/
|
||||
|
||||
ChainDB.prototype.getCoin = function getCoin(hash, index, callback) {
|
||||
var self = this;
|
||||
var key = hash + '/' + index;
|
||||
var coin;
|
||||
|
||||
coin = this.coinCache.get(key);
|
||||
if (coin)
|
||||
return utils.asyncify(callback)(null, coin);
|
||||
|
||||
this.db.fetch('c/' + key, function(data) {
|
||||
var coin = bcoin.coin.fromRaw(data);
|
||||
coin.hash = hash;
|
||||
coin.index = index;
|
||||
self.coinCache.set(key, coin);
|
||||
return coin;
|
||||
}, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get all transactions pertinent to an address.
|
||||
* @param {Base58Address|Base58Address[]} addresses
|
||||
@ -1141,43 +1178,6 @@ ChainDB.prototype.getTXByAddress = function getTXByAddress(addresses, callback)
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve a transaction (not filled with coins).
|
||||
* @param {Hash} hash
|
||||
* @param {Function} callback - Returns [Error, {@link TX}].
|
||||
*/
|
||||
|
||||
ChainDB.prototype.getTX = function getTX(hash, callback) {
|
||||
var self = this;
|
||||
|
||||
if (!this.options.indexTX)
|
||||
return utils.nextTick(callback);
|
||||
|
||||
this.db.fetch('t/' + hash, function(data) {
|
||||
return bcoin.tx.fromExtended(data);
|
||||
}, function(err, tx) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
if (self.options.paranoid)
|
||||
assert(tx.hash('hex') === hash, 'Database is corrupt.');
|
||||
|
||||
return callback(null, tx);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Hash} hash
|
||||
* @param {Function} callback - Returns [Error, Boolean].
|
||||
*/
|
||||
|
||||
ChainDB.prototype.hasTX = function hasTX(hash, callback) {
|
||||
if (!this.options.indexTX)
|
||||
return utils.asyncify(callback)(null, false);
|
||||
|
||||
return this.db.has('t/' + hash, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a transaction and fill it with coins (historical).
|
||||
* @param {Hash} hash
|
||||
@ -1458,51 +1458,6 @@ ChainDB.prototype._pruneBlock = function _pruneBlock(block, batch, callback) {
|
||||
});
|
||||
};
|
||||
|
||||
function DumbCache(size) {
|
||||
this.data = {};
|
||||
this.count = 0;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
DumbCache.prototype.set = function set(key, value) {
|
||||
key = key + '';
|
||||
|
||||
assert(value !== undefined);
|
||||
|
||||
if (this.count > this.size)
|
||||
this.reset();
|
||||
|
||||
if (this.data[key] === undefined)
|
||||
this.count++;
|
||||
|
||||
this.data[key] = value;
|
||||
};
|
||||
|
||||
DumbCache.prototype.remove = function remove(key) {
|
||||
key = key + '';
|
||||
|
||||
if (this.data[key] === undefined)
|
||||
return;
|
||||
|
||||
this.count--;
|
||||
delete this.data[key];
|
||||
};
|
||||
|
||||
DumbCache.prototype.get = function get(key) {
|
||||
key = key + '';
|
||||
return this.data[key];
|
||||
};
|
||||
|
||||
DumbCache.prototype.has = function has(key) {
|
||||
key = key + '';
|
||||
return this.data[key] !== undefined;
|
||||
};
|
||||
|
||||
DumbCache.prototype.reset = function reset() {
|
||||
this.data = {};
|
||||
this.count = 0;
|
||||
};
|
||||
|
||||
function NullCache(size) {}
|
||||
|
||||
NullCache.prototype.set = function set(key, value) {};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user