txdb orphan storage.

This commit is contained in:
Christopher Jeffrey 2016-03-28 15:45:47 -07:00
parent 2e0948faa5
commit 2c9756f38b
2 changed files with 28 additions and 23 deletions

View File

@ -11,6 +11,8 @@ var assert = bcoin.utils.assert;
var EventEmitter = require('events').EventEmitter;
var DUMMY = new Buffer([0]);
var pad32 = utils.pad32;
var BufferReader = require('./reader');
var BufferWriter = require('./writer');
/**
* TXPool
@ -89,8 +91,8 @@ TXPool.prototype.getMap = function getMap(tx, callback) {
};
TXPool.prototype.mapAddresses = function mapAddresses(address, callback) {
var prefix = this.prefix + '/';
var self = this;
var prefix = this.prefix + '/';
var map = {};
var iter;
@ -152,29 +154,28 @@ TXPool.prototype.mapAddresses = function mapAddresses(address, callback) {
TXPool.prototype._addOrphan = function _addOrphan(key, hash, index, callback) {
var prefix = this.prefix + '/';
var orphans;
var p;
this.db.get(prefix + 'o/' + key, function(err, buf) {
if (err && err.type !== 'NotFoundError')
return callback(err);
if (!buf)
buf = new Buffer([]);
p = new BufferWriter();
orphans = new Buffer(buf.length + 36);
utils.copy(buf, orphans, 0);
utils.copy(new Buffer(hash, 'hex'), orphans, buf.length);
utils.writeU32(orphans, index, buf.length + 32);
if (buf)
p.writeBytes(buf);
return callback(null, orphans);
p.writeHash(hash);
p.writeU32(index);
return callback(null, p.render());
});
};
TXPool.prototype._getOrphans = function _getOrphans(key, callback) {
var prefix = this.prefix + '/';
var self = this;
var orphans = [];
var i;
var prefix = this.prefix + '/';
var p, orphans;
this.db.get(prefix + 'o/' + key, function(err, buf) {
if (err && err.type !== 'NotFoundError')
@ -183,14 +184,18 @@ TXPool.prototype._getOrphans = function _getOrphans(key, callback) {
if (!buf)
return callback();
for (i = 0; i < buf.length; i += 36) {
if (i + 36 > buf.length)
return callback(new Error('Incomplete orphan list.'));
p = new BufferReader(buf);
orphans = [];
orphans.push({
hash: utils.toHex(buf.slice(i, i + 32)),
index: utils.readU32(buf, i + 32)
});
try {
while (p.left()) {
orphans.push({
hash: p.readHash('hex'),
index: p.readU32()
});
}
} catch (e) {
return callback(e);
}
utils.forEach(orphans, function(orphan, next) {
@ -933,8 +938,8 @@ TXPool.prototype._unconfirm = function unconfirm(tx, map, callback, force) {
};
TXPool.prototype.getTXHashes = function getTXHashes(address, callback) {
var prefix = this.prefix + '/';
var self = this;
var prefix = this.prefix + '/';
var txs = [];
var iter;
@ -996,8 +1001,8 @@ TXPool.prototype.getTXHashes = function getTXHashes(address, callback) {
};
TXPool.prototype.getPendingHashes = function getPendingHashes(address, callback) {
var prefix = this.prefix + '/';
var self = this;
var prefix = this.prefix + '/';
var txs = [];
var iter;
@ -1060,8 +1065,8 @@ TXPool.prototype.getPendingHashes = function getPendingHashes(address, callback)
};
TXPool.prototype.getCoinIDs = function getCoinIDs(address, callback) {
var prefix = this.prefix + '/';
var self = this;
var prefix = this.prefix + '/';
var coins = [];
var iter;

View File

@ -20,7 +20,7 @@ var HEADER_SIZE = 12;
* Master
*/
workers.MAX_WORKERS = +process.env.BCOIN_WORKERS || 30;
workers.MAX_WORKERS = +process.env.BCOIN_WORKERS || 6;
workers.TIMEOUT = 10000;
workers.children = {};
workers.uid = 0;