pool: wip

This commit is contained in:
Fedor Indutny 2014-05-02 00:08:07 +04:00
parent 1432608813
commit c71258103b
3 changed files with 43 additions and 13 deletions

View File

@ -11,7 +11,7 @@ function Chain() {
map: {},
count: 0
};
this.bloom = new bcoin.bloom(28 * 1024 * 1024, 33, 0);
this.bloom = new bcoin.bloom(28 * 1024 * 1024, 33, 0xdeadbeef);
this.last = null;
this.add(new bcoin.block(constants.genesis));
}

View File

@ -43,7 +43,7 @@ function Peer(pool, socket, options) {
this._ping = {
timer: null,
interval: this.options.pingInterval || 5000
interval: this.options.pingInterval || 30000
};
this._init();

View File

@ -8,16 +8,16 @@ function Pool(options) {
this.options = options || {};
this.size = options.size || 16;
this.parallel = options.parallel || 4000;
this.loadTimeout = options.loadTimeout || 3000;
this.loadDelay = options.loadDelay || 750;
this.parallel = options.parallel || 8000;
this.loadTimeout = options.loadTimeout || 10000;
this.loadWindow = options.loadWindow || 2500;
this.loadTimer = null;
this.loadWatermark = {
lo: options.lwm || 1000,
hi: options.hwm || 4000
hi: options.hwm || 32000
};
this.maxRetries = options.maxRetries || 1000;
this.requestTimeout = options.requestTimeout || 30000;
this.requestTimeout = options.requestTimeout || 15000;
this.chain = new bcoin.chain();
this.bloom = new bcoin.bloom(8 * 10 * 1024,
10,
@ -90,8 +90,10 @@ Pool.prototype._addLoader = function _addLoader() {
// Split blocks and request them using multiple peers
peer.on('blocks', function(hashes) {
if (hashes.length === 0)
if (hashes.length === 0) {
self.block.lastSeen = null;
return;
}
// Request each block
hashes.forEach(function(hash) {
@ -115,10 +117,14 @@ Pool.prototype._load = function load() {
// Load more blocks, starting from last hash
var hash;
if (this.block.lastSeen)
if (this.block.lastSeen) {
hash = this.block.lastSeen;
else
console.log('Loading from (last): ' + utils.toHex(hash.slice().reverse()));
} else {
hash = this.chain.getLast().hash();
console.log('Loading from (chain): ' + utils.toHex(hash.slice().reverse()));
}
this.peers.load.loadBlocks(hash);
return true;
@ -163,6 +169,18 @@ Pool.prototype._addPeer = function _addPeer() {
req.finish(block);
});
peer.on('notfound', function(items) {
items.forEach(function(item) {
if (item.type !== 'filtered')
return;
var req = self.block.requests[utils.toHex(item.hash)];
console.log('notfound', !!req);
if (req)
req.finish(null);
});
});
peer.on('tx', function(tx) {
console.log('got tx', tx.hash('hex'));
});
@ -199,6 +217,18 @@ Pool.prototype._requestBlock = function _requestBlock(hash) {
};
Pool.prototype._scheduleRequests = function _scheduleRequests() {
if (this.block.active > 100)
return;
// No need to wait - already have enough data
if (this.block.queue.length > this.parallel) {
if (this.loadTimer !== null)
clearTimeout(this.loadTimer);
this.loadTimer = null;
return this._doRequests();
}
// Already waiting
if (this.loadTimer !== null)
return;
@ -206,7 +236,7 @@ Pool.prototype._scheduleRequests = function _scheduleRequests() {
this.loadTimer = setTimeout(function() {
self.loadTimer = null;
self._doRequests();
}, this.loadDelay);
}, this.loadWindow);
};
Pool.prototype._doRequests = function _doRequests() {
@ -305,7 +335,7 @@ BlockRequest.prototype.retry = function retry() {
this.peer.destroy();
};
BlockRequest.prototype.finish = function finish(block) {
BlockRequest.prototype.finish = function finish() {
if (this.active) {
this.pool.block.active--;
this.active = false;
@ -316,7 +346,7 @@ BlockRequest.prototype.finish = function finish(block) {
if (index !== -1)
this.pool.block.queue.splice(index, 1);
}
delete this.pool.block.requests[block.hash('hex')];
delete this.pool.block.requests[utils.toHex(this.hash)];
if (this.timer)
clearTimeout(this.timer);
this.timer = null;